Add copysign and nearest to runtimes

This commit is contained in:
Rerumu 2022-05-21 15:51:23 -04:00
parent 8d671b99e4
commit 830727d8d4
2 changed files with 55 additions and 0 deletions

View File

@ -25,8 +25,19 @@ do
local mul = {}
local div = {}
local neg = {}
local copysign = {}
local nearest = {}
local to_signed = bit.tobit
local math_abs = math.abs
local function round(num)
if num >= 0 then
return (math_floor(num + 0.5))
else
return (math_ceil(num - 0.5))
end
end
function add.i32(a, b)
return (to_signed(a + b))
@ -65,11 +76,31 @@ do
return -num
end
function copysign.num(lhs, rhs)
if rhs >= 0 then
return (math_abs(lhs))
else
return -math_abs(lhs)
end
end
function nearest.num(num)
local result = round(num)
if math_abs(num) % 1 == 0.5 and temp_2 % 2 == 1 then
result = result - 1
end
return result
end
module.add = add
module.sub = sub
module.mul = mul
module.div = div
module.neg = neg
module.copysign = copysign
module.nearest = nearest
end
do

View File

@ -37,8 +37,12 @@ do
local mul = {}
local div = {}
local neg = {}
local copysign = {}
local nearest = {}
local assert = assert
local math_abs = math.abs
local math_round = math.round
function add.i32(a, b)
return to_u32(a + b)
@ -81,11 +85,31 @@ do
return -num
end
function copysign.num(lhs, rhs)
if rhs >= 0 then
return (math_abs(lhs))
else
return -math_abs(lhs)
end
end
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
end
return result
end
module.add = add
module.sub = sub
module.mul = mul
module.div = div
module.neg = neg
module.copysign = copysign
module.nearest = nearest
end
do