perf: Adler-32 by 88-byte SWAR blocks (2.4x interpreted); inflate tables carry length / distance base and extra bits; tools/bench_core.luau

This commit is contained in:
4DBug committed 2026-11-01 18:49:31 -06:00
1 parent 01c044805f
commit 158fa3d3f0
4 files changed
+341 -31

No files matched your search

+25 -11
View File
@@ -2,24 +2,38 @@
--!native
-- core/Adler32: Adler-32 (RFC 1950, the zlib checksum). Absorbed from deflate-luau src/Deflate/Checksum.luau (same author;
-- no third-party code). Never throws: the range is clamped.
local readu8 = buffer.readu8
local readu8, readu32 = buffer.readu8, buffer.readu32
local band, rshift = bit32.band, bit32.rshift
local function adler32(buf: buffer, off: number, len: number, adler: number?): number
local a0 = adler or 1
local a, b = a0 % 65536, a0 // 65536
local i, e = off, off + len
while i < e do
-- zlib NMAX: the largest n with 255 n (n + 1) / 2 + (n + 1) (BASE - 1) < 2^32
local stop = math.min(e, i + 5552)
while i < stop do
a += readu8(buf, i)
b += a
i += 1
-- 88-byte blocks, two bytes per 16-bit lane of each u32: pl / ph sum bytes 0, 2 / 1, 3 of the words, ql / qh sum
-- those prefix sums (each lane stays below 2^16 over 22 words). Byte t of word j (of m) weighs 4 (m - j) - t in b.
local e88 = e - 88
while i <= e88 do
local pl, ph, ql, qh = 0, 0, 0, 0
for j = i, i + 84, 4 do
local w = readu32(buf, j)
pl += band(w, 0x00FF00FF)
ph += band(rshift(w, 8), 0x00FF00FF)
ql += pl
qh += ph
end
a %= 65521
b %= 65521
i += 88
local s0, s2 = pl % 65536, pl // 65536
local s1, s3 = ph % 65536, ph // 65536
local q = ql % 65536 + ql // 65536 + qh % 65536 + qh // 65536
b = (b + 88 * a + 4 * q - s1 - 2 * s2 - 3 * s3) % 65521
a = (a + s0 + s1 + s2 + s3) % 65521
end
return b * 65536 + a
while i < e do
a += readu8(buf, i)
b += a
i += 1
end
return (b % 65521) * 65536 + a % 65521
end
local function range(buf: buffer, offset: number?, length: number?): (number, number)