stellar

Stellar - Chess engine written in C
Log | Files | Refs

utils.c (1915B)


      1 #include <stdio.h>
      2 #include <sys/time.h>
      3 
      4 #include "utils.h"
      5 
      6 const U64 universe = C64(0xffffffffffffffff);
      7 const U64 notAFile = C64(0xfefefefefefefefe);
      8 const U64 notHFile = C64(0x7f7f7f7f7f7f7f7f);
      9 
     10 inline uint8_t bit_count(U64 bitboard) {
     11 #if __has_builtin(__builtin_popcountll)
     12     return __builtin_popcountll(bitboard);
     13 #endif
     14 
     15     int count = 0;
     16     for (; bitboard > 0; bitboard &= bitboard - 1)
     17         count++;
     18     return count;
     19 }
     20 
     21 inline uint8_t bit_lsb_index(U64 bitboard) {
     22 #if __has_builtin(__builtin_ffsll)
     23     return __builtin_ffsll(bitboard) - 1;
     24 #endif
     25 
     26     if (!bitboard) return -1;
     27     return bit_count((bitboard & -bitboard) - 1);
     28 }
     29 
     30 U64 soutOne(U64 b) { return b >> 8; }
     31 U64 nortOne(U64 b) { return b << 8; }
     32 U64 eastOne(U64 b) { return (b & notHFile) << 1; }
     33 U64 westOne(U64 b) { return (b & notAFile) >> 1; }
     34 U64 soEaOne(U64 b) { return (b & notHFile) >> 7; }
     35 U64 soWeOne(U64 b) { return (b & notAFile) >> 9; }
     36 U64 noEaOne(U64 b) { return (b & notHFile) << 9; }
     37 U64 noWeOne(U64 b) { return (b & notAFile) << 7; }
     38 
     39 // board rotation
     40 U64 rotateLeft(U64 x, int s) { return (x << s) | (x >> (64 - s)); }
     41 U64 rotateRight(U64 x, int s) { return (x >> s) | (x << (64 - s)); }
     42 
     43 // clang-format off
     44 const char *square_to_coordinates[]={
     45   "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1",
     46   "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
     47   "a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
     48   "a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
     49   "a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
     50   "a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
     51   "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
     52   "a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8", " "
     53 };
     54 // clang-format on
     55 
     56 Square coordinates_to_square(const char *cord) {
     57     return (cord[1] - '1') * 8 + (cord[0] - 'a');
     58 }
     59 
     60 int get_time_ms(void) {
     61     struct timeval time;
     62     gettimeofday(&time, NULL);
     63     return time.tv_sec * 1000 + time.tv_usec / 1000;
     64 }