Minor cleanup and code reordering (bench 5777161)

This commit is contained in:
2025-02-21 09:58:26 +01:00
parent dcf9dd8c6a
commit e225fe8d96
33 changed files with 110 additions and 114 deletions

View File

@@ -58,7 +58,7 @@ APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
Copyright 2024 nocturn9x
Copyright 2025 nocturn9x
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -12,31 +12,31 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import heimdall/tui
import heimdall/movegen
import heimdall/moves
import heimdall/magics
import heimdall/position
import heimdall/board
import heimdall/transpositions
import heimdall/search
import heimdall/eval
import heimdall/util/tunables
import heimdall/uci
import heimdall/datagen/generate
import heimdall/datagen/tool
import heimdall/tui
import heimdall/eval
import heimdall/board
import heimdall/moves
import heimdall/search
import heimdall/movegen
import heimdall/position
import heimdall/util/magics
import heimdall/util/limits
import heimdall/datagen/tool
import heimdall/util/tunables
import heimdall/transpositions
import heimdall/datagen/generate
import std/os
import std/times
import std/math
import std/times
import std/options
import std/atomics
import std/cpuinfo
import std/parseopt
import std/strutils
import std/strformat
import std/options
when defined(mimalloc):

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -19,8 +19,8 @@ import std/bitops
import std/strutils
import pieces
import moves
import heimdall/moves
import heimdall/pieces
type

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -15,12 +15,12 @@
## Implementation of a simple chessboard
import heimdall/pieces
import heimdall/magics
import heimdall/util/magics
import heimdall/moves
import heimdall/rays
import heimdall/util/rays
import heimdall/bitboards
import heimdall/position
import heimdall/zobrist
import heimdall/util/zobrist

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,23 +13,20 @@
# limitations under the License.
## Position evaluation utilities
import heimdall/pieces
import heimdall/position
import heimdall/board
import heimdall/moves
import heimdall/pieces
import heimdall/position
import heimdall/nnue/util
import nnue/model
import heimdall/nnue/model
when defined(simd):
import heimdall/util/simd
import std/streams
const
MAX_ACCUMULATORS = 255
const MAX_ACCUMULATORS = 255
type

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -21,11 +21,11 @@ import std/tables
import heimdall/bitboards
import heimdall/board
import heimdall/magics
import heimdall/util/magics
import heimdall/pieces
import heimdall/moves
import heimdall/position
import heimdall/rays
import heimdall/util/rays
import heimdall/datagen/marlinformat
@@ -321,7 +321,7 @@ proc generateMoves*(self: var Position, moves: var MoveList, capturesOnly: bool
self.generateKnightMoves(moves, destinationMask)
self.generateRookMoves(moves, destinationMask)
self.generateBishopMoves(moves, destinationMask)
# Queens are just handled rooks + bishops
# Queens are just handled as rooks + bishops
proc generateMoves*(self: Chessboard, moves: var MoveList, capturesOnly=false) {.inline.} =

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -15,11 +15,12 @@
## Handling of moves
import heimdall/pieces
import std/strformat
const MAX_MOVES* = 218
type
MoveFlag* = enum
## An enumeration of move flags
@@ -127,18 +128,15 @@ func createMove*(startSquare, targetSquare: Square, flags: varargs[MoveFlag]): M
for flag in flags:
result.flags = result.flags or flag.uint8
proc createMove*(startSquare, targetSquare: string, flags: varargs[MoveFlag]): Move {.inline, noinit.} =
result = createMove(startSquare.toSquare(), targetSquare.toSquare(), flags)
func createMove*(startSquare, targetSquare: SomeInteger, flags: varargs[MoveFlag]): Move {.inline, noinit.} =
result = createMove(Square(startSquare.int8), Square(targetSquare.int8), flags)
func createMove*(startSquare: Square, targetSquare: SomeInteger, flags: varargs[MoveFlag]): Move {.inline, noinit.} =
result = createMove(startSquare, Square(targetSquare.int8), flags)
func nullMove*: Move {.inline, noinit.} = createMove(Square(0), Square(0))
@@ -201,14 +199,15 @@ func isDoublePush*(move: Move): bool {.inline.} =
func isTactical*(self: Move): bool {.inline.} =
## Returns whether the given move
## is considered tactical
## is considered tactical (changes
## the material balance on the board)
return self.isPromotion() or self.isCapture() or self.isEnPassant()
func isQuiet*(self: Move): bool {.inline.} =
## Returns whether the given move is
## a quiet
return not self.isCapture() and not self.isEnPassant() and not self.isPromotion()
## considered quiet (not a tactical move)
return not self.isTactical()
func getFlags*(move: Move): seq[MoveFlag] =

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -164,7 +164,7 @@ func queenSideCastling*(piece: Piece): Square {.inline.} =
discard
proc toPretty*(piece: Piece): string {.inline.} =
func toPretty*(piece: Piece): string {.inline.} =
case piece.color:
of White:
case piece.kind:

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -16,11 +16,11 @@ import std/strutils
import heimdall/bitboards
import heimdall/magics
import heimdall/util/magics
import heimdall/pieces
import heimdall/zobrist
import heimdall/util/zobrist
import heimdall/moves
import heimdall/rays
import heimdall/util/rays
export bitboards, magics, pieces, zobrist, moves, rays

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -15,25 +15,26 @@
import heimdall/see
import heimdall/eval
import heimdall/board
import heimdall/util/limits
import heimdall/movegen
import heimdall/util/wdl
import heimdall/util/tunables
import heimdall/util/limits
import heimdall/util/shared
import heimdall/util/aligned
import heimdall/util/tunables
import heimdall/transpositions
import heimdall/movegen
import std/math
import std/times
import std/options
import std/atomics
import std/terminal
import std/strutils
import std/monotimes
import std/strformat
import std/terminal
import std/heapqueue
export shared
# Miscellaneous parameters that are not meant to be tuned

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
## Implementation of Static Exchange Evaluation
import heimdall/position
import heimdall/pieces
import heimdall/board
import heimdall/position
const PIECE_SCORES: array[PieceKind.Pawn..PieceKind.Empty, int] = [100, 450, 450, 650, 1250, 0, 0]
@@ -54,8 +54,8 @@ func gain(position: Position, move: Move): int =
func popLeastValuable(position: Position, occupancy: var Bitboard, attackers: Bitboard, stm: PieceColor): PieceKind =
## Returns the piece type in the given position containing the lowest
## value victim in the given attackers bitboard
## Pops the piece type of the lowest value victim off
## the given attackers bitboard
for kind in PieceKind.all():
let board = attackers and position.getBitboard(kind, stm)
@@ -69,10 +69,10 @@ func popLeastValuable(position: Position, occupancy: var Bitboard, attackers: Bi
proc see*(position: Position, move: Move, threshold: int): bool =
## Statically evaluates a sequence of exchanges
## starting from the given one and returns whether
## the exchange can beat the given (positive!) threshold.
## A sequence of moves leading to a losing capture (score < 0)
## will short-circuit and return false regardless of the value
## of the threshold
## the exchange can beat the given threshold.
## A sequence of moves leading to a losing capture
## (score < 0) will short-circuit and return false
## regardless of the value of the threshold
# Yoinked from Stormphrax

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,13 +13,13 @@
# limitations under the License.
## Implementation of a transposition table
import std/options
import std/math
import std/options
import heimdall/zobrist
import heimdall/eval
import heimdall/moves
import heimdall/util/zobrist
import nint128

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -12,17 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import heimdall/movegen
import heimdall/eval
import heimdall/uci
import heimdall/datagen/scharnagl
import heimdall/movegen
import heimdall/nnue/model
import heimdall/datagen/scharnagl
import std/math
import std/times
import std/strformat
import std/strutils
import std/times
import std/math
from std/lenientops import `/`

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -14,23 +14,23 @@
## Implementation of a UCI compatible server
import std/os
import std/strutils
import std/strformat
import std/random
import std/atomics
import std/options
import std/terminal
import std/random
import std/strutils
import std/strformat
randomize()
import heimdall/board
import heimdall/movegen
import heimdall/search
import heimdall/eval
import heimdall/util/tunables
import heimdall/board
import heimdall/search
import heimdall/movegen
import heimdall/util/limits
import heimdall/util/aligned
import heimdall/util/tunables
import heimdall/transpositions

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,14 +13,14 @@
# limitations under the License.
## Time management routines for Heimdall's search
import std/monotimes
import std/atomics
import std/times
import std/atomics
import std/options
import std/monotimes
import heimdall/moves
import heimdall/eval
import heimdall/moves
import heimdall/util/shared
import heimdall/util/tunables

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -376,13 +376,13 @@ when not isMainModule:
BuildOSRelaDire = PathX[fdDire, arRela, BuildOS, true]
func buildPath: auto {.compileTime.} =
result = currentSourcePath().BuildOSAbsoFile.parentDir() / BuildOSRelaDire("resources") / BuildOSRelaDire("magics")
result = currentSourcePath().BuildOSAbsoFile.parentDir().parentDir() / BuildOSRelaDire("resources") / BuildOSRelaDire("magics")
const
path = buildPath()
magicFile = staticRead($(path / BuildOSRelaFile("magics.json")))
rookMovesFile = staticRead($(path / BuildOSRelaFile("rooks.json")))
bishopMovesFile = staticRead($(path / BuildOSRelaFile("bishops.json")))
magicFile = staticRead($(path / BuildOSRelaFile("/magics.json")))
rookMovesFile = staticRead($(path / BuildOSRelaFile("/rooks.json")))
bishopMovesFile = staticRead($(path / BuildOSRelaFile("/bishops.json")))
var magics = magicFile.fromJson(TableRef[string, array[Square(0)..Square(63), MagicEntry]])
var bishopMoves = bishopMovesFile.fromJSON(array[Square(0)..Square(63), array[512, Bitboard]])
var rookMoves = rookMovesFile.fromJSON(array[Square(0)..Square(63), array[4096, Bitboard]])

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
# limitations under the License.
import heimdall/bitboards
import heimdall/magics
import heimdall/util/magics
import heimdall/pieces
export bitboards, pieces

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
when defined(avx512):
{.localPassC:"-mavx512f -mavx512bw".}
import heimdall/util/avx512_intrin
import heimdall/util/avx/avx512_intrin
type
VEPI16* = M512i
@@ -26,7 +26,7 @@ when defined(avx512):
# Since Nim has apparently no way to grab the size of M256i using its own sizeof(),
# we have to fallback to writing a minimal C file from which we can then import the
# value
var sz* {.header:"heimdall/util/simd_avx512_size.h", importc:"CHUNK_SIZE".}: cint
var sz* {.header:"heimdall/util/avx/simd_avx512_size.h", importc:"CHUNK_SIZE".}: cint
let CHUNK_SIZE* = int(sz)
# Routines blatantly stolen from Alexandria. Many thanks cj!
@@ -53,7 +53,7 @@ else:
# Since Nim has apparently no way to grab the size of M256i using its own sizeof(),
# we have to fallback to writing a minimal C file from which we can then import the
# value
var sz* {.header:"heimdall/util/simd_avx2_size.h", importc:"CHUNK_SIZE".}: cint
var sz* {.header:"heimdall/util/avx/simd_avx2_size.h", importc:"CHUNK_SIZE".}: cint
let CHUNK_SIZE* = int(sz)
# Routines blatantly stolen from Alexandria. Many thanks cj!

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -14,8 +14,8 @@
import std/math
import std/tables
import std/strformat
import std/strutils
import std/strformat
const isTuningEnabled* {.booldefine:"enableTuning".} = false

View File

@@ -2,8 +2,8 @@ import heimdall/eval
import heimdall/util/shared
import std/sequtils
import std/math
import std/sequtils
# Yoinked from https://github.com/Ciekce/Stormphrax/blob/main/src/wdl.{cpp,h}

View File

@@ -1,4 +1,4 @@
# Copyright 2024 Mattia Giambirtone & All Contributors
# Copyright 2025 Mattia Giambirtone & All Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@ func `==`*(a, b: TruncatedZobristKey): bool {.borrow.}
func `$`*(a: TruncatedZobristKey): string {.borrow.}
proc computeZobristKeys: array[781, ZobristKey] =
func computeZobristKeys: array[781, ZobristKey] {.compileTime.} =
## Precomputes our zobrist keys
var prng = initRand(69420) # Nice.
@@ -58,19 +58,19 @@ proc computeZobristKeys: array[781, ZobristKey] =
let ZOBRIST_KEYS = computeZobristKeys()
const ZOBRIST_KEYS = computeZobristKeys()
const PIECE_TO_INDEX = [[3, 2, 0, 5, 4, 1], [9, 8, 6, 11, 10, 7]]
proc getKey*(piece: Piece, square: Square): ZobristKey {.inline.} =
func getKey*(piece: Piece, square: Square): ZobristKey {.inline.} =
let index = PIECE_TO_INDEX[piece.color.int][piece.kind.int] * 64 + square.int
return ZOBRIST_KEYS[index]
proc getBlackToMoveKey*: ZobristKey {.inline.} = ZOBRIST_KEYS[768]
func getBlackToMoveKey*: ZobristKey {.inline.} = ZOBRIST_KEYS[768]
proc getQueenSideCastlingKey*(color: PieceColor): ZobristKey {.inline.} =
func getQueenSideCastlingKey*(color: PieceColor): ZobristKey {.inline.} =
case color:
of White:
return ZOBRIST_KEYS[769]
@@ -80,7 +80,7 @@ proc getQueenSideCastlingKey*(color: PieceColor): ZobristKey {.inline.} =
discard
proc getKingSideCastlingKey*(color: PieceColor): ZobristKey {.inline.} =
func getKingSideCastlingKey*(color: PieceColor): ZobristKey {.inline.} =
case color:
of White:
return ZOBRIST_KEYS[770]
@@ -90,4 +90,4 @@ proc getKingSideCastlingKey*(color: PieceColor): ZobristKey {.inline.} =
discard
proc getEnPassantKey*(file: SomeInteger): ZobristKey {.inline.} = ZOBRIST_KEYS[773 + file]
func getEnPassantKey*(file: SomeInteger): ZobristKey {.inline.} = ZOBRIST_KEYS[773 + file]