stellar

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

utils.c (2304B)


      1 #include <stdio.h>
      2 #ifdef WIN64
      3 #include <widnows.h>
      4 #else
      5 #include <sys/time.h>
      6 #endif
      7 
      8 #include "utils.h"
      9 
     10 const U64 universe = C64(0xffffffffffffffff); //
     11 const U64 notAFile = C64(0xfefefefefefefefe); // ~0x0101010101010101
     12 const U64 notHFile = C64(0x7f7f7f7f7f7f7f7f); // ~0x8080808080808080
     13 
     14 U64 soutOne(U64 b) { return b >> 8; }
     15 U64 nortOne(U64 b) { return b << 8; }
     16 U64 eastOne(U64 b) { return (b & notHFile) << 1; }
     17 U64 westOne(U64 b) { return (b & notAFile) >> 1; }
     18 U64 soEaOne(U64 b) { return (b & notHFile) >> 7; }
     19 U64 soWeOne(U64 b) { return (b & notAFile) >> 9; }
     20 U64 noEaOne(U64 b) { return (b & notHFile) << 9; }
     21 U64 noWeOne(U64 b) { return (b & notAFile) << 7; }
     22 
     23 // board rotation
     24 U64 rotateLeft(U64 x, int s) { return (x << s) | (x >> (64 - s)); }
     25 U64 rotateRight(U64 x, int s) { return (x >> s) | (x << (64 - s)); }
     26 
     27 // clang-format off
     28 const char *square_to_coordinates[]={
     29   "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1",
     30   "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
     31   "a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
     32   "a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
     33   "a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
     34   "a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
     35   "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
     36   "a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8", " "
     37 };
     38 // clang-format on
     39 //
     40 Square coordinates_to_square(char *cord) {
     41     return (cord[1] - '1') * 8 + (cord[0] - 'a');
     42 }
     43 
     44 int bit_count(U64 bitboard) {
     45     int count = 0;
     46 
     47     while (bitboard > 0) {
     48         count++;
     49         bitboard &= bitboard - 1;
     50     }
     51 
     52     return count;
     53 }
     54 
     55 int bit_lsb_index(U64 bitboard) {
     56     if (!bitboard) return -1;
     57 
     58     return bit_count((bitboard & -bitboard) - 1);
     59 }
     60 
     61 void bitboard_print(U64 bitboard) {
     62     for (int rank = 0; rank < 8; rank++) {
     63         for (int file = 0; file < 8; file++) {
     64             Square square = (7 - rank) * 8 + file;
     65 
     66             if (!file) printf(" %d  ", 8 - rank);
     67 
     68             printf("%d ", bit_get(bitboard, square) ? 1 : 0);
     69         }
     70         printf("\n");
     71     }
     72 
     73     printf("\n    A B C D E F G H\n\n");
     74     printf("    Bitboard: %llud\n\n", bitboard);
     75 }
     76 
     77 int get_time_ms(void) {
     78 #ifdef WIN64
     79     return GetTickCount();
     80 #else
     81     struct timeval time;
     82     gettimeofday(&time, NULL);
     83     return time.tv_sec * 1000 + time.tv_usec / 1000;
     84 #endif
     85 }