#! @file bits.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains a real's bits, as IEEE 754 lays them out, and what they say of it: #! whether it is not a number, infinite, finite, or subnormal, and its sign. #! Also the pieces of a double's bits the functions of this module are written #! with, as the C they are ported from reads them. module std::math; #! The bits of an `f64`: its sign, then 11 bits of exponent, then 52 of #! significand. #! @param x The real. #! @returns Its bits. pub def bits_of(x: f64): u64 { var held := x; return ((&mut held) as *mut u64)[0]; } #! The bits of an `f32`: its sign, then 8 bits of exponent, then 23 of #! significand. #! @param x The real. #! @returns Its bits. pub def bits_of(x: f32): u32 { var held := x; return ((&mut held) as *mut u32)[0]; } #! The `f64` some bits are. #! @param bits The bits, as `bits_of` gives them. #! @returns The real. pub def real_of_bits(bits: u64): f64 { var held := bits; return ((&mut held) as *mut f64)[0]; } #! The `f32` some bits are. #! @param bits The bits, as `bits_of` gives them. #! @returns The real. pub def real_of_bits(bits: u32): f32 { var held := bits; return ((&mut held) as *mut f32)[0]; } #! Whether a real is not a number, which is the only value unequal to itself. #! @tparam T The type of real. #! @param x The real. #! @returns Whether it is NaN. pub def is_nan(x: T): bool where T: real { return x != x; } #! Whether a real is infinite, of either sign. #! @tparam T The type of real. #! @param x The real. #! @returns Whether it is. pub def is_infinite(x: T): bool where T: real { return (bits_of(x as f64) & 0x7FFFFFFFFFFFFFFF) == 0x7FF0000000000000; } #! Whether a real is finite: neither infinite nor NaN. #! @tparam T The type of real. #! @param x The real. #! @returns Whether it is. pub def is_finite(x: T): bool where T: real { return (bits_of(x as f64) & 0x7FF0000000000000) != 0x7FF0000000000000; } #! Whether a real is subnormal: not zero, but closer to it than the least normal #! value of its type, so it holds fewer bits of precision. #! @tparam T The type of real. #! @param x The real. #! @returns Whether it is. pub def is_subnormal(x: T): bool where T: real { if const size_of::() == 4 { val bits := bits_of(x as f32); return (bits & 0x7F800000) == 0 && (bits & 0x007FFFFF) != 0; } val bits := bits_of(x as f64); return (bits & 0x7FF0000000000000) == 0 && (bits & 0x000FFFFFFFFFFFFF) != 0; } #! Whether a real's sign bit is set, as it is for `-0` and some NaNs, which no #! comparison with zero says. #! @tparam T The type of real. #! @param x The real. #! @returns Whether it is. pub def sign_bit(x: T): bool where T: real { return (bits_of(x as f64) >> 63) != 0; } # The pieces of a double's bits. # The top 12 bits of a double: its sign and exponent. def top12(x: f64): u32 { return (bits_of(x) >> 52) as u32; } # The high 32 bits of a double: its sign, exponent, and 20 bits of significand. def high_word(x: f64): u32 { return (bits_of(x) >> 32) as u32; } # The low 32 bits of a double, of its significand. def low_word(x: f64): u32 { return (bits_of(x) & 0xFFFFFFFF) as u32; } # The double of a high and a low word. def of_words(high: u32, low: u32): f64 { return real_of_bits(((high as u64) << 32) | low as u64); } # The double with its high word replaced. def with_high_word(x: f64, high: u32): f64 { return real_of_bits(((high as u64) << 32) | (bits_of(x) & 0xFFFFFFFF)); } # The double with its low word replaced. def with_low_word(x: f64, low: u32): f64 { return real_of_bits((bits_of(x) & 0xFFFFFFFF00000000) | low as u64); } # What a function gives where its result is past what a double holds, as musl's # `__math_oflow`, `__math_uflow`, `__math_invalid`, and `__math_divzero` make # them: an infinity, a zero, NaN, or an infinity from dividing by zero, each of # the sign asked for. def overflowed(sign: u32): f64 { val y: f64 = 3.105036184601418e+231; return (sign != 0 ? -y : y) * y; } def underflowed(sign: u32): f64 { val y: f64 = 1.2882297539194267e-231; return (sign != 0 ? -y : y) * y; } def invalid(x: f64): f64 { return (x - x) / (x - x); } def divided_by_zero(sign: u32): f64 { return (sign != 0 ? -1.0 : 1.0) / 0.0; }