1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use az::Az;
use std::{fmt, ops};

/// Type to represent the number of bits.
type NBits = u16;

/// Types suitable for bit representations.
pub trait Bits: Copy + Eq + PartialEq
where
    Self: fmt::Binary,
    Self: ops::Shl<NBits, Output = Self>,
    Self: ops::Shr<NBits, Output = Self>,
    Self: ops::BitAnd<Output = Self>,
{
    /// The number of bits.
    const BITS: NBits;

    /// The value `0`.
    const ZERO: Self;

    /// The value `1`.
    const ONE: Self;

    /// Test if the value is zero.
    #[inline]
    fn is_zero(self) -> bool {
        self == Self::ZERO
    }
}

macro_rules! impl_Bits {
    ($($T:ty),*) => {$(
        impl Bits for $T {
            const BITS: NBits = <$T>::BITS as NBits;
            const ZERO: Self = 0;
            const ONE:Self=1;
        }
    )*};
}

impl_Bits!(/*u8, u16,*/ u32, u64 /*, u128*/);

/// Conversions to and from bit representations.
pub trait AsBits {
    /// Type for the bit representation of `Self`.
    type Bits: Bits;
    /// Convert to the underlying bit representation.
    fn to_bits(self) -> Self::Bits;
    /// Convert from the underlying bit representation.
    fn from_bits(b: Self::Bits) -> Self;
}

macro_rules! impl_AsBits {
    ($F:ty, $Bits:ty) => {
        impl AsBits for $F {
            type Bits = $Bits;

            #[inline]
            fn to_bits(self) -> Self::Bits {
                self.to_bits()
            }

            #[inline]
            fn from_bits(b: Self::Bits) -> Self {
                Self::from_bits(b)
            }
        }
    };
}

impl_AsBits!(f32, u32);
impl_AsBits!(f64, u64);

/// Types that represent a floating point number.
pub trait Float: Copy + PartialEq + PartialOrd + AsBits
where
    Self::Bits: az::Cast<Self::BiasedExponent>,
    Self::BiasedExponent: az::Cast<Self::Exponent>,
    Self::Bits: az::Cast<Self::Significand>,
    Self::Exponent: ops::Sub<Output = Self::Exponent>,
    Self::Significand: ops::Add<Output = Self::Significand>,
{
    /// Type for the the biased exponent.
    type BiasedExponent;
    /// Type for the true (unbiased) signed exponent.
    type Exponent;
    /// Type for the significand, both with and without implicit bit.
    type Significand;

    /// The number of bits (defaults to [`<Self as AsBits>::Bits::BITS`](Bits::BITS)).
    const BITS: NBits = Self::Bits::BITS as NBits;
    /// The number of bits of the exponent.
    const EXP_BITS: NBits;
    /// The number of bits of the stored significand (not counting the implicit bit).
    const SIGNIF_BITS: NBits;

    /// The mask for the sign bit.
    const SIGN_MASK: Self::Bits;
    /// The mask for the biased exponent bits.
    const EXP_MASK: Self::Bits;
    /// The mask for the stored significand bits (no implicit bit).
    const SIGNIF_MASK: Self::Bits;

    /// The exponent bias.
    const EXP_BIAS: Self::Exponent;

    #[inline]
    fn sign_bit(self) -> bool {
        !(self.to_bits() & Self::SIGN_MASK).is_zero()
    }

    #[inline]
    fn biased_exponent(self) -> Self::BiasedExponent {
        ((self.to_bits() & Self::EXP_MASK) >> Self::SIGNIF_BITS).az()
    }

    #[inline]
    fn exponent(self) -> Self::Exponent {
        self.biased_exponent().az() - Self::EXP_BIAS
    }

    #[inline]
    fn stored_significand(self) -> Self::Significand {
        (self.to_bits() & Self::SIGNIF_MASK).az()
    }

    #[inline]
    fn significand(self) -> Self::Significand {
        // Fixme
        self.stored_significand() + (Self::Bits::ONE << Self::SIGNIF_BITS).az()
    }

    #[inline]
    fn fraction(self) -> Self::Significand {
        self.stored_significand()
    }

    #[inline]
    fn raw_components(self) -> (bool, Self::BiasedExponent, Self::Significand) {
        (
            self.sign_bit(),
            self.biased_exponent(),
            self.stored_significand(),
        )
    }

    #[inline]
    fn components(self) -> (bool, Self::Exponent, Self::Significand) {
        (self.sign_bit(), self.exponent(), self.significand())
    }

    fn explain(self)
    where
        Self: fmt::Debug,
        Self::BiasedExponent: fmt::Debug,
        Self::Exponent: fmt::Debug,
        Self::Significand: fmt::Debug,
    {
        println!("value = {:?}", self);
        println!(
            "bits: {:0width$b}",
            self.to_bits(),
            width = Self::Bits::BITS as usize
        );
        println!(
            "      ±{:^<e$}{:_<f$}",
            "",
            "",
            e = Self::EXP_BITS as usize,
            f = Self::SIGNIF_BITS as usize
        );
        println!("sign: {}", if self.sign_bit() { "-" } else { "+" });
        println!(
            "exponent = {:?} - {:?} = {:?}",
            self.biased_exponent(),
            Self::EXP_BIAS,
            self.exponent()
        );
        println!(
            "significand = 2^{:?} + {:?} = {:?}",
            Self::SIGNIF_BITS,
            self.stored_significand(),
            self.significand()
        );
    }
}

macro_rules! impl_Float {
    ($F:ty, $BiasedExponent:ty, $Exponent:ty, $Significand:ty; $exp_bits:expr, $signif_bits:expr) => {
        impl Float for $F {
            type BiasedExponent = $BiasedExponent;
            type Exponent = $Exponent;
            type Significand = $Significand;

            const EXP_BITS: NBits = $exp_bits;
            const SIGNIF_BITS: NBits = $signif_bits;

            // The following could be default values defined in the trait Float
            // if `<<` and `-` were constant functions on `Self::Bits`.

            const SIGN_MASK: Self::Bits = 1 << (Self::EXP_BITS + Self::SIGNIF_BITS);
            const EXP_MASK: Self::Bits = (1 << Self::EXP_BITS) - 1 << Self::SIGNIF_BITS;
            const SIGNIF_MASK: Self::Bits = (1 << Self::SIGNIF_BITS) - 1;

            const EXP_BIAS: Self::Exponent = ((1 << Self::EXP_BITS - 1) - 1);
        }
    };
}

impl_Float!(f32, u8, i16, u32; 8, 23);
impl_Float!(f64, u16, i16, u64; 11, 52);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn f32_components() {
        let x = 0.032_f32;
        assert_eq!(x.components(), (false, -5, 8589935));
    }

    #[test]
    fn f64_components() {
        let x = 0.032_f64;
        assert_eq!(x.components(), (false, -5, 4611686018427388));
    }
}