rust/fpnum/src/lib.rs
author alfadur
Tue, 16 Oct 2018 05:09:42 +0300
changeset 13948 cf28d7a2b7fe
parent 13947 2717a5289d88
child 13949 5fdc41cd0841
permissions -rw-r--r--
add scalar operations
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]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    12
    fn new(numerator: i32, denominator: u32) -> Self {
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]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    17
    fn signum(&self) -> i8 {
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]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    26
    fn is_negative(&self) -> bool {
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]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    31
    fn is_positive(&self) -> bool {
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]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    36
    fn is_zero(&self) -> bool {
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]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    41
    fn abs(&self) -> Self {
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]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    49
    fn round(&self) -> i64 {
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]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    58
    fn sqr(&self) -> Self {
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
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
    65
    fn sqrt(&self) -> Self {
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]
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
    90
    fn with_sign(&self, is_negative: bool) -> FPNum {
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]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
    95
    fn with_sign_as(self, other: FPNum) -> FPNum {
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]
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   100
    fn point(self) -> FPPoint {
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
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   289
macro_rules! fp {
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   290
    (-$n: tt / $d: tt) => { FPNum::new(-$n, $d) };
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   291
    ($n: tt / $d: tt) => { FPNum::new($n, $d) };
13926
fed924791e06 add extra rule for negative integers
alfadur
parents: 13917
diff changeset
   292
    (-$n: tt) => { FPNum::from(-$n) };
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   293
    ($n: tt) => { FPNum::from($n) };
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   294
}
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   295
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   296
const LINEARIZE_TRESHOLD: u64 = 0x1_0000;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   297
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   298
#[derive(Clone, Copy, Debug)]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   299
struct FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   300
    x_is_negative: bool,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   301
    y_is_negative: bool,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   302
    x_value: u64,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   303
    y_value: u64,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   304
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   305
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   306
impl FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   307
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   308
    fn new(x: FPNum, y: FPNum) -> Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   309
        Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   310
            x_is_negative: x.is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   311
            y_is_negative: y.is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   312
            x_value: x.value,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   313
            y_value: y.value
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   314
        }
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
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   318
    fn zero() -> FPPoint { FPPoint::new(fp!(0), fp!(0)) }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   319
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   320
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   321
    fn unit_x() -> FPPoint { FPPoint::new(fp!(1), fp!(0)) }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   322
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   323
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   324
    fn unit_y() -> FPPoint { FPPoint::new(fp!(0), fp!(1)) }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   325
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   326
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   327
    fn x(&self) -> FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   328
        FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   329
            is_negative: self.x_is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   330
            value: self.x_value
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   331
        }
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
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   335
    fn y(&self) -> FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   336
        FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   337
            is_negative: self.y_is_negative,
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   338
            value: self.y_value
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   339
        }
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
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   343
    fn sqr_distance(&self) -> FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   344
        self.x().sqr() + self.y().sqr()
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   345
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   346
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   347
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   348
    fn distance(&self) -> FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   349
        let r = self.x_value | self.y_value;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   350
        if r < LINEARIZE_TRESHOLD {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   351
            FPNum::from(r as u32)
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   352
        } else {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   353
            self.sqr_distance().sqrt()
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   354
        }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   355
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   356
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   357
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   358
    fn is_in_range(&self, radius: FPNum) -> bool {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   359
        std::cmp::max(self.x(), self.y()) < radius
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   360
            && self.sqr_distance() < radius.sqr()
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]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   364
    fn dot(&self, other: &FPPoint) -> FPNum {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   365
        self.x() * other.x() + self.y() * other.y()
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
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   369
impl PartialEq for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   370
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   371
    fn eq(&self, other: &Self) -> bool {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   372
        self.x() == other.x() && self.y() == other.y()
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   373
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   374
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   375
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   376
impl Eq for FPPoint {}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   377
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   378
impl ops::Neg for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   379
    type Output = Self;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   380
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   381
    #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   382
    fn neg(self) -> Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   383
        Self::new(-self.x(), -self.y())
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   384
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   385
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   386
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   387
macro_rules! bin_op_impl {
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   388
    ($op: ty, $name: tt) => {
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   389
        impl $op for FPPoint {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   390
            type Output = Self;
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   391
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   392
            #[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   393
            fn $name(self, rhs: Self) -> Self {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   394
                Self::new(self.x().$name(rhs.x()),
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   395
                          self.y().$name(rhs.y()))
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
    }
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   399
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   400
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   401
macro_rules! scalar_bin_op_impl {
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   402
    ($op: ty, $name: tt) => {
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   403
        impl $op for FPPoint {
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   404
            type Output = Self;
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   405
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   406
            #[inline]
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   407
            fn $name(self, rhs: FPNum) -> Self {
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   408
                Self::new(self.x().$name(rhs),
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   409
                          self.y().$name(rhs))
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   410
            }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   411
        }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   412
    }
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   413
}
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   414
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   415
bin_op_impl!(ops::Add, add);
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   416
bin_op_impl!(ops::Sub, sub);
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   417
bin_op_impl!(ops::Mul, mul);
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   418
scalar_bin_op_impl!(ops::Add<FPNum>, add);
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   419
scalar_bin_op_impl!(ops::Mul<FPNum>, mul);
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   420
scalar_bin_op_impl!(ops::Sub<FPNum>, sub);
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   421
scalar_bin_op_impl!(ops::Div<FPNum>, div);
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   422
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   423
#[inline]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   424
fn distance<T>(x: T, y: T) -> FPNum
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   425
    where T: ops::Add + ops::Mul + Copy +
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   426
             From<<T as ops::Add>::Output> +
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   427
             From<<T as ops::Mul>::Output> +
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   428
             Into<FPNum>
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   429
{
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   430
    let sqr: FPNum = T::from(T::from(x * x) + T::from(y * y)).into();
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   431
    sqr.sqrt()
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   432
}
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   433
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   434
/* TODO:
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   435
 AngleSin
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   436
 AngleCos
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   437
*/
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   438
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   439
#[cfg(test)]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   440
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   441
fn basics() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   442
    let n = fp!(15/2);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   443
    assert!(n.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   444
    assert!(!n.is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   445
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   446
    assert!(!(-n).is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   447
    assert!((-n).is_negative());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   448
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   449
    assert_eq!(-(-n), n);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   450
    assert_eq!((-n).abs(), n);
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   451
    assert_eq!(-n, fp!(-15/2));
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   452
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   453
    assert_eq!(n.round(), 7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   454
    assert_eq!((-n).round(), -7);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   455
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   456
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   457
#[test]
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   458
fn zero() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   459
    let z = fp!(0);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   460
    let n = fp!(15/2);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   461
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   462
    assert!(z.is_zero());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   463
    assert!(z.is_positive());
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   464
    assert!((-z).is_negative);
13907
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   465
    assert_eq!(n - n, z);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   466
    assert_eq!(-n + n, z);
13947
2717a5289d88 fix test
alfadur
parents: 13946
diff changeset
   467
    assert_eq!(n.with_sign_as(-n), -n);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   468
}
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   469
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   470
#[test]
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   471
fn ord() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   472
    let z = fp!(0);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   473
    let n1_5 = fp!(3/2);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   474
    let n2_25 = fp!(9/4);
13908
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   475
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   476
    assert!(!(z > z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   477
    assert!(!(z < z));
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   478
    assert!(n2_25 > n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   479
    assert!(-n2_25 < n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   480
    assert!(-n2_25 < -n1_5);
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   481
}
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   482
fbbb4fcd6a75 delegate cmp to rustlib
alfadur
parents: 13907
diff changeset
   483
#[test]
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   484
fn arith() {
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   485
    let n1_5 = fp!(3/2);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   486
    let n2_25 = fp!(9/4);
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   487
    let n_0_15 = fp!(-15/100);
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   488
13911
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   489
    assert_eq!(n1_5 + n1_5, fp!(3));
9ae1184886db add fpnum literal macro
alfadur
parents: 13909
diff changeset
   490
    assert_eq!(-n1_5 - n1_5, fp!(-3));
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   491
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   492
    assert_eq!(n1_5 * n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   493
    assert_eq!(-n1_5 * -n1_5, n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   494
    assert_eq!(n1_5 * -n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   495
    assert_eq!(-n1_5 * n1_5, -n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   496
13907
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   497
    assert_eq!(-n2_25 / -n1_5, n1_5);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   498
    assert_eq!(n1_5 / -10, n_0_15);
2bfd7472ef1d Add some more tests
unc0rr
parents: 13905
diff changeset
   499
13905
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   500
    assert_eq!(n1_5.sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   501
    assert_eq!((-n1_5).sqr(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   502
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   503
    assert_eq!(n2_25.sqrt(), n1_5);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   504
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   505
    assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25);
b172a5d40eee Reimplement hwFloat library in rust for future use
unc0rr
parents:
diff changeset
   506
}
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   507
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   508
#[test]
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   509
fn point() {
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   510
    let z = FPPoint::zero();
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   511
    let p = FPPoint::new(fp!(1), fp!(-2));
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   512
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   513
    assert_eq!(p.sqr_distance(), fp!(5));
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   514
    assert_eq!(p + -p, FPPoint::zero());
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   515
    assert_eq!(p * z, z);
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   516
    assert_eq!(p.dot(&z), fp!(0));
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   517
    assert_eq!(distance(4, 3), fp!(5));
13948
cf28d7a2b7fe add scalar operations
alfadur
parents: 13947
diff changeset
   518
    assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
13946
2ee07e751171 add more fpnum functions
alfadur
parents: 13926
diff changeset
   519
}