Skip to content

Commit

Permalink
Added specialized clamp for clamping to type
Browse files Browse the repository at this point in the history
  • Loading branch information
tsoj committed Aug 12, 2023
1 parent fdae96a commit dd18e38
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
13 changes: 6 additions & 7 deletions move.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,22 @@ template create*(
move = cast[Move](cast[uint32](move) or ((capturedEnPassant.uint32 and 0b1) shl 24))
move = cast[Move](cast[uint32](move) or ((enPassantTarget.uint32 and 0b1111111) shl 25))

# TODO create a clampToType(number, type): type function that does this
template source*(move: Move): Square =
clamp(cast[uint32](move) and 0b1111111, Square.low.uint32, Square.high.uint32).Square
(cast[uint32](move) and 0b1111111).clampToType(Square)
template target*(move: Move): Square =
clamp((cast[uint32](move) shr 7) and 0b1111111, Square.low.uint32, Square.high.uint32).Square
((cast[uint32](move) shr 7) and 0b1111111).clampToType(Square)
template moved*(move: Move): Piece =
clamp((cast[uint32](move) shr 14) and 0b111, Piece.low.uint32, Piece.high.uint32).Piece
((cast[uint32](move) shr 14) and 0b111).clampToType(Piece)
template captured*(move: Move): Piece =
clamp((cast[uint32](move) shr 17) and 0b111, Piece.low.uint32, Piece.high.uint32).Piece
((cast[uint32](move) shr 17) and 0b111).clampToType(Piece)
template promoted*(move: Move): Piece =
clamp((cast[uint32](move) shr 20) and 0b111, Piece.low.uint32, Piece.high.uint32).Piece
((cast[uint32](move) shr 20) and 0b111).clampToType(Piece)
template castled*(move: Move): bool =
((cast[uint32](move) shr 23) and 0b1).bool
template capturedEnPassant*(move: Move): bool =
((cast[uint32](move) shr 24) and 0b1).bool
template enPassantTarget*(move: Move): Square =
clamp((cast[uint32](move) shr 25) and 0b1111111, Square.low.uint32, Square.high.uint32).Square
((cast[uint32](move) shr 25) and 0b1111111).clampToType(Square)

const noMove*: Move = block:
var move: Move
Expand Down
6 changes: 2 additions & 4 deletions search.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import
moveIterator,
hashTable,
evaluation,
utils,
see

import std/[
Expand All @@ -24,10 +25,7 @@ const
aspirationWindowMultiplier = 2.0

func futilityReduction(value: Value): Ply =
clamp(
value.toCp div 100,
Ply.low.int, Ply.high.int
).Ply
clampToType(value.toCp div 100, Ply)

func hashResultFutilityMargin(depthDifference: Ply): Value =
depthDifference.Value * 300.cp
Expand Down
1 change: 0 additions & 1 deletion tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ proc getPerftTestData(useInternal, useExternal: bool): seq[PerftData] =
"b1rkrbnq/1pp1pppp/2np4/p5N1/8/1P2P3/P1PP1PPP/BNRKRB1Q w CEce - 0 9,37,740,27073,581744,21156664,485803600",
"nrknbrqb/3p1ppp/ppN1p3/8/6P1/8/PPPPPP1P/1RKNBRQB w BFbf - 0 9,32,526,17267,319836,10755190,220058991",
"bbqrnnkr/1ppp1p1p/5p2/p5p1/P7/1P4P1/2PPPP1P/1BQRNNKR w DKdk - 0 9,20,322,7224,145818,3588435,82754650"

]
else:
@[]
Expand Down
2 changes: 1 addition & 1 deletion uci.nim
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ proc go(uciState: var UciState, params: seq[string], searchThreadResult: var Flo
if i+1 < params.len:
case params[i]:
of "depth":
searchInfo.targetDepth = params[i+1].parseInt.clamp(Ply.low, Ply.high).Ply
searchInfo.targetDepth = params[i+1].parseInt.clampToType(Ply)
of "movestogo":
searchInfo.movesToGo = params[i+1].parseInt.int16
of "winc":
Expand Down
3 changes: 3 additions & 0 deletions utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,6 @@ proc stopwatch*(flag: ptr Atomic[bool], duration: Duration): bool =
if now() - start >= duration:
flag[].store(true)
sleep(sleepTimeMs)

func clampToType*[In, Out](x: In, OutType: typedesc[Out]): Out =
x.clamp(OutType.low.In, OutType.high.In).Out

0 comments on commit dd18e38

Please sign in to comment.