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

Commit 049f942

Browse files
committed
Introduce math::arch::intrinsics
This module provides implementations of basic functions that defer to LLVM for what to do, rather than either using a builtin operation or calling another function in this library. `math::arch` will become the home of anything architecture-specific in the future.
1 parent 86a9a75 commit 049f942

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/math/arch/intrinsics.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Config is needed for times when this module is available but we don't call everything
2+
#![allow(dead_code)]
3+
4+
pub fn ceil(x: f64) -> f64 {
5+
// SAFETY: safe intrinsic with no preconditions
6+
unsafe { core::intrinsics::ceilf64(x) }
7+
}
8+
9+
pub fn ceilf(x: f32) -> f32 {
10+
// SAFETY: safe intrinsic with no preconditions
11+
unsafe { core::intrinsics::ceilf32(x) }
12+
}
13+
14+
pub fn fabs(x: f64) -> f64 {
15+
// SAFETY: safe intrinsic with no preconditions
16+
unsafe { core::intrinsics::fabsf64(x) }
17+
}
18+
19+
pub fn fabsf(x: f32) -> f32 {
20+
// SAFETY: safe intrinsic with no preconditions
21+
unsafe { core::intrinsics::fabsf32(x) }
22+
}
23+
24+
pub fn floor(x: f64) -> f64 {
25+
// SAFETY: safe intrinsic with no preconditions
26+
unsafe { core::intrinsics::floorf64(x) }
27+
}
28+
29+
pub fn floorf(x: f32) -> f32 {
30+
// SAFETY: safe intrinsic with no preconditions
31+
unsafe { core::intrinsics::floorf32(x) }
32+
}
33+
34+
pub fn sqrt(x: f64) -> f64 {
35+
// SAFETY: safe intrinsic with no preconditions
36+
unsafe { core::intrinsics::sqrtf64(x) }
37+
}
38+
39+
pub fn sqrtf(x: f32) -> f32 {
40+
// SAFETY: safe intrinsic with no preconditions
41+
unsafe { core::intrinsics::sqrtf32(x) }
42+
}
43+
44+
pub fn trunc(x: f64) -> f64 {
45+
// SAFETY: safe intrinsic with no preconditions
46+
unsafe { core::intrinsics::truncf64(x) }
47+
}
48+
49+
pub fn truncf(x: f32) -> f32 {
50+
// SAFETY: safe intrinsic with no preconditions
51+
unsafe { core::intrinsics::truncf32(x) }
52+
}

src/math/arch/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Architecture-specific routines and operations.
2+
//!
3+
//! LLVM will already optimize calls to some of these in cases that there are hardware
4+
//! instructions. Providing an implementation here just ensures that the faster implementation
5+
//! is used when calling the function directly. This helps anyone who uses `libm` directly, as
6+
//! well as improving things when these routines are called as part of other implementations.
7+
8+
#[cfg(intrinsics_enabled)]
9+
pub mod intrinsics;

src/math/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ pub use self::trunc::trunc;
302302
pub use self::truncf::truncf;
303303

304304
// Private modules
305+
mod arch;
305306
mod expo2;
306307
mod fenv;
307308
mod k_cos;

0 commit comments

Comments
 (0)