Fix wrapping integer operations

This commit is contained in:
Rerumu
2021-11-27 00:43:25 -05:00
parent fa649f26c1
commit e9642ef776
2 changed files with 20 additions and 5 deletions
+17 -2
View File
@@ -5,8 +5,6 @@ local ffi = require('ffi')
local module = {}
local to_signed = bit.tobit
local vla_u8 = ffi.typeof('uint8_t[?]')
local u32 = ffi.typeof('uint32_t')
@@ -31,10 +29,27 @@ local function truncate(num)
end
do
local add = {}
local sub = {}
local mul = {}
local div = {}
local to_signed = bit.tobit
module.add = add
module.sub = sub
module.mul = mul
module.div = div
function add.i32(a, b) return to_signed(a + b) end
function add.i64(a, b) return a + b end
function sub.i32(a, b) return to_signed(a - b) end
function sub.i64(a, b) return a - b end
function mul.i32(a, b) return to_signed(a * b) end
function mul.i64(a, b) return a * b end
function div.i32(lhs, rhs)
if rhs == 0 then error('division by zero') end