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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! Floating point operations

pub(crate) mod abs;
pub(crate) mod acos;
pub(crate) mod asin;
pub(crate) mod atan;
pub(crate) mod atan2;
pub(crate) mod ceil;
pub(crate) mod copysign;
pub(crate) mod cos;
pub(crate) mod div_euclid;
pub(crate) mod exp;
pub(crate) mod floor;
pub(crate) mod fract;
pub(crate) mod hypot;
pub(crate) mod inv;
pub(crate) mod invsqrt;
pub(crate) mod ln;
pub(crate) mod log;
pub(crate) mod log10;
pub(crate) mod log2;
pub(crate) mod mul_add;
pub(crate) mod powf;
pub(crate) mod powi;
pub(crate) mod recip;
pub(crate) mod rem_euclid;
pub(crate) mod round;
pub(crate) mod signum;
pub(crate) mod sin;
pub(crate) mod sin_cos;
pub(crate) mod sqrt;
pub(crate) mod tan;
pub(crate) mod trunc;

use core::{
    cmp::Ordering,
    fmt::{self, Display, LowerExp, UpperExp},
    iter::{Product, Sum},
    num::ParseFloatError,
    ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign},
    str::FromStr,
};

#[cfg(feature = "num-traits")]
use num_traits::{Inv, Num, One, Zero};

/// Sign mask.
pub(crate) const SIGN_MASK: u32 = 0b1000_0000_0000_0000_0000_0000_0000_0000;

/// Exponent mask.
pub(crate) const EXPONENT_MASK: u32 = 0b0111_1111_1000_0000_0000_0000_0000_0000;

/// Mantissa mask.
pub(crate) const MANTISSA_MASK: u32 = 0b0000_0000_0111_1111_1111_1111_1111_1111;

/// Exponent mask.
pub(crate) const EXPONENT_BIAS: u32 = 127;

/// Mantissa bits.
///
/// Note: `MANTISSA_DIGITS` is available in `core::f32`, but the actual bits taken up are 24 - 1.
pub(crate) const MANTISSA_BITS: u32 = 23;

/// 32-bit floating point wrapper which implements fast approximation-based
/// operations.
#[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd)]
pub struct F32(pub f32);

impl F32 {
    /// The value `0.0`.
    pub const ZERO: Self = Self(0.0);

    /// The value `1.0`.
    pub const ONE: Self = Self(1.0);

    /// The radix or base of the internal representation of `f32`.
    pub const RADIX: u32 = f32::RADIX;

    /// Number of significant digits in base 2.
    pub const MANTISSA_DIGITS: u32 = f32::MANTISSA_DIGITS;

    /// Approximate number of significant digits in base 10.
    pub const DIGITS: u32 = f32::DIGITS;

    /// [Machine epsilon] value for `f32`.
    ///
    /// This is the difference between `1.0` and the next larger representable number.
    ///
    /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon
    pub const EPSILON: Self = Self(f32::EPSILON);

    /// Smallest finite `f32` value.
    pub const MIN: Self = Self(f32::MIN);

    /// Smallest positive normal `f32` value.
    pub const MIN_POSITIVE: Self = Self(f32::MIN_POSITIVE);

    /// Largest finite `f32` value.
    pub const MAX: Self = Self(f32::MAX);

    /// One greater than the minimum possible normal power of 2 exponent.
    pub const MIN_EXP: i32 = f32::MIN_EXP;

    /// Maximum possible power of 2 exponent.
    pub const MAX_EXP: i32 = f32::MAX_EXP;

    /// Minimum possible normal power of 10 exponent.
    pub const MIN_10_EXP: i32 = f32::MIN_10_EXP;

    /// Maximum possible power of 10 exponent.
    pub const MAX_10_EXP: i32 = f32::MAX_10_EXP;

    /// Not a Number (NaN).
    pub const NAN: Self = Self(f32::NAN);

    /// Infinity (∞).
    pub const INFINITY: Self = Self(f32::INFINITY);

    /// Negative infinity (−∞).
    pub const NEG_INFINITY: Self = Self(f32::NEG_INFINITY);

