perf: slice-by-8 MSB-first CRC-32 (Crc32.msb) for Ogg pages and bzip2 blocks; bzip2 checks each block's CRC over its output at once and moves long MTF shifts with table.move

This commit is contained in:
4DBug committed 2026-11-01 19:46:46 -06:00
1 parent 2dac8b2a14
commit 5b4ee7f937
5 files changed
+56 -33

No files matched your search

+41 -2
View File
@@ -2,7 +2,7 @@
--!native
-- core/Crc32: CRC-32 (IEEE 802.3, the zlib / gzip / zip / PNG polynomial 0xEDB88320, reflected), slice-by-8.
-- Absorbed from deflate-luau src/Deflate/Checksum.luau (same author; no third-party code). Never throws.
local band, bxor, rshift, lshift = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift
local band, bxor, rshift, lshift, byteswap = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift, bit32.byteswap
local readu8, readu32 = buffer.readu8, buffer.readu32
local T0, T1, T2, T3 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0)
@@ -99,10 +99,49 @@ for n = 0, 255 do
end
Crc32.MSB_TABLE = MSB
-- slice-by-8 for the MSB-first CRC: M[k][n] = the CRC register after byte n and k zero bytes
local M1, M2, M3 = table.create(256, 0), table.create(256, 0), table.create(256, 0)
local M4, M5, M6, M7 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0)
for n = 0, 255 do
local c = MSB[n + 1]
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
M1[n + 1] = c
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
M2[n + 1] = c
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
M3[n + 1] = c
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
M4[n + 1] = c
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
M5[n + 1] = c
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
M6[n + 1] = c
c = bxor(lshift(c, 8), MSB[rshift(c, 24) + 1])
M7[n + 1] = c
end
function Crc32.msb(buf: buffer, off: number, len: number, init: number?): number
local c = init or 0
for i = off, off + len - 1 do
local i, e = off, off + len
local e8 = e - 8
while i <= e8 do
c = bxor(c, byteswap(readu32(buf, i)))
local d = byteswap(readu32(buf, i + 4))
c = bxor(
M7[rshift(c, 24) + 1],
M6[band(rshift(c, 16), 255) + 1],
M5[band(rshift(c, 8), 255) + 1],
M4[band(c, 255) + 1],
M3[rshift(d, 24) + 1],
M2[band(rshift(d, 16), 255) + 1],
M1[band(rshift(d, 8), 255) + 1],
MSB[band(d, 255) + 1]
)
i += 8
end
while i < e do
c = bxor(lshift(c, 8), MSB[bxor(rshift(c, 24), readu8(buf, i)) + 1])
i += 1
end
return c
end