Skip to content

Commit

Permalink
Remove float canonicalization from components (#9879)
Browse files Browse the repository at this point in the history
* Remove float canonicalization from components

This was removed from the spec quite awhile back so this catches up the
runtime to the spec where this is no longer required.

Closes #9826

* Update tests
  • Loading branch information
alexcrichton authored Dec 20, 2024
1 parent d0a5599 commit 7c4f0c7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
21 changes: 4 additions & 17 deletions crates/wasmtime/src/runtime/component/func/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,19 +875,6 @@ integers! {

macro_rules! floats {
($($float:ident/$get_float:ident = $ty:ident with abi:$abi:ident)*) => ($(const _: () = {
/// All floats in-and-out of the canonical abi always have their nan
/// payloads canonicalized. conveniently the `NAN` constant in rust has
/// the same representation as canonical nan, so we can use that for the
/// nan value.
#[inline]
fn canonicalize(float: $float) -> $float {
if float.is_nan() {
$float::NAN
} else {
float
}
}

unsafe impl ComponentType for $float {
type Lower = ValRaw;

Expand All @@ -910,7 +897,7 @@ macro_rules! floats {
dst: &mut MaybeUninit<Self::Lower>,
) -> Result<()> {
debug_assert!(matches!(ty, InterfaceType::$ty));
dst.write(ValRaw::$float(canonicalize(*self).to_bits()));
dst.write(ValRaw::$float(self.to_bits()));
Ok(())
}

Expand All @@ -924,7 +911,7 @@ macro_rules! floats {
debug_assert!(matches!(ty, InterfaceType::$ty));
debug_assert!(offset % Self::SIZE32 == 0);
let ptr = cx.get(offset);
*ptr = canonicalize(*self).to_bits().to_le_bytes();
*ptr = self.to_bits().to_le_bytes();
Ok(())
}
}
Expand All @@ -933,14 +920,14 @@ macro_rules! floats {
#[inline]
fn lift(_cx: &mut LiftContext<'_>, ty: InterfaceType, src: &Self::Lower) -> Result<Self> {
debug_assert!(matches!(ty, InterfaceType::$ty));
Ok(canonicalize($float::from_bits(src.$get_float())))
Ok($float::from_bits(src.$get_float()))
}

#[inline]
fn load(_cx: &mut LiftContext<'_>, ty: InterfaceType, bytes: &[u8]) -> Result<Self> {
debug_assert!(matches!(ty, InterfaceType::$ty));
debug_assert!((bytes.as_ptr() as usize) % Self::SIZE32 == 0);
Ok(canonicalize($float::from_le_bytes(bytes.try_into().unwrap())))
Ok($float::from_le_bytes(bytes.try_into().unwrap()))
}
}
};)*)
Expand Down
15 changes: 9 additions & 6 deletions tests/all/component_model/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,26 +503,26 @@ fn floats() -> Result<()> {
.call(&mut store, (CANON_32BIT_NAN | 1,))?
.0
.to_bits(),
CANON_32BIT_NAN
CANON_32BIT_NAN | 1
);
u32_to_f32.post_return(&mut store)?;
assert_eq!(
u64_to_f64
.call(&mut store, (CANON_64BIT_NAN | 1,))?
.0
.to_bits(),
CANON_64BIT_NAN,
CANON_64BIT_NAN | 1,
);
u64_to_f64.post_return(&mut store)?;

assert_eq!(
f32_to_u32.call(&mut store, (f32::from_bits(CANON_32BIT_NAN | 1),))?,
(CANON_32BIT_NAN,)
(CANON_32BIT_NAN | 1,)
);
f32_to_u32.post_return(&mut store)?;
assert_eq!(
f64_to_u64.call(&mut store, (f64::from_bits(CANON_64BIT_NAN | 1),))?,
(CANON_64BIT_NAN,)
(CANON_64BIT_NAN | 1,)
);
f64_to_u64.post_return(&mut store)?;

Expand Down Expand Up @@ -937,7 +937,10 @@ fn many_parameters() -> Result<()> {
assert_eq!(i8::from_le_bytes(*actual.take_n::<1>()), input.0);
actual.skip::<7>();
assert_eq!(u64::from_le_bytes(*actual.take_n::<8>()), input.1);
assert_eq!(u32::from_le_bytes(*actual.take_n::<4>()), CANON_32BIT_NAN);
assert_eq!(
u32::from_le_bytes(*actual.take_n::<4>()),
CANON_32BIT_NAN | 1
);
assert_eq!(u8::from_le_bytes(*actual.take_n::<1>()), input.3);
actual.skip::<1>();
assert_eq!(i16::from_le_bytes(*actual.take_n::<2>()), input.4);
Expand Down Expand Up @@ -1703,7 +1706,7 @@ fn expected() -> Result<()> {
let ret = to_expected_s16_f32
.call(&mut store, (1, CANON_32BIT_NAN | 1))?
.0;
assert_eq!(ret.unwrap_err().to_bits(), CANON_32BIT_NAN);
assert_eq!(ret.unwrap_err().to_bits(), CANON_32BIT_NAN | 1);
to_expected_s16_f32.post_return(&mut store)?;
assert!(to_expected_s16_f32.call(&mut store, (2, 0)).is_err());

Expand Down

0 comments on commit 7c4f0c7

Please sign in to comment.