Fix wrapping integer operations
This commit is contained in:
parent
fa649f26c1
commit
e9642ef776
@ -5,8 +5,6 @@ local ffi = require('ffi')
|
|||||||
|
|
||||||
local module = {}
|
local module = {}
|
||||||
|
|
||||||
local to_signed = bit.tobit
|
|
||||||
|
|
||||||
local vla_u8 = ffi.typeof('uint8_t[?]')
|
local vla_u8 = ffi.typeof('uint8_t[?]')
|
||||||
|
|
||||||
local u32 = ffi.typeof('uint32_t')
|
local u32 = ffi.typeof('uint32_t')
|
||||||
@ -31,10 +29,27 @@ local function truncate(num)
|
|||||||
end
|
end
|
||||||
|
|
||||||
do
|
do
|
||||||
|
local add = {}
|
||||||
|
local sub = {}
|
||||||
|
local mul = {}
|
||||||
local div = {}
|
local div = {}
|
||||||
|
|
||||||
|
local to_signed = bit.tobit
|
||||||
|
|
||||||
|
module.add = add
|
||||||
|
module.sub = sub
|
||||||
|
module.mul = mul
|
||||||
module.div = div
|
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)
|
function div.i32(lhs, rhs)
|
||||||
if rhs == 0 then error('division by zero') end
|
if rhs == 0 then error('division by zero') end
|
||||||
|
|
||||||
|
@ -345,9 +345,9 @@ pub enum BinOp {
|
|||||||
impl BinOp {
|
impl BinOp {
|
||||||
pub fn as_operator(self) -> Option<&'static str> {
|
pub fn as_operator(self) -> Option<&'static str> {
|
||||||
let op = match self {
|
let op = match self {
|
||||||
Self::Add_I32 | Self::Add_I64 | Self::Add_FN => "+",
|
Self::Add_FN => "+",
|
||||||
Self::Sub_I32 | Self::Sub_I64 | Self::Sub_FN => "-",
|
Self::Sub_FN => "-",
|
||||||
Self::Mul_I32 | Self::Mul_I64 | Self::Mul_FN => "*",
|
Self::Mul_FN => "*",
|
||||||
Self::Div_FN => "/",
|
Self::Div_FN => "/",
|
||||||
Self::RemS_I32 | Self::RemU_I32 | Self::RemS_I64 | Self::RemU_I64 => "%",
|
Self::RemS_I32 | Self::RemU_I32 | Self::RemS_I64 | Self::RemU_I64 => "%",
|
||||||
_ => return None,
|
_ => return None,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user