Add min and max for f32 and f64

This commit is contained in:
Rerumu 2022-06-27 20:18:55 -04:00
parent b8362d9e66
commit 5463b48e63
2 changed files with 30 additions and 1 deletions

View File

@ -29,11 +29,16 @@ do
local div = {}
local rem = {}
local neg = {}
local min = {}
local max = {}
local copysign = {}
local nearest = {}
local to_signed = bit.tobit
local math_abs = math.abs
local math_min = math.min
local math_max = math.max
local RE_INSTANCE = ffi.new([[union {
double f64;
@ -100,6 +105,26 @@ do
return -num
end
function min.f32(lhs, rhs)
if lhs ~= lhs then
return lhs
elseif rhs ~= rhs then
return rhs
else
return math_min(lhs, rhs)
end
end
function max.f32(lhs, rhs)
if lhs ~= lhs then
return lhs
elseif rhs ~= rhs then
return rhs
else
return math_max(lhs, rhs)
end
end
function copysign.f32(lhs, rhs)
RE_INSTANCE.f64 = rhs
@ -121,6 +146,8 @@ do
end
neg.f64 = neg.f32
min.f64 = min.f32
max.f64 = max.f32
copysign.f64 = copysign.f32
nearest.f64 = nearest.f32
@ -129,6 +156,8 @@ do
module.mul = mul
module.div = div
module.rem = rem
module.min = min
module.max = max
module.neg = neg
module.copysign = copysign
module.nearest = nearest

View File

@ -219,7 +219,7 @@ fn write_local_operation(head: &str, tail: &str, w: &mut dyn Write) -> Result<()
write!(w, "local {head}_{tail} = ")?;
match (head, tail) {
("abs" | "ceil" | "floor" | "sqrt" | "min" | "max", _) => write!(w, "math.{head} "),
("abs" | "ceil" | "floor" | "sqrt", _) => write!(w, "math.{head} "),
("rem", "i32") => write!(w, "math.fmod "),
("band" | "bor" | "bxor" | "bnot", _) => write!(w, "bit.{head} "),
("shl", _) => write!(w, "bit.lshift "),