rust/fpnum/src/lib.rs
author alfadur
Sat, 13 Oct 2018 00:43:04 +0300
changeset 13895 2a3d119b9fe9
parent 13887 b172a5d40eee
child 13901 ac1801fe51d9
permissions -rw-r--r--
delegate cmp to rustlib
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13887
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    15
    fn signum(&self) -> i8 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    16
        if self.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    17
            -1
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    18
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    19
            1
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    20
        }
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
    fn is_negative(&self) -> bool {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    24
        self.is_negative
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    25
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    26
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    27
    fn is_positive(&self) -> bool {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    28
        !self.is_negative
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    29
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    30
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    31
    fn is_zero(&self) -> bool {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    32
        self.value == 0
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    33
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    34
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    35
    fn abs(&self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    36
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    37
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    38
            value: self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    39
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    40
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    41
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    42
    fn round(&self) -> i64 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    43
        if self.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    44
            -((self.value >> 32) as i64)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    45
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    46
            (self.value >> 32) as i64
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    47
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    48
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    49
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    50
    fn sqr(&self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    51
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    52
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    53
            value: ((self.value as u128).pow(2) >> 32) as u64,
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
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    56
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    57
    fn sqrt(&self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    58
        debug_assert!(!self.is_negative);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    59
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    60
        let mut t: u64 = 0x4000000000000000;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    61
        let mut r: u64 = 0;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    62
        let mut q = self.value;
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
        for _ in 0..32 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    65
            let s = r + t;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    66
            r >>= 1;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    67
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    68
            if s <= q {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    69
                q -= s;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    70
                r += t;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    71
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    72
            t >>= 2;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    73
        }
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
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    76
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    77
            value: r << 16,
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
    }
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
impl From<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    83
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    84
    fn from(n: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    85
        FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    86
            is_negative: n < 0,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    87
            value: (n.abs() as u64) << 32,
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
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    90
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    91
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    92
impl From<u32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    93
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    94
    fn from(n: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    95
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    96
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    97
            value: (n as u64) << 32,
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
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   100
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   101
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   102
impl From<FPNum> for f64 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   103
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   104
    fn from(n: FPNum) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   105
        if n.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   106
            n.value as f64 / (-0x10000000 as f64)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   107
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   108
            n.value as f64 / 0x10000000 as f64
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   109
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   110
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   111
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   112
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   113
impl PartialEq for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   114
    fn eq(&self, other: &Self) -> bool {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   115
        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
   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
impl Eq for FPNum {}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   120
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   121
impl PartialOrd for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   122
    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
   123
        Some(self.cmp(rhs))
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 Ord for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   128
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   129
    fn cmp(&self, rhs: &Self) -> cmp::Ordering {
13895
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   130
        #[inline]
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   131
        fn extend(n: &FPNum) -> i128 {
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   132
            if n.is_negative {
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   133
                -(n.value as i128)
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   134
            } else {
13895
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   135
                n.value as i128
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   136
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   137
        }
13895
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   138
        extend(self).cmp(&(extend(rhs)))
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   139
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   140
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   141
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   142
impl ops::Add for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   143
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   144
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   145
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   146
    fn add(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   147
        if self.is_negative == rhs.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   148
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   149
                is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   150
                value: self.value + rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   151
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   152
        } else if self.value > rhs.value {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   153
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   154
                is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   155
                value: self.value - rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   156
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   157
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   158
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   159
                is_negative: rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   160
                value: rhs.value - self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   161
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   162
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   163
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   164
}
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
impl ops::Sub for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   167
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   168
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   169
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   170
    fn sub(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   171
        if self.is_negative == rhs.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   172
            if self.value > rhs.value {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   173
                Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   174
                    is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   175
                    value: self.value - rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   176
                }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   177
            } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   178
                Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   179
                    is_negative: !rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   180
                    value: rhs.value - self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   181
                }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   182
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   183
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   184
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   185
                is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   186
                value: self.value + rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   187
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   188
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   189
    }
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
impl ops::Neg for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   193
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   194
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   195
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   196
    fn neg(self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   197
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   198
            is_negative: !self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   199
            value: self.value,
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
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   202
}
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
impl ops::Mul for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   205
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   206
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   207
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   208
    fn mul(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   209
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   210
            is_negative: self.is_negative ^ rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   211
            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
   212
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   213
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   214
}
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
impl ops::Mul<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   217
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   218
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   219
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   220
    fn mul(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   221
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   222
            is_negative: self.is_negative ^ (rhs < 0),
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   223
            value: self.value * rhs.abs() as u64,
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
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   226
}
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
impl ops::Div for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   229
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   230
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   231
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   232
    fn div(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   233
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   234
            is_negative: self.is_negative ^ rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   235
            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
   236
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   237
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   238
}
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
impl ops::Div<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   241
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   242
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   243
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   244
    fn div(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   245
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   246
            is_negative: self.is_negative ^ (rhs < 0),
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   247
            value: self.value / rhs.abs() as u64,
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
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   250
}
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
impl ops::Div<u32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   253
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   254
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   255
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   256
    fn div(self, rhs: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   257
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   258
            is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   259
            value: self.value / rhs as u64,
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
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   262
}
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
/* TODO:
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   265
 Distance
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   266
 DistanceI
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   267
 SignAs
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   268
 AngleSin
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   269
 AngleCos
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
#[cfg(test)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   273
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   274
fn basics() {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   275
    let n = FPNum::new(15, 2);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   276
    assert!(n.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   277
    assert!(!n.is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   278
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   279
    assert!(!(-n).is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   280
    assert!((-n).is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   281
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   282
    assert_eq!(-(-n), n);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   283
    assert_eq!((-n).abs(), n);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   284
    assert_eq!(-n, FPNum::new(-15, 2));
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
    assert_eq!(n.round(), 7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   287
    assert_eq!((-n).round(), -7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   288
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   289
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   290
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   291
fn zero() {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   292
    let z = FPNum::from(0);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   293
    let n = FPNum::new(15, 2);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   294
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   295
    assert!(z.is_zero());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   296
    assert!(z.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   297
    assert!((-z).is_negative);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   298
    assert_eq!(n - n, z)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   299
}
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
#[test]
13895
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   302
fn ord() {
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   303
    let z = FPNum::from(0);;
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   304
    let n1_5 = FPNum::new(3, 2);
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   305
    let n2_25 = FPNum::new(9, 4);
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   306
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   307
    assert!(!(z > z));
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   308
    assert!(!(z < z));
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   309
    assert!(n2_25 > n1_5);
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   310
    assert!(-n2_25 < n1_5);
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   311
    assert!(-n2_25 < -n1_5);
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   312
}
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   313
2a3d119b9fe9 delegate cmp to rustlib
alfadur
parents: 13887
diff changeset
   314
#[test]
13887
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   315
fn arith() {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   316
    let n1_5 = FPNum::new(3, 2);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   317
    let n2_25 = FPNum::new(9, 4);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   318
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   319
    assert_eq!(n1_5 + n1_5, FPNum::from(3));
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   320
    assert_eq!(-n1_5 - n1_5, FPNum::from(-3));
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   321
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   322
    assert_eq!(n1_5 * n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   323
    assert_eq!(-n1_5 * -n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   324
    assert_eq!(n1_5 * -n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   325
    assert_eq!(-n1_5 * n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   326
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   327
    assert_eq!(n1_5.sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   328
    assert_eq!((-n1_5).sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   329
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   330
    assert_eq!(n2_25.sqrt(), n1_5);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   331
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   332
    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
   333
}