softfpu 0.3.0

32-, 64- and 80-bit floating-point arithmetic in safe Rust, including Intel x87 transcendental functions
Documentation
// The SSE operations on binary32 and binary64.

use super::{Computed, Flags, Rounded, X86, indefinite};
use crate::nan::NanPolicy;
use crate::{Double, Relation, Rounding, Single, Tininess, Unrounded, round};

/// SSE arithmetic shared by binary32 and binary64. These associated functions
/// use [`super::X86`] and report [`Flags`].
pub trait Real: Copy {
    /// The default NaN. It is negative and quiet, with a zero payload.
    const DEFAULT_NAN: Self;

    /// Tests whether the operand is a signalling NaN.
    fn is_signalling_nan(value: Self) -> bool;

    /// Returns the sign bit, including for zero and NaN.
    fn negative(value: Self) -> bool;

    /// Tests whether the operand is a NaN.
    fn is_nan(value: Self) -> bool;

    /// Tests whether the operand is denormal: a zero exponent field and a
    /// nonzero fraction.
    fn is_denormal(value: Self) -> bool;

    /// Encodes zero with the given sign.
    fn zero(negative: bool) -> Self;

    /// Adds two lanes. See [`crate::Single::add`] for the operand cases.
    /// NaNs and exceptions follow the [x86 rules](super).
    fn add(a: Self, b: Self, rounding: Rounding) -> Computed<Self>;

    /// Subtracts the second lane from the first. See [`crate::Single::subtract`] for the operand cases.
    /// NaNs and exceptions follow the [x86 rules](super).
    fn subtract(a: Self, b: Self, rounding: Rounding) -> Computed<Self>;

    /// Multiplies two lanes. See [`crate::Single::multiply`] for the operand cases.
    /// NaNs and exceptions follow the [x86 rules](super).
    fn multiply(a: Self, b: Self) -> Computed<Self>;

    /// Divides the first lane by the second. See [`crate::Single::divide`] for the operand cases.
    /// NaNs and exceptions follow the [x86 rules](super).
    fn divide(a: Self, b: Self) -> Computed<Self>;

    /// Takes the square root of a lane. See [`crate::Single::sqrt`] for the operand cases.
    /// NaNs and exceptions follow the [x86 rules](super).
    fn sqrt(a: Self) -> Computed<Self>;

    /// Compares two lanes. See [`crate::Single::compare`] for the operand cases.
    /// NaNs and exceptions follow the [x86 rules](super).
    fn compare(a: Self, b: Self, signalling: bool) -> (Relation, Flags);

    /// Converts a signed 32-bit integer. Binary64 finishes exactly. A nonzero
    /// binary32 result goes through the destination rounding.
    fn from_i32(value: i32) -> Computed<Self>;

    /// Rounds to the destination integer width. NaN, infinity or an out-of-range
    /// rounded value returns the most negative integer with invalid alone.
    fn to_i32(value: Self, rounding: Rounding) -> Rounded<i32>;

    /// Converts a signed 64-bit integer. A nonzero result goes through the
    /// destination rounding.
    fn from_i64(value: i64) -> Computed<Self>;

    /// Rounds to a signed 64-bit integer. NaN, infinity or an out-of-range
    /// rounded value returns the most negative integer with invalid alone.
    fn to_i64(value: Self, rounding: Rounding) -> Rounded<i64>;

    /// Applies the masked response. It rounds on the format's grid, overflows
    /// to infinity or the largest finite value, and underflows onto the
    /// denormal grid.
    fn round(value: Unrounded<Self>, rounding: Rounding) -> Rounded<Self>;

    /// Returns the flags of the unmasked response: overflow for a result above
    /// the format's range, underflow for a tiny result and inexact for inexact
    /// rounding at the format's precision with an unbounded exponent. The instruction
    /// caller suppresses the destination write.
    fn unmasked_flags(value: Unrounded<Self>, rounding: Rounding) -> Flags;
}

macro_rules! real_methods {
    (discarded: $discarded:literal, normal_exponents: $min:literal ..= $max:literal) => {
        const DEFAULT_NAN: Self = <X86 as NanPolicy<Self>>::DEFAULT;

        #[inline]
        fn is_signalling_nan(value: Self) -> bool {
            <X86 as NanPolicy<Self>>::is_signalling(value)
        }

        #[inline]
        fn negative(value: Self) -> bool {
            value.negative()
        }

        #[inline]
        fn is_nan(value: Self) -> bool {
            value.is_nan()
        }

        #[inline]
        fn is_denormal(value: Self) -> bool {
            value.is_denormal()
        }

        #[inline]
        fn zero(negative: bool) -> Self {
            Self::zero(negative)
        }

        #[inline]
        fn add(a: Self, b: Self, rounding: Rounding) -> Computed<Self> {
            a.add::<X86>(b, rounding)
        }

        #[inline]
        fn subtract(a: Self, b: Self, rounding: Rounding) -> Computed<Self> {
            a.subtract::<X86>(b, rounding)
        }

        #[inline]
        fn multiply(a: Self, b: Self) -> Computed<Self> {
            a.multiply::<X86>(b)
        }

        #[inline]
        fn divide(a: Self, b: Self) -> Computed<Self> {
            a.divide::<X86>(b)
        }

        #[inline]
        fn sqrt(a: Self) -> Computed<Self> {
            a.sqrt::<X86>()
        }

        #[inline]
        fn compare(a: Self, b: Self, signalling: bool) -> (Relation, Flags) {
            a.compare::<X86>(b, signalling)
        }

        #[inline]
        fn to_i32(value: Self, rounding: Rounding) -> Rounded<i32> {
            indefinite(value.to_i32(rounding), i32::MIN)
        }

        #[inline]
        fn from_i64(value: i64) -> Computed<Self> {
            Self::from_i64(value)
        }

        #[inline]
        fn to_i64(value: Self, rounding: Rounding) -> Rounded<i64> {
            indefinite(value.to_i64(rounding), i64::MIN)
        }

        #[inline]
        fn round(value: Unrounded<Self>, rounding: Rounding) -> Rounded<Self> {
            value.round(rounding, Tininess::AfterRounding)
        }

        #[inline]
        fn unmasked_flags(value: Unrounded<Self>, rounding: Rounding) -> Flags {
            let (negative, exponent, significand, extra) = value.parts();
            let (exponent, _, inexact, _) =
                round::unbounded(negative, exponent, significand, extra, rounding, $discarded);
            Flags::OVERFLOW.when(exponent > $max)
                | Flags::UNDERFLOW.when(exponent < $min)
                | Flags::INEXACT.when(inexact)
        }
    };
}

impl Real for Single {
    real_methods!(discarded: 40, normal_exponents: -126..=127);

    #[inline]
    fn from_i32(value: i32) -> Computed<Single> {
        Single::from_i32(value)
    }
}

impl Real for Double {
    real_methods!(discarded: 11, normal_exponents: -1022..=1023);

    // The conversion is exact, so the result is always Finished.
    #[inline]
    fn from_i32(value: i32) -> Computed<Double> {
        Computed::Finished(Double::from_i32(value), Flags::NONE)
    }
}