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

Commit f216aab

Browse files
committed
Introduce a select_implementation macro
Currently there is a macro called `llvm_intrinsically_optimized` that uses an intrinsic rather than the function implementation if the configuration is correct. Add a new macro `select_implementation` that is somewhat cleaner to use. In the future, we can update this macro with more fields to specify other implementations that may be selected, such as something architecture-specific or e.g. using a generic implementation for `f32` routines, rather than those that convert to `f64`. This introduces a `macros` module within `math/support`. We will be able to move more things here later.
1 parent 049f942 commit f216aab

File tree

3 files changed

+69
-30
lines changed

3 files changed

+69
-30
lines changed

src/math/mod.rs

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ macro_rules! div {
7474
};
7575
}
7676

77+
// FIXME: phase this out, to be replaced by the more flexible `select_implementation`
7778
macro_rules! llvm_intrinsically_optimized {
7879
(#[cfg($($clause:tt)*)] $e:expr) => {
7980
#[cfg(all(intrinsics_enabled, not(feature = "force-soft-floats"), $($clause)*))]
@@ -85,6 +86,38 @@ macro_rules! llvm_intrinsically_optimized {
8586
};
8687
}
8788

89+
// Private modules
90+
#[macro_use]
91+
mod support;
92+
mod arch;
93+
mod expo2;
94+
mod fenv;
95+
mod k_cos;
96+
mod k_cosf;
97+
mod k_expo2;
98+
mod k_expo2f;
99+
mod k_sin;
100+
mod k_sinf;
101+
mod k_tan;
102+
mod k_tanf;
103+
mod rem_pio2;
104+
mod rem_pio2_large;
105+
mod rem_pio2f;
106+
107+
// Private re-imports
108+
use self::expo2::expo2;
109+
use self::k_cos::k_cos;
110+
use self::k_cosf::k_cosf;
111+
use self::k_expo2::k_expo2;
112+
use self::k_expo2f::k_expo2f;
113+
use self::k_sin::k_sin;
114+
use self::k_sinf::k_sinf;
115+
use self::k_tan::k_tan;
116+
use self::k_tanf::k_tanf;
117+
use self::rem_pio2::rem_pio2;
118+
use self::rem_pio2_large::rem_pio2_large;
119+
use self::rem_pio2f::rem_pio2f;
120+
88121
// Public modules
89122
mod acos;
90123
mod acosf;
@@ -301,36 +334,6 @@ pub use self::tgammaf::tgammaf;
301334
pub use self::trunc::trunc;
302335
pub use self::truncf::truncf;
303336

304-
// Private modules
305-
mod arch;
306-
mod expo2;
307-
mod fenv;
308-
mod k_cos;
309-
mod k_cosf;
310-
mod k_expo2;
311-
mod k_expo2f;
312-
mod k_sin;
313-
mod k_sinf;
314-
mod k_tan;
315-
mod k_tanf;
316-
mod rem_pio2;
317-
mod rem_pio2_large;
318-
mod rem_pio2f;
319-
320-
// Private re-imports
321-
use self::expo2::expo2;
322-
use self::k_cos::k_cos;
323-
use self::k_cosf::k_cosf;
324-
use self::k_expo2::k_expo2;
325-
use self::k_expo2f::k_expo2f;
326-
use self::k_sin::k_sin;
327-
use self::k_sinf::k_sinf;
328-
use self::k_tan::k_tan;
329-
use self::k_tanf::k_tanf;
330-
use self::rem_pio2::rem_pio2;
331-
use self::rem_pio2_large::rem_pio2_large;
332-
use self::rem_pio2f::rem_pio2f;
333-
334337
#[inline]
335338
fn get_high_word(x: f64) -> u32 {
336339
(x.to_bits() >> 32) as u32

src/math/support/macros.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/// Choose among using an intrinsic (if available) and falling back to the default function body.
2+
/// Returns directly if the intrinsic version is used, otherwise continues to the rest of the
3+
/// function.
4+
///
5+
/// Use this if the intrinsic is likely to be more performant on the platform(s) specified
6+
/// in `intrinsic_available`.
7+
///
8+
/// The `cfg` used here is controlled by `build.rs` so the passed meta does not need to account
9+
/// for e.g. the `unstable-intrinsics` or `force-soft-float` features.
10+
macro_rules! select_implementation {
11+
(
12+
name: $fname:ident,
13+
// Configuration meta for when to call intrinsics and let LLVM figure it out
14+
$( use_intrinsic: $use_intrinsic:meta, )?
15+
args: $($arg:ident),+ ,
16+
) => {
17+
// FIXME: these use paths that are a pretty fragile (`super`). We should figure out
18+
// something better w.r.t. how this is vendored into compiler-builtins.
19+
20+
// Never use intrinsics if we are forcing soft floats, and only enable with the
21+
// `unstable-intrinsics` feature.
22+
#[cfg(intrinsics_enabled)]
23+
select_implementation! {
24+
@cfg $( $use_intrinsic )?;
25+
if true {
26+
return super::arch::intrinsics::$fname( $($arg),+ );
27+
}
28+
}
29+
};
30+
31+
// Coalesce helper to construct an expression only if a config is provided
32+
(@cfg ; $ex:expr) => { };
33+
(@cfg $provided:meta; $ex:expr) => { #[cfg($provided)] $ex };
34+
}

src/math/support/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[macro_use]
2+
pub mod macros;

0 commit comments

Comments
 (0)