amplify_num/
posit.rs

1// Rust language amplification library providing multiple generic trait
2// implementations, type wrappers, derive macros and other language enhancements
3//
4// Written in 2022 by
5//     Yudai Kiyofuji <own7000hr@gmail.com>
6//
7// To the extent possible under law, the author(s) have dedicated all
8// copyright and related and neighboring rights to this software to
9// the public domain worldwide. This software is distributed without
10// any warranty.
11//
12// You should have received a copy of the MIT License
13// along with this software.
14// If not, see <https://opensource.org/licenses/MIT>.
15
16use crate::error::PositDecodeError;
17use crate::{u1024, u256, u512};
18
19macro_rules! construct_posit {
20    (
21        $name:ident,
22        $bits:expr,
23        $es:expr,
24        $internal:ident,
25        $zeros:expr,
26        $ones:expr,
27        $nar:expr,
28        $guard:ident,
29        $guard_zero:expr,
30        $guard_max:expr,
31        $to:ident,
32        $into:ident
33    ) => {
34        #[derive(Copy, Clone, PartialEq, Eq, Hash, Default)]
35        pub struct $name($internal);
36
37        impl $name {
38            pub const ZERO: $name = $name($zeros);
39            pub const NAR: $name = $name($nar);
40
41            #[inline]
42            #[allow(clippy::wrong_self_convention)]
43            #[deprecated(since = "1.5.3", note = "use `into`")]
44            pub const fn $to(&self) -> $internal { self.0 }
45
46            #[inline]
47            pub const fn $into(self) -> $internal { self.0 }
48
49            #[inline]
50            pub fn is_nar(&self) -> bool { self == &Self::NAR }
51
52            #[inline]
53            pub fn is_zero(&self) -> bool { self.0 == $zeros }
54
55            #[inline]
56            pub fn is_negative(&self) -> bool {
57                !self.is_nar() && (self.0 & Self::NAR.0 == Self::NAR.0)
58            }
59
60            #[inline]
61            pub fn is_positive(&self) -> bool {
62                !self.is_nar() && !self.is_negative() && !self.is_zero()
63            }
64
65            #[inline]
66            pub fn abs(self) -> Self {
67                match self.is_negative() {
68                    true => -self,
69                    false => self,
70                }
71            }
72
73            fn regime<T: Into<i32>>(exp: T) -> (i16, $internal) {
74                let exp = exp.into();
75                let regime = exp >> $es;
76                (regime as i16, $internal::from((exp - (regime << $es)) as u8))
77            }
78
79            fn exp(regime: i16, exp: $internal) -> i32 {
80                ((regime as i32) << $es) + exp.to_le_bytes()[0] as i32
81            }
82
83            pub fn from_bits(bits: $internal) -> Self { Self(bits) }
84
85            pub fn decode(&self) -> Result<(bool, i16, $internal, $internal), PositDecodeError> {
86                if self.is_zero() {
87                    return Err(PositDecodeError::Zero);
88                }
89                if self.is_nar() {
90                    return Err(PositDecodeError::NaR);
91                }
92                let sign = self.is_negative();
93                let input = self.abs().0 << 1;
94                let (regime, input) = match ((!input).leading_zeros(), input.leading_zeros()) {
95                    (0, zeros) => {
96                        (-(zeros as i16), input.checked_shl(zeros as u32 + 1).unwrap_or($zeros))
97                    }
98                    (ones, _) => {
99                        ((ones - 1) as i16, input.checked_shl(ones as u32 + 1).unwrap_or($zeros))
100                    }
101                };
102                let exp = input.checked_shr($bits - $es).unwrap_or($zeros);
103                let mantissa = input.checked_shl($es).unwrap_or($zeros);
104                Ok((sign, regime, exp, mantissa))
105            }
106
107            pub fn encode(sign: bool, regime: i16, exp: $internal, mantissa: $internal) -> Self {
108                Self::_encode(sign, regime, $guard::from(exp), $guard::from(mantissa) << $bits)
109            }
110
111            fn _encode(sign: bool, regime: i16, exp: $guard, mantissa: $guard) -> Self {
112                let shl = |x: $guard, shift| x.checked_shl(shift).unwrap_or($guard_zero);
113                let shr = |x: $guard, shift| x.checked_shr(shift).unwrap_or($guard_zero);
114                let mut res = $guard_zero;
115                let len: u32 = (regime.unsigned_abs() + (!regime.is_negative() as u16)).into();
116                let regime_mask = match regime.is_negative() {
117                    true => shr(!($guard_max >> 1), len + 1),
118                    false => ($guard_max ^ shr($guard_max, len)) >> 1,
119                };
120                let mantissa_mask = shr(mantissa, len + $es + 2);
121                let exp_mask = shl(exp, $bits * 2 - len - 2 - $es);
122                res |= regime_mask | exp_mask | mantissa_mask;
123                let (mut high, low) = {
124                    let mut h = [0u8; $bits / 8];
125                    let mut l = [0u8; $bits / 8];
126                    let bytes = res.to_le_bytes();
127                    for i in 0..($bits / 8) {
128                        h[i] = bytes[$bits / 8 + i]
129                    }
130                    for i in 0..($bits / 8) {
131                        l[i] = bytes[i]
132                    }
133                    ($internal::from_le_bytes(h), $internal::from_le_bytes(l))
134                };
135                match (high == ($ones >> 1), low.cmp(&$nar)) {
136                    (true, _) | (_, ::core::cmp::Ordering::Less) => (),
137                    (_, ::core::cmp::Ordering::Greater) => high += !($ones << 1),
138                    (_, ::core::cmp::Ordering::Equal) => high += (high & !($ones << 1)),
139                };
140                Self(if sign { high.wrapping_neg() } else { high })
141            }
142        }
143
144        impl PartialOrd for $name {
145            #[inline]
146            fn partial_cmp(&self, other: &$name) -> Option<::core::cmp::Ordering> {
147                Some(self.cmp(&other))
148            }
149        }
150
151        impl Ord for $name {
152            #[inline]
153            fn cmp(&self, other: &$name) -> ::core::cmp::Ordering {
154                match (self.is_nar(), other.is_nar()) {
155                    (true, true) => return ::core::cmp::Ordering::Equal,
156                    (true, false) => return ::core::cmp::Ordering::Less,
157                    (false, true) => return ::core::cmp::Ordering::Greater,
158                    _ => (),
159                }
160                match (self.is_negative(), other.is_negative()) {
161                    (false, true) => ::core::cmp::Ordering::Greater,
162                    (true, false) => ::core::cmp::Ordering::Less,
163                    _ => self.0.cmp(&other.0),
164                }
165            }
166        }
167
168        impl ::core::ops::Neg for $name {
169            type Output = Self;
170            fn neg(self) -> Self::Output { Self(self.0.wrapping_neg()) }
171        }
172
173        impl<T> ::core::ops::Add<T> for $name
174        where T: Into<$name>
175        {
176            type Output = $name;
177            fn add(self, other: T) -> $name {
178                let other = other.into();
179                let (lhs, rhs, sign) = {
180                    let (l, r) = (self.abs(), other.abs());
181                    match (l > r, self.is_negative(), other.is_negative()) {
182                        (true, true, true) => (l, r, true),
183                        (true, true, false) => (l, r, true),
184                        (true, false, true) => (l, r, false),
185                        (true, false, false) => (l, r, false),
186                        (false, true, true) => (r, l, true),
187                        (false, false, true) => (r, l, true),
188                        (false, true, false) => (r, l, false),
189                        (false, false, false) => (r, l, false),
190                    }
191                };
192                let (lhs, rhs) = match (lhs.decode(), rhs.decode()) {
193                    (Err(PositDecodeError::NaR), _) | (_, Err(PositDecodeError::NaR)) => {
194                        return Self::NAR;
195                    }
196                    (Err(PositDecodeError::Zero), _) => return (if sign { -rhs } else { rhs }),
197                    (_, Err(PositDecodeError::Zero)) => return (if sign { -lhs } else { lhs }),
198                    (Ok(l), Ok(r)) => (l, r),
199                };
200                let is_add = self.is_negative() == other.is_negative();
201                if !is_add && lhs == rhs {
202                    return Self::ZERO;
203                }
204                let exp_lhs = Self::exp(lhs.1, lhs.2);
205                let exp_rhs = Self::exp(rhs.1, rhs.2);
206                let shift = (exp_lhs - exp_rhs) as u32;
207                let mantissa_lhs = ($guard::from(lhs.3) << ($bits - 2)) | (!($guard_max >> 1) >> 1);
208                let mantissa_rhs = (($guard::from(rhs.3) << ($bits - 2)) |
209                    (!($guard_max >> 1) >> 1))
210                    .checked_shr(shift)
211                    .unwrap_or($guard_zero);
212                let mantissa = match self.is_negative() == other.is_negative() {
213                    true => mantissa_lhs + mantissa_rhs,
214                    false => mantissa_lhs - mantissa_rhs,
215                };
216                let leading_zeros = mantissa.leading_zeros();
217                let scaling_factor = 1 - leading_zeros as i32;
218                let mantissa = mantissa
219                    .checked_shl(leading_zeros as u32 + 1)
220                    .unwrap_or($guard_zero);
221                let (regime, exp) = Self::regime(exp_lhs + scaling_factor);
222                Self::_encode(sign, regime, exp.into(), mantissa)
223            }
224        }
225
226        impl<T> ::core::ops::Sub<T> for $name
227        where T: Into<$name>
228        {
229            type Output = $name;
230            fn sub(self, other: T) -> $name { self + (-(other.into())) }
231        }
232
233        impl<T> ::core::ops::Mul<T> for $name
234        where T: Into<$name>
235        {
236            type Output = $name;
237            fn mul(self, other: T) -> $name {
238                let other = other.into();
239                let sign = self.is_negative() != other.is_negative();
240                let (lhs, rhs) = match (self.decode(), other.decode()) {
241                    (Err(PositDecodeError::NaR), _) | (_, Err(PositDecodeError::NaR)) => {
242                        return Self::NAR;
243                    }
244                    (Err(PositDecodeError::Zero), _) | (_, Err(PositDecodeError::Zero)) => {
245                        return Self::ZERO;
246                    }
247                    (Ok(l), Ok(r)) => (l, r),
248                };
249                let exp_lhs = Self::exp(lhs.1, lhs.2);
250                let exp_rhs = Self::exp(rhs.1, rhs.2);
251                let mantissa_lhs = $guard::from((lhs.3 >> 2) | (Self::NAR.0 >> 1));
252                let mantissa_rhs = $guard::from((rhs.3 >> 2) | (Self::NAR.0 >> 1));
253                let mut mantissa = mantissa_lhs * mantissa_rhs;
254                let shift = mantissa.leading_zeros();
255                let scaling_factor = 3 - shift as i32;
256                mantissa <<= shift as usize;
257                mantissa <<= 1;
258                let (regime, exp) = Self::regime(exp_lhs + exp_rhs + scaling_factor);
259                Self::_encode(sign, regime, exp.into(), mantissa)
260            }
261        }
262
263        impl<T> ::core::ops::Div<T> for $name
264        where T: Into<$name>
265        {
266            type Output = $name;
267            fn div(self, other: T) -> $name {
268                let other = other.into();
269                let sign = self.is_negative() != other.is_negative();
270                let (lhs, rhs) = match (self.decode(), other.decode()) {
271                    (Err(PositDecodeError::NaR), _) | (_, Err(PositDecodeError::NaR)) => {
272                        return Self::NAR;
273                    }
274                    (_, Err(PositDecodeError::Zero)) => return Self::NAR,
275                    (Err(PositDecodeError::Zero), _) => return Self::ZERO,
276                    (Ok(l), Ok(r)) => (l, r),
277                };
278                let exp_lhs = Self::exp(lhs.1, lhs.2);
279                let exp_rhs = Self::exp(rhs.1, rhs.2);
280                let mut mantissa_lhs = $guard::from((lhs.3 >> 1) | Self::NAR.0);
281                let mut mantissa_rhs = $guard::from((rhs.3 >> 1) | Self::NAR.0);
282                let cut_lhs = mantissa_lhs.leading_zeros();
283                let cut_rhs = mantissa_rhs.trailing_zeros();
284                mantissa_lhs <<= cut_lhs as usize;
285                mantissa_rhs >>= cut_rhs as usize;
286                let mantissa = mantissa_lhs / mantissa_rhs;
287                let rem = mantissa_lhs % mantissa_rhs;
288                let cut_rem = rem.leading_zeros();
289                let rem_mantissa = match rem != $guard_zero {
290                    true => (rem << cut_rem as usize) / mantissa_rhs,
291                    false => rem,
292                };
293                let shift = mantissa.leading_zeros();
294                let scaling_factor =
295                    $bits as i32 * 2 - 1 - shift as i32 - (cut_lhs + cut_rhs) as i32;
296                let mantissa = mantissa
297                    .checked_shl(shift as u32 + 1)
298                    .unwrap_or($guard_zero);
299                let rem_mantissa = rem_mantissa
300                    .checked_shr(cut_rem - shift - 1)
301                    .unwrap_or($guard_zero);
302                let (regime, exp) = Self::regime(exp_lhs - exp_rhs + scaling_factor);
303                Self::_encode(sign, regime, exp.into(), mantissa | rem_mantissa)
304            }
305        }
306
307        impl ::core::ops::AddAssign for $name {
308            #[inline]
309            fn add_assign(&mut self, other: Self) { *self = *self + other }
310        }
311
312        impl ::core::ops::SubAssign for $name {
313            #[inline]
314            fn sub_assign(&mut self, other: Self) { *self = *self - other }
315        }
316
317        impl ::core::ops::MulAssign for $name {
318            #[inline]
319            fn mul_assign(&mut self, other: Self) { *self = *self * other }
320        }
321
322        impl ::core::ops::DivAssign for $name {
323            #[inline]
324            fn div_assign(&mut self, other: Self) { *self = *self / other }
325        }
326
327        impl ::core::fmt::Debug for $name {
328            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
329                let &$name(ref data) = self;
330                write!(f, "{:?}", data)?;
331                Ok(())
332            }
333        }
334
335        impl From<$name> for f32 {
336            fn from(init: $name) -> f32 {
337                let (sign, regime, exp, mantissa): (bool, i16, $internal, $internal) =
338                    match init.decode() {
339                        Err(PositDecodeError::Zero) => return 0.,
340                        Err(PositDecodeError::NaR) => return f32::NAN,
341                        Ok(v) => v,
342                    };
343                let sign = if sign { 0x80000000u32 } else { 0u32 };
344                let exp = $name::exp(regime, exp);
345                let (exp, mantissa) = match (exp > 127, exp < -149, exp < -126) {
346                    (true, _, _) => return f32::MAX,
347                    (_, true, _) => return f32::MIN,
348                    (_, _, true) => (0u32, ((mantissa >> 1) | $nar) >> (-127 - exp) as usize),
349                    _ => (((exp + 127) as u32) << 23, mantissa),
350                };
351                let mut m = [0u8; 4];
352                let _ = mantissa
353                    .to_be_bytes()
354                    .iter()
355                    .enumerate()
356                    .filter(|&(i, _)| i < 4)
357                    .map(|(i, e)| m[i] = *e)
358                    .collect::<()>();
359                f32::from_bits(sign | exp | u32::from_be_bytes(m) >> 9)
360            }
361        }
362
363        impl From<f32> for $name {
364            fn from(init: f32) -> $name {
365                if init == 0. {
366                    return Self::ZERO;
367                }
368                if init.is_infinite() || init.is_nan() {
369                    return Self::NAR;
370                }
371                let bits = (if init.is_sign_negative() { -init } else { init }).to_bits();
372                let mut mantissa = [0u8; $bits * 2 / 8];
373                let _ = (bits << 9)
374                    .to_be_bytes()
375                    .iter()
376                    .enumerate()
377                    .filter(|&(i, _)| i < $bits * 2 / 8)
378                    .map(|(i, e)| mantissa[i] = *e)
379                    .collect::<()>();
380                let (mantissa, init_exp) = match init.is_normal() {
381                    true => ($guard::from_be_bytes(mantissa), (bits >> 23) as i16 - 127),
382                    false => {
383                        let m = $guard::from_be_bytes(mantissa);
384                        let shift = m.leading_zeros() + 1;
385                        (m << shift as usize, -126 - shift as i16)
386                    }
387                };
388                let (regime, exp) = Self::regime(init_exp);
389                Self::_encode(init.is_sign_negative(), regime, exp.into(), mantissa)
390            }
391        }
392
393        impl From<$name> for f64 {
394            fn from(init: $name) -> f64 {
395                let (sign, regime, exp, mantissa): (bool, i16, $internal, $internal) =
396                    match init.decode() {
397                        Err(PositDecodeError::Zero) => return 0.,
398                        Err(PositDecodeError::NaR) => return f64::NAN,
399                        Ok(v) => v,
400                    };
401                let sign = if sign { 0x80000000_00000000u64 } else { 0u64 };
402                let exp = $name::exp(regime, exp);
403                let (exp, mantissa) = match (exp > 1023, exp < -1074, exp < -1022) {
404                    (true, _, _) => return f64::MAX,
405                    (_, true, _) => return f64::MIN,
406                    (_, _, true) => (0u64, ((mantissa >> 1) | $nar) >> (-1023 - exp) as usize),
407                    _ => (((exp + 1023) as u64) << 52, mantissa),
408                };
409                let mut m = [0u8; 8];
410                let _ = mantissa
411                    .to_be_bytes()
412                    .iter()
413                    .enumerate()
414                    .filter(|&(i, _)| i < 8)
415                    .map(|(i, e)| m[i] = *e)
416                    .collect::<()>();
417                f64::from_bits(sign | exp | u64::from_be_bytes(m) >> 12)
418            }
419        }
420
421        impl From<f64> for $name {
422            fn from(init: f64) -> $name {
423                if init == 0. {
424                    return Self::ZERO;
425                }
426                if init.is_infinite() || init.is_nan() {
427                    return Self::NAR;
428                }
429                let bits = (if init.is_sign_negative() { -init } else { init }).to_bits();
430                let mut mantissa = [0u8; $bits * 2 / 8];
431                let _ = (bits << 12)
432                    .to_be_bytes()
433                    .iter()
434                    .enumerate()
435                    .filter(|&(i, _)| i < $bits * 2 / 8)
436                    .map(|(i, e)| mantissa[i] = *e)
437                    .collect::<()>();
438                let (mantissa, init_exp) = match init.is_normal() {
439                    true => ($guard::from_be_bytes(mantissa), (bits >> 52) as i16 - 1023),
440                    false => {
441                        let m = $guard::from_be_bytes(mantissa);
442                        let shift = m.leading_zeros() + 1;
443                        (m << shift as usize, -1022 - shift as i16)
444                    }
445                };
446                let (regime, exp) = Self::regime(init_exp);
447                Self::_encode(init.is_sign_negative(), regime, exp.into(), mantissa)
448            }
449        }
450    };
451}
452
453construct_posit!(Posit8, 8, 0, u8, 0, u8::MAX, 0x80, u16, 0, u16::MAX, to_u8, into_u8);
454construct_posit!(Posit16, 16, 1, u16, 0, u16::MAX, 0x8000, u32, 0, u32::MAX, to_u16, into_u16);
455construct_posit!(Posit32, 32, 2, u32, 0, u32::MAX, 0x8000_0000, u64, 0, u64::MAX, to_u32, into_u32);
456construct_posit!(
457    Posit64,
458    64,
459    3,
460    u64,
461    0,
462    u64::MAX,
463    0x8000_0000_0000_0000,
464    u128,
465    0,
466    u128::MAX,
467    to_u64,
468    into_u64
469);
470construct_posit!(
471    Posit128,
472    128,
473    4,
474    u128,
475    0,
476    u128::MAX,
477    0x8000_0000_0000_0000_0000_0000_0000_0000,
478    u256,
479    u256::ZERO,
480    u256::MAX,
481    to_u128,
482    into_u128
483);
484construct_posit!(
485    Posit256,
486    256,
487    5,
488    u256,
489    u256::ZERO,
490    u256::MAX,
491    u256::from_inner([0, 0, 0, 0x8000_0000_0000_0000]),
492    u512,
493    u512::ZERO,
494    u512::MAX,
495    to_u256,
496    into_u256
497);
498construct_posit!(
499    Posit512,
500    512,
501    6,
502    u512,
503    u512::ZERO,
504    u512::MAX,
505    u512::from_inner([0, 0, 0, 0, 0, 0, 0, 0x8000_0000_0000_0000]),
506    u1024,
507    u1024::ZERO,
508    u1024::MAX,
509    to_u512,
510    into_u512
511);
512
513#[cfg(test)]
514mod tests {
515    #![allow(unused)]
516
517    use super::*;
518
519    construct_posit!(Posit8Es1, 8, 1, u8, 0, 0xff, 0x80, u16, 0, 0xffff, to_u8, into_u8);
520
521    #[test]
522    fn posit_test() {
523        assert_eq!(Posit16::from(1.).into_u16(), 0b0100_0000_0000_0000);
524        assert_eq!(Posit16::from(1.125).into_u16(), 0b0100_0010_0000_0000);
525        assert_eq!(Posit16::from(3.25).into_u16(), 0b0101_1010_0000_0000);
526        assert_eq!(Posit16::from(4.).into_u16(), 0b0110_0000_0000_0000);
527        assert_eq!(Posit16::from(8.).into_u16(), 0b0110_1000_0000_0000);
528        assert_eq!(Posit16::from(1024.).into_u16(), 0b0111_1110_0000_0000);
529        assert_eq!(Posit16::from(-10.).into_u16(), 0b1001_0110_0000_0000);
530        assert_eq!(Posit16::from(-7. / 16.).into_u16(), 0b1101_0100_0000_0000);
531        assert_eq!(Posit16::from(-256.).into_u16(), 0b1000_0100_0000_0000);
532        assert_eq!(Posit16::from(0.).into_u16(), 0b0000_0000_0000_0000);
533        assert_eq!(Posit16::from(-0.).into_u16(), 0b0000_0000_0000_0000);
534    }
535
536    #[test]
537    fn posit_from_subnormal_test() {
538        let sub = f32::from_bits(0b0000_0000_0000_1000 << 16); //2 ^ (-130)
539        assert!(!sub.is_normal());
540        // With es = 3, regime is -17 and exp is 6. -17 * 8 + 6 = 130
541        assert_eq!(Posit64::from(sub).into_u64(), 0b0000_0000_0000_0000_0011_1000 << 40);
542        assert_eq!(f32::from(Posit64::from(sub)), sub);
543    }
544
545    #[test]
546    fn posit8es1_test() {
547        assert_eq!(Posit8Es1::from(1.).into_u8(), 0b0100_0000);
548        assert_eq!(Posit8Es1::from(1.125).into_u8(), 0b0100_0010);
549        assert_eq!(Posit8Es1::from(3.25).into_u8(), 0b0101_1010);
550        assert_eq!(Posit8Es1::from(4.).into_u8(), 0b0110_0000);
551        assert_eq!(Posit8Es1::from(8.).into_u8(), 0b0110_1000);
552        assert_eq!(Posit8Es1::from(1024.).into_u8(), 0b0111_1110);
553        assert_eq!(Posit8Es1::from(-10.).into_u8(), 0b1001_0110);
554        assert_eq!(Posit8Es1::from(-7. / 16.).into_u8(), 0b1101_0100);
555        assert_eq!(Posit8Es1::from(-256.).into_u8(), 0b1000_0100);
556    }
557
558    #[test]
559    fn posit32_test() {
560        assert_eq!(Posit32::from(1.).into_u32(), 0b0100_0000 << 24);
561    }
562
563    #[test]
564    fn posit256_test() {
565        assert_eq!(Posit256::from(1.).into_u256(), u256::from(0b0100_0000u64) << 248);
566        assert_eq!(Posit256::from(1.125).into_u256(), u256::from(0b0100_0000_0010_0000u64) << 240);
567    }
568
569    #[test]
570    fn posit8_es1_round_test() {
571        assert_eq!(Posit8Es1::from(0.9999), Posit8Es1::from(1.));
572        assert_eq!(Posit8Es1::from(73. / 64.), Posit8Es1::from(18. / 16.));
573        assert_eq!(Posit8Es1::from(74. / 64.), Posit8Es1::from(18. / 16.));
574        assert_eq!(Posit8Es1::from(75. / 64.), Posit8Es1::from(19. / 16.));
575        assert_eq!(
576            Posit8Es1::encode(true, 1, 1, 0b0111_1111u8),
577            Posit8Es1::encode(true, 1, 1, 0b1000_0000u8),
578        );
579        assert_eq!(
580            Posit8Es1::encode(true, 1, 0, 0b1111_1111u8),
581            Posit8Es1::encode(true, 1, 1, 0b0000_0000u8),
582        );
583        assert_eq!(
584            Posit8Es1::encode(true, 1, 1, 0b1111_1111u8),
585            Posit8Es1::encode(true, 2, 0, 0b0000_0000u8),
586        );
587    }
588
589    #[test]
590    fn posit256_nar_test() {
591        assert_eq!(Posit256::from(f32::INFINITY), Posit256::NAR);
592        assert_eq!(Posit256::from(f32::NEG_INFINITY), Posit256::NAR);
593        assert_eq!(Posit256::from(f32::NAN), Posit256::NAR);
594    }
595
596    #[test]
597    fn posit_neg_test() {
598        assert_eq!((-Posit256::from(1.)).into_u256(), Posit256::from(-1.).into_u256(),);
599        assert_eq!((-Posit256::from(0.)).into_u256(), Posit256::from(0.).into_u256(),);
600    }
601
602    #[test]
603    fn posit_is_nar_test() {
604        assert!(Posit256::from(f32::INFINITY).is_nar());
605        assert!(Posit256::from(f32::NEG_INFINITY).is_nar());
606        assert!(Posit256::from(f32::NAN).is_nar());
607        assert!(!(Posit256::ZERO.is_nar()));
608        assert!(!(Posit256::from(1.).is_nar()));
609    }
610
611    #[test]
612    fn posit_is_negative_test() {
613        assert!(!(Posit256::from(f32::INFINITY).is_negative()));
614        assert!(!(Posit256::from(f32::NEG_INFINITY).is_negative()));
615        assert!(!(Posit256::from(f32::NAN).is_negative()));
616        assert!(!(Posit256::from(0.)).is_negative());
617        assert!(!(Posit256::from(3.)).is_negative());
618        assert!(Posit256::from(-2.).is_negative());
619    }
620
621    #[test]
622    fn posit_is_positive_test() {
623        assert!(!(Posit256::from(f32::INFINITY).is_positive()));
624        assert!(!(Posit256::from(f32::NEG_INFINITY).is_positive()));
625        assert!(!(Posit256::from(f32::NAN).is_positive()));
626        assert!(!(Posit256::from(0.)).is_positive());
627        assert!(Posit256::from(3.).is_positive());
628        assert!(!(Posit256::from(-2.).is_positive()));
629    }
630
631    #[test]
632    fn posit_is_zero_test() {
633        assert!(!(Posit256::from(f32::INFINITY).is_zero()));
634        assert!(!(Posit256::from(f32::NEG_INFINITY).is_zero()));
635        assert!(!(Posit256::from(f32::NAN).is_zero()));
636        assert!(Posit256::from(0.).is_zero());
637        assert!(!(Posit256::from(3.).is_zero()));
638        assert!(!(Posit256::from(-2.).is_zero()));
639    }
640
641    #[test]
642    fn posit_decode_test() {
643        assert_eq!(Posit8Es1::encode(false, 1, 1, 0x80u8), Posit8Es1::from(12.));
644        assert_eq!(Ok((false, 1, 1, 0x80)), Posit8Es1::from(12.).decode());
645        assert_eq!(Posit16::encode(true, 1, 1, 0x8000u16), Posit16::from(-12.));
646        assert_eq!(Ok((true, 1, 1, 0x8000)), Posit16::from(-12.).decode());
647        assert_eq!(Ok((false, 0, 0, 0b00001000)), Posit8::from(1.03125).decode());
648    }
649
650    #[test]
651    #[allow(clippy::nonminimal_bool)]
652    fn posit_cmp_test() {
653        assert!(Posit16::from(4.) < Posit16::from(5.));
654        assert!(Posit16::from(0.) <= (Posit16::from(0.)));
655        assert!(!(Posit16::from(0.) > (Posit16::from(0.))));
656        assert!(Posit16::from(-1.) < Posit16::from(-0.9));
657        assert!(Posit16::from(-2.) < Posit16::from(3.));
658        assert!(Posit16::NAR < Posit16::from(0.));
659        assert!(Posit16::NAR < Posit16::from(3.));
660        assert!(Posit16::NAR < Posit16::from(-0.2));
661        assert!(Posit16::NAR <= (Posit16::NAR));
662        assert!(!(Posit16::NAR > (Posit16::NAR)));
663    }
664
665    #[test]
666    fn posit_add_test() {
667        assert_eq!(Posit16::from(-1.) + Posit16::from(-2.), Posit16::from(-3.));
668        assert_eq!(Posit16::from(-1.) + Posit16::from(2.25), Posit16::from(1.25));
669        assert_eq!(Posit16::from(1.) + Posit16::from(2.), Posit16::from(3.));
670        assert_eq!(Posit16::from(16.) + Posit16::from(-64.), Posit16::from(-48.));
671        assert_eq!(Posit16::from(2.125) + Posit16::from(3.5), Posit16::from(5.625));
672        assert_eq!(Posit16::from(1.3) + Posit16::from(3.0), Posit16::from(4.3));
673        assert_eq!(Posit16::from(1. / 3.) + Posit16::from(1. / 3.), Posit16::from(2. / 3.));
674        assert_eq!(Posit16::from(4.) + Posit16::from(0.), Posit16::from(4.));
675        assert_eq!(Posit16::from(0.) + Posit16::from(3.), Posit16::from(3.));
676        assert_eq!(Posit16::from(0.) + Posit16::from(0.), Posit16::from(0.));
677        assert_eq!(Posit16::NAR + Posit16::from(3.), Posit16::NAR);
678        assert_eq!(Posit16::from(0.) + Posit16::NAR, Posit16::NAR);
679        assert_eq!(Posit8::from(10.) + Posit8::from(1.0935), Posit8::from(12.));
680        assert_eq!(Posit8::from(10.) + Posit8::from(-10.), Posit8::from(0.));
681        assert_eq!(
682            Posit16::from(
683                f64::from(Posit16::from_bits(32769)) + f64::from(Posit16::from_bits(1457))
684            ),
685            Posit16::from_bits(32769)
686        );
687    }
688
689    #[test]
690    fn posit_add_assign_test() {
691        let mut x = Posit8::from(1.0);
692        x += Posit8::from(2.0);
693        assert_eq!(x, Posit8::from(3.0))
694    }
695
696    #[test]
697    fn posit_sub_assign_test() {
698        let mut x = Posit8::from(1.0);
699        x -= Posit8::from(2.0);
700        assert_eq!(x, Posit8::from(-1.0))
701    }
702
703    #[test]
704    fn posit_mul_assign_test() {
705        let mut x = Posit8::from(2.0);
706        x *= Posit8::from(3.0);
707        assert_eq!(x, Posit8::from(6.0))
708    }
709
710    #[test]
711    fn posit_div_assign_test() {
712        let mut x = Posit8::from(1.0);
713        x /= Posit8::from(2.0);
714        assert_eq!(x, Posit8::from(0.5))
715    }
716
717    #[test]
718    fn posit_sub_test() {
719        assert_eq!(Posit16::from(-1.) - Posit16::from(-2.), Posit16::from(1.));
720        assert_eq!(Posit16::from(-1.) - Posit16::from(2.25), Posit16::from(-3.25));
721        assert_eq!(Posit16::from(6.) - Posit16::from(-4.25), Posit16::from(10.25));
722        assert_eq!(Posit16::from(1.) - Posit16::from(2.), Posit16::from(-1.));
723        assert_eq!(Posit16::from(2.125) - Posit16::from(3.5), Posit16::from(-1.375));
724    }
725
726    #[test]
727    fn posit_mul_test() {
728        assert_eq!(Posit16::from(2.) * Posit16::from(3.), Posit16::from(6.));
729        assert_eq!(Posit16::from(1.25) * Posit16::from(3.5), Posit16::from(4.375));
730        assert_eq!(Posit16::from(-0.5) * Posit16::from(8.), Posit16::from(-4.));
731        assert_eq!(Posit16::from(-16.) * Posit16::from(-16.), Posit16::from(256.));
732        assert_eq!(Posit16::from(7.) * Posit16::from(0.), Posit16::from(0.));
733        assert_eq!(Posit16::NAR * Posit16::from(3.), Posit16::NAR);
734        assert_eq!(Posit16::from(0.) * Posit16::NAR, Posit16::NAR);
735    }
736
737    #[test]
738    fn posit_div_test() {
739        assert_eq!(Posit16::from(1.) / Posit16::from(2.), Posit16::from(0.5));
740        assert_eq!(Posit16::from(1.) / Posit16::from(8.), Posit16::from(0.125));
741        assert_eq!(Posit32::from(1.) / Posit32::from(8.), Posit32::from(0.125));
742        assert_eq!(Posit32::from(1.) / Posit32::from(64.), Posit32::from(1. / 64.));
743        assert_eq!(Posit8::from(1.75) / Posit8::from(1.), Posit8::from(1.75));
744        assert_eq!(Posit8::from(-3.) / Posit8::from(1.15625), Posit8::from(-2.625));
745    }
746
747    fn rand_posit8(fun: fn(Posit8, Posit8, f32, f32) -> (Posit8, f32)) {
748        use rand::Rng;
749        let mut rng = rand::thread_rng();
750        for _ in 0..100000 {
751            let x: u8 = rng.gen();
752            let y: u8 = rng.gen();
753            let (p, f) = fun(
754                Posit8::from_bits(x),
755                Posit8::from_bits(y),
756                f32::from(Posit8::from_bits(x)),
757                f32::from(Posit8::from_bits(y)),
758            );
759            assert_eq!(p, Posit8::from(f));
760        }
761    }
762
763    fn rand_posit16(fun: fn(Posit16, Posit16, f64, f64) -> (Posit16, f64)) {
764        use rand::Rng;
765        let mut rng = rand::thread_rng();
766        for _ in 0..100000 {
767            let x: u16 = rng.gen();
768            let y: u16 = rng.gen();
769            let (p, f) = fun(
770                Posit16::from_bits(x),
771                Posit16::from_bits(y),
772                f64::from(Posit16::from_bits(x)),
773                f64::from(Posit16::from_bits(y)),
774            );
775            assert_eq!(p, Posit16::from(f));
776        }
777    }
778
779    #[test]
780    fn posit8_add() { rand_posit8(|p_a, p_b, f_a, f_b| (p_a + p_b, f_a + f_b)); }
781
782    #[test]
783    fn posit8_sub() { rand_posit8(|p_a, p_b, f_a, f_b| (p_a - p_b, f_a - f_b)); }
784
785    #[test]
786    fn posit8_mul() { rand_posit8(|p_a, p_b, f_a, f_b| (p_a * p_b, f_a * f_b)); }
787
788    #[test]
789    fn posit8_div() { rand_posit8(|p_a, p_b, f_a, f_b| (p_a / p_b, f_a / f_b)); }
790
791    #[test]
792    fn posit16_add() { rand_posit16(|p_a, p_b, f_a, f_b| (p_a + p_b, f_a + f_b)); }
793
794    #[test]
795    fn posit16_sub() { rand_posit16(|p_a, p_b, f_a, f_b| (p_a - p_b, f_a - f_b)); }
796
797    #[test]
798    fn posit16_mul() { rand_posit16(|p_a, p_b, f_a, f_b| (p_a * p_b, f_a * f_b)); }
799
800    #[test]
801    fn posit16_div() { rand_posit16(|p_a, p_b, f_a, f_b| (p_a / p_b, f_a / f_b)); }
802}