From 2c7495c41bfd5fd2245823f870a2cd97fcfd7f0e Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Fri, 7 Feb 2025 05:22:07 +0000 Subject: [PATCH 1/3] Add a way to avoid `f32` implementations that do math as an `f64` --- ci/run.sh | 3 +++ configure.rs | 10 ++++++++++ etc/has-nowiden-impl.txt | 2 ++ src/math/fmaf.rs | 6 +++++- src/math/generic/fma.rs | 10 ++++++++++ 5 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 etc/has-nowiden-impl.txt diff --git a/ci/run.sh b/ci/run.sh index a946d325e..dcbd0bac4 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -14,6 +14,8 @@ if [ -z "$target" ]; then target="$host_target" fi +nowiden_tests=$($cmd "$profile" release-checked --features force-soft-floats) + # We enumerate features manually. flags="$flags --no-default-features" @@ -112,6 +114,7 @@ $cmd --features unstable-intrinsics --benches # Test the same in release mode, which also increases coverage. Also ensure # the soft float routines are checked. $cmd "$profile" release-checked +LIBM_F32_NO_WIDEN=1 $cmd "$profile" release-checked -- $nowiden_tests $cmd "$profile" release-checked --features force-soft-floats $cmd "$profile" release-checked --features unstable-intrinsics $cmd "$profile" release-checked --features unstable-intrinsics --benches diff --git a/configure.rs b/configure.rs index 8b8ba9815..e696492cf 100644 --- a/configure.rs +++ b/configure.rs @@ -50,6 +50,7 @@ pub fn emit_libm_config(cfg: &Config) { emit_intrinsics_cfg(); emit_arch_cfg(); emit_optimization_cfg(cfg); + emit_widen_cfg(cfg); emit_cfg_shorthands(cfg); emit_cfg_env(cfg); emit_f16_f128_cfg(cfg); @@ -96,6 +97,15 @@ fn emit_optimization_cfg(cfg: &Config) { } } +fn emit_widen_cfg(_cfg: &Config) { + println!("cargo:rustc-check-cfg=cfg(f32_no_widen)"); + println!("cargo:rerun-if-env-changed=LIBM_F32_NO_WIDEN"); + + if env::var_os("LIBM_F32_NO_WIDEN").is_some() { + println!("cargo:rustc-cfg=f32_no_widen"); + } +} + /// Provide an alias for common longer config combinations. fn emit_cfg_shorthands(cfg: &Config) { println!("cargo:rustc-check-cfg=cfg(x86_no_sse)"); diff --git a/etc/has-nowiden-impl.txt b/etc/has-nowiden-impl.txt new file mode 100644 index 000000000..4b61bec79 --- /dev/null +++ b/etc/has-nowiden-impl.txt @@ -0,0 +1,2 @@ +# Functions that have a different version based on f32_no_widen +fmaf diff --git a/src/math/fmaf.rs b/src/math/fmaf.rs index 40d7f40d6..0fcc96b93 100644 --- a/src/math/fmaf.rs +++ b/src/math/fmaf.rs @@ -5,7 +5,11 @@ /// according to the rounding mode characterized by the value of FLT_ROUNDS. #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] pub fn fmaf(x: f32, y: f32, z: f32) -> f32 { - super::generic::fma_wide(x, y, z) + if cfg!(f32_no_widen) { + super::generic::fma(x, y, z) + } else { + super::generic::fma_wide(x, y, z) + } } #[cfg(test)] diff --git a/src/math/generic/fma.rs b/src/math/generic/fma.rs index a40d7aaaf..719154553 100644 --- a/src/math/generic/fma.rs +++ b/src/math/generic/fma.rs @@ -345,6 +345,16 @@ pub trait FmaHelper { fn raise_underflow_ret_zero(self) -> Self; } +impl FmaHelper for f32 { + fn raise_underflow_as_min_positive(self) -> Self { + f32::MIN_POSITIVE.copysign(self) + } + + fn raise_underflow_ret_zero(self) -> Self { + f32::ZERO + } +} + impl FmaHelper for f64 { fn raise_underflow_as_min_positive(self) -> Self { /* min normal after rounding, underflow depends From 888e51c7f407247f88c8dd27a4d1f7d9a71e7965 Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Fri, 7 Feb 2025 05:24:21 +0000 Subject: [PATCH 2/3] sh update --- ci/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/run.sh b/ci/run.sh index dcbd0bac4..b42959af4 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -114,7 +114,7 @@ $cmd --features unstable-intrinsics --benches # Test the same in release mode, which also increases coverage. Also ensure # the soft float routines are checked. $cmd "$profile" release-checked -LIBM_F32_NO_WIDEN=1 $cmd "$profile" release-checked -- $nowiden_tests +[ -n "${nowiden_tests:-}" ] && LIBM_F32_NO_WIDEN=1 $cmd "$profile" release-checked -- $nowiden_tests $cmd "$profile" release-checked --features force-soft-floats $cmd "$profile" release-checked --features unstable-intrinsics $cmd "$profile" release-checked --features unstable-intrinsics --benches From 98c6fa65ed5033d171853c96b29a71260fdde6e8 Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Fri, 7 Feb 2025 05:38:42 +0000 Subject: [PATCH 3/3] sh update --- ci/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/run.sh b/ci/run.sh index b42959af4..739a644dd 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -14,7 +14,7 @@ if [ -z "$target" ]; then target="$host_target" fi -nowiden_tests=$($cmd "$profile" release-checked --features force-soft-floats) +nowiden_tests=$(grep -vE '^#' etc/has-nowiden-impl.txt | tr '\n' ' ') # We enumerate features manually. flags="$flags --no-default-features"