rust/fpnum/src/lib.rs
author unc0rr
Tue, 06 Nov 2018 00:02:23 +0100
changeset 14140 3078123e84ea
parent 14139 37c99587825d
child 14178 a4c17cfaa4c9
permissions -rw-r--r--
Bezierize land outline
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13882
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;
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
     3
use std::ops::Shl;
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     4
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     5
#[derive(Clone, Debug, Copy)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     6
pub struct FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     7
    is_negative: bool,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     8
    value: u64,
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    11
impl FPNum {
13927
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
    12
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
    13
    pub fn new(numerator: i32, denominator: u32) -> Self {
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    14
        FPNum::from(numerator) / denominator
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    15
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    16
13885
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13884
diff changeset
    17
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
    18
    pub fn signum(&self) -> i8 {
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    19
        if self.is_negative {
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
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    22
            1
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    23
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    24
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    25
13885
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13884
diff changeset
    26
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
    27
    pub fn is_negative(&self) -> bool {
13882
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
13885
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13884
diff changeset
    31
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
    32
    pub fn is_positive(&self) -> bool {
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    33
        !self.is_negative
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
13885
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13884
diff changeset
    36
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
    37
    pub fn is_zero(&self) -> bool {
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    38
        self.value == 0
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
13885
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13884
diff changeset
    41
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
    42
    pub fn abs(&self) -> Self {
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    43
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    44
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    45
            value: self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    46
        }
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
13885
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13884
diff changeset
    49
    #[inline]
14139
37c99587825d Implement BeizerCurveSegments, no tests
unc0rr
parents: 14122
diff changeset
    50
    pub fn round(&self) -> i32 {
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    51
        if self.is_negative {
14139
37c99587825d Implement BeizerCurveSegments, no tests
unc0rr
parents: 14122
diff changeset
    52
            -((self.value >> 32) as i32)
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    53
        } else {
14139
37c99587825d Implement BeizerCurveSegments, no tests
unc0rr
parents: 14122
diff changeset
    54
            (self.value >> 32) as i32
13882
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
13885
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13884
diff changeset
    58
    #[inline]
14081
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 13939
diff changeset
    59
    pub fn abs_round(&self) -> u32 {
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 13939
diff changeset
    60
        (self.value >> 32) as u32
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 13939
diff changeset
    61
    }
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 13939
diff changeset
    62
5d42204ac35e Start convertion of FindPoint()
unC0Rr
parents: 13939
diff changeset
    63
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
    64
    pub fn sqr(&self) -> Self {
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    65
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    66
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    67
            value: ((self.value as u128).pow(2) >> 32) as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    68
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    69
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    70
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
    71
    pub fn sqrt(&self) -> Self {
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    72
        debug_assert!(!self.is_negative);
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
        let mut t: u64 = 0x4000000000000000;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    75
        let mut r: u64 = 0;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    76
        let mut q = self.value;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    77
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    78
        for _ in 0..32 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    79
            let s = r + t;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    80
            r >>= 1;
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
            if s <= q {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    83
                q -= s;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    84
                r += t;
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
            t >>= 2;
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
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    90
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    91
            value: r << 16,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    92
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    93
    }
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
    94
13927
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
    95
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
    96
    pub fn with_sign(&self, is_negative: bool) -> FPNum {
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
    97
        FPNum {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
    98
            is_negative,
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
    99
            ..*self
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   100
        }
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   101
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   102
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   103
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   104
    pub fn with_sign_as(self, other: FPNum) -> FPNum {
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   105
        self.with_sign(other.is_negative)
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   106
    }
13927
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   107
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   108
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   109
    pub fn point(self) -> FPPoint {
13927
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   110
        FPPoint::new(self, self)
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   111
    }
13882
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   114
impl From<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   115
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   116
    fn from(n: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   117
        FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   118
            is_negative: n < 0,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   119
            value: (n.abs() as u64) << 32,
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
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   122
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   123
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   124
impl From<u32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   125
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   126
    fn from(n: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   127
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   128
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   129
            value: (n as u64) << 32,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   130
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   131
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   132
}
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
impl From<FPNum> for f64 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   135
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   136
    fn from(n: FPNum) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   137
        if n.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   138
            n.value as f64 / (-0x10000000 as f64)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   139
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   140
            n.value as f64 / 0x10000000 as f64
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
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   143
}
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
impl PartialEq for FPNum {
13885
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13884
diff changeset
   146
    #[inline]
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   147
    fn eq(&self, other: &Self) -> bool {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   148
        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
   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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   152
impl Eq for FPNum {}
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
impl PartialOrd for FPNum {
13885
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13884
diff changeset
   155
    #[inline]
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   156
    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
   157
        Some(self.cmp(rhs))
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   158
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   159
}
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
impl Ord for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   162
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   163
    fn cmp(&self, rhs: &Self) -> cmp::Ordering {
13884
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   164
        #[inline]
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   165
        fn extend(n: &FPNum) -> i128 {
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   166
            if n.is_negative {
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   167
                -(n.value as i128)
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   168
            } else {
13884
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   169
                n.value as i128
13882
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
        }
13884
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   172
        extend(self).cmp(&(extend(rhs)))
13882
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   176
impl ops::Add for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   177
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   178
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   179
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   180
    fn add(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   181
        if self.is_negative == rhs.is_negative {
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 if self.value > rhs.value {
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: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   189
                value: self.value - rhs.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
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   192
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   193
                is_negative: rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   194
                value: rhs.value - self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   195
            }
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
impl ops::Sub for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   201
    type Output = Self;
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
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   204
    fn sub(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   205
        if self.is_negative == rhs.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   206
            if self.value > rhs.value {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   207
                Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   208
                    is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   209
                    value: self.value - rhs.value,
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
            } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   212
                Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   213
                    is_negative: !rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   214
                    value: rhs.value - self.value,
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
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   217
        } else {
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,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   220
                value: self.value + rhs.value,
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   226
impl ops::Neg for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   227
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   228
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   229
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   230
    fn neg(self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   231
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   232
            is_negative: !self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   233
            value: self.value,
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   238
impl ops::Mul for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   239
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   240
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   241
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   242
    fn mul(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   243
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   244
            is_negative: self.is_negative ^ rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   245
            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
   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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   250
impl ops::Mul<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   251
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   252
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   253
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   254
    fn mul(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   255
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   256
            is_negative: self.is_negative ^ (rhs < 0),
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   257
            value: self.value * rhs.abs() as u64,
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   262
impl ops::Div for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   263
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   264
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   265
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   266
    fn div(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   267
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   268
            is_negative: self.is_negative ^ rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   269
            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
   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
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   273
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   274
impl ops::Div<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   275
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   276
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   277
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   278
    fn div(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   279
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   280
            is_negative: self.is_negative ^ (rhs < 0),
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   281
            value: self.value / rhs.abs() as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   282
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   283
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   284
}
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
impl ops::Div<u32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   287
    type Output = Self;
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
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   290
    fn div(self, rhs: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   291
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   292
            is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   293
            value: self.value / rhs as u64,
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
    }
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
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   298
#[macro_export]
13891
9ae1184886db add fpnum literal macro
alfadur
parents: 13885
diff changeset
   299
macro_rules! fp {
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   300
    (-$n: tt / $d: tt) => {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   301
        FPNum::new(-$n, $d)
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   302
    };
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   303
    ($n: tt / $d: tt) => {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   304
        FPNum::new($n, $d)
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   305
    };
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   306
    (-$n: tt) => {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   307
        FPNum::from(-$n)
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   308
    };
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   309
    ($n: tt) => {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   310
        FPNum::from($n)
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   311
    };
13891
9ae1184886db add fpnum literal macro
alfadur
parents: 13885
diff changeset
   312
}
9ae1184886db add fpnum literal macro
alfadur
parents: 13885
diff changeset
   313
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   314
const LINEARIZE_TRESHOLD: u64 = 0x1_0000;
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   315
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   316
#[derive(Clone, Copy, Debug)]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   317
pub struct FPPoint {
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   318
    x_is_negative: bool,
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   319
    y_is_negative: bool,
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   320
    x_value: u64,
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   321
    y_value: u64,
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   322
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   323
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   324
impl FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   325
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   326
    pub fn new(x: FPNum, y: FPNum) -> Self {
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   327
        Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   328
            x_is_negative: x.is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   329
            y_is_negative: y.is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   330
            x_value: x.value,
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   331
            y_value: y.value,
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   332
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   333
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   334
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   335
    #[inline]
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   336
    pub fn zero() -> FPPoint {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   337
        FPPoint::new(fp!(0), fp!(0))
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   338
    }
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   339
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   340
    #[inline]
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   341
    pub fn unit_x() -> FPPoint {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   342
        FPPoint::new(fp!(1), fp!(0))
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   343
    }
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   344
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   345
    #[inline]
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   346
    pub fn unit_y() -> FPPoint {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   347
        FPPoint::new(fp!(0), fp!(1))
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   348
    }
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   349
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   350
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   351
    pub fn x(&self) -> FPNum {
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   352
        FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   353
            is_negative: self.x_is_negative,
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   354
            value: self.x_value,
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   355
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   356
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   357
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   358
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   359
    pub fn y(&self) -> FPNum {
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   360
        FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   361
            is_negative: self.y_is_negative,
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   362
            value: self.y_value,
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   363
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   364
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   365
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   366
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   367
    pub fn max_norm(&self) -> FPNum {
13939
665b4c6612ee fix fppoint.max_norm
alfadur
parents: 13932
diff changeset
   368
        std::cmp::max(self.x().abs(), self.y().abs())
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   369
    }
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   370
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   371
    #[inline]
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   372
    pub fn sqr_distance(&self) -> FPNum {
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   373
        self.x().sqr() + self.y().sqr()
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   374
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   375
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   376
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   377
    pub fn distance(&self) -> FPNum {
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   378
        let r = self.x_value | self.y_value;
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   379
        if r < LINEARIZE_TRESHOLD {
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   380
            FPNum::from(r as u32)
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   381
        } else {
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   382
            let mut sqr: u128 = (self.x_value as u128).pow(2) + (self.y_value as u128).pow(2);
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   383
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   384
            let mut t: u128 = 0x40000000_00000000_00000000_00000000;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   385
            let mut r: u128 = 0;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   386
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   387
            for _ in 0..64 {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   388
                let s = r + t;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   389
                r >>= 1;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   390
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   391
                if s <= sqr {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   392
                    sqr -= s;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   393
                    r += t;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   394
                }
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   395
                t >>= 2;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   396
            }
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   397
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   398
            FPNum {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   399
                is_negative: false,
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   400
                value: r as u64,
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   401
            }
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   402
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   403
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   404
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   405
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   406
    pub fn is_in_range(&self, radius: FPNum) -> bool {
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   407
        self.max_norm() < radius && self.sqr_distance() < radius.sqr()
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   408
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   409
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   410
    #[inline]
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   411
    pub fn dot(&self, other: &FPPoint) -> FPNum {
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   412
        self.x() * other.x() + self.y() * other.y()
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   413
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   414
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   415
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   416
impl PartialEq for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   417
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   418
    fn eq(&self, other: &Self) -> bool {
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   419
        self.x() == other.x() && self.y() == other.y()
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   420
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   421
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   422
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   423
impl Eq for FPPoint {}
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   424
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   425
impl ops::Neg for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   426
    type Output = Self;
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   427
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   428
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   429
    fn neg(self) -> Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   430
        Self::new(-self.x(), -self.y())
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   431
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   432
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   433
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   434
macro_rules! bin_op_impl {
13927
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   435
    ($op: ty, $name: tt) => {
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   436
        impl $op for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   437
            type Output = Self;
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   438
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   439
            #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   440
            fn $name(self, rhs: Self) -> Self {
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   441
                Self::new(self.x().$name(rhs.x()), self.y().$name(rhs.y()))
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   442
            }
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   443
        }
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   444
    };
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   445
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   446
13932
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   447
macro_rules! right_scalar_bin_op_impl {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   448
    ($($op: tt)::+, $name: tt) => {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   449
        impl $($op)::+<FPNum> for FPPoint {
13927
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   450
            type Output = Self;
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   451
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   452
            #[inline]
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   453
            fn $name(self, rhs: FPNum) -> Self {
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   454
                Self::new(self.x().$name(rhs),
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   455
                          self.y().$name(rhs))
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   456
            }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   457
        }
14140
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   458
    };
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   459
    ($($op: tt)::+<$arg: tt>, $name: tt) => {
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   460
        impl $($op)::+<$arg> for FPPoint {
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   461
            type Output = Self;
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   462
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   463
            #[inline]
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   464
            fn $name(self, rhs: $arg) -> Self {
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   465
                Self::new(self.x().$name(rhs),
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   466
                          self.y().$name(rhs))
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   467
            }
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   468
        }
13927
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   469
    }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   470
}
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   471
13932
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   472
macro_rules! left_scalar_bin_op_impl {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   473
    ($($op: tt)::+, $name: tt) => {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   474
        impl $($op)::+<FPPoint> for FPNum {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   475
            type Output = FPPoint;
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   476
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   477
            #[inline]
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   478
            fn $name(self, rhs: FPPoint) -> Self::Output {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   479
                Self::Output::new(self.$name(rhs.x()),
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   480
                                  self.$name(rhs.y()))
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   481
            }
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   482
        }
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   483
    }
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   484
}
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   485
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   486
bin_op_impl!(ops::Add, add);
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   487
bin_op_impl!(ops::Sub, sub);
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   488
bin_op_impl!(ops::Mul, mul);
13929
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   489
bin_op_impl!(ops::Div, div);
13932
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   490
right_scalar_bin_op_impl!(ops::Add, add);
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   491
right_scalar_bin_op_impl!(ops::Mul, mul);
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   492
right_scalar_bin_op_impl!(ops::Sub, sub);
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   493
right_scalar_bin_op_impl!(ops::Div, div);
14140
3078123e84ea Bezierize land outline
unc0rr
parents: 14139
diff changeset
   494
right_scalar_bin_op_impl!(ops::Div<u32>, div);
13932
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   495
left_scalar_bin_op_impl!(ops::Mul, mul);
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   496
13929
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   497
macro_rules! bin_assign_op_impl {
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   498
    ($typ: tt, $($op: tt)::+, $name: tt, $delegate: tt) => {
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   499
        bin_assign_op_impl!($typ, $($op)::+<$typ>, $name, $delegate);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   500
    };
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   501
    ($typ: tt, $($op: tt)::+<$arg: tt>, $name: tt, $delegate: tt) => {
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   502
        impl $($op)::+<$arg> for $typ {
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   503
            #[inline]
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   504
            fn $name(&mut self, rhs: $arg) {
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   505
                *self = *self $delegate rhs;
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   506
            }
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   507
        }
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   508
    }
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   509
}
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   510
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   511
bin_assign_op_impl!(FPNum, ops::AddAssign, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   512
bin_assign_op_impl!(FPNum, ops::SubAssign, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   513
bin_assign_op_impl!(FPNum, ops::MulAssign, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   514
bin_assign_op_impl!(FPNum, ops::DivAssign, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   515
bin_assign_op_impl!(FPNum, ops::MulAssign<i32>, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   516
bin_assign_op_impl!(FPNum, ops::DivAssign<i32>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   517
bin_assign_op_impl!(FPNum, ops::DivAssign<u32>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   518
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   519
bin_assign_op_impl!(FPPoint, ops::AddAssign, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   520
bin_assign_op_impl!(FPPoint, ops::SubAssign, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   521
bin_assign_op_impl!(FPPoint, ops::MulAssign, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   522
bin_assign_op_impl!(FPPoint, ops::DivAssign, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   523
bin_assign_op_impl!(FPPoint, ops::AddAssign<FPNum>, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   524
bin_assign_op_impl!(FPPoint, ops::SubAssign<FPNum>, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   525
bin_assign_op_impl!(FPPoint, ops::MulAssign<FPNum>, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   526
bin_assign_op_impl!(FPPoint, ops::DivAssign<FPNum>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   527
13928
5fdc41cd0841 make methods public
alfadur
parents: 13927
diff changeset
   528
pub fn distance<T>(x: T, y: T) -> FPNum
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   529
where
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   530
    T: Into<i64> + std::fmt::Debug,
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   531
{
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   532
    let mut sqr: u128 = (x.into().pow(2) as u128).shl(64) + (y.into().pow(2) as u128).shl(64);
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   533
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   534
    let mut t: u128 = 0x40000000_00000000_00000000_00000000;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   535
    let mut r: u128 = 0;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   536
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   537
    for _ in 0..64 {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   538
        let s = r + t;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   539
        r >>= 1;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   540
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   541
        if s <= sqr {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   542
            sqr -= s;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   543
            r += t;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   544
        }
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   545
        t >>= 2;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   546
    }
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   547
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   548
    FPNum {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   549
        is_negative: false,
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   550
        value: r as u64,
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   551
    }
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   552
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   553
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   554
/* TODO:
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   555
 AngleSin
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   556
 AngleCos
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   557
*/
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   558
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   559
#[cfg(test)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   560
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   561
fn basics() {
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   562
    let n = fp!(15 / 2);
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   563
    assert!(n.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   564
    assert!(!n.is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   565
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   566
    assert!(!(-n).is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   567
    assert!((-n).is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   568
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   569
    assert_eq!(-(-n), n);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   570
    assert_eq!((-n).abs(), n);
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   571
    assert_eq!(-n, fp!(-15 / 2));
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   572
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   573
    assert_eq!(n.round(), 7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   574
    assert_eq!((-n).round(), -7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   575
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   576
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   577
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   578
fn zero() {
13891
9ae1184886db add fpnum literal macro
alfadur
parents: 13885
diff changeset
   579
    let z = fp!(0);
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   580
    let n = fp!(15 / 2);
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   581
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   582
    assert!(z.is_zero());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   583
    assert!(z.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   584
    assert!((-z).is_negative);
13883
2bfd7472ef1d Add some more tests
unc0rr
parents: 13882
diff changeset
   585
    assert_eq!(n - n, z);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13882
diff changeset
   586
    assert_eq!(-n + n, z);
13926
2717a5289d88 fix test
alfadur
parents: 13925
diff changeset
   587
    assert_eq!(n.with_sign_as(-n), -n);
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   588
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   589
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   590
#[test]
13884
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   591
fn ord() {
13891
9ae1184886db add fpnum literal macro
alfadur
parents: 13885
diff changeset
   592
    let z = fp!(0);
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   593
    let n1_5 = fp!(3 / 2);
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   594
    let n2_25 = fp!(9 / 4);
13884
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   595
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   596
    assert!(!(z > z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   597
    assert!(!(z < z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   598
    assert!(n2_25 > n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   599
    assert!(-n2_25 < n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   600
    assert!(-n2_25 < -n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   601
}
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   602
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13883
diff changeset
   603
#[test]
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   604
fn arith() {
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   605
    let n1_5 = fp!(3 / 2);
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   606
    let n2_25 = fp!(9 / 4);
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   607
    let n_0_15 = fp!(-15 / 100);
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   608
13891
9ae1184886db add fpnum literal macro
alfadur
parents: 13885
diff changeset
   609
    assert_eq!(n1_5 + n1_5, fp!(3));
9ae1184886db add fpnum literal macro
alfadur
parents: 13885
diff changeset
   610
    assert_eq!(-n1_5 - n1_5, fp!(-3));
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   611
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   612
    assert_eq!(n1_5 * n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   613
    assert_eq!(-n1_5 * -n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   614
    assert_eq!(n1_5 * -n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   615
    assert_eq!(-n1_5 * n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   616
13883
2bfd7472ef1d Add some more tests
unc0rr
parents: 13882
diff changeset
   617
    assert_eq!(-n2_25 / -n1_5, n1_5);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13882
diff changeset
   618
    assert_eq!(n1_5 / -10, n_0_15);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13882
diff changeset
   619
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   620
    assert_eq!(n1_5.sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   621
    assert_eq!((-n1_5).sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   622
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   623
    assert_eq!(n2_25.sqrt(), n1_5);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   624
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   625
    assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25);
13929
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   626
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   627
    let mut m = fp!(1);
ff77c9920007 add opassign implementations
alfadur
parents: 13928
diff changeset
   628
    m += n1_5;
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   629
    assert_eq!(m, fp!(5 / 2));
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   630
}
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   631
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   632
#[test]
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   633
fn test_distance_high_values() {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   634
    assert_eq!(distance(1_000_000i32, 0), fp!(1_000_000));
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   635
    assert_eq!(
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   636
        FPPoint::new(fp!(1_000_000), fp!(0)).distance(),
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   637
        fp!(1_000_000)
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   638
    );
13882
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   639
}
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   640
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   641
#[test]
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   642
fn point() {
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   643
    let z = FPPoint::zero();
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   644
    let n = fp!(16 / 9);
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   645
    let p = FPPoint::new(fp!(1), fp!(-2));
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   646
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   647
    assert_eq!(p.sqr_distance(), fp!(5));
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   648
    assert_eq!(p + -p, FPPoint::zero());
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   649
    assert_eq!(p * z, z);
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   650
    assert_eq!(p.dot(&z), fp!(0));
13932
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13929
diff changeset
   651
    assert_eq!(n * p, p * n);
13925
2ee07e751171 add more fpnum functions
alfadur
parents: 13905
diff changeset
   652
    assert_eq!(distance(4, 3), fp!(5));
13927
cf28d7a2b7fe add scalar operations
alfadur
parents: 13926
diff changeset
   653
    assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
13939
665b4c6612ee fix fppoint.max_norm
alfadur
parents: 13932
diff changeset
   654
    assert_eq!(p.max_norm(), fp!(2));
14122
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14081
diff changeset
   655
}