diff --git a/codegen-luau/runtime/numeric.lua b/codegen-luau/runtime/numeric.lua index 6add68e..8734fac 100644 --- a/codegen-luau/runtime/numeric.lua +++ b/codegen-luau/runtime/numeric.lua @@ -25,7 +25,7 @@ local math_pow = math.pow local table_freeze = table.freeze local from_u32, into_u32, from_u64, into_u64 -local num_add, num_subtract, num_multiply, num_divide_unsigned, num_negate +local num_add, num_subtract, num_multiply, num_divide_unsigned, num_negate, num_bit_not local num_is_negative, num_is_zero, num_is_equal, num_is_less_unsigned, num_is_greater_unsigned -- TODO: Eventually support Vector3 @@ -231,7 +231,7 @@ function Numeric.divide_signed(lhs, rhs) end function Numeric.negate(value) - return num_add(bit_not(value), K_ONE) + return num_add(num_bit_not(value), K_ONE) end function Numeric.bit_and(lhs, rhs) @@ -393,6 +393,7 @@ num_subtract = Numeric.subtract num_multiply = Numeric.multiply num_divide_unsigned = Numeric.divide_unsigned num_negate = Numeric.negate +num_bit_not = Numeric.bit_not num_is_negative = Numeric.is_negative num_is_zero = Numeric.is_zero diff --git a/codegen-luau/runtime/runtime.lua b/codegen-luau/runtime/runtime.lua index 61ce67b..8ee8103 100644 --- a/codegen-luau/runtime/runtime.lua +++ b/codegen-luau/runtime/runtime.lua @@ -37,12 +37,18 @@ do local mul = {} local div = {} local neg = {} + local min = {} + local max = {} local copysign = {} local nearest = {} local assert = assert local math_abs = math.abs local math_round = math.round + local math_floor = math.floor + local math_sign = math.sign + local math_min = math.min + local math_max = math.max function add.i32(a, b) return to_u32(a + b) @@ -85,6 +91,20 @@ do return -num end + function min.num(a, b) + if b ~= b then + return b + end + return math_min(a, b) + end + + function max.num(a, b) + if b ~= b then + return b + end + return math_max(a, b) + end + function copysign.num(lhs, rhs) if rhs >= 0 then return (math_abs(lhs)) @@ -96,8 +116,8 @@ do 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 + if math_abs(num) % 1 == 0.5 and math_floor(math_abs(num) % 2) == 0 then + result -= math_sign(result) end return result @@ -108,6 +128,8 @@ do module.mul = mul module.div = div module.neg = neg + module.min = min + module.max = max module.copysign = copysign module.nearest = nearest end @@ -309,6 +331,10 @@ do end end + function trunc.num(num) + return if num >= 0 then math.floor(num) else math.ceil(num) + end + trunc.u64_f32 = num_from_u64 trunc.u64_f64 = num_from_u64 @@ -403,6 +429,7 @@ do local bit_replace = bit32.replace local bit_bor = bit32.bor + local bit_band = bit32.band local bit_lshift = bit32.lshift local bit_rshift = bit32.rshift diff --git a/codegen-luau/src/translator.rs b/codegen-luau/src/translator.rs index b6cd852..40d31d9 100644 --- a/codegen-luau/src/translator.rs +++ b/codegen-luau/src/translator.rs @@ -232,7 +232,7 @@ fn write_local_operation(head: &str, tail: &str, w: &mut dyn Write) -> Result<() ("band" | "bor" | "bxor", "i32") => { write!(w, "local {head}_{tail} = bit32.{head} ") } - ("abs" | "ceil" | "floor" | "sqrt" | "min" | "max", _) => { + ("abs" | "ceil" | "floor" | "sqrt", _) => { write!(w, "local {head}_{tail} = math.{head} ") } _ => write!(w, "local {head}_{tail} = rt.{head}.{tail} "),