perf: CRC-64/XZ slice-by-4

This commit is contained in:
4DBug committed 2026-11-01 19:48:05 -06:00
1 parent 5b4ee7f937
commit 4eca395b00
1 file changed
+31 -2
+31 -2
View File
@@ -4,7 +4,7 @@
-- codecs/compress/Lzma/Crc64: CRC-64/XZ (ECMA-182 polynomial, reflected: 0xC96C5795D7870F42), xz check type 4.
-- Written from the xz file format specification; the 64-bit value is kept as two u32 halves (hi, lo).
local band, bxor, rshift, lshift, bor = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift, bit32.bor
local readu8 = buffer.readu8
local readu8, readu32 = buffer.readu8, buffer.readu32
local THI, TLO = table.create(256, 0), table.create(256, 0)
for n = 0, 255 do
@@ -21,14 +21,43 @@ for n = 0, 255 do
THI[n + 1], TLO[n + 1] = hi, lo
end
-- slice-by-4: H[k] / L[k][n] = the register after byte n and k zero bytes
local H1, H2, H3 = table.create(256, 0), table.create(256, 0), table.create(256, 0)
local L1, L2, L3 = table.create(256, 0), table.create(256, 0), table.create(256, 0)
for n = 0, 255 do
local hi, lo = THI[n + 1], TLO[n + 1]
for k = 1, 3 do
local idx = band(lo, 255) + 1
lo = bxor(TLO[idx], bor(rshift(lo, 8), lshift(band(hi, 255), 24)))
hi = bxor(THI[idx], rshift(hi, 8))
if k == 1 then
H1[n + 1], L1[n + 1] = hi, lo
elseif k == 2 then
H2[n + 1], L2[n + 1] = hi, lo
else
H3[n + 1], L3[n + 1] = hi, lo
end
end
end
local Crc64 = {}
function Crc64.of(b: buffer, o: number, n: number): (number, number)
local hi, lo = 0xFFFFFFFF, 0xFFFFFFFF
for i = o, o + n - 1 do
local i, e = o, o + n
local e4 = e - 4
while i <= e4 do
local w = bxor(lo, readu32(b, i))
local a, c, d, f = band(w, 255) + 1, band(rshift(w, 8), 255) + 1, band(rshift(w, 16), 255) + 1, rshift(w, 24) + 1
lo = bxor(L3[a], L2[c], L1[d], TLO[f], hi)
hi = bxor(H3[a], H2[c], H1[d], THI[f])
i += 4
end
while i < e do
local idx = band(bxor(lo, readu8(b, i)), 255) + 1
lo = bxor(TLO[idx], bor(rshift(lo, 8), lshift(band(hi, 255), 24)))
hi = bxor(THI[idx], rshift(hi, 8))
i += 1
end
return bxor(hi, 0xFFFFFFFF), bxor(lo, 0xFFFFFFFF)
end