99 CRC32Fast (HlpCRC32Fast.pas): PKZIP/Castagnoli only; FCurrentCRC with not/xor
1010 convention; uses CRCDispatch_UpdateReflectedCrc32 + TCRCFoldRuntimeCtx32.
1111
12- Keep MSB SIMD kernels aligned: CRCFoldMsbSse2.inc (x64 offsets +224/+228) vs
13- CRCFoldMsbSse2_i386.inc (+160/+164 for 32-bit TableRow pointers).
12+ TCRCFoldRuntimeCtx64 matches Ctx32 shape: FoldConstants + TableRow only.
13+ MSB fold reads CRC width from FoldConstants.CrcBits (see TGF2.GenerateFoldConstants)
14+ and derives the state mask the same way as TCRC.FCRCMask.
1415}
1516
1617{ $I ..\Include\HashLib.inc}
@@ -25,12 +26,11 @@ interface
2526 MinSimdBytes = Int32(16 );
2627
2728type
28- // Runtime context: PCLMUL reads first field only (offset 0).
29+ // Runtime context: PCLMUL reads first field only (offset 0). Same layout as
30+ // TCRCFoldRuntimeCtx32 apart from TableRow pointer size (PUInt64 vs PUInt32).
2931 TCRCFoldRuntimeCtx64 = packed record
3032 FoldConstants: TCRCFoldConstants;
3133 TableRow: array [0 .. 15 ] of PUInt64;
32- Width: Int32;
33- CrcMask: UInt64;
3434 end ;
3535
3636 PCRCFoldRuntimeCtx64 = ^TCRCFoldRuntimeCtx64;
@@ -94,6 +94,11 @@ function CrcTableU32(const Row: PUInt32; B: Byte): UInt32; inline;
9494 Result := PUInt32(NativeUInt(Row) + UInt64(B) * SizeOf(UInt32))^;
9595end ;
9696
97+ function CRCMaskFromWidth (AWidth: Int32): UInt64; inline;
98+ begin
99+ Result := ((UInt64(1 ) shl (AWidth - 1 )) - 1 ) shl 1 or 1 ;
100+ end ;
101+
97102function CRC_Fold_Lsb_Scalar (AData: PByte; ALength: UInt32;
98103 AState: Pointer; AConstants: Pointer): UInt64;
99104var
@@ -144,14 +149,18 @@ function CRC_Fold_Msb_Scalar(AData: PByte; ALength: UInt32;
144149 LTemp, LNewTemp, LTempCopy: UInt64;
145150 LPtr: PByte;
146151 LLen: UInt32;
152+ LWidth: Int32;
153+ LCrcMask: UInt64;
147154 LCrcBytes, LBIdx: Int32;
148155 LByte: Byte;
149156begin
150157 Ctx := PCRCFoldRuntimeCtx64(AConstants);
151158 LPtr := AData;
152159 LLen := ALength;
153160 LTemp := PUInt64(AState)^;
154- LCrcBytes := (Ctx.Width + 7 ) shr 3 ;
161+ LWidth := Int32(Ctx.FoldConstants.CrcBits);
162+ LCrcMask := CRCMaskFromWidth(LWidth);
163+ LCrcBytes := (LWidth + 7 ) shr 3 ;
155164
156165 while LLen >= 16 do
157166 begin
@@ -161,8 +170,8 @@ function CRC_Fold_Msb_Scalar(AData: PByte; ALength: UInt32;
161170 LBIdx := 0 ;
162171 while LBIdx < LCrcBytes do
163172 begin
164- LByte := LPtr[LBIdx] xor Byte(LTempCopy shr (Ctx.Width - 8 ));
165- LTempCopy := (LTempCopy shl 8 ) and Ctx.CrcMask ;
173+ LByte := LPtr[LBIdx] xor Byte(LTempCopy shr (LWidth - 8 ));
174+ LTempCopy := (LTempCopy shl 8 ) and LCrcMask ;
166175 LNewTemp := LNewTemp xor CrcTableU64(Ctx.TableRow[15 - LBIdx], LByte);
167176 System.Inc(LBIdx);
168177 end ;
@@ -295,8 +304,6 @@ procedure CRCDispatch_InitRuntimeCtx64(const Table: THashLibMatrixUInt64Array;
295304 TGF2.GenerateFoldConstants(APoly, AWidth, AReflected, Ctx.FoldConstants);
296305 for I := 0 to 15 do
297306 Ctx.TableRow[I] := PUInt64(@Table[I][0 ]);
298- Ctx.Width := AWidth;
299- Ctx.CrcMask := ((UInt64(1 ) shl (AWidth - 1 )) - 1 ) shl 1 or 1 ;
300307end ;
301308
302309procedure CRCDispatch_InitRuntimeCtx32 (const Table: THashLibMatrixUInt32Array;
0 commit comments