rust/fpnum/src/lib.rs
author alfadur
Thu, 04 Jul 2019 21:40:50 +0300
changeset 15234 517f3a1dd5c2
parent 15233 293250953317
child 15235 58a0f2a6527b
permissions -rw-r--r--
reduce convertions in signum
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14648
2e2b31cf0871 make stuff const
alfadur
parents: 14228
diff changeset
     1
use std::{cmp, ops, ops::Shl};
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     2
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
     3
const POSITIVE_MASK: u64 = 0x0000_0000_0000_0000;
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
     4
const NEGATIVE_MASK: u64 = 0xFFFF_FFFF_FFFF_FFFF;
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
     5
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
     6
#[inline]
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
     7
fn bool_mask(is_negative: bool) -> u64 {
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
     8
    if is_negative {
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
     9
        NEGATIVE_MASK
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    10
    } else {
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    11
        POSITIVE_MASK
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    12
    }
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    13
}
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    14
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    15
#[derive(Clone, Debug, Copy)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    16
pub struct FPNum {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    17
    sign_mask: u64,
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    18
    value: u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    19
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    20
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    21
impl FPNum {
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
    22
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    23
    pub fn new(numerator: i32, denominator: u32) -> Self {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    24
        FPNum::from(numerator) / denominator
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
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    27
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    28
    pub fn signum(&self) -> i8 {
15234
517f3a1dd5c2 reduce convertions in signum
alfadur
parents: 15233
diff changeset
    29
        (1u64 ^ self.sign_mask).wrapping_sub(self.sign_mask) as i8
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    30
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    31
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    32
    #[inline]
14648
2e2b31cf0871 make stuff const
alfadur
parents: 14228
diff changeset
    33
    pub const fn is_negative(&self) -> bool {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    34
        self.sign_mask != POSITIVE_MASK
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    35
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    36
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    37
    #[inline]
14648
2e2b31cf0871 make stuff const
alfadur
parents: 14228
diff changeset
    38
    pub const fn is_positive(&self) -> bool {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    39
        self.sign_mask == POSITIVE_MASK
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    40
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    41
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    42
    #[inline]
14648
2e2b31cf0871 make stuff const
alfadur
parents: 14228
diff changeset
    43
    pub const fn is_zero(&self) -> bool {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    44
        self.value == 0
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    45
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    46
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    47
    #[inline]
14648
2e2b31cf0871 make stuff const
alfadur
parents: 14228
diff changeset
    48
    pub const fn abs(&self) -> Self {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    49
        Self {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    50
            sign_mask: POSITIVE_MASK,
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    51
            value: self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    52
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    53
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    54
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    55
    #[inline]
14160
37c99587825d Implement BeizerCurveSegments, no tests
unc0rr
parents: 14143
diff changeset
    56
    pub fn round(&self) -> i32 {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    57
        ((self.value >> 32) as i32 ^ self.sign_mask as i32).wrapping_sub(self.sign_mask as i32)
13905
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]
14648
2e2b31cf0871 make stuff const
alfadur
parents: 14228
diff changeset
    61
    pub const fn abs_round(&self) -> u32 {
14102
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 {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    68
            sign_mask: 0,
13905
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 {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    74
        debug_assert!(self.is_positive());
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    75
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    76
        let mut t: u64 = 0x4000_0000_0000_0000;
13905
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 {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
    92
            sign_mask: POSITIVE_MASK,
13905
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]
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
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 {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   100
            sign_mask: bool_mask(is_negative),
14143
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]
14648
2e2b31cf0871 make stuff const
alfadur
parents: 14228
diff changeset
   106
    pub const fn with_sign_as(self, other: FPNum) -> FPNum {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   107
        FPNum {
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   108
            sign_mask: other.sign_mask,
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   109
            ..self
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   110
        }
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   111
    }
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   112
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   113
    #[inline]
14648
2e2b31cf0871 make stuff const
alfadur
parents: 14228
diff changeset
   114
    pub const fn point(self) -> FPPoint {
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   115
        FPPoint::new(self, self)
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   116
    }
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   117
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   118
    #[inline]
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   119
    const fn temp_i128(self) -> i128 {
15233
293250953317 fix u32 conversion sign
alfadur
parents: 15232
diff changeset
   120
        ((self.value ^ self.sign_mask) as i128).wrapping_sub(self.sign_mask as i128)
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   121
    }
13905
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<i32> 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: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   127
        FPNum {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   128
            sign_mask: bool_mask(n < 0),
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   129
            value: (n.abs() 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<u32> for FPNum {
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: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   137
        Self {
15233
293250953317 fix u32 conversion sign
alfadur
parents: 15232
diff changeset
   138
            sign_mask: POSITIVE_MASK,
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   139
            value: (n as u64) << 32,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   140
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   141
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   142
}
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
impl From<FPNum> for f64 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   145
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   146
    fn from(n: FPNum) -> Self {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   147
        if n.is_negative() {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   148
            n.value as f64 / (-0x10000000 as f64)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   149
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   150
            n.value as f64 / 0x10000000 as f64
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   155
impl PartialEq for FPNum {
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
   156
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   157
    fn eq(&self, other: &Self) -> bool {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   158
        self.value == other.value && (self.sign_mask == other.sign_mask || self.value == 0)
13905
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   162
impl Eq for FPNum {}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   163
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   164
impl PartialOrd for FPNum {
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
   165
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   166
    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
   167
        Some(self.cmp(rhs))
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   168
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   169
}
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
impl Ord for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   172
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   173
    fn cmp(&self, rhs: &Self) -> cmp::Ordering {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   174
        self.temp_i128().cmp(&(rhs.temp_i128()))
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 {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   183
        let tmp = self.temp_i128() + rhs.temp_i128();
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   184
        let mask = bool_mask(tmp < 0);
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   185
        Self {
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   186
            sign_mask: mask,
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   187
            value: ((tmp as u64) ^ mask).wrapping_sub(mask),
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   188
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   189
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   190
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   191
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   192
impl ops::Sub for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   193
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   194
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   195
    #[inline]
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   196
    fn sub(self, mut rhs: Self) -> Self {
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   197
        rhs.sign_mask = !rhs.sign_mask;
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   198
        self + rhs
13905
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::Neg 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 neg(self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   207
        Self {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   208
            sign_mask: !self.sign_mask,
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   209
            value: self.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
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   212
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   213
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   214
impl ops::Mul for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   215
    type Output = Self;
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
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   218
    fn mul(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   219
        Self {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   220
            sign_mask: self.sign_mask ^ rhs.sign_mask,
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   221
            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
   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::Mul<i32> 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 mul(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   231
        Self {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   232
            sign_mask: self.sign_mask ^ bool_mask(rhs < 0),
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   233
            value: self.value * rhs.abs() as u64,
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::Div 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 div(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   243
        Self {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   244
            sign_mask: self.sign_mask ^ rhs.sign_mask,
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   245
            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
   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::Div<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 div(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   255
        Self {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   256
            sign_mask: self.sign_mask ^ bool_mask(rhs < 0),
13905
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<u32> 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: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   267
        Self {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   268
            sign_mask: self.sign_mask,
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   269
            value: self.value / rhs 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
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   274
#[macro_export]
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   275
macro_rules! fp {
14722
5e2c892b0222 allow fp! take denominator tokens
alfadur
parents: 14688
diff changeset
   276
    ($n: literal / $d: tt) => {
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   277
        FPNum::new($n, $d)
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   278
    };
14685
def1b9870078 match literals in fp macro
alfadur
parents: 14648
diff changeset
   279
    ($n: literal) => {
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   280
        FPNum::from($n)
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   281
    };
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   282
}
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   283
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   284
const LINEARIZE_TRESHOLD: u64 = 0x1_0000;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   285
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   286
#[derive(Clone, Copy, Debug)]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   287
pub struct FPPoint {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   288
    x_sign_mask: u32,
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   289
    y_sign_mask: u32,
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   290
    x_value: u64,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   291
    y_value: u64,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   292
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   293
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   294
impl FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   295
    #[inline]
14648
2e2b31cf0871 make stuff const
alfadur
parents: 14228
diff changeset
   296
    pub const fn new(x: FPNum, y: FPNum) -> Self {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   297
        Self {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   298
            x_sign_mask: x.sign_mask as u32,
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   299
            y_sign_mask: y.sign_mask as u32,
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   300
            x_value: x.value,
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   301
            y_value: y.value,
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   302
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   303
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   304
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   305
    #[inline]
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   306
    pub fn zero() -> FPPoint {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   307
        FPPoint::new(fp!(0), fp!(0))
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   308
    }
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   309
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   310
    #[inline]
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   311
    pub fn unit_x() -> FPPoint {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   312
        FPPoint::new(fp!(1), fp!(0))
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   313
    }
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   314
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   315
    #[inline]
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   316
    pub fn unit_y() -> FPPoint {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   317
        FPPoint::new(fp!(0), fp!(1))
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   318
    }
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   319
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   320
    #[inline]
14648
2e2b31cf0871 make stuff const
alfadur
parents: 14228
diff changeset
   321
    pub const fn x(&self) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   322
        FPNum {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   323
            sign_mask: self.x_sign_mask as i32 as u64,
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   324
            value: self.x_value,
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   325
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   326
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   327
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   328
    #[inline]
14648
2e2b31cf0871 make stuff const
alfadur
parents: 14228
diff changeset
   329
    pub const fn y(&self) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   330
        FPNum {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   331
            sign_mask: self.y_sign_mask as i32 as u64,
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   332
            value: self.y_value,
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   333
        }
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
    #[inline]
14199
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14161
diff changeset
   337
    pub fn is_zero(&self) -> bool {
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14161
diff changeset
   338
        self.x().is_zero() && self.y().is_zero()
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14161
diff changeset
   339
    }
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14161
diff changeset
   340
a4c17cfaa4c9 split hwphysics into modules
alfadur
parents: 14161
diff changeset
   341
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   342
    pub fn max_norm(&self) -> FPNum {
13960
665b4c6612ee fix fppoint.max_norm
alfadur
parents: 13953
diff changeset
   343
        std::cmp::max(self.x().abs(), self.y().abs())
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   344
    }
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   345
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   346
    #[inline]
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   347
    pub fn sqr_distance(&self) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   348
        self.x().sqr() + self.y().sqr()
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   349
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   350
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   351
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   352
    pub fn distance(&self) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   353
        let r = self.x_value | self.y_value;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   354
        if r < LINEARIZE_TRESHOLD {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   355
            FPNum::from(r as u32)
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   356
        } else {
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   357
            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
   358
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   359
            let mut t: u128 = 0x40000000_00000000_00000000_00000000;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   360
            let mut r: u128 = 0;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   361
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   362
            for _ in 0..64 {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   363
                let s = r + t;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   364
                r >>= 1;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   365
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   366
                if s <= sqr {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   367
                    sqr -= s;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   368
                    r += t;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   369
                }
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   370
                t >>= 2;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   371
            }
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   372
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   373
            FPNum {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   374
                sign_mask: POSITIVE_MASK,
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   375
                value: r as u64,
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   376
            }
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   377
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   378
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   379
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   380
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   381
    pub fn is_in_range(&self, radius: FPNum) -> bool {
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   382
        self.max_norm() < radius && self.sqr_distance() < radius.sqr()
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   383
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   384
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   385
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   386
    pub fn dot(&self, other: &FPPoint) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   387
        self.x() * other.x() + self.y() * other.y()
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   388
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   389
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   390
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   391
impl PartialEq for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   392
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   393
    fn eq(&self, other: &Self) -> bool {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   394
        self.x() == other.x() && self.y() == other.y()
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   395
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   396
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   397
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   398
impl Eq for FPPoint {}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   399
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   400
impl ops::Neg for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   401
    type Output = Self;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   402
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   403
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   404
    fn neg(self) -> Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   405
        Self::new(-self.x(), -self.y())
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   406
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   407
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   408
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   409
macro_rules! bin_op_impl {
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   410
    ($op: ty, $name: tt) => {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   411
        impl $op for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   412
            type Output = Self;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   413
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   414
            #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   415
            fn $name(self, rhs: Self) -> Self {
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   416
                Self::new(self.x().$name(rhs.x()), self.y().$name(rhs.y()))
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   417
            }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   418
        }
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   419
    };
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   420
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   421
13953
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   422
macro_rules! right_scalar_bin_op_impl {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   423
    ($($op: tt)::+, $name: tt) => {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   424
        impl $($op)::+<FPNum> for FPPoint {
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   425
            type Output = Self;
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   426
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   427
            #[inline]
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   428
            fn $name(self, rhs: FPNum) -> Self {
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   429
                Self::new(self.x().$name(rhs),
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   430
                          self.y().$name(rhs))
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   431
            }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   432
        }
14161
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   433
    };
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   434
    ($($op: tt)::+<$arg: tt>, $name: tt) => {
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   435
        impl $($op)::+<$arg> for FPPoint {
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   436
            type Output = Self;
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   437
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   438
            #[inline]
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   439
            fn $name(self, rhs: $arg) -> Self {
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   440
                Self::new(self.x().$name(rhs),
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   441
                          self.y().$name(rhs))
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   442
            }
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   443
        }
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   444
    }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   445
}
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   446
13953
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   447
macro_rules! left_scalar_bin_op_impl {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   448
    ($($op: tt)::+, $name: tt) => {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   449
        impl $($op)::+<FPPoint> for FPNum {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   450
            type Output = FPPoint;
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   451
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   452
            #[inline]
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   453
            fn $name(self, rhs: FPPoint) -> Self::Output {
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   454
                Self::Output::new(self.$name(rhs.x()),
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   455
                                  self.$name(rhs.y()))
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   456
            }
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   457
        }
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   458
    }
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   459
}
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   460
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   461
bin_op_impl!(ops::Add, add);
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   462
bin_op_impl!(ops::Sub, sub);
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   463
bin_op_impl!(ops::Mul, mul);
13950
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   464
bin_op_impl!(ops::Div, div);
13953
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   465
right_scalar_bin_op_impl!(ops::Add, add);
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   466
right_scalar_bin_op_impl!(ops::Mul, mul);
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   467
right_scalar_bin_op_impl!(ops::Sub, sub);
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   468
right_scalar_bin_op_impl!(ops::Div, div);
14161
3078123e84ea Bezierize land outline
unc0rr
parents: 14160
diff changeset
   469
right_scalar_bin_op_impl!(ops::Div<u32>, div);
13953
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   470
left_scalar_bin_op_impl!(ops::Mul, mul);
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   471
13950
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   472
macro_rules! bin_assign_op_impl {
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   473
    ($typ: tt, $($op: tt)::+, $name: tt, $delegate: tt) => {
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   474
        bin_assign_op_impl!($typ, $($op)::+<$typ>, $name, $delegate);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   475
    };
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   476
    ($typ: tt, $($op: tt)::+<$arg: tt>, $name: tt, $delegate: tt) => {
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   477
        impl $($op)::+<$arg> for $typ {
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   478
            #[inline]
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   479
            fn $name(&mut self, rhs: $arg) {
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   480
                *self = *self $delegate rhs;
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   481
            }
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   482
        }
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   483
    }
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   484
}
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   485
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   486
bin_assign_op_impl!(FPNum, ops::AddAssign, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   487
bin_assign_op_impl!(FPNum, ops::SubAssign, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   488
bin_assign_op_impl!(FPNum, ops::MulAssign, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   489
bin_assign_op_impl!(FPNum, ops::DivAssign, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   490
bin_assign_op_impl!(FPNum, ops::MulAssign<i32>, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   491
bin_assign_op_impl!(FPNum, ops::DivAssign<i32>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   492
bin_assign_op_impl!(FPNum, ops::DivAssign<u32>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   493
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   494
bin_assign_op_impl!(FPPoint, ops::AddAssign, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   495
bin_assign_op_impl!(FPPoint, ops::SubAssign, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   496
bin_assign_op_impl!(FPPoint, ops::MulAssign, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   497
bin_assign_op_impl!(FPPoint, ops::DivAssign, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   498
bin_assign_op_impl!(FPPoint, ops::AddAssign<FPNum>, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   499
bin_assign_op_impl!(FPPoint, ops::SubAssign<FPNum>, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   500
bin_assign_op_impl!(FPPoint, ops::MulAssign<FPNum>, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   501
bin_assign_op_impl!(FPPoint, ops::DivAssign<FPNum>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   502
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   503
pub fn distance<T>(x: T, y: T) -> FPNum
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   504
where
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   505
    T: Into<i64> + std::fmt::Debug,
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   506
{
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   507
    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
   508
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   509
    let mut t: u128 = 0x40000000_00000000_00000000_00000000;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   510
    let mut r: u128 = 0;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   511
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   512
    for _ in 0..64 {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   513
        let s = r + t;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   514
        r >>= 1;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   515
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   516
        if s <= sqr {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   517
            sqr -= s;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   518
            r += t;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   519
        }
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   520
        t >>= 2;
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   521
    }
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   522
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   523
    FPNum {
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   524
        sign_mask: POSITIVE_MASK,
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   525
        value: r as u64,
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   526
    }
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   527
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   528
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   529
/* TODO:
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   530
 AngleSin
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   531
 AngleCos
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   532
*/
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   533
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   534
#[cfg(test)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   535
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   536
fn basics() {
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   537
    let n = fp!(15 / 2);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   538
    assert!(n.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   539
    assert!(!n.is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   540
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   541
    assert!(!(-n).is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   542
    assert!((-n).is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   543
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   544
    assert_eq!(-(-n), n);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   545
    assert_eq!((-n).abs(), n);
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   546
    assert_eq!(-n, fp!(-15 / 2));
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   547
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   548
    assert_eq!(n.round(), 7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   549
    assert_eq!((-n).round(), -7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   550
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   551
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   552
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   553
fn zero() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   554
    let z = fp!(0);
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   555
    let n = fp!(15 / 2);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   556
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   557
    assert!(z.is_zero());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   558
    assert!(z.is_positive());
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   559
    assert!((-z).is_negative());
13907
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   560
    assert_eq!(n - n, z);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   561
    assert_eq!(-n + n, z);
13947
2717a5289d88 fix test
alfadur
parents: 13946
diff changeset
   562
    assert_eq!(n.with_sign_as(-n), -n);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   563
}
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
#[test]
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   566
fn ord() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   567
    let z = fp!(0);
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   568
    let n1_5 = fp!(3 / 2);
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   569
    let n2_25 = fp!(9 / 4);
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   570
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   571
    assert!(!(z > z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   572
    assert!(!(z < z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   573
    assert!(n2_25 > n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   574
    assert!(-n2_25 < n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   575
    assert!(-n2_25 < -n1_5);
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   576
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   577
    assert_eq!(n1_5.signum(), 1);
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   578
    assert_eq!((-n1_5).signum(), -1);
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   579
}
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   580
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   581
#[test]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   582
fn arith() {
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   583
    let n1_5 = fp!(3 / 2);
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   584
    let n2_25 = fp!(9 / 4);
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   585
    let n_0_15 = fp!(-15 / 100);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   586
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   587
    assert_eq!(n1_5 + n1_5, fp!(3));
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   588
    assert_eq!(-n1_5 - n1_5, fp!(-3));
15232
924f7e38815e optimize fpnum operations
alfadur
parents: 14722
diff changeset
   589
    assert_eq!(n1_5 - n1_5, fp!(0));
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   590
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   591
    assert_eq!(n1_5 * n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   592
    assert_eq!(-n1_5 * -n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   593
    assert_eq!(n1_5 * -n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   594
    assert_eq!(-n1_5 * n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   595
13907
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   596
    assert_eq!(-n2_25 / -n1_5, n1_5);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   597
    assert_eq!(n1_5 / -10, n_0_15);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   598
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   599
    assert_eq!(n1_5.sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   600
    assert_eq!((-n1_5).sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   601
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   602
    assert_eq!(n2_25.sqrt(), n1_5);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   603
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   604
    assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25);
13950
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   605
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   606
    let mut m = fp!(1);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   607
    m += n1_5;
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   608
    assert_eq!(m, fp!(5 / 2));
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   609
}
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   610
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   611
#[test]
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   612
fn test_distance_high_values() {
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   613
    assert_eq!(distance(1_000_000i32, 0), fp!(1_000_000));
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   614
    assert_eq!(
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   615
        FPPoint::new(fp!(1_000_000), fp!(0)).distance(),
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   616
        fp!(1_000_000)
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   617
    );
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   618
}
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   619
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   620
#[test]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   621
fn point() {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   622
    let z = FPPoint::zero();
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   623
    let n = fp!(16 / 9);
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   624
    let p = FPPoint::new(fp!(1), fp!(-2));
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   625
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   626
    assert_eq!(p.sqr_distance(), fp!(5));
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   627
    assert_eq!(p + -p, FPPoint::zero());
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   628
    assert_eq!(p * z, z);
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   629
    assert_eq!(p.dot(&z), fp!(0));
13953
7bb60596c27e add left scalar operations to fpnum
alfadur
parents: 13950
diff changeset
   630
    assert_eq!(n * p, p * n);
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   631
    assert_eq!(distance(4, 3), fp!(5));
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   632
    assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
13960
665b4c6612ee fix fppoint.max_norm
alfadur
parents: 13953
diff changeset
   633
    assert_eq!(p.max_norm(), fp!(2));
14143
c27461e6a9eb Implement non-overflowing calculations for high values
unc0rr
parents: 14102
diff changeset
   634
}