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

Commit a498d47

Browse files
committed
Replace "intrinsic" config with "arch" config
WASM is the only architecture we use `intrinsics::` for. We probably don't want to do this for any other architectures since it is better to use assembly, or work toward getting the functions available in `core`. To more accurately reflect the relationship between arch and intrinsics, make wasm32 an `arch` module and call the intrinsics from there.
1 parent 71ef733 commit a498d47

14 files changed

+37
-50
lines changed

libm/etc/function-definitions.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@
108108
"sources": [
109109
"src/libm_helper.rs",
110110
"src/math/arch/i586.rs",
111-
"src/math/arch/intrinsics.rs",
111+
"src/math/arch/wasm32.rs",
112112
"src/math/ceil.rs"
113113
],
114114
"type": "f64"
115115
},
116116
"ceilf": {
117117
"sources": [
118-
"src/math/arch/intrinsics.rs",
118+
"src/math/arch/wasm32.rs",
119119
"src/math/ceilf.rs"
120120
],
121121
"type": "f32"
@@ -258,15 +258,15 @@
258258
"fabs": {
259259
"sources": [
260260
"src/libm_helper.rs",
261-
"src/math/arch/intrinsics.rs",
261+
"src/math/arch/wasm32.rs",
262262
"src/math/fabs.rs",
263263
"src/math/generic/fabs.rs"
264264
],
265265
"type": "f64"
266266
},
267267
"fabsf": {
268268
"sources": [
269-
"src/math/arch/intrinsics.rs",
269+
"src/math/arch/wasm32.rs",
270270
"src/math/fabsf.rs",
271271
"src/math/generic/fabs.rs"
272272
],
@@ -303,14 +303,14 @@
303303
"sources": [
304304
"src/libm_helper.rs",
305305
"src/math/arch/i586.rs",
306-
"src/math/arch/intrinsics.rs",
306+
"src/math/arch/wasm32.rs",
307307
"src/math/floor.rs"
308308
],
309309
"type": "f64"
310310
},
311311
"floorf": {
312312
"sources": [
313-
"src/math/arch/intrinsics.rs",
313+
"src/math/arch/wasm32.rs",
314314
"src/math/floorf.rs"
315315
],
316316
"type": "f32"
@@ -683,15 +683,15 @@
683683
"sources": [
684684
"src/libm_helper.rs",
685685
"src/math/arch/i686.rs",
686-
"src/math/arch/intrinsics.rs",
686+
"src/math/arch/wasm32.rs",
687687
"src/math/sqrt.rs"
688688
],
689689
"type": "f64"
690690
},
691691
"sqrtf": {
692692
"sources": [
693693
"src/math/arch/i686.rs",
694-
"src/math/arch/intrinsics.rs",
694+
"src/math/arch/wasm32.rs",
695695
"src/math/sqrtf.rs"
696696
],
697697
"type": "f32"
@@ -738,14 +738,14 @@
738738
"trunc": {
739739
"sources": [
740740
"src/libm_helper.rs",
741-
"src/math/arch/intrinsics.rs",
741+
"src/math/arch/wasm32.rs",
742742
"src/math/trunc.rs"
743743
],
744744
"type": "f64"
745745
},
746746
"truncf": {
747747
"sources": [
748-
"src/math/arch/intrinsics.rs",
748+
"src/math/arch/wasm32.rs",
749749
"src/math/truncf.rs"
750750
],
751751
"type": "f32"

libm/src/math/arch/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
//! is used when calling the function directly. This helps anyone who uses `libm` directly, as
66
//! well as improving things when these routines are called as part of other implementations.
77
8-
#[cfg(intrinsics_enabled)]
9-
pub mod intrinsics;
10-
118
// Most implementations should be defined here, to ensure they are not made available when
129
// soft floats are required.
1310
#[cfg(arch_enabled)]
1411
cfg_if! {
15-
if #[cfg(target_feature = "sse2")] {
12+
if #[cfg(all(target_arch = "wasm32", intrinsics_enabled))] {
13+
mod wasm32;
14+
pub use wasm32::{ceil, ceilf, fabs, fabsf, floor, floorf, sqrt, sqrtf, trunc, truncf};
15+
} else if #[cfg(target_feature = "sse2")] {
1616
mod i686;
1717
pub use i686::{sqrt, sqrtf};
1818
}

libm/src/math/arch/intrinsics.rs renamed to libm/src/math/arch/wasm32.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// Config is needed for times when this module is available but we don't call everything
2-
#![allow(dead_code)]
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.
35
46
pub fn ceil(x: f64) -> f64 {
57
// SAFETY: safe intrinsic with no preconditions

libm/src/math/ceil.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const TOINT: f64 = 1. / f64::EPSILON;
1010
pub fn ceil(x: f64) -> f64 {
1111
select_implementation! {
1212
name: ceil,
13+
use_arch: all(target_arch = "wasm32", intrinsics_enabled),
1314
use_arch_required: all(target_arch = "x86", not(target_feature = "sse2")),
14-
use_intrinsic: target_arch = "wasm32",
1515
args: x,
1616
}
1717

libm/src/math/ceilf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::f32;
77
pub fn ceilf(x: f32) -> f32 {
88
select_implementation! {
99
name: ceilf,
10-
use_intrinsic: target_arch = "wasm32",
10+
use_arch: all(target_arch = "wasm32", intrinsics_enabled),
1111
args: x,
1212
}
1313

libm/src/math/fabs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
pub fn fabs(x: f64) -> f64 {
77
select_implementation! {
88
name: fabs,
9-
use_intrinsic: target_arch = "wasm32",
9+
use_arch: all(target_arch = "wasm32", intrinsics_enabled),
1010
args: x,
1111
}
1212

libm/src/math/fabsf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
pub fn fabsf(x: f32) -> f32 {
77
select_implementation! {
88
name: fabsf,
9-
use_intrinsic: target_arch = "wasm32",
9+
use_arch: all(target_arch = "wasm32", intrinsics_enabled),
1010
args: x,
1111
}
1212

libm/src/math/floor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const TOINT: f64 = 1. / f64::EPSILON;
1010
pub fn floor(x: f64) -> f64 {
1111
select_implementation! {
1212
name: floor,
13+
use_arch: all(target_arch = "wasm32", intrinsics_enabled),
1314
use_arch_required: all(target_arch = "x86", not(target_feature = "sse2")),
14-
use_intrinsic: target_arch = "wasm32",
1515
args: x,
1616
}
1717

libm/src/math/floorf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::f32;
77
pub fn floorf(x: f32) -> f32 {
88
select_implementation! {
99
name: floorf,
10-
use_intrinsic: target_arch = "wasm32",
10+
use_arch: all(target_arch = "wasm32", intrinsics_enabled),
1111
args: x,
1212
}
1313

libm/src/math/sqrt.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ use core::f64;
8383
pub fn sqrt(x: f64) -> f64 {
8484
select_implementation! {
8585
name: sqrt,
86-
use_arch: target_feature = "sse2",
87-
use_intrinsic: target_arch = "wasm32",
86+
use_arch: any(
87+
all(target_arch = "wasm32", intrinsics_enabled),
88+
target_feature = "sse2"
89+
),
8890
args: x,
8991
}
9092

libm/src/math/sqrtf.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
pub fn sqrtf(x: f32) -> f32 {
1919
select_implementation! {
2020
name: sqrtf,
21-
use_arch: target_feature = "sse2",
22-
use_intrinsic: target_arch = "wasm32",
21+
use_arch: any(
22+
all(target_arch = "wasm32", intrinsics_enabled),
23+
target_feature = "sse2"
24+
),
2325
args: x,
2426
}
2527

libm/src/math/support/macros.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,16 @@ macro_rules! cfg_if {
3939
(@__identity $($tokens:tt)*) => { $($tokens)* };
4040
}
4141

42-
/// Choose among using an intrinsic, an arch-specific implementation, and the function body.
43-
/// Returns directly if the intrinsic or arch is used, otherwise continue with the rest of the
44-
/// function.
45-
///
46-
/// Specify a `use_intrinsic` meta field if the intrinsic is (1) available on the platforms (i.e.
47-
/// LLVM lowers it without libcalls that may recurse), (2) it is likely to be more performant.
48-
/// Intrinsics require wrappers in the `math::arch::intrinsics` module.
42+
/// Choose between using an arch-specific implementation and the function body. Returns directly
43+
/// if the arch implementation is used, otherwise continue with the rest of the function.
4944
///
5045
/// Specify a `use_arch` meta field if an architecture-specific implementation is provided.
5146
/// These live in the `math::arch::some_target_arch` module.
5247
///
5348
/// Specify a `use_arch_required` meta field if something architecture-specific must be used
5449
/// regardless of feature configuration (`force-soft-floats`).
5550
///
56-
/// The passed meta options do not need to account for relevant Cargo features
57-
/// (`unstable-intrinsics`, `arch`, `force-soft-floats`), this macro handles that part.
51+
/// The passed meta options do not need to account for the `arch` target feature.
5852
macro_rules! select_implementation {
5953
(
6054
name: $fn_name:ident,
@@ -64,15 +58,12 @@ macro_rules! select_implementation {
6458
// Configuration meta for when to use the arch module regardless of whether softfloats
6559
// have been requested.
6660
$( use_arch_required: $use_arch_required:meta, )?
67-
// Configuration meta for when to call intrinsics and let LLVM figure it out
68-
$( use_intrinsic: $use_intrinsic:meta, )?
6961
args: $($arg:ident),+ ,
7062
) => {
7163
// FIXME: these use paths that are a pretty fragile (`super`). We should figure out
7264
// something better w.r.t. how this is vendored into compiler-builtins.
7365

7466
// However, we do need a few things from `arch` that are used even with soft floats.
75-
//
7667
select_implementation! {
7768
@cfg $($use_arch_required)?;
7869
if true {
@@ -89,16 +80,6 @@ macro_rules! select_implementation {
8980
return super::arch::$fn_name( $($arg),+ );
9081
}
9182
}
92-
93-
// Never use intrinsics if we are forcing soft floats, and only enable with the
94-
// `unstable-intrinsics` feature.
95-
#[cfg(intrinsics_enabled)]
96-
select_implementation! {
97-
@cfg $( $use_intrinsic )?;
98-
if true {
99-
return super::arch::intrinsics::$fn_name( $($arg),+ );
100-
}
101-
}
10283
};
10384

10485
// Coalesce helper to construct an expression only if a config is provided

libm/src/math/trunc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::f64;
77
pub fn trunc(x: f64) -> f64 {
88
select_implementation! {
99
name: trunc,
10-
use_intrinsic: target_arch = "wasm32",
10+
use_arch: all(target_arch = "wasm32", intrinsics_enabled),
1111
args: x,
1212
}
1313

libm/src/math/truncf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::f32;
77
pub fn truncf(x: f32) -> f32 {
88
select_implementation! {
99
name: truncf,
10-
use_intrinsic: target_arch = "wasm32",
10+
use_arch: all(target_arch = "wasm32", intrinsics_enabled),
1111
args: x,
1212
}
1313

0 commit comments

Comments
 (0)