    /// Returns `true` if this value is `NaN`.
    #[inline]
    pub fn is_nan(self) -> bool {
        self.0.is_nan()
    }

    /// Returns `true` if this value is positive infinity or negative infinity, and
    /// `false` otherwise.
    #[inline]
    pub fn is_infinite(self) -> bool {
        self.0.is_infinite()
    }

    /// Returns `true` if this number is neither infinite nor `NaN`.
    #[inline]
    pub fn is_finite(self) -> bool {
        self.0.is_finite()
    }

    /// Returns `true` if `self` has a positive sign, including `+0.0`, `NaN`s with
    /// positive sign bit and positive infinity.
    #[inline]
    pub fn is_sign_positive(self) -> bool {
        self.0.is_sign_positive()
    }

    /// Returns `true` if `self` has a negative sign, including `-0.0`, `NaN`s with
    /// negative sign bit and negative infinity.
    #[inline]
    pub fn is_sign_negative(self) -> bool {
        self.0.is_sign_negative()
    }

    /// Raw transmutation to `u32`.
    ///
    /// This is currently identical to `transmute::<f32, u32>(self)` on all platforms.
    ///
    /// See [`F32::from_bits`] for some discussion of the portability of this operation
    /// (there are almost no issues).
    #[inline]
    pub fn to_bits(self) -> u32 {
        self.0.to_bits()
    }

    /// Raw transmutation from `u32`.
    ///
    /// This is currently identical to `transmute::<u32, f32>(v)` on all platforms.
    /// It turns out this is incredibly portable, for two reasons:
    ///
    /// - Floats and Ints have the same endianness on all supported platforms.
    /// - IEEE-754 very precisely specifies the bit layout of floats.
    ///
    /// See [`f32::from_bits`] for more information.
    #[inline]
    pub fn from_bits(v: u32) -> Self {
        Self(f32::from_bits(v))
    }

    /// Extract exponent bits.
    pub(crate) fn extract_exponent_bits(self) -> u32 {
        (self.to_bits() & EXPONENT_MASK)
            .overflowing_shr(MANTISSA_BITS)
            .0
    }

    /// Extract the exponent of a float's value.
    pub(crate) fn extract_exponent_value(self) -> i32 {
        (self.extract_exponent_bits() as i32) - EXPONENT_BIAS as i32
    }

    /// Remove sign.
    pub(crate) fn without_sign(self) -> Self {
        Self::from_bits(self.to_bits() & !SIGN_MASK)
    }

    /// Set the exponent to the given value.
    pub(crate) fn set_exponent(self, exponent: i32) -> Self {
        debug_assert!(exponent <= 127 && exponent >= -128);
        let without_exponent: u32 = self.to_bits() & !EXPONENT_MASK;
        let only_exponent: u32 = ((exponent + EXPONENT_BIAS as i32) as u32)
            .overflowing_shl(MANTISSA_BITS)
            .0;

        Self::from_bits(without_exponent | only_exponent)
    }

    /// Is this floating point value equivalent to an integer?
    pub(crate) fn is_integer(&self) -> bool {
        let exponent = self.extract_exponent_value();
        let self_bits = self.to_bits();

        // if exponent is negative we shouldn't remove anything, this stops an opposite shift.
        let exponent_clamped = i32::max(exponent, 0) as u32;

        // find the part of the fraction that would be left over
        let fractional_part = (self_bits).overflowing_shl(exponent_clamped).0 & MANTISSA_MASK;

        // if fractional part contains anything, we know it *isn't* an integer.
        // if zero there will be nothing in the fractional part
        // if it is whole, there will be nothing in the fractional part
        fractional_part == 0
    }

    /// Is this floating point value even?
    fn is_even(&self) -> bool {
        // any floating point value that doesn't fit in an i32 range is even,
        // and will loose 1's digit precision at exp values of 23+
        if self.extract_exponent_value() >= 31 {
            true
        } else {
            (self.0 as i32) % 2 == 0
        }
    }
}

impl Add for F32 {
    type Output = F32;

    #[inline]
    fn add(self, rhs: F32) -> F32 {
        F32(self.0 + rhs.0)
    }
}

impl Add<f32> for F32 {
    type Output = F32;

