Skip to content

Commit 7c4f0c7

Browse files
authored
Remove float canonicalization from components (#9879)
* 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
1 parent d0a5599 commit 7c4f0c7

2 files changed

Lines changed: 13 additions & 23 deletions

File tree

  • crates/wasmtime/src/runtime/component/func
  • tests/all/component_model

crates/wasmtime/src/runtime/component/func/typed.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -875,19 +875,6 @@ integers! {
875875

876876
macro_rules! floats {
877877
($($float:ident/$get_float:ident = $ty:ident with abi:$abi:ident)*) => ($(const _: () = {
878-
/// All floats in-and-out of the canonical abi always have their nan
879-
/// payloads canonicalized. conveniently the `NAN` constant in rust has
880-
/// the same representation as canonical nan, so we can use that for the
881-
/// nan value.
882-
#[inline]
883-
fn canonicalize(float: $float) -> $float {
884-
if float.is_nan() {
885-
$float::NAN
886-
} else {
887-
float
888-
}
889-
}
890-
891878
unsafe impl ComponentType for $float {
892879
type Lower = ValRaw;
893880

@@ -910,7 +897,7 @@ macro_rules! floats {
910897
dst: &mut MaybeUninit<Self::Lower>,
911898
) -> Result<()> {
912899
debug_assert!(matches!(ty, InterfaceType::$ty));
913-
dst.write(ValRaw::$float(canonicalize(*self).to_bits()));
900+
dst.write(ValRaw::$float(self.to_bits()));
914901
Ok(())
915902
}
916903

@@ -924,7 +911,7 @@ macro_rules! floats {
924911
debug_assert!(matches!(ty, InterfaceType::$ty));
925912
debug_assert!(offset % Self::SIZE32 == 0);
926913
let ptr = cx.get(offset);
927-
*ptr = canonicalize(*self).to_bits().to_le_bytes();
914+
*ptr = self.to_bits().to_le_bytes();
928915
Ok(())
929916
}
930917
}
@@ -933,14 +920,14 @@ macro_rules! floats {
933920
#[inline]
934921
fn lift(_cx: &mut LiftContext<'_>, ty: InterfaceType, src: &Self::Lower) -> Result<Self> {
935922
debug_assert!(matches!(ty, InterfaceType::$ty));
936-
Ok(canonicalize($float::from_bits(src.$get_float())))
923+
Ok($float::from_bits(src.$get_float()))
937924
}
938925

939926
#[inline]
940927
fn load(_cx: &mut LiftContext<'_>, ty: InterfaceType, bytes: &[u8]) -> Result<Self> {
941928
debug_assert!(matches!(ty, InterfaceType::$ty));
942929
debug_assert!((bytes.as_ptr() as usize) % Self::SIZE32 == 0);
943-
Ok(canonicalize($float::from_le_bytes(bytes.try_into().unwrap())))
930+
Ok($float::from_le_bytes(bytes.try_into().unwrap()))
944931
}
945932
}
946933
};)*)

tests/all/component_model/func.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,26 +503,26 @@ fn floats() -> Result<()> {
503503
.call(&mut store, (CANON_32BIT_NAN | 1,))?
504504
.0
505505
.to_bits(),
506-
CANON_32BIT_NAN
506+
CANON_32BIT_NAN | 1
507507
);
508508
u32_to_f32.post_return(&mut store)?;
509509
assert_eq!(
510510
u64_to_f64
511511
.call(&mut store, (CANON_64BIT_NAN | 1,))?
512512
.0
513513
.to_bits(),
514-
CANON_64BIT_NAN,
514+
CANON_64BIT_NAN | 1,
515515
);
516516
u64_to_f64.post_return(&mut store)?;
517517

518518
assert_eq!(
519519
f32_to_u32.call(&mut store, (f32::from_bits(CANON_32BIT_NAN | 1),))?,
520-
(CANON_32BIT_NAN,)
520+
(CANON_32BIT_NAN | 1,)
521521
);
522522
f32_to_u32.post_return(&mut store)?;
523523
assert_eq!(
524524
f64_to_u64.call(&mut store, (f64::from_bits(CANON_64BIT_NAN | 1),))?,
525-
(CANON_64BIT_NAN,)
525+
(CANON_64BIT_NAN | 1,)
526526
);
527527
f64_to_u64.post_return(&mut store)?;
528528

@@ -937,7 +937,10 @@ fn many_parameters() -> Result<()> {
937937
assert_eq!(i8::from_le_bytes(*actual.take_n::<1>()), input.0);
938938
actual.skip::<7>();
939939
assert_eq!(u64::from_le_bytes(*actual.take_n::<8>()), input.1);
940-
assert_eq!(u32::from_le_bytes(*actual.take_n::<4>()), CANON_32BIT_NAN);
940+
assert_eq!(
941+
u32::from_le_bytes(*actual.take_n::<4>()),
942+
CANON_32BIT_NAN | 1
943+
);
941944
assert_eq!(u8::from_le_bytes(*actual.take_n::<1>()), input.3);
942945
actual.skip::<1>();
943946
assert_eq!(i16::from_le_bytes(*actual.take_n::<2>()), input.4);
@@ -1703,7 +1706,7 @@ fn expected() -> Result<()> {
17031706
let ret = to_expected_s16_f32
17041707
.call(&mut store, (1, CANON_32BIT_NAN | 1))?
17051708
.0;
1706-
assert_eq!(ret.unwrap_err().to_bits(), CANON_32BIT_NAN);
1709+
assert_eq!(ret.unwrap_err().to_bits(), CANON_32BIT_NAN | 1);
17071710
to_expected_s16_f32.post_return(&mut store)?;
17081711
assert!(to_expected_s16_f32.call(&mut store, (2, 0)).is_err());
17091712

0 commit comments

Comments
 (0)