From 5463b48e639eea5c1bb53cf0ade7ddcd5abe6fc9 Mon Sep 17 00:00:00 2001 From: Rerumu Date: Mon, 27 Jun 2022 20:18:55 -0400 Subject: [PATCH] Add min and max for `f32` and `f64` --- codegen/luajit/runtime/runtime.lua | 29 +++++++++++++++++++++++++++++ codegen/luajit/src/translator.rs | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/codegen/luajit/runtime/runtime.lua b/codegen/luajit/runtime/runtime.lua index 5681f42..d739931 100644 --- a/codegen/luajit/runtime/runtime.lua +++ b/codegen/luajit/runtime/runtime.lua @@ -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 diff --git a/codegen/luajit/src/translator.rs b/codegen/luajit/src/translator.rs index b71062a..f741cff 100644 --- a/codegen/luajit/src/translator.rs +++ b/codegen/luajit/src/translator.rs @@ -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 "),