    #[inline]
    fn add(self, rhs: f32) -> F32 {
        F32(self.0 + rhs)
    }
}

impl Add<F32> for f32 {
    type Output = F32;

    #[inline]
    fn add(self, rhs: F32) -> F32 {
        F32(self + rhs.0)
    }
}

impl AddAssign for F32 {
    #[inline]
    fn add_assign(&mut self, rhs: F32) {
        self.0 += rhs.0;
    }
}

impl AddAssign<f32> for F32 {
    #[inline]
    fn add_assign(&mut self, rhs: f32) {
        self.0 += rhs;
    }
}

impl AddAssign<F32> for f32 {
    #[inline]
    fn add_assign(&mut self, rhs: F32) {
        *self += rhs.0;
    }
}

impl Display for F32 {
    #[inline]
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "{}", self.0)
    }
}

impl Div for F32 {
    type Output = F32;

    #[inline]
    fn div(self, rhs: F32) -> F32 {
        F32(self.0 / rhs.0)
    }
}

impl Div<f32> for F32 {
    type Output = F32;

    #[inline]
    fn div(self, rhs: f32) -> F32 {
        F32(self.0 / rhs)
    }
}

impl Div<F32> for f32 {
    type Output = F32;

    #[inline]
    fn div(self, rhs: F32) -> F32 {
        F32(self / rhs.0)
    }
}

impl DivAssign for F32 {
    #[inline]
    fn div_assign(&mut self, rhs: F32) {
        self.0 /= rhs.0;
    }
}

impl DivAssign<f32> for F32 {
    #[inline]
    fn div_assign(&mut self, rhs: f32) {
        self.0 /= rhs;
    }
}

impl DivAssign<F32> for f32 {
    #[inline]
    fn div_assign(&mut self, rhs: F32) {
        *self /= rhs.0;
    }
}

impl From<f32> for F32 {
    #[inline]
    fn from(n: f32) -> F32 {
        F32(n)
    }
}

impl From<F32> for f32 {
    #[inline]
    fn from(n: F32) -> f32 {
        n.0
    }
}

impl From<i8> for F32 {
    #[inline]
    fn from(n: i8) -> F32 {
        F32(n.into())
    }
}

impl From<i16> for F32 {
    #[inline]
    fn from(n: i16) -> F32 {
        F32(n.into())
    }
}

impl From<u8> for F32 {
    #[inline]
    fn from(n: u8) -> F32 {
        F32(n.into())
    }
}

impl From<u16> for F32 {
    #[inline]
    fn from(n: u16) -> F32 {
        F32(n.into())
    }
}

impl FromStr for F32 {
    type Err = ParseFloatError;

    #[inline]
    fn from_str(src: &str) -> Result<F32, ParseFloatError> {
        f32::from_str(src).map(F32)
    }
}

impl LowerExp for F32 {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:e}", self.0)
    }
}

impl Mul for F32 {
    type Output = F32;

    #[inline]
    fn mul(self, rhs: F32) -> F32 {
        F32(self.0 * rhs.0)
    }
}

impl Mul<f32> for F32 {
    type Output = F32;

    #[inline]
    fn mul(self, rhs: f32) -> F32 {
        F32(self.0 * rhs)
    }
}

impl Mul<F32> for f32 {
    type Output = F32;

    #[inline]
    fn mul(self, rhs: F32) -> F32 {
        F32(self * rhs.0)
    }
}

impl MulAssign for F32 {
    #[inline]
    fn mul_assign(&mut self, rhs: F32) {
        self.0 *= rhs.0;
    }
}

impl MulAssign<f32> for F32 {
    #[inline]
    fn mul_assign(&mut self, rhs: f32) {
        self.0 *= rhs;
    }
}

impl MulAssign<F32> for f32 {
    #[inline]
    fn mul_assign(&mut self, rhs: F32) {
        *self *= rhs.0;
    }
}

impl Neg for F32 {
    type Output = F32;

    #[inline]
    fn neg(self) -> F32 {
        F32(-self.0)
    }
}

impl PartialEq<f32> for F32 {
    fn eq(&self, other: &f32) -> bool {
        self.0.eq(other)
    }
}

