Skip to content

Commit

Permalink
Add only-soft-floats feature to prevent using any intrinsics or arc…
Browse files Browse the repository at this point in the history
…h-specific code
  • Loading branch information
GuillaumeGomez committed Aug 6, 2024
1 parent 279e5f6 commit 230107d
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ unstable = []
# musl libc.
musl-reference-tests = ['rand']

# Used to prevent using any intrinsics or arch-specific code.
only-soft-floats = []

[workspace]
members = [
"crates/compiler-builtins-smoke-test",
Expand Down
2 changes: 1 addition & 1 deletion src/math/ceil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn ceil(x: f64) -> f64 {
// `f64.ceil` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "only-soft-floats")))] {
return unsafe { ::core::intrinsics::ceilf64(x) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/ceilf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn ceilf(x: f32) -> f32 {
// `f32.ceil` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "only-soft-floats")))] {
return unsafe { ::core::intrinsics::ceilf32(x) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/fabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn fabs(x: f64) -> f64 {
// `f64.abs` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "only-soft-floats")))] {
return unsafe { ::core::intrinsics::fabsf64(x) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/fabsf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn fabsf(x: f32) -> f32 {
// `f32.abs` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "only-soft-floats")))] {
return unsafe { ::core::intrinsics::fabsf32(x) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/floor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn floor(x: f64) -> f64 {
// `f64.floor` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "only-soft-floats")))] {
return unsafe { ::core::intrinsics::floorf64(x) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/floorf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn floorf(x: f32) -> f32 {
// `f32.floor` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "only-soft-floats")))] {
return unsafe { ::core::intrinsics::floorf32(x) }
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/math/sqrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ pub fn sqrt(x: f64) -> f64 {
// `f64.sqrt` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "only-soft-floats")))] {
return if x < 0.0 {
f64::NAN
} else {
unsafe { ::core::intrinsics::sqrtf64(x) }
}
}
}
#[cfg(target_feature = "sse2")]
#[cfg(all(target_feature = "sse2", not(feature = "only-soft-floats")))]
{
// Note: This path is unlikely since LLVM will usually have already
// optimized sqrt calls into hardware instructions if sse2 is available,
Expand All @@ -107,7 +107,7 @@ pub fn sqrt(x: f64) -> f64 {
_mm_cvtsd_f64(m_sqrt)
}
}
#[cfg(not(target_feature = "sse2"))]
#[cfg(any(not(target_feature = "sse2"), feature = "only-soft-floats"))]
{
use core::num::Wrapping;

Expand Down
6 changes: 3 additions & 3 deletions src/math/sqrtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ pub fn sqrtf(x: f32) -> f32 {
// `f32.sqrt` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "only-soft-floats")))] {
return if x < 0.0 {
::core::f32::NAN
} else {
unsafe { ::core::intrinsics::sqrtf32(x) }
}
}
}
#[cfg(target_feature = "sse")]
#[cfg(all(target_feature = "sse", not(feature = "only-soft-floats")))]
{
// Note: This path is unlikely since LLVM will usually have already
// optimized sqrt calls into hardware instructions if sse is available,
Expand All @@ -42,7 +42,7 @@ pub fn sqrtf(x: f32) -> f32 {
_mm_cvtss_f32(m_sqrt)
}
}
#[cfg(not(target_feature = "sse"))]
#[cfg(any(not(target_feature = "sse"), feature = "only-soft-floats"))]
{
const TINY: f32 = 1.0e-30;

Expand Down
2 changes: 1 addition & 1 deletion src/math/trunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn trunc(x: f64) -> f64 {
// `f64.trunc` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "only-soft-floats")))] {
return unsafe { ::core::intrinsics::truncf64(x) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/truncf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn truncf(x: f32) -> f32 {
// `f32.trunc` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "only-soft-floats")))] {
return unsafe { ::core::intrinsics::truncf32(x) }
}
}
Expand Down

0 comments on commit 230107d

Please sign in to comment.