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