From 5d4aa97cd546448ceb36115cc7efb5b604b8bf85 Mon Sep 17 00:00:00 2001 From: Rerumu Date: Sun, 26 Jun 2022 01:51:52 -0400 Subject: [PATCH] Fix signed `i32` remainder --- codegen/luau/runtime/runtime.lua | 12 ++++++++++++ codegen/luau/src/analyzer/as_symbol.rs | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/codegen/luau/runtime/runtime.lua b/codegen/luau/runtime/runtime.lua index ce624d7..528b398 100644 --- a/codegen/luau/runtime/runtime.lua +++ b/codegen/luau/runtime/runtime.lua @@ -43,6 +43,7 @@ do local sub = {} local mul = {} local div = {} + local rem = {} local neg = {} local min = {} local max = {} @@ -52,6 +53,7 @@ do local assert = assert local math_abs = math.abs + local math_fmod = math.fmod local math_floor = math.floor local math_round = math.round local math_sign = math.sign @@ -110,6 +112,15 @@ do div.u64 = I64.divide_unsigned + function rem.i32(lhs, rhs) + assert(rhs ~= 0, "division by zero") + + lhs = to_i32(lhs) + rhs = to_i32(rhs) + + return to_u32(math_fmod(lhs, rhs)) + end + function neg.f32(num) return -num end @@ -161,6 +172,7 @@ do module.sub = sub module.mul = mul module.div = div + module.rem = rem module.neg = neg module.min = min module.max = max diff --git a/codegen/luau/src/analyzer/as_symbol.rs b/codegen/luau/src/analyzer/as_symbol.rs index a48f6d5..c19ebfc 100644 --- a/codegen/luau/src/analyzer/as_symbol.rs +++ b/codegen/luau/src/analyzer/as_symbol.rs @@ -11,7 +11,7 @@ impl AsSymbol for BinOpType { Self::Sub_F32 | Self::Sub_F64 => "-", Self::Mul_F32 | Self::Mul_F64 => "*", Self::Div_F32 | Self::Div_F64 => "/", - Self::RemS_I32 | Self::RemU_I32 => "%", + Self::RemU_I32 => "%", _ => return None, };