impl PartialEq<F32> for f32 {
    fn eq(&self, other: &F32) -> bool {
        self.eq(&other.0)
    }
}

impl PartialOrd<f32> for F32 {
    fn partial_cmp(&self, other: &f32) -> Option<Ordering> {
        self.0.partial_cmp(other)
    }
}

impl PartialOrd<F32> for f32 {
    fn partial_cmp(&self, other: &F32) -> Option<Ordering> {
        self.partial_cmp(&other.0)
    }
}

impl Product for F32 {
    #[inline]
    fn product<I>(iter: I) -> Self
    where
        I: Iterator<Item = F32>,
    {
        F32(f32::product(iter.map(f32::from)))
    }
}

impl Rem for F32 {
    type Output = F32;

    #[inline]
    fn rem(self, rhs: F32) -> F32 {
        F32(self.0 % rhs.0)
    }
}

impl Rem<f32> for F32 {
    type Output = F32;

    #[inline]
    fn rem(self, rhs: f32) -> F32 {
        F32(self.0 % rhs)
    }
}

impl Rem<F32> for f32 {
    type Output = F32;

    #[inline]
    fn rem(self, rhs: F32) -> F32 {
        F32(self % rhs.0)
    }
}

impl RemAssign for F32 {
    #[inline]
    fn rem_assign(&mut self, rhs: F32) {
        self.0 %= rhs.0;
    }
}

impl RemAssign<f32> for F32 {
    #[inline]
    fn rem_assign(&mut self, rhs: f32) {
        self.0 %= rhs;
    }
}

impl Sub for F32 {
    type Output = F32;

    #[inline]
    fn sub(self, rhs: F32) -> F32 {
        F32(self.0 - rhs.0)
    }
}

impl Sub<f32> for F32 {
    type Output = F32;

    #[inline]
    fn sub(self, rhs: f32) -> F32 {
        F32(self.0 - rhs)
    }
}

impl Sub<F32> for f32 {
    type Output = F32;

    #[inline]
    fn sub(self, rhs: F32) -> F32 {
        F32(self - rhs.0)
    }
}

impl SubAssign for F32 {
    #[inline]
    fn sub_assign(&mut self, rhs: F32) {
        self.0 -= rhs.0;
    }
}

impl SubAssign<f32> for F32 {
    #[inline]
    fn sub_assign(&mut self, rhs: f32) {
        self.0 -= rhs;
    }
}

impl SubAssign<F32> for f32 {
    #[inline]
    fn sub_assign(&mut self, rhs: F32) {
        *self -= rhs.0;
    }
}

impl Sum for F32 {
    #[inline]
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = F32>,
    {
        F32(f32::sum(iter.map(f32::from)))
    }
}

impl UpperExp for F32 {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:E}", self.0)
    }
}

#[cfg(feature = "num-traits")]
#[cfg_attr(docsrs, doc(cfg(feature = "num-traits")))]
impl Zero for F32 {
    fn zero() -> Self {
        Self::ZERO
    }

    fn is_zero(&self) -> bool {
        Self::ZERO == *self
    }
}

#[cfg(feature = "num-traits")]
#[cfg_attr(docsrs, doc(cfg(feature = "num-traits")))]
impl One for F32 {
    fn one() -> Self {
        Self::ONE
    }

    fn is_one(&self) -> bool {
        Self::ONE == *self
    }
}

#[cfg(feature = "num-traits")]
#[cfg_attr(docsrs, doc(cfg(feature = "num-traits")))]
impl Num for F32 {
    type FromStrRadixErr = num_traits::ParseFloatError;

    fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
        f32::from_str_radix(str, radix).map(Self)
    }
}

#[cfg(feature = "num-traits")]
#[cfg_attr(docsrs, doc(cfg(feature = "num-traits")))]
impl Inv for F32 {
    type Output = Self;

    fn inv(self) -> Self {
        self.inv()
    }
}

#[cfg(test)]
mod tests {
    #[allow(unused_imports)] // remove when we have more tests
    use super::F32;

    #[cfg(feature = "num-traits")]
    #[test]
    fn inv_trait() {
        assert_eq!(num_traits::Inv::inv(F32(2.0)), F32(0.5));
    }
}