rsmc
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2// Copyright (c) 2026 01eksa
3
10#ifndef RSMC_UTILS_H
11#define RSMC_UTILS_H
12
13#include "rsmc/abstractions.h"
14#include "rsmc/core.h"
15#include <assert.h>
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#if defined(_MSC_VER)
22#include <intrin.h>
23#define popcnt64(x) __popcnt64(x)
24#elif defined(__GNUC__) || defined(__clang__)
25#define popcnt64(x) __builtin_popcountll(x)
26#else
27static inline int popcnt64(uint64_t x) {
28 x = x - ((x >> 1) & 0x5555555555555555ULL);
29 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
30 x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
31 return (x * 0x0101010101010101ULL) >> 56;
32}
33#endif
34
41static inline RsmcBoardCell rsmc_player_to_cell(const RsmcPlayer player)
42{
43 static_assert(RsmcPlayerBlack + 1 == RsmcBoardCellBlack, "cannot convert black player to cell");
44 static_assert(RsmcPlayerWhite + 1 == RsmcBoardCellWhite, "cannot convert white player to cell");
45 return player + 1;
46}
47
54static inline RsmcPlayer rsmc_player_opposite(const RsmcPlayer player)
55{
57 "cannot toggle players with !");
58 return (RsmcPlayer)!player;
59}
60
68static inline RsmcCoords rsmc_coords_add(const RsmcCoords left, const RsmcCoords right)
69{
70 RsmcCoords result;
71 result.x = (int8_t)(left.x + right.x);
72 result.y = (int8_t)(left.y + right.y);
73 return result;
74}
75
83static inline RsmcCoords rsmc_coords_sub(const RsmcCoords left, const RsmcCoords right)
84{
85 RsmcCoords result;
86 result.x = (int8_t)(left.x - right.x);
87 result.y = (int8_t)(left.y - right.y);
88 return result;
89}
90
91#ifdef __cplusplus
92}
93#endif
94
95#endif // RSMC_UTILS_H
Thin abstract layer for rsmc.
@ RsmcBoardCellBlack
Definition abstractions.h:28
@ RsmcBoardCellWhite
Definition abstractions.h:29
uint8_t RsmcBoardCell
Board cell (empty, black, white)
Definition abstractions.h:25
Types and constants for rsmc.
uint8_t RsmcPlayer
Player's color.
Definition core.h:70
@ RsmcPlayerWhite
Definition core.h:73
@ RsmcPlayerBlack
Definition core.h:72
Stores coords for the board. Coords might be invalid and negative.
Definition abstractions.h:55
int8_t x
Definition abstractions.h:56
int8_t y
Definition abstractions.h:57