Skip to content

Instantly share code, notes, and snippets.

@jLn0n
Last active January 7, 2026 09:18
Show Gist options
  • Select an option

  • Save jLn0n/a8f7a77444460ccd21faf761874d9c72 to your computer and use it in GitHub Desktop.

Select an option

Save jLn0n/a8f7a77444460ccd21faf761874d9c72 to your computer and use it in GitHub Desktop.
cursed luau snippets
--!optimize 2
-- originally from: https://en.wikipedia.org/wiki/Fast_inverse_square_root
-- ported to luau
local _buf = buffer.create(8)
local function Q_rsqrt(value: number): number
buffer.writef32(_buf, 0, value)
local int_value = buffer.readu32(_buf, 0) -- converts `value` into uint32
local y = (0x5F1FFFF9 - bit32.rshift(int_value, 1))
buffer.writeu32(_buf, 4, y)
local float_y = buffer.readf32(_buf, 4) -- converts `y` to float
return float_y * (0.703952253 * (2.38924456 - value * float_y * float_y))
end
local function rsqrt_native(value: number): number
return 1 / math.sqrt(value)
end
-- similar as rsqrt_native but uses less instructions which can be inlined too
local function rsqrt_optimized(value: number): number
return 1 / (value^0.5)
end
local x = 0.15625
print(Q_rsqrt(x)) -- 2.5314229150749705
print(rsqrt_native(x)) -- 2.5298221281347035
print(rsqrt_optimized(x)) -- 2.5298221281347035
--!native
-- this thing is not cursed mate, just saving it here for later use
local function math_max(a: number, b: number): number
local diff = a - b
return (a + b + (diff * diff) ^ 0.5) / 2
end
local function count_digits(n: number): number
local result = 1
if n >= 100000000 then
result += 8
n /= 100000000
end
if n >= 10000 then
result += 4
n /= 10000
end
if n >= 100 then
result += 2
n /= 100
end
if n >= 10 then
result += 1
end
return result
end
local function karatsuba(x: number, y: number): number
if x < 10 or y < 10 then
return x * y
end
local m = math_max(count_digits(x), count_digits(y)) // 2
local power = 10 ^ m
local a, c = x // power, y // power
local b, d = x % power, y % power
local ac = karatsuba(a, c)
local bd = karatsuba(b, d)
local e = karatsuba(a + b, c + d) - ac - bd
return ac * (power * power) + (e * power) + bd
end
local native_mult
function native_mult(x: number, y: number): number
return x * y
end
local function gen_numbers(
population: {number},
x: number, y: number
): (number, number)
local ix = 0x7f + bit32.rshift(bit32.bxor(bit32.band(y, x)), 24) + (bit32.countlz(y) - bit32.countrz(x))
local iy = 0x7f + bit32.rshift(bit32.band(bit32.bxor(x, y)), 24) + (bit32.countrz(x) - bit32.countlz(y))
return population[bit32.band(ix, 0xff)], population[bit32.band(iy, 0xff)]
end
local function benchmark(reps: number)
local population = {}
for i = 0, 255 do
table.insert(population, i, math.random(2^30, 2^40))
end
local a, b = gen_numbers(population, population[math.random(0, 255)], population[math.random(0, 255)])
local start = os.clock()
for _ = 1, reps do
karatsuba(a, b)
end
print(string.format("karatsuba_optl2 took %.7f seconds!", os.clock() - start))
local start = os.clock()
for _ = 1, reps do
native_mult(a, b)
end
print(string.format("native_multiply took %.7f seconds!", os.clock() - start))
end
benchmark(10000)
local multiplicand, multiplier = 69564788907, 67756456
print(native_mult(multiplicand, multiplier), karatsuba(multiplicand, multiplier))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment