88#if ! NETSTANDARD2_0
99using System . Runtime . Intrinsics ;
1010using System . Runtime . Intrinsics . X86 ;
11+
1112#endif
1213
1314#if NET6_0_OR_GREATER
@@ -169,19 +170,31 @@ private void EnsureCapacity(long maximumSize)
169170
170171 private unsafe int EstimateFrequencyStd ( T value )
171172 {
172- var count = stackalloc int [ 4 ] ;
173173 int blockHash = Spread ( comparer . GetHashCode ( value ) ) ;
174174 int counterHash = Rehash ( blockHash ) ;
175175 int block = ( blockHash & blockMask ) << 3 ;
176176
177- for ( int i = 0 ; i < 4 ; i ++ )
178- {
179- int h = ( int ) ( ( uint ) counterHash >> ( i << 3 ) ) ;
180- int index = ( h >> 1 ) & 15 ;
181- int offset = h & 1 ;
182- count [ i ] = ( int ) ( ( ( ulong ) table [ block + offset + ( i << 1 ) ] >> ( index << 2 ) ) & 0xfL ) ;
183- }
184- return Math . Min ( Math . Min ( count [ 0 ] , count [ 1 ] ) , Math . Min ( count [ 2 ] , count [ 3 ] ) ) ;
177+ int h0 = counterHash ;
178+ int h1 = counterHash >>> 8 ;
179+ int h2 = counterHash >>> 16 ;
180+ int h3 = counterHash >>> 24 ;
181+
182+ int index0 = ( h0 >>> 1 ) & 15 ;
183+ int index1 = ( h1 >>> 1 ) & 15 ;
184+ int index2 = ( h2 >>> 1 ) & 15 ;
185+ int index3 = ( h3 >>> 1 ) & 15 ;
186+
187+ int slot0 = block + ( h0 & 1 ) ;
188+ int slot1 = block + ( h1 & 1 ) + 2 ;
189+ int slot2 = block + ( h2 & 1 ) + 4 ;
190+ int slot3 = block + ( h3 & 1 ) + 6 ;
191+
192+ int count0 = ( int ) ( ( table [ slot0 ] >>> ( index0 << 2 ) ) & 0xfL ) ;
193+ int count1 = ( int ) ( ( table [ slot1 ] >>> ( index1 << 2 ) ) & 0xfL ) ;
194+ int count2 = ( int ) ( ( table [ slot2 ] >>> ( index2 << 2 ) ) & 0xfL ) ;
195+ int count3 = ( int ) ( ( table [ slot3 ] >>> ( index3 << 2 ) ) & 0xfL ) ;
196+
197+ return Math . Min ( Math . Min ( count0 , count1 ) , Math . Min ( count2 , count3 ) ) ;
185198 }
186199
187200 private unsafe void IncrementStd ( T value )
@@ -190,7 +203,7 @@ private unsafe void IncrementStd(T value)
190203 int counterHash = Rehash ( blockHash ) ;
191204 int block = ( blockHash & blockMask ) << 3 ;
192205
193- // Loop unrolling improves throughput by 10m ops/s
206+ // Loop unrolling improves throughput
194207 int h0 = counterHash ;
195208 int h1 = counterHash >>> 8 ;
196209 int h2 = counterHash >>> 16 ;
0 commit comments