From 6116fea2e3789b8965c07ffbf807c66cc6c4d376 Mon Sep 17 00:00:00 2001 From: Rerumu Date: Mon, 4 Jul 2022 04:19:43 -0400 Subject: [PATCH] Merge #9 commit 4239e59c679801281cb6412cd9ca20bcfd36c05c Author: Hexcede <8394472+Hexcede@users.noreply.github.com> Date: Mon Jun 20 06:30:23 2022 -0400 Group num_bit_not local with num_negate commit cdf4a93fcbfd0cbd2e200d01e594b8a2d1b77580 Author: Hexcede <8394472+Hexcede@users.noreply.github.com> Date: Mon Jun 20 06:28:56 2022 -0400 Localize math.min & math.max commit 61639bc722299e06a1edf6b15ab1fdccdf99496c Author: Hexcede <8394472+Hexcede@users.noreply.github.com> Date: Mon Jun 20 06:28:12 2022 -0400 Flip trunc.num branches (< to >=) commit 02fc78a67181a671ed89c48ad915ff142430e5e5 Author: Hexcede <8394472+Hexcede@users.noreply.github.com> Date: Mon Jun 20 06:26:52 2022 -0400 Localize math.floor & math.sign usages commit 01448b9dbe6f19c7d475b246ee61efa2670da951 Author: Hexcede <8394472+Hexcede@users.noreply.github.com> Date: Sun Jun 19 14:15:27 2022 -0400 Add missing bit_band local commit be50c8a7fec15e5b7b5659264f59b7a044360a62 Author: Hexcede <8394472+Hexcede@users.noreply.github.com> Date: Sun Jun 19 13:55:23 2022 -0400 Fix naming commit cf7be1f377362f90355cc6d5a2b707b11069f457 Merge: 2772011 a32a5ba Author: Hexcede <8394472+Hexcede@users.noreply.github.com> Date: Sun Jun 19 12:43:13 2022 -0400 Merge pull request #1 from Hexcede/fix-bit_negate Localize rust bit_not and fix Numeric.negate commit a32a5ba3fc4b7c2cdd3a29bcb102b687caaa178c Author: Hexcede <8394472+Hexcede@users.noreply.github.com> Date: Sun Jun 19 12:35:27 2022 -0400 Localize rust bit_not and fix Numeric.negate commit 2772011a1279ac51fb5428a0ddf74fbf65a4b683 Author: Hexcede <8394472+Hexcede@users.noreply.github.com> Date: Sun Jun 19 12:16:21 2022 -0400 Add runtime.trunc.num commit b7d79a2de383e0ce30f0ac75ecc262af1b61e9db Author: Hexcede <8394472+Hexcede@users.noreply.github.com> Date: Sun Jun 19 12:05:17 2022 -0400 Fix runtime.nearest.num condition & round towards zero instead of with negative bias commit 2b3713df25e67dd1c2269b6e53bc4adef6b8ffbf Author: Hexcede <8394472+Hexcede@users.noreply.github.com> Date: Sun Jun 19 12:03:50 2022 -0400 Add runtime.min.num & runtime.max.num Wasm min/max returns NaN if either operator is NaN. However, Luau min/max follows different behaviour. --- codegen-luau/runtime/numeric.lua | 5 +++-- codegen-luau/runtime/runtime.lua | 31 +++++++++++++++++++++++++++++-- codegen-luau/src/translator.rs | 2 +- 3 files changed, 33 insertions(+), 5 deletions(-) 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} "),