Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit ba30264

Browse files
feat: bit operations
Signed-off-by: Henry <[email protected]>
1 parent d20e788 commit ba30264

File tree

4 files changed

+841
-783
lines changed

4 files changed

+841
-783
lines changed

crates/tinywasm/src/runtime/executor/mod.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::ops::{BitAnd, BitOr, BitXor};
2+
13
use super::{DefaultRuntime, Stack};
24
use crate::{
35
get_label_args,
@@ -10,7 +12,9 @@ use log::info;
1012
use tinywasm_types::Instruction;
1113

1214
mod macros;
15+
mod traits;
1316
use macros::*;
17+
use traits::*;
1418

1519
impl DefaultRuntime {
1620
pub(crate) fn exec(&self, store: &mut Store, stack: &mut Stack, module: ModuleInstance) -> Result<()> {
@@ -311,10 +315,19 @@ fn exec_one(
311315
I32DivU => checked_arithmetic_method_cast!(checked_div, i32, u32, stack, crate::Trap::DivisionByZero),
312316
I64DivU => checked_arithmetic_method_cast!(checked_div, i64, u64, stack, crate::Trap::DivisionByZero),
313317

314-
I32RemS => checked_arithmetic_method!(checked_rem, i32, stack, crate::Trap::DivisionByZero),
315-
I64RemS => checked_arithmetic_method!(checked_rem, i64, stack, crate::Trap::DivisionByZero),
316-
I32RemU => checked_arithmetic_method_cast!(checked_rem, i32, u32, stack, crate::Trap::DivisionByZero),
317-
I64RemU => checked_arithmetic_method_cast!(checked_rem, i64, u64, stack, crate::Trap::DivisionByZero),
318+
I32RemS => checked_arithmetic_method!(checked_wrapping_rem, i32, stack, crate::Trap::DivisionByZero),
319+
I64RemS => checked_arithmetic_method!(checked_wrapping_rem, i64, stack, crate::Trap::DivisionByZero),
320+
I32RemU => checked_arithmetic_method_cast!(checked_wrapping_rem, i32, u32, stack, crate::Trap::DivisionByZero),
321+
I64RemU => checked_arithmetic_method_cast!(checked_wrapping_rem, i64, u64, stack, crate::Trap::DivisionByZero),
322+
323+
I32And => arithmetic_method!(bitand, i32, stack),
324+
I64And => arithmetic_method!(bitand, i64, stack),
325+
I32Or => arithmetic_method!(bitor, i32, stack),
326+
I64Or => arithmetic_method!(bitor, i64, stack),
327+
I32Xor => arithmetic_method!(bitxor, i32, stack),
328+
I64Xor => arithmetic_method!(bitxor, i64, stack),
329+
I32Shl => arithmetic_method!(shl_i32, i32, stack),
330+
I64Shl => arithmetic_method!(shl_i64, i64, stack),
318331

319332
F32ConvertI32S => conv_1!(i32, f32, stack),
320333
F32ConvertI64S => conv_1!(i64, f32, stack),
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
pub(crate) trait CheckedWrappingRem
2+
where
3+
Self: Sized,
4+
{
5+
fn checked_wrapping_rem(self, rhs: Self) -> Option<Self>;
6+
}
7+
8+
pub(crate) trait ShlI32 {
9+
fn shl_i32(self, rhs: i32) -> Self;
10+
}
11+
12+
impl ShlI32 for i32 {
13+
#[inline]
14+
fn shl_i32(self, rhs: i32) -> Self {
15+
self.wrapping_shl(rhs as u32)
16+
}
17+
}
18+
19+
pub(crate) trait ShlI64 {
20+
fn shl_i64(self, rhs: i64) -> Self;
21+
}
22+
23+
impl ShlI64 for i64 {
24+
#[inline]
25+
fn shl_i64(self, rhs: i64) -> Self {
26+
self.wrapping_shl(rhs as u32)
27+
}
28+
}
29+
30+
macro_rules! impl_checked_wrapping_rem {
31+
($($t:ty)*) => ($(
32+
impl CheckedWrappingRem for $t {
33+
#[inline]
34+
fn checked_wrapping_rem(self, rhs: Self) -> Option<Self> {
35+
if rhs == 0 {
36+
None
37+
} else {
38+
Some(self.wrapping_rem(rhs))
39+
}
40+
}
41+
}
42+
)*)
43+
}
44+
45+
impl_checked_wrapping_rem! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }

0 commit comments

Comments
 (0)