Chess-Plisco

 view release on metacpan or  search on metacpan

lib/Chess/Plisco/Engine/TranspositionTable.pm  view on Meta::CPAN

#! /bin/false

# Copyright (C) 2021-2026 Guido Flohr <guido.flohr@cantanea.com>,
# all rights reserved.

# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What the Fuck You Want
# to Public License, Version 2, as published by Sam Hocevar. See
# http://www.wtfpl.net/ for more details.

# Rename that back to TranspositionTable, once done.
package Chess::Plisco::Engine::TranspositionTable;
$Chess::Plisco::Engine::TranspositionTable::VERSION = 'v1.0.3';
use strict;
use integer;

use Time::HiRes qw(gettimeofday tv_interval);

# Macros from Chess::Plisco::Macro are already expanded here!
use Chess::Plisco::Engine::Constants;

# The transposition table is organised into clusters, which in turn contain
# 4 buckets.
use constant CLUSTER_CAPACITY => 4;

# We store the keys separately from the actual entries, because that is easier
# to scan. The layout of a cluster is then like this:

# key0       16 bit
# key1       16 bit
# key2       16 bit
# key3       16 bit
# depth       8 bit start of bucket 0
# generation  5 bit
# pv node     1 bit
# bound type  2 bit
# move       16 bit
# value      16 bit
# evaluation 16 bit end of bucket 0
# bucket1    64 bit 
# bucket2    64 bit 
# bucket3    64 bit 

use constant KEY_BYTES => 2;
use constant BUCKET_BYTES => 8;

use constant TT_ENTRY_SIZE => 16;

use constant GENERATION_BITS => 3;
use constant GENERATION_DELTA => (1 << (GENERATION_BITS));
use constant GENERATION_CYCLE => 255 + GENERATION_DELTA;
use constant GENERATION_MASK => (0xFF << (GENERATION_BITS)) & 0xFF;

my $generation = 0;
my $last_hashfull;
my $last_hashfull_probe = 0;

sub new {
	my ($class, $size) = @_;

	# Reading array entries is almost 50 % faster than access strings with
	# substr.
	my $self = [];
	bless $self, $class;

	return $self->resize($size);
}

sub clear {
	my ($self) = @_;

	my $cluster_bytes = CLUSTER_CAPACITY * (KEY_BYTES + BUCKET_BYTES);

	$self->[$_] = "\0" x $cluster_bytes for 0 .. $#$self;

	return $self;
}

sub resize {



( run in 0.978 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )