use super::super::simd::next_representable::scalar::{step_u32, step_u64};
use std::ops::{Add, Div, Mul, Neg, Sub};
pub trait LaneFloat:
Copy
+ PartialOrd
+ Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
+ Neg<Output = Self>
+ Send
+ Sync
+ 'static
{
const ZERO: Self;
const ONE: Self;
const GUARD_MIN: Self;
const GUARD_MAX: Self;
const HALF_MAX: Self;
const MAX: Self;
fn of(value: f64) -> Self;
fn up(self) -> Self;
fn down(self) -> Self;
fn finite(self) -> bool;
fn magnitude(self) -> Self;
fn root(self) -> Self;
fn fused(self, b: Self, c: Self) -> Self;
fn truncated(self) -> Self;
fn positive_zero(self) -> bool;
}
impl LaneFloat for f32 {
const ZERO: Self = 0.0;
const ONE: Self = 1.0;
const GUARD_MIN: Self = f32::from_bits((127 - 40) << 23);
const GUARD_MAX: Self = f32::from_bits((127 + 40) << 23);
const HALF_MAX: Self = f32::MAX * 0.5;
const MAX: Self = f32::MAX;
#[inline(always)]
fn of(value: f64) -> Self {
value as f32
}
#[inline(always)]
fn up(self) -> Self {
f32::from_bits(step_u32(self.to_bits(), true))
}
#[inline(always)]
fn down(self) -> Self {
f32::from_bits(step_u32(self.to_bits(), false))
}
#[inline(always)]
fn finite(self) -> bool {
self.is_finite()
}
#[inline(always)]
fn magnitude(self) -> Self {
self.abs()
}
#[inline(always)]
fn root(self) -> Self {
self.sqrt()
}
#[inline(always)]
fn fused(self, b: Self, c: Self) -> Self {
self.mul_add(b, c)
}
#[inline(always)]
fn truncated(self) -> Self {
self.trunc()
}
#[inline(always)]
fn positive_zero(self) -> bool {
self.to_bits() == 0
}
}
impl LaneFloat for f64 {
const ZERO: Self = 0.0;
const ONE: Self = 1.0;
const GUARD_MIN: Self = f64::from_bits((1023 - 450) << 52);
const GUARD_MAX: Self = f64::from_bits((1023 + 450) << 52);
const HALF_MAX: Self = f64::MAX * 0.5;
const MAX: Self = f64::MAX;
#[inline(always)]
fn of(value: f64) -> Self {
value
}
#[inline(always)]
fn up(self) -> Self {
f64::from_bits(step_u64(self.to_bits(), true))
}
#[inline(always)]
fn down(self) -> Self {
f64::from_bits(step_u64(self.to_bits(), false))
}
#[inline(always)]
fn finite(self) -> bool {
self.is_finite()
}
#[inline(always)]
fn magnitude(self) -> Self {
self.abs()
}
#[inline(always)]
fn root(self) -> Self {
self.sqrt()
}
#[inline(always)]
fn fused(self, b: Self, c: Self) -> Self {
self.mul_add(b, c)
}
#[inline(always)]
fn truncated(self) -> Self {
self.trunc()
}
#[inline(always)]
fn positive_zero(self) -> bool {
self.to_bits() == 0
}
}
#[inline(always)]
pub fn positive<T: LaneFloat>(value: T) -> bool {
value > T::ZERO
}
#[inline(always)]
pub fn max<T: LaneFloat>(a: T, b: T) -> T {
if a > b { a } else { b }
}
#[inline(always)]
pub fn min<T: LaneFloat>(a: T, b: T) -> T {
if a < b { a } else { b }
}
#[inline(always)]
pub fn select<T: LaneFloat>(condition: bool, x: T, y: T) -> T {
if condition { x } else { y }
}
#[inline(always)]
pub fn two_sum<T: LaneFloat>(a: T, b: T) -> (T, T) {
let sum = a + b;
let b_virtual = sum - a;
let a_virtual = sum - b_virtual;
let b_roundoff = b - b_virtual;
let a_roundoff = a - a_virtual;
(sum, a_roundoff + b_roundoff)
}
#[inline(always)]
pub fn midpoint<T: LaneFloat>(a: T, b: T) -> T {
let half = T::of(0.5);
let small = a.magnitude() <= T::HALF_MAX && b.magnitude() <= T::HALF_MAX;
let sum = (select(small, a, T::ZERO) + select(small, b, T::ZERO)) * half;
select(small, sum, a * half + b * half)
}
#[inline(always)]
pub fn guarded<T: LaneFloat>(value: T) -> bool {
let magnitude = value.magnitude();
magnitude >= T::GUARD_MIN && magnitude <= T::GUARD_MAX
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn guard_constants_are_exact_powers_of_two() {
assert_eq!(f32::GUARD_MIN as f64, 2f64.powi(-40));
assert_eq!(f32::GUARD_MAX as f64, 2f64.powi(40));
assert_eq!(f64::GUARD_MIN, 2f64.powi(-450));
assert_eq!(f64::GUARD_MAX, 2f64.powi(450));
}
#[test]
fn neighbors_match_integer_stepping() {
assert_eq!(0.0f64.down().to_bits(), 0x8000_0000_0000_0001);
assert_eq!((-0.0f32).up().to_bits(), 1);
assert_eq!(f64::INFINITY.up(), f64::INFINITY);
assert_eq!(f32::from_bits(0x7fc0_1234).up().to_bits(), 0x7fc0_1234);
}
}