bpaul-chess

UCI chess engine to replace Omelette Chess Engine
Log | Files | Refs | README | LICENSE

bb.c (616B)


      1 #include <stdbool.h>
      2 
      3 #include "bb.h"
      4 
      5 bool
      6 testbit(bb b, unsigned char sq) {
      7 	return b & 1ULL << sq;
      8 }
      9 
     10 void
     11 setbit(bb *b, unsigned char sq) {
     12 	*b |= 1ULL << sq;
     13 }
     14 
     15 void
     16 popbit(bb *b, unsigned char sq) {
     17 	/* If the bit is known to be set, it would be one instruction shorter to
     18 	 * subtract */
     19 	*b &= ~(1ULL << sq);
     20 }
     21 
     22 unsigned char
     23 getlsb(bb b) {
     24 	return __builtin_ctzll(b);
     25 }
     26 
     27 unsigned char
     28 poplsb(bb *b) {
     29 	unsigned char bit;
     30 	bit = getlsb(*b);
     31 	*b &= *b - 1;
     32 	return bit;
     33 }
     34 
     35 bb
     36 shift(bb b, char s) {
     37 	if (s < 0)
     38 		return b >> -s;
     39 	else
     40 		return b << s;
     41 }
     42 
     43 char
     44 popcnt(bb b) {
     45 	return __builtin_popcountll(b);
     46 }