Skip to content

Commit 72b33a9

Browse files
Aarav SethiAarav Sethi
authored andcommitted
inline i32, u32, f32, idiv, fclass, etc functions
1 parent 1160b98 commit 72b33a9

4 files changed

Lines changed: 221 additions & 126 deletions

File tree

compiler/boilerplate.luau

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -19,94 +19,12 @@ local stdout_cache: string = ""
1919
local fflags: number = 0 -- floating-point exception flags (NV,DZ,OF,UF,NX)
2020

2121
-- Utility
22-
--- 32-bit helpers
23-
local function u32(v: number): number
24-
local n = math.floor(v % 0x100000000)
25-
if n < 0 then
26-
n += 0x100000000
27-
end
28-
return n
29-
end
30-
local function i32(v)
31-
local n = u32(v)
32-
if n >= 0x80000000 then
33-
return n - 0x100000000
34-
end
35-
return n
36-
end
37-
local function f32(v: number): number
38-
buffer.writef32(temp, 0, v)
39-
return buffer.readf32(temp, 0)
40-
end
41-
4222
--- Base (can be found in generated code)
43-
local function idiv_trunc(a: number, b: number): number
44-
if b == 0 then error("division by zero") end
45-
if a >= 0 then
46-
return (a - (a % b)) // b
47-
else
48-
return -((-a) - ((-a) % b)) // b
49-
end
50-
end
51-
local function float_to_int(f: number): number
52-
buffer.writef32(temp, 0, f)
53-
return buffer.readi32(temp, 0)
54-
end
55-
local function int_to_float(i: number): number
56-
buffer.writei32(temp, 0, i)
57-
return buffer.readf32(temp, 0)
58-
end
59-
local function float_to_double(f: number): number
60-
buffer.writef32(temp, 0, f)
61-
buffer.writei32(temp, 4, 0)
62-
return buffer.readf64(temp, 0)
63-
end
6423
local function two_words_to_double(highWord: number, lowWord: number): number
65-
buffer.writei32(temp, 0, u32(lowWord))
66-
buffer.writei32(temp, 4, u32(highWord))
24+
buffer.writei32(temp, 0, ((math.floor(lowWord % 0x100000000) + 0x100000000) % 0x100000000))
25+
buffer.writei32(temp, 4, ((math.floor(highWord % 0x100000000) + 0x100000000) % 0x100000000))
6726
return buffer.readf64(temp, 0)
6827
end
69-
local function hi(addr: number): number
70-
return bit32.lshift(bit32.rshift(addr, 12), 12)
71-
end
72-
local function lo(addr: number): number
73-
return bit32.band(addr, 0xFFF)
74-
end
75-
function fclass(x: number): number
76-
local result = 0
77-
78-
if x ~= x then
79-
result = bit32.bor(result, bit32.lshift(1, 9)) -- qNaN (cannot distinguish sNaN in Luau)
80-
elseif x == math.huge then
81-
result = bit32.bor(result, bit32.lshift(1, 7)) -- +Inf
82-
elseif x == -math.huge then
83-
result = bit32.bor(result, bit32.lshift(1, 0)) -- -Inf
84-
elseif x == 0 then
85-
if 1/x == -math.huge then
86-
result = bit32.bor(result, bit32.lshift(1, 3)) -- -Zero
87-
else
88-
result = bit32.bor(result, bit32.lshift(1, 4)) -- +Zero
89-
end
90-
else
91-
local absx = math.abs(x)
92-
local min_normal = 2.2250738585072014e-308 -- 2^-1022
93-
if absx < min_normal then
94-
if x > 0 then
95-
result = bit32.bor(result, bit32.lshift(1, 5)) -- +Subnormal
96-
else
97-
result = bit32.bor(result, bit32.lshift(1, 2)) -- -Subnormal
98-
end
99-
else
100-
if x > 0 then
101-
result = bit32.bor(result, bit32.lshift(1, 6)) -- +Normal
102-
else
103-
result = bit32.bor(result, bit32.lshift(1, 1)) -- -Normal
104-
end
105-
end
106-
end
107-
108-
return result
109-
end
11028
function reset_registers(): ()
11129
r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16 = 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
11230
r17,r18,r19,r20,r21,r22,r23,r24,r25,r26,r27,r28,r29,r30,r31,r32 = 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
@@ -136,11 +54,13 @@ function format_string(fmt: number, args: {number}): string
13654
if spec:sub(-1) == "d" then
13755
local val = args[arg_index]
13856
arg_index += 1
139-
return string.format("%"..fmtSpec, i32(val))
57+
58+
local n = --[[u32]] ((math.floor(val % 0x100000000) + 0x100000000) % 0x100000000);
59+
return string.format("%"..fmtSpec, --[[i32]] (n >= 0x80000000) and (n - 0x100000000) or n)
14060
elseif spec:sub(-1) == "X" then
14161
local val = args[arg_index]
14262
arg_index += 1
143-
return string.format("%"..fmtSpec, u32(val))
63+
return string.format("%"..fmtSpec, --[[u32]]((math.floor(val % 0x100000000) + 0x100000000) % 0x100000000))
14464
elseif spec:sub(-1) == "f" then
14565
if arg_index % 2 == 1 then
14666
arg_index += 1 -- 64-bit varargs are aligned to even a-registers (skip a1/a3/...)

compiler/compilation.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,10 @@ func AfterCompilation(writer *OutputWriter) []byte {
360360
push_args = push_args,
361361
get_f_args = get_f_args,
362362
push_f_args = push_f_args,
363-
int_to_float = int_to_float,
364-
float_to_int = float_to_int,
365-
hi = hi,
366-
lo = lo,
367363
read_string = read_string,
368364
format_string = format_string,
369365
malloc = malloc,
370-
float_to_double = float_to_double,
371366
two_words_to_double = two_words_to_double,
372-
fclass = fclass,
373367
reset_registers = reset_registers,
374368
},
375369

0 commit comments

Comments
 (0)