|
| 1 | +/* SPDX-License-Identifier: MIT */ |
| 2 | +/* origin: musl src/math/fma.c, fmaf.c Ported to generic Rust algorithm in 2025, TG. */ |
| 3 | + |
| 4 | +use super::generic; |
| 5 | +use crate::support::Round; |
| 6 | + |
| 7 | +// Placeholder so we can have `fmaf16` in the `Float` trait. |
| 8 | +#[allow(unused)] |
| 9 | +#[cfg(f16_enabled)] |
| 10 | +#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] |
| 11 | +pub(crate) fn fmaf16(_x: f16, _y: f16, _z: f16) -> f16 { |
| 12 | + unimplemented!() |
| 13 | +} |
| 14 | + |
| 15 | +/// Floating multiply add (f32) |
| 16 | +/// |
| 17 | +/// Computes `(x*y)+z`, rounded as one ternary operation (i.e. calculated with infinite precision). |
| 18 | +#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] |
| 19 | +pub fn fmaf(x: f32, y: f32, z: f32) -> f32 { |
| 20 | + select_implementation! { |
| 21 | + name: fmaf, |
| 22 | + use_arch: all(target_arch = "aarch64", target_feature = "neon"), |
| 23 | + args: x, y, z, |
| 24 | + } |
| 25 | + |
| 26 | + generic::fma_wide_round(x, y, z, Round::Nearest).val |
| 27 | +} |
| 28 | + |
| 29 | +/// Fused multiply add (f64) |
| 30 | +/// |
| 31 | +/// Computes `(x*y)+z`, rounded as one ternary operation (i.e. calculated with infinite precision). |
| 32 | +#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] |
| 33 | +pub fn fma(x: f64, y: f64, z: f64) -> f64 { |
| 34 | + select_implementation! { |
| 35 | + name: fma, |
| 36 | + use_arch: all(target_arch = "aarch64", target_feature = "neon"), |
| 37 | + args: x, y, z, |
| 38 | + } |
| 39 | + |
| 40 | + generic::fma_round(x, y, z, Round::Nearest).val |
| 41 | +} |
| 42 | + |
| 43 | +/// Fused multiply add (f128) |
| 44 | +/// |
| 45 | +/// Computes `(x*y)+z`, rounded as one ternary operation (i.e. calculated with infinite precision). |
| 46 | +#[cfg(f128_enabled)] |
| 47 | +#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] |
| 48 | +pub fn fmaf128(x: f128, y: f128, z: f128) -> f128 { |
| 49 | + generic::fma_round(x, y, z, Round::Nearest).val |
| 50 | +} |
| 51 | + |
| 52 | +#[cfg(test)] |
| 53 | +mod tests { |
| 54 | + use super::*; |
| 55 | + use crate::support::{CastFrom, CastInto, Float, FpResult, HInt, MinInt, Round, Status}; |
| 56 | + |
| 57 | + /// Test the generic `fma_round` algorithm for a given float. |
| 58 | + fn spec_test<F>(f: impl Fn(F, F, F) -> F) |
| 59 | + where |
| 60 | + F: Float, |
| 61 | + F: CastFrom<F::SignedInt>, |
| 62 | + F: CastFrom<i8>, |
| 63 | + F::Int: HInt, |
| 64 | + u32: CastInto<F::Int>, |
| 65 | + { |
| 66 | + let x = F::from_bits(F::Int::ONE); |
| 67 | + let y = F::from_bits(F::Int::ONE); |
| 68 | + let z = F::ZERO; |
| 69 | + |
| 70 | + // 754-2020 says "When the exact result of (a × b) + c is non-zero yet the result of |
| 71 | + // fusedMultiplyAdd is zero because of rounding, the zero result takes the sign of the |
| 72 | + // exact result" |
| 73 | + assert_biteq!(f(x, y, z), F::ZERO); |
| 74 | + assert_biteq!(f(x, -y, z), F::NEG_ZERO); |
| 75 | + assert_biteq!(f(-x, y, z), F::NEG_ZERO); |
| 76 | + assert_biteq!(f(-x, -y, z), F::ZERO); |
| 77 | + } |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn spec_test_f32() { |
| 81 | + spec_test::<f32>(fmaf); |
| 82 | + |
| 83 | + // Also do a small check that the non-widening version works for f32 (this should ideally |
| 84 | + // get tested some more). |
| 85 | + spec_test::<f32>(|x, y, z| generic::fma_round(x, y, z, Round::Nearest).val); |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn spec_test_f64() { |
| 90 | + spec_test::<f64>(fma); |
| 91 | + |
| 92 | + let expect_underflow = [ |
| 93 | + ( |
| 94 | + hf64!("0x1.0p-1070"), |
| 95 | + hf64!("0x1.0p-1070"), |
| 96 | + hf64!("0x1.ffffffffffffp-1023"), |
| 97 | + hf64!("0x0.ffffffffffff8p-1022"), |
| 98 | + ), |
| 99 | + ( |
| 100 | + // FIXME: we raise underflow but this should only be inexact (based on C and |
| 101 | + // `rustc_apfloat`). |
| 102 | + hf64!("0x1.0p-1070"), |
| 103 | + hf64!("0x1.0p-1070"), |
| 104 | + hf64!("-0x1.0p-1022"), |
| 105 | + hf64!("-0x1.0p-1022"), |
| 106 | + ), |
| 107 | + ]; |
| 108 | + |
| 109 | + for (x, y, z, res) in expect_underflow { |
| 110 | + let FpResult { val, status } = generic::fma_round(x, y, z, Round::Nearest); |
| 111 | + assert_biteq!(val, res); |
| 112 | + assert_eq!(status, Status::UNDERFLOW); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + #[cfg(f128_enabled)] |
| 118 | + fn spec_test_f128() { |
| 119 | + spec_test::<f128>(fmaf128); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn issue_263() { |
| 124 | + let a = f32::from_bits(1266679807); |
| 125 | + let b = f32::from_bits(1300234242); |
| 126 | + let c = f32::from_bits(1115553792); |
| 127 | + let expected = f32::from_bits(1501560833); |
| 128 | + assert_eq!(fmaf(a, b, c), expected); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn fma_segfault() { |
| 133 | + // These two inputs cause fma to segfault on release due to overflow: |
| 134 | + assert_eq!( |
| 135 | + fma( |
| 136 | + -0.0000000000000002220446049250313, |
| 137 | + -0.0000000000000002220446049250313, |
| 138 | + -0.0000000000000002220446049250313 |
| 139 | + ), |
| 140 | + -0.00000000000000022204460492503126, |
| 141 | + ); |
| 142 | + |
| 143 | + let result = fma(-0.992, -0.992, -0.992); |
| 144 | + //force rounding to storage format on x87 to prevent superious errors. |
| 145 | + #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))] |
| 146 | + let result = force_eval!(result); |
| 147 | + assert_eq!(result, -0.007936000000000007,); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn fma_sbb() { |
| 152 | + assert_eq!( |
| 153 | + fma(-(1.0 - f64::EPSILON), f64::MIN, f64::MIN), |
| 154 | + -3991680619069439e277 |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn fma_underflow() { |
| 160 | + assert_eq!( |
| 161 | + fma(1.1102230246251565e-16, -9.812526705433188e-305, 1.0894e-320), |
| 162 | + 0.0, |
| 163 | + ); |
| 164 | + } |
| 165 | +} |
0 commit comments