2016-08-18 04:51:37 +08:00
|
|
|
use core::mem;
|
2017-09-14 07:57:52 +08:00
|
|
|
use core::ops;
|
2016-08-18 04:51:37 +08:00
|
|
|
|
2017-09-14 04:08:15 +08:00
|
|
|
use super::int::Int;
|
|
|
|
|
2016-12-06 12:16:19 +08:00
|
|
|
pub mod conv;
|
2017-12-28 11:41:14 +08:00
|
|
|
pub mod cmp;
|
2016-08-18 04:51:37 +08:00
|
|
|
pub mod add;
|
2016-10-01 06:15:44 +08:00
|
|
|
pub mod pow;
|
2017-02-08 23:10:40 +08:00
|
|
|
pub mod sub;
|
2017-11-09 06:36:34 +08:00
|
|
|
pub mod mul;
|
2017-11-13 03:40:17 +08:00
|
|
|
pub mod div;
|
2016-08-18 04:51:37 +08:00
|
|
|
|
|
|
|
/// Trait for some basic operations on floats
|
2017-09-14 07:57:52 +08:00
|
|
|
pub trait Float:
|
|
|
|
Copy +
|
|
|
|
PartialEq +
|
|
|
|
PartialOrd +
|
|
|
|
ops::AddAssign +
|
|
|
|
ops::MulAssign +
|
|
|
|
ops::Add<Output = Self> +
|
|
|
|
ops::Sub<Output = Self> +
|
|
|
|
ops::Div<Output = Self> +
|
|
|
|
ops::Rem<Output = Self> +
|
|
|
|
{
|
2016-08-18 04:51:37 +08:00
|
|
|
/// A uint of the same with as the float
|
2017-09-14 04:08:15 +08:00
|
|
|
type Int: Int;
|
2016-09-27 13:22:10 +08:00
|
|
|
|
2017-09-14 07:57:52 +08:00
|
|
|
const ZERO: Self;
|
|
|
|
const ONE: Self;
|
|
|
|
|
2017-09-14 04:08:15 +08:00
|
|
|
/// The bitwidth of the float type
|
|
|
|
const BITS: u32;
|
2016-08-18 04:51:37 +08:00
|
|
|
|
2017-09-14 04:08:15 +08:00
|
|
|
/// The bitwidth of the significand
|
|
|
|
const SIGNIFICAND_BITS: u32;
|
2016-08-18 04:51:37 +08:00
|
|
|
|
2017-09-14 04:08:15 +08:00
|
|
|
/// The bitwidth of the exponent
|
|
|
|
const EXPONENT_BITS: u32 = Self::BITS - Self::SIGNIFICAND_BITS - 1;
|
2016-12-06 12:16:19 +08:00
|
|
|
|
2017-09-14 04:08:15 +08:00
|
|
|
/// The maximum value of the exponent
|
|
|
|
const EXPONENT_MAX: u32 = (1 << Self::EXPONENT_BITS) - 1;
|
|
|
|
|
|
|
|
/// The exponent bias value
|
|
|
|
const EXPONENT_BIAS: u32 = Self::EXPONENT_MAX >> 1;
|
2016-10-04 00:58:16 +08:00
|
|
|
|
2017-09-14 04:08:15 +08:00
|
|
|
/// A mask for the sign bit
|
|
|
|
const SIGN_MASK: Self::Int;
|
2016-10-04 00:58:16 +08:00
|
|
|
|
2017-09-14 04:08:15 +08:00
|
|
|
/// A mask for the significand
|
|
|
|
const SIGNIFICAND_MASK: Self::Int;
|
2016-10-04 00:58:16 +08:00
|
|
|
|
2017-09-14 04:08:15 +08:00
|
|
|
// The implicit bit of the float format
|
|
|
|
const IMPLICIT_BIT: Self::Int;
|
2017-02-05 17:57:36 +08:00
|
|
|
|
2017-09-14 04:08:15 +08:00
|
|
|
/// A mask for the exponent
|
|
|
|
const EXPONENT_MASK: Self::Int;
|
2016-10-04 00:58:16 +08:00
|
|
|
|
2016-09-30 09:48:33 +08:00
|
|
|
/// Returns `self` transmuted to `Self::Int`
|
|
|
|
fn repr(self) -> Self::Int;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
/// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
|
2016-10-04 00:58:16 +08:00
|
|
|
/// represented in multiple different ways. This method returns `true` if two NaNs are
|
2016-09-30 09:48:33 +08:00
|
|
|
/// compared.
|
|
|
|
fn eq_repr(self, rhs: Self) -> bool;
|
|
|
|
|
|
|
|
/// Returns a `Self::Int` transmuted back to `Self`
|
|
|
|
fn from_repr(a: Self::Int) -> Self;
|
|
|
|
|
2016-10-04 00:58:16 +08:00
|
|
|
/// Constructs a `Self` from its parts. Inputs are treated as bits and shifted into position.
|
|
|
|
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self;
|
|
|
|
|
2016-08-18 04:51:37 +08:00
|
|
|
/// Returns (normalized exponent, normalized significand)
|
|
|
|
fn normalize(significand: Self::Int) -> (i32, Self::Int);
|
|
|
|
}
|
|
|
|
|
2016-10-04 00:58:16 +08:00
|
|
|
// FIXME: Some of this can be removed if RFC Issue #1424 is resolved
|
|
|
|
// https://github.com/rust-lang/rfcs/issues/1424
|
2017-09-14 04:44:56 +08:00
|
|
|
macro_rules! float_impl {
|
|
|
|
($ty:ident, $ity:ident, $bits:expr, $significand_bits:expr) => {
|
|
|
|
impl Float for $ty {
|
|
|
|
type Int = $ity;
|
2017-09-14 07:57:52 +08:00
|
|
|
const ZERO: Self = 0.0;
|
|
|
|
const ONE: Self = 1.0;
|
|
|
|
|
2017-09-14 04:44:56 +08:00
|
|
|
const BITS: u32 = $bits;
|
|
|
|
const SIGNIFICAND_BITS: u32 = $significand_bits;
|
|
|
|
|
|
|
|
const SIGN_MASK: Self::Int = 1 << (Self::BITS - 1);
|
|
|
|
const SIGNIFICAND_MASK: Self::Int = (1 << Self::SIGNIFICAND_BITS) - 1;
|
|
|
|
const IMPLICIT_BIT: Self::Int = 1 << Self::SIGNIFICAND_BITS;
|
|
|
|
const EXPONENT_MASK: Self::Int = !(Self::SIGN_MASK | Self::SIGNIFICAND_MASK);
|
|
|
|
|
|
|
|
fn repr(self) -> Self::Int {
|
|
|
|
unsafe { mem::transmute(self) }
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
fn eq_repr(self, rhs: Self) -> bool {
|
|
|
|
if self.is_nan() && rhs.is_nan() {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
self.repr() == rhs.repr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn from_repr(a: Self::Int) -> Self {
|
|
|
|
unsafe { mem::transmute(a) }
|
|
|
|
}
|
|
|
|
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
|
|
|
|
Self::from_repr(((sign as Self::Int) << (Self::BITS - 1)) |
|
|
|
|
((exponent << Self::SIGNIFICAND_BITS) & Self::EXPONENT_MASK) |
|
|
|
|
(significand & Self::SIGNIFICAND_MASK))
|
|
|
|
}
|
|
|
|
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
|
|
|
|
let shift = significand.leading_zeros()
|
|
|
|
.wrapping_sub((Self::Int::ONE << Self::SIGNIFICAND_BITS).leading_zeros());
|
|
|
|
(1i32.wrapping_sub(shift as i32), significand << shift as Self::Int)
|
|
|
|
}
|
2016-08-22 00:24:58 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-18 04:51:37 +08:00
|
|
|
}
|
2017-09-14 04:44:56 +08:00
|
|
|
|
|
|
|
float_impl!(f32, u32, 32, 23);
|
|
|
|
float_impl!(f64, u64, 64, 52);
|