Fix signed remainder assumption

This commit is contained in:
Rerumu 2022-06-16 04:08:52 -04:00
parent 860fef391f
commit c5f7e386c2
3 changed files with 21 additions and 4 deletions

View File

@ -24,6 +24,7 @@ do
local sub = {} local sub = {}
local mul = {} local mul = {}
local div = {} local div = {}
local rem = {}
local neg = {} local neg = {}
local copysign = {} local copysign = {}
local nearest = {} local nearest = {}
@ -78,6 +79,21 @@ do
return (i64(u64(lhs) / u64(rhs))) return (i64(u64(lhs) / u64(rhs)))
end end
function rem.u32(lhs, rhs)
assert(rhs ~= 0, "division by zero")
lhs = to_number(u32(lhs))
rhs = to_number(u32(rhs))
return (to_signed(lhs % rhs))
end
function rem.u64(lhs, rhs)
assert(rhs ~= 0, "division by zero")
return (i64(u64(lhs) % u64(rhs)))
end
function neg.num(num) function neg.num(num)
return -num return -num
end end
@ -106,6 +122,7 @@ do
module.sub = sub module.sub = sub
module.mul = mul module.mul = mul
module.div = div module.div = div
module.rem = rem
module.neg = neg module.neg = neg
module.copysign = copysign module.copysign = copysign
module.nearest = nearest module.nearest = nearest

View File

@ -6,9 +6,7 @@ pub fn bin_symbol_of(op: BinOpType) -> Option<&'static str> {
BinOpType::Sub_I64 | BinOpType::Sub_FN => "-", BinOpType::Sub_I64 | BinOpType::Sub_FN => "-",
BinOpType::Mul_I64 | BinOpType::Mul_FN => "*", BinOpType::Mul_I64 | BinOpType::Mul_FN => "*",
BinOpType::DivS_I64 | BinOpType::Div_FN => "/", BinOpType::DivS_I64 | BinOpType::Div_FN => "/",
BinOpType::RemS_I32 | BinOpType::RemU_I32 | BinOpType::RemS_I64 | BinOpType::RemU_I64 => { BinOpType::RemS_I64 => "%",
"%"
}
_ => return None, _ => return None,
}; };

View File

@ -238,7 +238,9 @@ fn write_local_operation(head: &str, tail: &str, w: &mut dyn Write) -> Result<()
("abs" | "ceil" | "floor" | "sqrt" | "min" | "max", _) => { ("abs" | "ceil" | "floor" | "sqrt" | "min" | "max", _) => {
write!(w, "local {head}_{tail} = math.{head} ") write!(w, "local {head}_{tail} = math.{head} ")
} }
("rem", "i32") => {
write!(w, "local {head}_{tail} = math.fmod ")
}
_ => write!(w, "local {head}_{tail} = rt.{head}.{tail} "), _ => write!(w, "local {head}_{tail} = rt.{head}.{tail} "),
} }
} }