rust/fpnum/src/lib.rs
author alfadur
Tue, 16 Oct 2018 23:49:44 +0300
changeset 13950 ff77c9920007
parent 13949 5fdc41cd0841
child 13953 7bb60596c27e
permissions -rw-r--r--
add opassign implementations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     1
use std::cmp;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     2
use std::ops;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     3
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     4
#[derive(Clone, Debug, Copy)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     5
pub struct FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     6
    is_negative: bool,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     7
    value: u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     8
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
     9
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    10
impl FPNum {
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
    11
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    12
    pub fn new(numerator: i32, denominator: u32) -> Self {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    13
        FPNum::from(numerator) / denominator
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    14
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    15
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    16
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    17
    pub fn signum(&self) -> i8 {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    18
        if self.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    19
            -1
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    20
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    21
            1
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    22
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    23
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    24
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    25
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    26
    pub fn is_negative(&self) -> bool {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    27
        self.is_negative
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    28
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    29
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    30
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    31
    pub fn is_positive(&self) -> bool {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    32
        !self.is_negative
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    33
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    34
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    35
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    36
    pub fn is_zero(&self) -> bool {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    37
        self.value == 0
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    38
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    39
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    40
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    41
    pub fn abs(&self) -> Self {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    42
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    43
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    44
            value: self.value,
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
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    47
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    48
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    49
    pub fn round(&self) -> i64 {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    50
        if self.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    51
            -((self.value >> 32) as i64)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    52
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    53
            (self.value >> 32) as i64
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    54
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    55
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    56
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
    57
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    58
    pub fn sqr(&self) -> Self {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    59
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    60
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    61
            value: ((self.value as u128).pow(2) >> 32) as u64,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    62
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    63
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    64
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    65
    pub fn sqrt(&self) -> Self {
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    66
        debug_assert!(!self.is_negative);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    67
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    68
        let mut t: u64 = 0x4000000000000000;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    69
        let mut r: u64 = 0;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    70
        let mut q = self.value;
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
        for _ in 0..32 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    73
            let s = r + t;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    74
            r >>= 1;
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
            if s <= q {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    77
                q -= s;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    78
                r += t;
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
            t >>= 2;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    81
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    82
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    83
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    84
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    85
            value: r << 16,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    86
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    87
    }
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
    88
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
    89
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    90
    pub fn with_sign(&self, is_negative: bool) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
    91
        FPNum{ is_negative, ..*self}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
    92
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
    93
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
    94
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
    95
    pub fn with_sign_as(self, other: FPNum) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
    96
        self.with_sign(other.is_negative)
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
    97
    }
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
    98
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
    99
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   100
    pub fn point(self) -> FPPoint {
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   101
        FPPoint::new(self, self)
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   102
    }
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   103
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   104
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   105
impl From<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   106
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   107
    fn from(n: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   108
        FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   109
            is_negative: n < 0,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   110
            value: (n.abs() as u64) << 32,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   111
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   112
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   113
}
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
impl From<u32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   116
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   117
    fn from(n: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   118
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   119
            is_negative: false,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   120
            value: (n as u64) << 32,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   121
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   122
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   123
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   124
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   125
impl From<FPNum> for f64 {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   126
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   127
    fn from(n: FPNum) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   128
        if n.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   129
            n.value as f64 / (-0x10000000 as f64)
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   130
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   131
            n.value as f64 / 0x10000000 as f64
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 PartialEq for FPNum {
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
   137
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   138
    fn eq(&self, other: &Self) -> bool {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   139
        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
   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
impl Eq for FPNum {}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   144
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   145
impl PartialOrd for FPNum {
13909
cd39e87d7a80 inline more operators in fpnum
unc0rr
parents: 13908
diff changeset
   146
    #[inline]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   147
    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
   148
        Some(self.cmp(rhs))
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   149
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   150
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   151
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   152
impl Ord for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   153
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   154
    fn cmp(&self, rhs: &Self) -> cmp::Ordering {
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   155
        #[inline]
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   156
        fn extend(n: &FPNum) -> i128 {
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   157
            if n.is_negative {
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   158
                -(n.value as i128)
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   159
            } else {
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   160
                n.value as i128
13905
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
        }
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   163
        extend(self).cmp(&(extend(rhs)))
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   164
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   165
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   166
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   167
impl ops::Add for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   168
    type Output = Self;
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
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   171
    fn add(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   172
        if self.is_negative == rhs.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   173
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   174
                is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   175
                value: self.value + rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   176
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   177
        } else if self.value > rhs.value {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   178
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   179
                is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   180
                value: self.value - rhs.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   181
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   182
        } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   183
            Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   184
                is_negative: rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   185
                value: rhs.value - self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   186
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   187
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   188
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   189
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   190
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   191
impl ops::Sub for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   192
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   193
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   194
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   195
    fn sub(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   196
        if self.is_negative == rhs.is_negative {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   197
            if self.value > rhs.value {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   198
                Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   199
                    is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   200
                    value: self.value - rhs.value,
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
            } else {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   203
                Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   204
                    is_negative: !rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   205
                    value: rhs.value - self.value,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   206
                }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   207
            }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   208
        } else {
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
        }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   214
    }
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   215
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   216
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   217
impl ops::Neg for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   218
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   219
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   220
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   221
    fn neg(self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   222
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   223
            is_negative: !self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   224
            value: self.value,
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   229
impl ops::Mul for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   230
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   231
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   232
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   233
    fn mul(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   234
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   235
            is_negative: self.is_negative ^ rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   236
            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
   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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   241
impl ops::Mul<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   242
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   243
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   244
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   245
    fn mul(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   246
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   247
            is_negative: self.is_negative ^ (rhs < 0),
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   248
            value: self.value * rhs.abs() as u64,
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   253
impl ops::Div for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   254
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   255
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   256
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   257
    fn div(self, rhs: Self) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   258
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   259
            is_negative: self.is_negative ^ rhs.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   260
            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
   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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   265
impl ops::Div<i32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   266
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   267
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   268
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   269
    fn div(self, rhs: i32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   270
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   271
            is_negative: self.is_negative ^ (rhs < 0),
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   272
            value: self.value / rhs.abs() as u64,
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   277
impl ops::Div<u32> for FPNum {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   278
    type Output = Self;
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   279
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   280
    #[inline]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   281
    fn div(self, rhs: u32) -> Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   282
        Self {
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   283
            is_negative: self.is_negative,
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   284
            value: self.value / rhs as u64,
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
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   289
#[macro_export]
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   290
macro_rules! fp {
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   291
    (-$n: tt / $d: tt) => { FPNum::new(-$n, $d) };
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   292
    ($n: tt / $d: tt) => { FPNum::new($n, $d) };
13926
fed924791e06 add extra rule for negative integers
alfadur
parents: 13917
diff changeset
   293
    (-$n: tt) => { FPNum::from(-$n) };
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   294
    ($n: tt) => { FPNum::from($n) };
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   295
}
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   296
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   297
const LINEARIZE_TRESHOLD: u64 = 0x1_0000;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   298
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   299
#[derive(Clone, Copy, Debug)]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   300
pub struct FPPoint {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   301
    x_is_negative: bool,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   302
    y_is_negative: bool,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   303
    x_value: u64,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   304
    y_value: u64,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   305
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   306
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   307
impl FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   308
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   309
    pub fn new(x: FPNum, y: FPNum) -> Self {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   310
        Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   311
            x_is_negative: x.is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   312
            y_is_negative: y.is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   313
            x_value: x.value,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   314
            y_value: y.value
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   315
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   316
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   317
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   318
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   319
    pub fn zero() -> FPPoint { FPPoint::new(fp!(0), fp!(0)) }
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   320
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   321
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   322
    pub fn unit_x() -> FPPoint { FPPoint::new(fp!(1), fp!(0)) }
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   323
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   324
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   325
    pub fn unit_y() -> FPPoint { FPPoint::new(fp!(0), fp!(1)) }
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   326
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 x(&self) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   329
        FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   330
            is_negative: self.x_is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   331
            value: self.x_value
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   332
        }
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
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   336
    pub fn y(&self) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   337
        FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   338
            is_negative: self.y_is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   339
            value: self.y_value
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   340
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   341
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   342
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   343
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   344
    pub fn max_norm(&self) -> FPNum {
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   345
        std::cmp::max(self.x(), self.y())
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   346
    }
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   347
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   348
    #[inline]
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   349
    pub fn sqr_distance(&self) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   350
        self.x().sqr() + self.y().sqr()
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   351
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   352
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   353
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   354
    pub fn distance(&self) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   355
        let r = self.x_value | self.y_value;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   356
        if r < LINEARIZE_TRESHOLD {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   357
            FPNum::from(r as u32)
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   358
        } else {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   359
            self.sqr_distance().sqrt()
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   360
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   361
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   362
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   363
    #[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   364
    pub fn is_in_range(&self, radius: FPNum) -> bool {
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   365
        self.max_norm() < radius && self.sqr_distance() < radius.sqr()
13946
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]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   369
    pub fn dot(&self, other: &FPPoint) -> FPNum {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   370
        self.x() * other.x() + self.y() * other.y()
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   371
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   372
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   373
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   374
impl PartialEq for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   375
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   376
    fn eq(&self, other: &Self) -> bool {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   377
        self.x() == other.x() && self.y() == other.y()
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
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   381
impl Eq for FPPoint {}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   382
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   383
impl ops::Neg for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   384
    type Output = Self;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   385
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   386
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   387
    fn neg(self) -> Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   388
        Self::new(-self.x(), -self.y())
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
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   392
macro_rules! bin_op_impl {
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   393
    ($op: ty, $name: tt) => {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   394
        impl $op for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   395
            type Output = Self;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   396
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   397
            #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   398
            fn $name(self, rhs: Self) -> Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   399
                Self::new(self.x().$name(rhs.x()),
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   400
                          self.y().$name(rhs.y()))
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   401
            }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   402
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   403
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   404
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   405
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   406
macro_rules! scalar_bin_op_impl {
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   407
    ($op: ty, $name: tt) => {
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   408
        impl $op for FPPoint {
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   409
            type Output = Self;
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   410
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   411
            #[inline]
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   412
            fn $name(self, rhs: FPNum) -> Self {
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   413
                Self::new(self.x().$name(rhs),
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   414
                          self.y().$name(rhs))
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   415
            }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   416
        }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   417
    }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   418
}
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   419
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   420
bin_op_impl!(ops::Add, add);
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   421
bin_op_impl!(ops::Sub, sub);
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   422
bin_op_impl!(ops::Mul, mul);
13950
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   423
bin_op_impl!(ops::Div, div);
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   424
scalar_bin_op_impl!(ops::Add<FPNum>, add);
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   425
scalar_bin_op_impl!(ops::Mul<FPNum>, mul);
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   426
scalar_bin_op_impl!(ops::Sub<FPNum>, sub);
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   427
scalar_bin_op_impl!(ops::Div<FPNum>, div);
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   428
13950
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   429
macro_rules! bin_assign_op_impl {
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   430
    ($typ: tt, $($op: tt)::+, $name: tt, $delegate: tt) => {
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   431
        bin_assign_op_impl!($typ, $($op)::+<$typ>, $name, $delegate);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   432
    };
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   433
    ($typ: tt, $($op: tt)::+<$arg: tt>, $name: tt, $delegate: tt) => {
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   434
        impl $($op)::+<$arg> for $typ {
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   435
            #[inline]
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   436
            fn $name(&mut self, rhs: $arg) {
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   437
                *self = *self $delegate rhs;
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   438
            }
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   439
        }
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   440
    }
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   441
}
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   442
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   443
bin_assign_op_impl!(FPNum, ops::AddAssign, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   444
bin_assign_op_impl!(FPNum, ops::SubAssign, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   445
bin_assign_op_impl!(FPNum, ops::MulAssign, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   446
bin_assign_op_impl!(FPNum, ops::DivAssign, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   447
bin_assign_op_impl!(FPNum, ops::MulAssign<i32>, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   448
bin_assign_op_impl!(FPNum, ops::DivAssign<i32>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   449
bin_assign_op_impl!(FPNum, ops::DivAssign<u32>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   450
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   451
bin_assign_op_impl!(FPPoint, ops::AddAssign, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   452
bin_assign_op_impl!(FPPoint, ops::SubAssign, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   453
bin_assign_op_impl!(FPPoint, ops::MulAssign, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   454
bin_assign_op_impl!(FPPoint, ops::DivAssign, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   455
bin_assign_op_impl!(FPPoint, ops::AddAssign<FPNum>, add_assign, +);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   456
bin_assign_op_impl!(FPPoint, ops::SubAssign<FPNum>, sub_assign, -);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   457
bin_assign_op_impl!(FPPoint, ops::MulAssign<FPNum>, mul_assign, *);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   458
bin_assign_op_impl!(FPPoint, ops::DivAssign<FPNum>, div_assign, /);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   459
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   460
#[inline]
13949
5fdc41cd0841 make methods public
alfadur
parents: 13948
diff changeset
   461
pub fn distance<T>(x: T, y: T) -> FPNum
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   462
    where T: ops::Add + ops::Mul + Copy +
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   463
             From<<T as ops::Add>::Output> +
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   464
             From<<T as ops::Mul>::Output> +
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   465
             Into<FPNum>
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   466
{
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   467
    let sqr: FPNum = T::from(T::from(x * x) + T::from(y * y)).into();
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   468
    sqr.sqrt()
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   469
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   470
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   471
/* TODO:
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   472
 AngleSin
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   473
 AngleCos
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   474
*/
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   475
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   476
#[cfg(test)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   477
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   478
fn basics() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   479
    let n = fp!(15/2);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   480
    assert!(n.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   481
    assert!(!n.is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   482
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   483
    assert!(!(-n).is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   484
    assert!((-n).is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   485
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   486
    assert_eq!(-(-n), n);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   487
    assert_eq!((-n).abs(), n);
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   488
    assert_eq!(-n, fp!(-15/2));
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   489
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   490
    assert_eq!(n.round(), 7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   491
    assert_eq!((-n).round(), -7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   492
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   493
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   494
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   495
fn zero() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   496
    let z = fp!(0);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   497
    let n = fp!(15/2);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   498
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   499
    assert!(z.is_zero());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   500
    assert!(z.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   501
    assert!((-z).is_negative);
13907
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   502
    assert_eq!(n - n, z);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   503
    assert_eq!(-n + n, z);
13947
2717a5289d88 fix test
alfadur
parents: 13946
diff changeset
   504
    assert_eq!(n.with_sign_as(-n), -n);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   505
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   506
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   507
#[test]
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   508
fn ord() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   509
    let z = fp!(0);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   510
    let n1_5 = fp!(3/2);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   511
    let n2_25 = fp!(9/4);
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   512
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   513
    assert!(!(z > z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   514
    assert!(!(z < z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   515
    assert!(n2_25 > n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   516
    assert!(-n2_25 < n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   517
    assert!(-n2_25 < -n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   518
}
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   519
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   520
#[test]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   521
fn arith() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   522
    let n1_5 = fp!(3/2);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   523
    let n2_25 = fp!(9/4);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   524
    let n_0_15 = fp!(-15/100);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   525
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   526
    assert_eq!(n1_5 + n1_5, fp!(3));
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   527
    assert_eq!(-n1_5 - n1_5, fp!(-3));
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   528
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   529
    assert_eq!(n1_5 * n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   530
    assert_eq!(-n1_5 * -n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   531
    assert_eq!(n1_5 * -n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   532
    assert_eq!(-n1_5 * n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   533
13907
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   534
    assert_eq!(-n2_25 / -n1_5, n1_5);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   535
    assert_eq!(n1_5 / -10, n_0_15);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   536
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   537
    assert_eq!(n1_5.sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   538
    assert_eq!((-n1_5).sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   539
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   540
    assert_eq!(n2_25.sqrt(), n1_5);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   541
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   542
    assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25);
13950
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   543
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   544
    let mut m = fp!(1);
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   545
    m += n1_5;
ff77c9920007 add opassign implementations
alfadur
parents: 13949
diff changeset
   546
    assert_eq!(m, fp!(5/2));
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   547
}
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   548
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   549
#[test]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   550
fn point() {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   551
    let z = FPPoint::zero();
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   552
    let p = FPPoint::new(fp!(1), fp!(-2));
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   553
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   554
    assert_eq!(p.sqr_distance(), fp!(5));
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   555
    assert_eq!(p + -p, FPPoint::zero());
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   556
    assert_eq!(p * z, z);
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   557
    assert_eq!(p.dot(&z), fp!(0));
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   558
    assert_eq!(distance(4, 3), fp!(5));
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   559
    assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   560
}