From 830727d8d4f3cae440efdb779da0b71ccabdd994 Mon Sep 17 00:00:00 2001 From: Rerumu Date: Sat, 21 May 2022 15:51:23 -0400 Subject: [PATCH] Add `copysign` and `nearest` to runtimes --- codegen-luajit/runtime/runtime.lua | 31 ++++++++++++++++++++++++++++++ codegen-luau/runtime/runtime.lua | 24 +++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/codegen-luajit/runtime/runtime.lua b/codegen-luajit/runtime/runtime.lua index 660fe27..7aff91d 100644 --- a/codegen-luajit/runtime/runtime.lua +++ b/codegen-luajit/runtime/runtime.lua @@ -25,8 +25,19 @@ do local mul = {} local div = {} local neg = {} + local copysign = {} + local nearest = {} local to_signed = bit.tobit + local math_abs = math.abs + + local function round(num) + if num >= 0 then + return (math_floor(num + 0.5)) + else + return (math_ceil(num - 0.5)) + end + end function add.i32(a, b) return (to_signed(a + b)) @@ -65,11 +76,31 @@ do return -num end + function copysign.num(lhs, rhs) + if rhs >= 0 then + return (math_abs(lhs)) + else + return -math_abs(lhs) + end + end + + function nearest.num(num) + local result = round(num) + + if math_abs(num) % 1 == 0.5 and temp_2 % 2 == 1 then + result = result - 1 + end + + return result + end + module.add = add module.sub = sub module.mul = mul module.div = div module.neg = neg + module.copysign = copysign + module.nearest = nearest end do diff --git a/codegen-luau/runtime/runtime.lua b/codegen-luau/runtime/runtime.lua index ab30319..eec501b 100644 --- a/codegen-luau/runtime/runtime.lua +++ b/codegen-luau/runtime/runtime.lua @@ -37,8 +37,12 @@ do local mul = {} local div = {} local neg = {} + local copysign = {} + local nearest = {} local assert = assert + local math_abs = math.abs + local math_round = math.round function add.i32(a, b) return to_u32(a + b) @@ -81,11 +85,31 @@ do return -num end + function copysign.num(lhs, rhs) + if rhs >= 0 then + return (math_abs(lhs)) + else + return -math_abs(lhs) + end + end + + function nearest.num(num) + local result = math_round(num) + + if math_abs(num) % 1 == 0.5 and temp_2 % 2 == 1 then + result = result - 1 + end + + return result + end + module.add = add module.sub = sub module.mul = mul module.div = div module.neg = neg + module.copysign = copysign + module.nearest = nearest end do