audio perf: FLAC keeps floor(x * inv) / floor(u * 0.5) (LuaJIT: a division is slower)

This commit is contained in:
4DBug committed 2026-11-01 20:07:09 -06:00
1 parent 688503fb2d
commit a5a03ecb8d
3 files changed
+523 -519

No files matched your search

+61 -60
View File
@@ -26724,7 +26724,7 @@ local history = st.mb
if pos > limit then
return -1 -- each value reads at most 41 bits past here: inside the 16 padding bytes
end
local k = 31 - countlz(floor(history / 512) + 3)
local k = 31 - countlz(math.floor(history / 512 )+ 3)
if k > kb then
k = kb
end
@@ -26736,15 +26736,15 @@ local history = st.mb
))) if x > 0xFFFF then
history = 0xFFFF
else
history =history+ (x * mult - floor(history * mult / 512)
) end
history =history+ (x * mult - math.floor((history * mult) / 512
)) end
if history < 128 and i < n then
if pos > limit then
return -1
end
local lg = (history > 0 and((31 - countlz(history) ))or(0
)) k = 7 - lg + floor((history + 16) / 64)
if k > kb then
)) k = 7 - lg + math.floor((history + 16) / 64
) if k > kb then
k = kb
end
local run
@@ -26801,8 +26801,8 @@ local function predict(err, out, n, bps, coefs, order, quant)
for j = 1, order do
val =val+ (out[i - j] - d) * coefs[j]
end
val = floor((val + half) / qd)
local e = err[i]
val = math.floor((val + half) / qd -- qd is 2^quant: exact
) local e = err[i]
out[i] = signExtend(val + d + e, bps)
if e ~= 0 then
local es = e > 0 and 1 or-1
@@ -26814,7 +26814,7 @@ local function predict(err, out, n, bps, coefs, order, quant)
local v = d - out[i - j]
local sgn = (v > 0 and{es }or{(v < 0 and{(-es )}or{0
})[1]})[1] coefs[j]=coefs[j]- sgn
e =e- floor(v * sgn / qd) * (order - j + 1)
e =e- (math.floor((v * sgn) / qd)) * (order - j + 1)
end
end
i =i+ 1
@@ -29541,33 +29541,10 @@ for i = 0, 255 do
CRC16[i + 1] = d
end
-- slice-by-4: C1 = CRC16; Ck[i] = the CRC16 state after i (high byte) and k - 1 zero bytes
local C1, C2, C3, C4 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0)
for i = 0, 255 do
C1[i + 1] = CRC16[i + 1]
end
for i = 0, 255 do
local c = C1[i + 1]
c = bxor(band(lshift(c, 8), 0xFFFF), C1[rshift(c, 8) + 1])
C2[i + 1] = c
c = bxor(band(lshift(c, 8), 0xFFFF), C1[rshift(c, 8) + 1])
C3[i + 1] = c
c = bxor(band(lshift(c, 8), 0xFFFF), C1[rshift(c, 8) + 1])
C4[i + 1] = c
end
local function crc16(b, from, to)
local c = 0
local i = from
-- four bytes w (big-endian) at a time: the state is linear, so the new state is the xor of each byte of
-- (c << 16) ~ w pushed through 4, 3, 2 and 1 table steps
while i + 4 <= to do
local x = bxor(lshift(c, 16), byteswap(readu32(b, i)))
c = bxor(C4[rshift(x, 24) + 1], C3[band(rshift(x, 16), 255) + 1], C2[band(rshift(x, 8), 255) + 1], C1[band(x, 255) + 1])
i =i+ 4
end
for j = i, to - 1 do
c = bxor(band(lshift(c, 8), 0xFFFF), CRC16[bxor(rshift(c, 8), readu8(b, j)) + 1])
for i = from, to - 1 do
c = bxor(band(lshift(c, 8), 0xFFFF), CRC16[bxor(rshift(c, 8), readu8(b, i)) + 1])
end
return c
end
@@ -29826,8 +29803,8 @@ local method = bits(b, p, 2)
p =p+ k
end
local u = q * pk + r
local h = math.floor(u / 2
) s[j] = (h + h == u and{h }or{(-h - 1
local h = floor(u * 0.5)
s[j] = (h + h == u and{h }or{(-h - 1
)})[1] end
else
-- k = 25..30 (Rice2): the remainder takes two reads
@@ -29855,8 +29832,8 @@ local method = bits(b, p, 2)
local r = bits(b, p, k)
p =p+ k
local u = q * pk + r
local h = math.floor(u / 2
) s[j] = (h + h == u and{h }or{(-h - 1
local h = floor(u * 0.5)
s[j] = (h + h == u and{h }or{(-h - 1
)})[1] end
end
i = stop + 1
@@ -29903,13 +29880,14 @@ end
-- LPC restoration: s[i] += floor(sum(coef[j] * s[i - j]) / 2^shift); unrolled for orders up to 12
local function lpc(s, n, cf, order, shift)
local div = POW2[shift + 1] -- x // 2^shift is floor(x / 2^shift), exactly
local div = POW2[shift + 1]
local inv = 1 / div
if order <= 4 then
local c1, c2, c3, c4 = cf[1], cf[2] or 0, cf[3] or 0, cf[4] or 0
local s1, s2, s3, s4 = s[order], s[order - 1] or 0, s[order - 2] or 0, s[order - 3] or 0
for i = order + 1, n do
local v = s[i] + math.floor((c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4) / div
) s[i] = v
local v = s[i] + floor((c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4) * inv)
s[i] = v
s4 = s3
s3 = s2
s2 = s1
@@ -29921,8 +29899,8 @@ local function lpc(s, n, cf, order, shift)
local s6, s7, s8 = s[order - 5] or 0, s[order - 6] or 0, s[order - 7] or 0
for i = order + 1, n do
local v = s[i]
+ math.floor((c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4 + c5 * s5 + c6 * s6 + c7 * s7 + c8 * s8) / div
) s[i] = v
+ floor((c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4 + c5 * s5 + c6 * s6 + c7 * s7 + c8 * s8) * inv)
s[i] = v
s8 = s7
s7 = s6
s6 = s5
@@ -29940,11 +29918,13 @@ local function lpc(s, n, cf, order, shift)
local s10, s11, s12 = s[order - 9] or 0, s[order - 10] or 0, s[order - 11] or 0
for i = order + 1, n do
local v = s[i]
+ math.floor((
c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4 + c5 * s5 + c6 * s6
+ c7 * s7 + c8 * s8 + c9 * s9 + c10 * s10 + c11 * s11 + c12 * s12
) / div
) s[i] = v
+ floor(
(
c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4 + c5 * s5 + c6 * s6
+ c7 * s7 + c8 * s8 + c9 * s9 + c10 * s10 + c11 * s11 + c12 * s12
) * inv
)
s[i] = v
s12 = s11
s11 = s10
s10 = s9
@@ -29964,8 +29944,8 @@ local function lpc(s, n, cf, order, shift)
for j = 1, order do
sum =sum+ cf[j] * s[i - j]
end
s[i]=s[i]+ math.floor(sum / div
) end
s[i]=s[i]+ floor(sum * inv)
end
end
end
@@ -39050,14 +39030,16 @@ local function combFilter(mem, base, T0, T1, N, g0, g1,
overlap = 0
end
local i = 0
local d1 = 2 - T1
while i < overlap do
local p = base + i
local x0 = mem[p - T1 + 2]
local x0 = mem[p + d1]
local w = WINDOW[i + 1]
local f = w * w
local of = 1 - f
mem[p] = mem[p] + (of * g00) * mem[p - T0] + (of * g01) * (mem[p - T0 + 1] + mem[p - T0 - 1])
+ (of * g02) * (mem[p - T0 + 2] + mem[p - T0 - 2]) + (f * g10) * x2 + (f * g11) * (x1 + x3)
local t = p - T0
mem[p] = mem[p] + (of * g00) * mem[t] + (of * g01) * (mem[t + 1] + mem[t - 1])
+ (of * g02) * (mem[t + 2] + mem[t - 2]) + (f * g10) * x2 + (f * g11) * (x1 + x3)
+ (f * g12) * (x0 + x4)
x4 = x3
x3 = x2
@@ -39075,7 +39057,7 @@ local function combFilter(mem, base, T0, T1, N, g0, g1,
x2 = mem[p - T1]
x1 = mem[p - T1 + 1]
for q = p, base + N - 1 do
local x0 = mem[q - T1 + 2]
local x0 = mem[q + d1]
mem[q] = mem[q] + g10 * x2 + g11 * (x1 + x3) + g12 * (x0 + x4)
x4 = x3
x3 = x2
@@ -42376,14 +42358,14 @@ local function up2HQ(S, out, oo, inp, io, len)
end
-- FIR12 per phase ti, in tap order: FIR12[4 ti + 1 .. 4 ti + 4], FIR12[4 (11 - ti) + 4 .. 4 (11 - ti) + 1]
local FIR12_ROWS = table.create(12)
local FIR12_ROWS= (table.create(12) )
for ti = 0, 11 do
local f0, f1 = ti * 4, (11 - ti) * 4
FIR12_ROWS[ti + 1] = { FIR12[f0 + 1], FIR12[f0 + 2], FIR12[f0 + 3], FIR12[f0 + 4], FIR12[f1 + 4], FIR12[f1 + 3], FIR12[f1 + 2], FIR12[f1 + 1] }
end
local function iirFirInterpol(out, oo, buf, max_index_Q16, incr)
local rows = FIR12_ROWS
local rows= FIR12_ROWS
local index_Q16 = 0
while index_Q16 < max_index_Q16 do
local r = rows[math.floor(((index_Q16 % 65536) * 12) / 65536 )+ 1]
@@ -45202,8 +45184,8 @@ if type(from) ~= "number" or type(to) ~= "number" or from ~= from or to ~= to or
local L, M= math.floor(ti / g), math.floor(fi / g
) local exact = L <= MAX_EXACT
local phases = (exact and{L }or{INTERP_PHASES
})[1] local hist = table.create(channels)
for c = 1, channels do
})[1] local hist= (table.create(channels) )
for c = 1, channels do
hist[c] = {}
end
local st= {
@@ -115326,7 +115308,7 @@ local __compat = __compat local buffer, bit32 = __compat.buffer, __compat.bit32
-- 18 sequence (u32) | 22 crc (u32) | 26 segment count | 27 lacing values | data
local Crc32 = __include("core/Crc32")
local readu8, readu32, readi32 = buffer.readu8, buffer.readu32, buffer.readi32
local band, bxor, lshift, rshift = bit32.band, bit32.bxor, bit32.lshift, bit32.rshift
local band, bxor, lshift, rshift, byteswap = bit32.band, bit32.bxor, bit32.lshift, bit32.rshift, bit32.byteswap
local Page = {}
@@ -115348,9 +115330,28 @@ local Page = {}
local CRC = Crc32.MSB_TABLE
Page.CRC_TABLE = CRC
-- slice-by-4: T2 .. T4[i] = the CRC of byte i followed by 1 .. 3 zero bytes; the CRC is linear, so four bytes w
-- (big-endian) update crc to the xor of the bytes of crc ~ w through T4, T3, T2 and CRC
local T2, T3, T4 = table.create(256, 0), table.create(256, 0), table.create(256, 0)
for i = 0, 255 do
local c = CRC[i + 1]
c = bxor(lshift(c, 8), CRC[rshift(c, 24) + 1])
T2[i + 1] = c
c = bxor(lshift(c, 8), CRC[rshift(c, 24) + 1])
T3[i + 1] = c
c = bxor(lshift(c, 8), CRC[rshift(c, 24) + 1])
T4[i + 1] = c
end
local function crcRange(b, from, to, crc)
for i = from, to - 1 do
crc = bxor(lshift(crc, 8), CRC[bxor(rshift(crc, 24), readu8(b, i)) + 1])
local i = from
while i + 4 <= to do
local x = bxor(crc, byteswap(readu32(b, i)))
crc = bxor(T4[rshift(x, 24) + 1], T3[band(rshift(x, 16), 255) + 1], T2[band(rshift(x, 8), 255) + 1], CRC[band(x, 255) + 1])
i =i+ 4
end
for j = i, to - 1 do
crc = bxor(lshift(crc, 8), CRC[bxor(rshift(crc, 24), readu8(b, j)) + 1])
end
return crc
end