#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Contraction {
Unfused,
Clang,
Gcc,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FusedProduct {
First,
Second,
}
impl Contraction {
#[must_use]
pub const fn fuses(self) -> bool {
!matches!(self, Self::Unfused)
}
#[must_use]
pub const fn fuses_iso_c(self) -> bool {
matches!(self, Self::Clang)
}
#[inline]
#[must_use]
pub fn fused(self, a: f64, b: f64, c: f64) -> f64 {
if self.fuses() {
rounded_once(a, b, c)
} else {
rounded_twice(a, b, c)
}
}
#[inline]
#[must_use]
pub fn difference_of_products(
self,
fused: FusedProduct,
a: f64,
b: f64,
c: f64,
d: f64,
) -> f64 {
match fused {
FusedProduct::First => self.fused(a, b, -(c * d)),
FusedProduct::Second => self.fused(-c, d, a * b),
}
}
#[inline]
#[must_use]
pub fn fused_iso_c(self, a: f64, b: f64, c: f64) -> f64 {
if self.fuses_iso_c() {
rounded_once(a, b, c)
} else {
rounded_twice(a, b, c)
}
}
}
pub const CONTRACTION: Contraction = if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
Contraction::Clang
} else if cfg!(all(target_os = "linux", target_arch = "aarch64")) {
Contraction::Gcc
} else {
Contraction::Unfused
};
pub const FUSES_PRODUCTS: bool = CONTRACTION.fuses();
#[inline]
#[must_use]
pub fn fused(a: f64, b: f64, c: f64) -> f64 {
CONTRACTION.fused(a, b, c)
}
#[inline]
#[must_use]
pub fn fused_iso_c(a: f64, b: f64, c: f64) -> f64 {
CONTRACTION.fused_iso_c(a, b, c)
}
#[inline]
#[must_use]
pub fn fused_f32(a: f32, b: f32, c: f32) -> f32 {
if FUSES_PRODUCTS {
a.mul_add(b, c)
} else {
a * b + c
}
}
#[inline]
#[must_use]
pub fn sum_of_products(a: f64, b: f64, c: f64, d: f64) -> f64 {
fused(a, b, c * d)
}
#[inline]
#[must_use]
pub fn difference_of_products(a: f64, b: f64, c: f64, d: f64) -> f64 {
fused(a, b, -(c * d))
}
#[inline]
fn rounded_once(a: f64, b: f64, c: f64) -> f64 {
a.mul_add(b, c)
}
#[inline]
fn rounded_twice(a: f64, b: f64, c: f64) -> f64 {
a * b + c
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fused_products_round_once_or_twice() {
assert_eq!(rounded_twice(0.1, 10.0, -1.0), 0.0);
assert_eq!(rounded_once(0.1, 10.0, -1.0), 2f64.powi(-54));
let x = 1.0 + 2f64.powi(-30);
assert_eq!(rounded_twice(x, x, -(1.0 + 2f64.powi(-29))), 0.0);
assert_eq!(rounded_once(x, x, -(1.0 + 2f64.powi(-29))), 2f64.powi(-60));
let third = 1.0 / 3.0;
assert_eq!(rounded_twice(third, 3.0, -(third * 3.0)), 0.0);
assert_eq!(rounded_once(third, 3.0, -(third * 3.0)), -(2f64.powi(-54)));
for (a, b, c) in [(3.0, 4.0, 5.0), (-0.5, 8.0, 4.0), (0.0, -1.0, 0.0)] {
assert_eq!(
rounded_once(a, b, c).to_bits(),
rounded_twice(a, b, c).to_bits()
);
}
let (a, b, c) = (0.3, 0.7, 0.21);
assert_eq!(rounded_twice(-a, b, c).to_bits(), (c - a * b).to_bits());
assert_eq!(rounded_twice(a, b, -c).to_bits(), (a * b - c).to_bits());
assert_eq!(
fused(a, b, -c),
if FUSES_PRODUCTS {
rounded_once(a, b, -c)
} else {
rounded_twice(a, b, -c)
}
);
assert!(fused(f64::NAN, 1.0, 1.0).is_nan());
assert_eq!(fused(f64::INFINITY, 1.0, 1.0), f64::INFINITY);
}
#[test]
fn each_compiler_fuses_its_own_languages() {
let (a, b, c) = (0.1, 10.0, -1.0);
let once = 2f64.powi(-54);
assert_eq!(Contraction::Unfused.fused(a, b, c), 0.0);
assert_eq!(Contraction::Clang.fused(a, b, c), once);
assert_eq!(Contraction::Gcc.fused(a, b, c), once);
assert_eq!(Contraction::Unfused.fused_iso_c(a, b, c), 0.0);
assert_eq!(Contraction::Clang.fused_iso_c(a, b, c), once);
assert_eq!(Contraction::Gcc.fused_iso_c(a, b, c), 0.0);
assert_eq!(fused(a, b, c), CONTRACTION.fused(a, b, c));
assert_eq!(fused_iso_c(a, b, c), CONTRACTION.fused_iso_c(a, b, c));
}
#[test]
fn either_product_of_a_difference_may_be_fused() {
let third = 1.0 / 3.0;
let gcc = Contraction::Gcc;
assert_eq!(
gcc.difference_of_products(FusedProduct::First, third, 3.0, 1.0, 1.0),
-(2f64.powi(-54))
);
assert_eq!(
gcc.difference_of_products(FusedProduct::Second, third, 3.0, 1.0, 1.0),
0.0
);
let unfused = Contraction::Unfused;
assert_eq!(
unfused.difference_of_products(FusedProduct::First, third, 3.0, 1.0, 1.0),
0.0
);
}
#[test]
fn the_platforms_compilers() {
let expected = if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
Contraction::Clang
} else if cfg!(all(target_os = "linux", target_arch = "aarch64")) {
Contraction::Gcc
} else {
Contraction::Unfused
};
assert_eq!(CONTRACTION, expected);
assert_eq!(FUSES_PRODUCTS, expected != Contraction::Unfused);
}
}