@@ -83,35 +83,55 @@ public static void Set(this ref Transposition entry, ulong hash, byte depth, int
83
83
entry . Flag = nodeType ;
84
84
entry . Move = move != 0 ? move : entry . Move ; //Don't clear TT move if no best move is provided: keep old one
85
85
}
86
- public static unsafe int GetMaxTranspositionTableSizeInMb ( )
87
- {
88
- return ( int ) ( ( long ) int . MaxValue * sizeof ( Transposition ) / ( 1024 * 1024 ) ) ;
89
- }
90
- public static unsafe uint CalculateTranspositionTableSize ( int sizeInMb )
86
+
87
+ public static unsafe long CalculateTranspositionTableSize ( long sizeInMb )
91
88
{
92
- int maxAllowedSizeInMb = ( int ) ( ( long ) int . MaxValue * sizeof ( Transposition ) / ( 1024 * 1024 ) ) ;
89
+ if ( sizeInMb <= 0 )
90
+ throw new ArgumentOutOfRangeException ( nameof ( sizeInMb ) , "Size must be positive." ) ;
93
91
94
- // Cap to the maximum if necessary
95
- if ( sizeInMb > maxAllowedSizeInMb )
96
- {
97
- sizeInMb = maxAllowedSizeInMb ;
98
- }
92
+ const long EightGB = 8L * 1024 ;
93
+ const long TwoGB = 2L * 1024 ;
94
+
95
+ ulong estimatedCount = ( ulong ) sizeInMb * 1024 * 1024 / ( ulong ) sizeof ( Transposition ) ;
96
+
97
+ if ( estimatedCount == 0 )
98
+ throw new OverflowException ( "Requested size is too small to store even one transposition." ) ;
99
+
100
+ // Find powers of two above and below the estimated count
101
+ ulong lower = BitOperations . IsPow2 ( estimatedCount )
102
+ ? estimatedCount
103
+ : BitOperations . RoundUpToPowerOf2 ( estimatedCount ) >> 1 ;
99
104
100
- ulong transpositionCount = ( ulong ) sizeInMb * 1024 * 1024 / ( ulong ) sizeof ( Transposition ) ;
105
+ ulong upper = lower << 1 ;
101
106
102
- // Round to nearest lower power of two (adjust if needed)
103
- if ( ! BitOperations . IsPow2 ( transpositionCount ) )
107
+ // Calculate size in MB for the rounded counts
108
+ long lowerMb = ( long ) ( lower * ( ulong ) sizeof ( Transposition ) / ( 1024 * 1024 ) ) ;
109
+ long upperMb = ( long ) ( upper * ( ulong ) sizeof ( Transposition ) / ( 1024 * 1024 ) ) ;
110
+
111
+ long chosenMb ;
112
+
113
+ if ( sizeInMb <= EightGB )
104
114
{
105
- transpositionCount = BitOperations . RoundUpToPowerOf2 ( transpositionCount ) >> 1 ;
115
+ // Always round up under 8GB
116
+ chosenMb = upperMb ;
106
117
}
107
-
108
- // If still too large, clamp to int.MaxValue
109
- if ( transpositionCount > int . MaxValue )
118
+ else if ( ( upperMb - sizeInMb ) <= TwoGB )
110
119
{
111
- transpositionCount = ( uint ) ( 1u << 31 ) ; // 2^31 (still fits in uint)
120
+ // Round up if it's within 2GB
121
+ chosenMb = upperMb ;
112
122
}
123
+ else
124
+ {
125
+ // Otherwise round down
126
+ chosenMb = lowerMb ;
127
+ }
128
+
129
+ ulong finalCount = ( ulong ) chosenMb * 1024 * 1024 / ( ulong ) sizeof ( Transposition ) ;
130
+
131
+ if ( finalCount == 0 || finalCount > long . MaxValue )
132
+ throw new OverflowException ( "Final transposition table size is too large." ) ;
113
133
114
- return ( uint ) transpositionCount ;
134
+ return ( long ) finalCount ;
115
135
}
116
136
117
137
public static unsafe int CalculateSizeInMb ( uint transpositionCount )
0 commit comments