Skip to content

Commit 5b927ee

Browse files
committed
Use core::arch::wasm functions rather than intrinsics
These wasm functions are available in `core::arch::wasm32` since [1], so we can use them while avoiding the possibly-recursive `intrinsics::*` calls (in practice none of those should always lower to libcalls on wasm, but that is up to LLVM). Since these require an unstable feature, they are still gated under `unstable-intrinsics`. [1]: rust-lang/stdarch#1677
1 parent 0f6b1bb commit 5b927ee

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![no_std]
33
#![cfg_attr(intrinsics_enabled, allow(internal_features))]
44
#![cfg_attr(intrinsics_enabled, feature(core_intrinsics))]
5+
#![cfg_attr(intrinsics_enabled, feature(wasm_numeric_instr))]
56
#![cfg_attr(f128_enabled, feature(f128))]
67
#![cfg_attr(f16_enabled, feature(f16))]
78
#![allow(clippy::assign_op_pattern)]

src/math/arch/wasm32.rs

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
//! Wasm asm is not stable; just use intrinsics for operations that have asm routine equivalents.
2-
//!
3-
//! Note that we need to be absolutely certain that everything here lowers to assembly operations,
4-
//! otherwise libcalls will be recursive.
1+
//! Wasm has builtins for simple float operations. Use the unstable `core::arch` intrinsics which
2+
//! are significantly faster than soft float operations.
53
64
pub fn ceil(x: f64) -> f64 {
7-
// SAFETY: safe intrinsic with no preconditions
8-
unsafe { core::intrinsics::ceilf64(x) }
5+
core::arch::wasm32::f64_ceil(x)
96
}
107

118
pub fn ceilf(x: f32) -> f32 {
12-
// SAFETY: safe intrinsic with no preconditions
13-
unsafe { core::intrinsics::ceilf32(x) }
9+
core::arch::wasm32::f32_ceil(x)
1410
}
1511

1612
pub fn fabs(x: f64) -> f64 {
@@ -22,31 +18,25 @@ pub fn fabsf(x: f32) -> f32 {
2218
}
2319

2420
pub fn floor(x: f64) -> f64 {
25-
// SAFETY: safe intrinsic with no preconditions
26-
unsafe { core::intrinsics::floorf64(x) }
21+
core::arch::wasm32::f64_floor(x)
2722
}
2823

2924
pub fn floorf(x: f32) -> f32 {
30-
// SAFETY: safe intrinsic with no preconditions
31-
unsafe { core::intrinsics::floorf32(x) }
25+
core::arch::wasm32::f32_floor(x)
3226
}
3327

3428
pub fn sqrt(x: f64) -> f64 {
35-
// SAFETY: safe intrinsic with no preconditions
36-
unsafe { core::intrinsics::sqrtf64(x) }
29+
core::arch::wasm32::f64_sqrt(x)
3730
}
3831

3932
pub fn sqrtf(x: f32) -> f32 {
40-
// SAFETY: safe intrinsic with no preconditions
41-
unsafe { core::intrinsics::sqrtf32(x) }
33+
core::arch::wasm32::f32_sqrt(x)
4234
}
4335

4436
pub fn trunc(x: f64) -> f64 {
45-
// SAFETY: safe intrinsic with no preconditions
46-
unsafe { core::intrinsics::truncf64(x) }
37+
core::arch::wasm32::f64_trunc(x)
4738
}
4839

4940
pub fn truncf(x: f32) -> f32 {
50-
// SAFETY: safe intrinsic with no preconditions
51-
unsafe { core::intrinsics::truncf32(x) }
41+
core::arch::wasm32::f32_trunc(x)
5242
}

0 commit comments

Comments
 (0)