stellar

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

commit 317a3513ae4fca24f5ab9d34b38e198c396b5774
parent ef2b4628adb6a029cc81d0267d80fc9537fdb89f
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Mon,  7 Aug 2023 12:30:42 +0200

Extract random number generation

Diffstat:
MCMakeLists.txt | 2+-
Msrc/attacks/CMakeLists.txt | 1+
Msrc/attacks/magic_generate.c | 31++++---------------------------
Asrc/include/random.h | 10++++++++++
Msrc/utils/CMakeLists.txt | 8++++++++
Asrc/utils/random.c | 26++++++++++++++++++++++++++
6 files changed, 50 insertions(+), 28 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) project( Stellar - VERSION 0.0.15 + VERSION 0.0.16 DESCRIPTION "Chess engine written in C" HOMEPAGE_URL https://git.dimitrijedobrota.com/stellar.git LANGUAGES C diff --git a/src/attacks/CMakeLists.txt b/src/attacks/CMakeLists.txt @@ -26,6 +26,7 @@ add_executable(magic target_link_libraries(magic PRIVATE utils + PRIVATE random ) set_target_properties(magic PROPERTIES diff --git a/src/attacks/magic_generate.c b/src/attacks/magic_generate.c @@ -1,4 +1,5 @@ #include "internal.h" +#include "random.h" #include <inttypes.h> #include <stdio.h> @@ -8,34 +9,9 @@ const char *FORMAT = "C64(0x%llx),\n"; -// pseudo random numbers - -U32 state = C32(1804289383); - -U32 get_random_U32_number() { - U32 number = state; - - number ^= number << 13; - number ^= number >> 17; - number ^= number << 5; - - return state = number; -} - -U64 get_random_U64_number() { - U64 n1, n2, n3, n4; - - n1 = (U64)(get_random_U32_number()) & C64(0xFFFF); - n2 = (U64)(get_random_U32_number()) & C64(0xFFFF); - n3 = (U64)(get_random_U32_number()) & C64(0xFFFF); - n4 = (U64)(get_random_U32_number()) & C64(0xFFFF); - - return n1 | (n2 << 16) | (n3 << 32) | (n4 << 48); -} - U64 generate_magic_number() { - return get_random_U64_number() & get_random_U64_number() & - get_random_U64_number(); + return random_get_U64() & random_get_U64() & + random_get_U64(); } U64 find_magic_number(Square square, int relevant_bits, int bishop) { @@ -76,6 +52,7 @@ U64 find_magic_number(Square square, int relevant_bits, int bishop) { } int main(void) { + random_state_reset(); printf("Bishup Magic Numbers:\n"); for (int i = 0; i < 64; i++) diff --git a/src/include/random.h b/src/include/random.h @@ -0,0 +1,10 @@ +#ifndef STELLAR_RANDOM_H +#define STELLAR_RANDOM_H + +#include "utils.h" + +void random_state_reset(); +U32 random_get_U32(); +U64 random_get_U64(); + +#endif diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt @@ -5,3 +5,11 @@ add_library(utils OBJECT target_include_directories(utils PUBLIC "${PROJECT_SOURCE_DIR}/src/include" ) + +add_library(random OBJECT + random.c +) + +target_include_directories(random + PUBLIC "${PROJECT_SOURCE_DIR}/src/include" +) diff --git a/src/utils/random.c b/src/utils/random.c @@ -0,0 +1,26 @@ +#include "random.h" + +U32 state = C32(1804289383); + +void random_state_reset() { state = C32(1804289383); } + +U32 random_get_U32() { + U32 number = state; + + number ^= number << 13; + number ^= number >> 17; + number ^= number << 5; + + return state = number; +} + +U64 random_get_U64() { + U64 n1, n2, n3, n4; + + n1 = (U64)(random_get_U32()) & C64(0xFFFF); + n2 = (U64)(random_get_U32()) & C64(0xFFFF); + n3 = (U64)(random_get_U32()) & C64(0xFFFF); + n4 = (U64)(random_get_U32()) & C64(0xFFFF); + + return n1 | (n2 << 16) | (n3 << 32) | (n4 << 48); +}