Macros | Functions | Variables
bit.h File Reference
#include <stdio.h>

Go to the source code of this file.

Macros

#define DLL_API
 
#define foreach_bit(i, b)   for (i = first_bit(b); b; i = next_bit(&b))
 
#define x_to_bit(x)   X_TO_BIT[x]
 

Functions

DLL_API int bit_count (unsigned long long)
 Count the number of bits set to one in an unsigned long long. More...
 
int bit_weighted_count (const unsigned long long)
 count the number of discs, counting the corners twice. More...
 
DLL_API int first_bit (unsigned long long)
 Search the first bit set. More...
 
int next_bit (unsigned long long *)
 Search the next bit set. More...
 
DLL_API int last_bit (unsigned long long)
 Search the last bit set (same as log2()). More...
 
void bitboard_write (const unsigned long long, FILE *)
 Print an unsigned long long as a board. More...
 
unsigned long long transpose (unsigned long long)
 Transpose the unsigned long long (symetry % A1-H8 diagonal). More...
 
unsigned long long vertical_mirror (unsigned long long)
 Mirror the unsigned long long (exchange the lines A - H, B - G, C - F & D - E.). More...
 
unsigned long long horizontal_mirror (unsigned long long)
 Mirror the unsigned long long (exchange the line 1 - 8, 2 - 7, 3 - 6 & 4 - 5). More...
 
unsigned int bswap_int (unsigned int)
 Mirror the unsigned int (little <-> big endian). More...
 
unsigned short bswap_short (unsigned short)
 Swap bytes of a short (little <-> big endian). More...
 
int get_rand_bit (unsigned long long, struct Random *)
 Get a random set bit index. More...
 

Variables

const unsigned long long X_TO_BIT []
 

Detailed Description

Bitwise operations header file.

Date
1998 - 2017
Author
Richard Delorme
Version
4.4

Macro Definition Documentation

◆ DLL_API

#define DLL_API

◆ foreach_bit

#define foreach_bit (   i,
 
)    for (i = first_bit(b); b; i = next_bit(&b))

Loop over each bit set.

◆ x_to_bit

#define x_to_bit (   x)    X_TO_BIT[x]

Return a bitboard with bit x set.

Function Documentation

◆ bit_count()

DLL_API int bit_count ( unsigned long long  b)

Count the number of bits set to one in an unsigned long long.

This is the classical popcount function. Since 2007, it is part of the instruction set of some modern CPU, (>= barcelona for AMD or >= nelhacem for Intel). Alternatively, a fast SWAR algorithm, adding bits in parallel is provided here. This function is massively used to count discs on the board, the mobility, or the stability.

Parameters
b64-bit integer to count bits of.
Returns
the number of bits set.

◆ bit_weighted_count()

int bit_weighted_count ( const unsigned long long  v)

count the number of discs, counting the corners twice.

This is a variation of the above algorithm to count the mobility and favour the corners. This function is usefull for move sorting.

Parameters
v64-bit integer to count bits of.
Returns
the number of bit set, counting the corners twice.

◆ bitboard_write()

void bitboard_write ( const unsigned long long  b,
FILE *  f 
)

Print an unsigned long long as a board.

Write a 64-bit long number as an Othello board.

Parameters
bThe unsigned long long.
fOutput stream.

◆ bswap_int()

unsigned int bswap_int ( unsigned int  i)

Mirror the unsigned int (little <-> big endian).

Parameters
iAn unsigned int.
Returns
The mirrored int.

◆ bswap_short()

unsigned short bswap_short ( unsigned short  s)

Swap bytes of a short (little <-> big endian).

Parameters
sAn unsigned short.
Returns
The mirrored short.

◆ first_bit()

DLL_API int first_bit ( unsigned long long  b)

Search the first bit set.

On CPU with AMD64 or EMT64 instructions, a fast asm code is provided. Alternatively, a fast algorithm based on magic numbers is provided.

Parameters
b64-bit integer.
Returns
the index of the first bit set.

◆ get_rand_bit()

int get_rand_bit ( unsigned long long  b,
Random r 
)

Get a random set bit index.

Parameters
bThe unsigned long long.
rThe pseudo-number generator.
Returns
a random bit index, or -1 if b value is zero.

◆ horizontal_mirror()

unsigned long long horizontal_mirror ( unsigned long long  b)

Mirror the unsigned long long (exchange the line 1 - 8, 2 - 7, 3 - 6 & 4 - 5).

Parameters
bAn unsigned long long.
Returns
The mirrored unsigned long long.

◆ last_bit()

DLL_API int last_bit ( unsigned long long  b)

Search the last bit set (same as log2()).

On CPU with AMD64 or EMT64 instructions, a fast asm code is provided. Alternatively, a fast algorithm based on magic numbers is provided.

Parameters
b64-bit integer.
Returns
the index of the last bit set.

◆ next_bit()

int next_bit ( unsigned long long *  b)

Search the next bit set.

In practice, clear the first bit set and search the next one.

Parameters
b64-bit integer.
Returns
the index of the next bit set.

◆ transpose()

unsigned long long transpose ( unsigned long long  b)

Transpose the unsigned long long (symetry % A1-H8 diagonal).

Parameters
bAn unsigned long long
Returns
The transposed unsigned long long.

◆ vertical_mirror()

unsigned long long vertical_mirror ( unsigned long long  b)

Mirror the unsigned long long (exchange the lines A - H, B - G, C - F & D - E.).

Parameters
bAn unsigned long long
Returns
The mirrored unsigned long long.

Variable Documentation

◆ X_TO_BIT

const unsigned long long X_TO_BIT[]

coordinate to bit table converter