Skip to content

Commit 541af71

Browse files
committed
use macro to generate reinterpret functions
1 parent b7d4dd2 commit 541af71

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

crates/core/src/wasm.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -489,22 +489,19 @@ pub fn i64_mul_wide_u(lhs: i64, rhs: i64) -> (i64, i64) {
489489
split128(result as i128)
490490
}
491491

492-
/// Execute an `i32.reinterpret_f32` Wasm instruction.
493-
pub fn i32_reinterpret_f32(value: f32) -> i32 {
494-
i32::from_ne_bytes(f32::to_ne_bytes(value))
495-
}
496-
497-
/// Execute an `i64.reinterpret_f64` Wasm instruction.
498-
pub fn i64_reinterpret_f64(value: f64) -> i64 {
499-
i64::from_ne_bytes(f64::to_ne_bytes(value))
500-
}
501-
502-
/// Execute an `f32.reinterpret_i32` Wasm instruction.
503-
pub fn f32_reinterpret_i32(value: i32) -> f32 {
504-
f32::from_ne_bytes(i32::to_ne_bytes(value))
492+
macro_rules! impl_reinterpret_cast {
493+
( $(fn $name:ident($from:ty) -> $to:ty);* $(;)? ) => {
494+
$(
495+
#[doc = concat!("Execute the `", stringify!($name), "` Wasm instruction.")]
496+
pub fn $name(value: $from) -> $to {
497+
<$to>::from_ne_bytes(<$from>::to_ne_bytes(value))
498+
}
499+
)*
500+
};
505501
}
506-
507-
/// Execute an `f64.reinterpret_i64` Wasm instruction.
508-
pub fn f64_reinterpret_i64(value: i64) -> f64 {
509-
f64::from_ne_bytes(i64::to_ne_bytes(value))
502+
impl_reinterpret_cast! {
503+
fn i32_reinterpret_f32(f32) -> i32;
504+
fn i64_reinterpret_f64(f64) -> i64;
505+
fn f32_reinterpret_i32(i32) -> f32;
506+
fn f64_reinterpret_i64(i64) -> f64;
510507
}

0 commit comments

Comments
 (0)