stellar

Stellar - UCI Chess engine written in C++20
git clone git://git.dimitrijedobrota.com/stellar.git
Log | Files | Refs | README | LICENSE

commit 9ce8b54dde4118111d96fb90d307b9fa691c92f5
parent 6056b66a9b9eac41ea5d442fc9f877598e9a6eed
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Mon, 31 Jul 2023 18:13:04 +0200

Use builtin bit instructions when available

Diffstat:
MCMakeLists.txt | 2+-
Msrc/include/utils.h | 8++++----
Msrc/utils/utils.c | 67+++++++++++++++++++++++--------------------------------------------
3 files changed, 28 insertions(+), 49 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) project( Stellar - VERSION 0.0.5 + VERSION 0.0.6 DESCRIPTION "Chess engine written in C" HOMEPAGE_URL https://git.dimitrijedobrota.com/stellar.git LANGUAGES C diff --git a/src/include/utils.h b/src/include/utils.h @@ -1,6 +1,8 @@ #ifndef STELLAR_UTILS_H #define STELLAR_UTILS_H +#include <inttypes.h> + // useful macros #define MAX(a, b) ((a > b) ? a : b) #define MIN(a, b) ((a < b) ? a : b) @@ -21,14 +23,12 @@ extern const U64 notHFile; #define bit_get(bitboard, square) (((bitboard) >> (square)) & C64(1)) #define bit_set(bitboard, square) ((bitboard) |= C64(1) << (square)) #define bit_pop(bitboard, square) ((bitboard) &= ~(C64(1) << (square))) -int bit_count(U64 bitboard); -int bit_lsb_index(U64 bitboard); +uint8_t bit_count(U64 bitboard); +uint8_t bit_lsb_index(U64 bitboard); #define bitboard_for_each_bit(var, bb) \ for (var = bit_lsb_index(bb); bb; bit_pop(bb, var), var = bit_lsb_index(bb)) -void bitboard_print(U64 bitboard); - // squares // clang-format off enum enumSquare { diff --git a/src/utils/utils.c b/src/utils/utils.c @@ -1,15 +1,31 @@ #include <stdio.h> -#ifdef WIN64 -#include <widnows.h> -#else #include <sys/time.h> -#endif #include "utils.h" -const U64 universe = C64(0xffffffffffffffff); // -const U64 notAFile = C64(0xfefefefefefefefe); // ~0x0101010101010101 -const U64 notHFile = C64(0x7f7f7f7f7f7f7f7f); // ~0x8080808080808080 +const U64 universe = C64(0xffffffffffffffff); +const U64 notAFile = C64(0xfefefefefefefefe); +const U64 notHFile = C64(0x7f7f7f7f7f7f7f7f); + +inline uint8_t bit_count(U64 bitboard) { +#if __has_builtin(__builtin_popcountll) + return __builtin_popcountll(bitboard); +#endif + + int count = 0; + for (; bitboard > 0; bitboard &= bitboard - 1) + count++; + return count; +} + +inline uint8_t bit_lsb_index(U64 bitboard) { +#if __has_builtin(__builtin_ffsll) + return __builtin_ffsll(bitboard) - 1; +#endif + + if (!bitboard) return -1; + return bit_count((bitboard & -bitboard) - 1); +} U64 soutOne(U64 b) { return b >> 8; } U64 nortOne(U64 b) { return b << 8; } @@ -41,45 +57,8 @@ Square coordinates_to_square(const char *cord) { return (cord[1] - '1') * 8 + (cord[0] - 'a'); } -int bit_count(U64 bitboard) { - int count = 0; - - while (bitboard > 0) { - count++; - bitboard &= bitboard - 1; - } - - return count; -} - -int bit_lsb_index(U64 bitboard) { - if (!bitboard) return -1; - - return bit_count((bitboard & -bitboard) - 1); -} - -void bitboard_print(U64 bitboard) { - for (int rank = 0; rank < 8; rank++) { - for (int file = 0; file < 8; file++) { - Square square = (7 - rank) * 8 + file; - - if (!file) printf(" %d ", 8 - rank); - - printf("%d ", bit_get(bitboard, square) ? 1 : 0); - } - printf("\n"); - } - - printf("\n A B C D E F G H\n\n"); - printf(" Bitboard: %llud\n\n", bitboard); -} - int get_time_ms(void) { -#ifdef WIN64 - return GetTickCount(); -#else struct timeval time; gettimeofday(&time, NULL); return time.tv_sec * 1000 + time.tv_usec / 1000; -#endif }