rust/fpnum/src/lib.rs
author alfadur
Sun, 14 Oct 2018 09:43:42 -0400
changeset 13917 ac1801fe51d9
parent 13911 9ae1184886db
parent 13906 2a3d119b9fe9
child 13926 fed924791e06
permissions -rw-r--r--
merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     1
use std::cmp;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     2
use std::ops;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     3
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     4
#[derive(Clone, Debug, Copy)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     5
pub struct FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     6
    is_negative: bool,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     7
    value: u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     8
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     9
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    10
impl FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    11
    fn new(numerator: i32, denominator: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    12
        FPNum::from(numerator) / denominator
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    13
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    14
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    15
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    16
    fn signum(&self) -> i8 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    17
        if self.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    18
            -1
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    19
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    20
            1
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    21
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    22
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    23
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    24
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    25
    fn is_negative(&self) -> bool {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    26
        self.is_negative
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    27
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    28
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    29
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    30
    fn is_positive(&self) -> bool {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    31
        !self.is_negative
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    32
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    33
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    34
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    35
    fn is_zero(&self) -> bool {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    36
        self.value == 0
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    37
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    38
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    39
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    40
    fn abs(&self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    41
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    42
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    43
            value: self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    44
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    45
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    46
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    47
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    48
    fn round(&self) -> i64 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    49
        if self.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    50
            -((self.value >> 32) as i64)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    51
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    52
            (self.value >> 32) as i64
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    53
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    54
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    55
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    56
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    57
    fn sqr(&self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    58
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    59
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    60
            value: ((self.value as u128).pow(2) >> 32) as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    61
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    62
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    63
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    64
    fn sqrt(&self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    65
        debug_assert!(!self.is_negative);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    66
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    67
        let mut t: u64 = 0x4000000000000000;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    68
        let mut r: u64 = 0;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    69
        let mut q = self.value;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    70
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    71
        for _ in 0..32 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    72
            let s = r + t;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    73
            r >>= 1;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    74
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    75
            if s <= q {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    76
                q -= s;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    77
                r += t;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    78
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    79
            t >>= 2;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    80
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    81
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    82
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    83
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    84
            value: r << 16,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    85
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    86
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    87
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    88
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    89
impl From<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    90
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    91
    fn from(n: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    92
        FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    93
            is_negative: n < 0,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    94
            value: (n.abs() as u64) << 32,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    95
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    96
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    97
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    98
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    99
impl From<u32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   100
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   101
    fn from(n: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   102
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   103
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   104
            value: (n as u64) << 32,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   105
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   106
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   107
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   108
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   109
impl From<FPNum> for f64 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   110
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   111
    fn from(n: FPNum) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   112
        if n.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   113
            n.value as f64 / (-0x10000000 as f64)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   114
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   115
            n.value as f64 / 0x10000000 as f64
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   116
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   117
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   118
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   119
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   120
impl PartialEq for FPNum {
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
   121
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   122
    fn eq(&self, other: &Self) -> bool {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   123
        self.value == other.value && (self.is_negative == other.is_negative || self.value == 0)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   124
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   125
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   126
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   127
impl Eq for FPNum {}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   128
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   129
impl PartialOrd for FPNum {
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
   130
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   131
    fn partial_cmp(&self, rhs: &Self) -> std::option::Option<std::cmp::Ordering> {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   132
        Some(self.cmp(rhs))
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   133
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   134
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   135
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   136
impl Ord for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   137
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   138
    fn cmp(&self, rhs: &Self) -> cmp::Ordering {
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   139
        #[inline]
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   140
        fn extend(n: &FPNum) -> i128 {
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   141
            if n.is_negative {
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   142
                -(n.value as i128)
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   143
            } else {
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   144
                n.value as i128
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   145
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   146
        }
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   147
        extend(self).cmp(&(extend(rhs)))
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   148
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   149
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   150
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   151
impl ops::Add for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   152
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   153
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   154
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   155
    fn add(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   156
        if self.is_negative == rhs.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   157
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   158
                is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   159
                value: self.value + rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   160
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   161
        } else if self.value > rhs.value {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   162
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   163
                is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   164
                value: self.value - rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   165
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   166
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   167
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   168
                is_negative: rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   169
                value: rhs.value - self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   170
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   171
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   172
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   173
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   174
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   175
impl ops::Sub for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   176
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   177
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   178
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   179
    fn sub(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   180
        if self.is_negative == rhs.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   181
            if self.value > rhs.value {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   182
                Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   183
                    is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   184
                    value: self.value - rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   185
                }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   186
            } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   187
                Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   188
                    is_negative: !rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   189
                    value: rhs.value - self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   190
                }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   191
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   192
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   193
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   194
                is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   195
                value: self.value + rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   196
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   197
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   198
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   199
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   200
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   201
impl ops::Neg for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   202
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   203
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   204
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   205
    fn neg(self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   206
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   207
            is_negative: !self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   208
            value: self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   209
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   210
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   211
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   212
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   213
impl ops::Mul for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   214
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   215
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   216
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   217
    fn mul(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   218
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   219
            is_negative: self.is_negative ^ rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   220
            value: ((self.value as u128 * rhs.value as u128) >> 32) as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   221
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   222
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   223
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   224
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   225
impl ops::Mul<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   226
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   227
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   228
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   229
    fn mul(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   230
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   231
            is_negative: self.is_negative ^ (rhs < 0),
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   232
            value: self.value * rhs.abs() as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   233
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   234
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   235
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   236
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   237
impl ops::Div for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   238
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   239
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   240
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   241
    fn div(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   242
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   243
            is_negative: self.is_negative ^ rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   244
            value: (((self.value as u128) << 32) / rhs.value as u128) as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   245
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   246
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   247
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   248
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   249
impl ops::Div<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   250
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   251
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   252
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   253
    fn div(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   254
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   255
            is_negative: self.is_negative ^ (rhs < 0),
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   256
            value: self.value / rhs.abs() as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   257
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   258
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   259
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   260
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   261
impl ops::Div<u32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   262
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   263
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   264
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   265
    fn div(self, rhs: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   266
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   267
            is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   268
            value: self.value / rhs as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   269
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   270
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   271
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   272
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   273
macro_rules! fp {
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   274
    (-$n: tt / $d: tt) => { FPNum::new(-$n, $d) };
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   275
    ($n: tt / $d: tt) => { FPNum::new($n, $d) };
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   276
    ($n: tt) => { FPNum::from($n) };
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   277
}
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   278
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   279
/* TODO:
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   280
 Distance
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   281
 DistanceI
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   282
 SignAs
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   283
 AngleSin
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   284
 AngleCos
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   285
*/
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   286
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   287
#[cfg(test)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   288
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   289
fn basics() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   290
    let n = fp!(15/2);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   291
    assert!(n.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   292
    assert!(!n.is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   293
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   294
    assert!(!(-n).is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   295
    assert!((-n).is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   296
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   297
    assert_eq!(-(-n), n);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   298
    assert_eq!((-n).abs(), n);
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   299
    assert_eq!(-n, fp!(-15/2));
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   300
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   301
    assert_eq!(n.round(), 7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   302
    assert_eq!((-n).round(), -7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   303
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   304
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   305
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   306
fn zero() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   307
    let z = fp!(0);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   308
    let n = fp!(15/2);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   309
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   310
    assert!(z.is_zero());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   311
    assert!(z.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   312
    assert!((-z).is_negative);
13907
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   313
    assert_eq!(n - n, z);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   314
    assert_eq!(-n + n, z);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   315
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   316
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   317
#[test]
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   318
fn ord() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   319
    let z = fp!(0);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   320
    let n1_5 = fp!(3/2);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   321
    let n2_25 = fp!(9/4);
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   322
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   323
    assert!(!(z > z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   324
    assert!(!(z < z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   325
    assert!(n2_25 > n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   326
    assert!(-n2_25 < n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   327
    assert!(-n2_25 < -n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   328
}
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   329
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   330
#[test]
13906
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   331
fn ord() {
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   332
    let z = FPNum::from(0);;
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   333
    let n1_5 = FPNum::new(3, 2);
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   334
    let n2_25 = FPNum::new(9, 4);
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   335
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   336
    assert!(!(z > z));
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   337
    assert!(!(z < z));
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   338
    assert!(n2_25 > n1_5);
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   339
    assert!(-n2_25 < n1_5);
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   340
    assert!(-n2_25 < -n1_5);
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   341
}
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   342
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13905
diff changeset
   343
#[test]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   344
fn arith() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   345
    let n1_5 = fp!(3/2);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   346
    let n2_25 = fp!(9/4);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   347
    let n_0_15 = fp!(-15/100);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   348
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   349
    assert_eq!(n1_5 + n1_5, fp!(3));
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   350
    assert_eq!(-n1_5 - n1_5, fp!(-3));
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   351
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   352
    assert_eq!(n1_5 * n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   353
    assert_eq!(-n1_5 * -n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   354
    assert_eq!(n1_5 * -n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   355
    assert_eq!(-n1_5 * n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   356
13907
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   357
    assert_eq!(-n2_25 / -n1_5, n1_5);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   358
    assert_eq!(n1_5 / -10, n_0_15);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   359
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   360
    assert_eq!(n1_5.sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   361
    assert_eq!((-n1_5).sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   362
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   363
    assert_eq!(n2_25.sqrt(), n1_5);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   364
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   365
    assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   366
}