author | alfadur |
Thu, 04 Jul 2019 17:59:37 +0300 | |
changeset 15211 | 924f7e38815e |
parent 14701 | 5e2c892b0222 |
child 15212 | 293250953317 |
permissions | -rw-r--r-- |
14627 | 1 |
use std::{cmp, ops, ops::Shl}; |
13882 | 2 |
|
15211 | 3 |
const POSITIVE_MASK: u64 = 0x0000_0000_0000_0000; |
4 |
const NEGATIVE_MASK: u64 = 0xFFFF_FFFF_FFFF_FFFF; |
|
5 |
||
6 |
#[inline] |
|
7 |
fn bool_mask(is_negative: bool) -> u64 { |
|
8 |
if is_negative { |
|
9 |
NEGATIVE_MASK |
|
10 |
} else { |
|
11 |
POSITIVE_MASK |
|
12 |
} |
|
13 |
} |
|
14 |
||
13882 | 15 |
#[derive(Clone, Debug, Copy)] |
16 |
pub struct FPNum { |
|
15211 | 17 |
sign_mask: u64, |
13882 | 18 |
value: u64, |
19 |
} |
|
20 |
||
21 |
impl FPNum { |
|
13927 | 22 |
#[inline] |
13928 | 23 |
pub fn new(numerator: i32, denominator: u32) -> Self { |
13882 | 24 |
FPNum::from(numerator) / denominator |
25 |
} |
|
26 |
||
13885 | 27 |
#[inline] |
13928 | 28 |
pub fn signum(&self) -> i8 { |
15211 | 29 |
(1u8 ^ self.sign_mask as u8).wrapping_sub(self.sign_mask as u8) as i8 |
13882 | 30 |
} |
31 |
||
13885 | 32 |
#[inline] |
14627 | 33 |
pub const fn is_negative(&self) -> bool { |
15211 | 34 |
self.sign_mask != POSITIVE_MASK |
13882 | 35 |
} |
36 |
||
13885 | 37 |
#[inline] |
14627 | 38 |
pub const fn is_positive(&self) -> bool { |
15211 | 39 |
self.sign_mask == POSITIVE_MASK |
13882 | 40 |
} |
41 |
||
13885 | 42 |
#[inline] |
14627 | 43 |
pub const fn is_zero(&self) -> bool { |
13882 | 44 |
self.value == 0 |
45 |
} |
|
46 |
||
13885 | 47 |
#[inline] |
14627 | 48 |
pub const fn abs(&self) -> Self { |
13882 | 49 |
Self { |
15211 | 50 |
sign_mask: POSITIVE_MASK, |
13882 | 51 |
value: self.value, |
52 |
} |
|
53 |
} |
|
54 |
||
13885 | 55 |
#[inline] |
14139 | 56 |
pub fn round(&self) -> i32 { |
15211 | 57 |
((self.value >> 32) as i32 ^ self.sign_mask as i32).wrapping_sub(self.sign_mask as i32) |
13882 | 58 |
} |
59 |
||
13885 | 60 |
#[inline] |
14627 | 61 |
pub const fn abs_round(&self) -> u32 { |
14081 | 62 |
(self.value >> 32) as u32 |
63 |
} |
|
64 |
||
65 |
#[inline] |
|
13928 | 66 |
pub fn sqr(&self) -> Self { |
13882 | 67 |
Self { |
15211 | 68 |
sign_mask: 0, |
13882 | 69 |
value: ((self.value as u128).pow(2) >> 32) as u64, |
70 |
} |
|
71 |
} |
|
72 |
||
13928 | 73 |
pub fn sqrt(&self) -> Self { |
15211 | 74 |
debug_assert!(self.is_positive()); |
13882 | 75 |
|
15211 | 76 |
let mut t: u64 = 0x4000_0000_0000_0000; |
13882 | 77 |
let mut r: u64 = 0; |
78 |
let mut q = self.value; |
|
79 |
||
80 |
for _ in 0..32 { |
|
81 |
let s = r + t; |
|
82 |
r >>= 1; |
|
83 |
||
84 |
if s <= q { |
|
85 |
q -= s; |
|
86 |
r += t; |
|
87 |
} |
|
88 |
t >>= 2; |
|
89 |
} |
|
90 |
||
91 |
Self { |
|
15211 | 92 |
sign_mask: POSITIVE_MASK, |
13882 | 93 |
value: r << 16, |
94 |
} |
|
95 |
} |
|
13925 | 96 |
|
13927 | 97 |
#[inline] |
15211 | 98 |
pub fn with_sign(&self, is_negative: bool) -> FPNum { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
99 |
FPNum { |
15211 | 100 |
sign_mask: bool_mask(is_negative), |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
101 |
..*self |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
102 |
} |
13925 | 103 |
} |
104 |
||
105 |
#[inline] |
|
14627 | 106 |
pub const fn with_sign_as(self, other: FPNum) -> FPNum { |
15211 | 107 |
FPNum { |
108 |
sign_mask: other.sign_mask, |
|
109 |
..self |
|
110 |
} |
|
13925 | 111 |
} |
13927 | 112 |
|
113 |
#[inline] |
|
14627 | 114 |
pub const fn point(self) -> FPPoint { |
13927 | 115 |
FPPoint::new(self, self) |
116 |
} |
|
15211 | 117 |
|
118 |
#[inline] |
|
119 |
const fn temp_i128(self) -> i128 { |
|
120 |
((self.value ^ self.sign_mask) as u128 as i128).wrapping_sub(self.sign_mask as i128) |
|
121 |
} |
|
13882 | 122 |
} |
123 |
||
124 |
impl From<i32> for FPNum { |
|
125 |
#[inline] |
|
126 |
fn from(n: i32) -> Self { |
|
127 |
FPNum { |
|
15211 | 128 |
sign_mask: bool_mask(n < 0), |
13882 | 129 |
value: (n.abs() as u64) << 32, |
130 |
} |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
impl From<u32> for FPNum { |
|
135 |
#[inline] |
|
136 |
fn from(n: u32) -> Self { |
|
137 |
Self { |
|
15211 | 138 |
sign_mask: NEGATIVE_MASK, |
13882 | 139 |
value: (n as u64) << 32, |
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
impl From<FPNum> for f64 { |
|
145 |
#[inline] |
|
146 |
fn from(n: FPNum) -> Self { |
|
15211 | 147 |
if n.is_negative() { |
13882 | 148 |
n.value as f64 / (-0x10000000 as f64) |
149 |
} else { |
|
150 |
n.value as f64 / 0x10000000 as f64 |
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
impl PartialEq for FPNum { |
|
13885 | 156 |
#[inline] |
13882 | 157 |
fn eq(&self, other: &Self) -> bool { |
15211 | 158 |
self.value == other.value && (self.sign_mask == other.sign_mask || self.value == 0) |
13882 | 159 |
} |
160 |
} |
|
161 |
||
162 |
impl Eq for FPNum {} |
|
163 |
||
164 |
impl PartialOrd for FPNum { |
|
13885 | 165 |
#[inline] |
13882 | 166 |
fn partial_cmp(&self, rhs: &Self) -> std::option::Option<std::cmp::Ordering> { |
167 |
Some(self.cmp(rhs)) |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
impl Ord for FPNum { |
|
172 |
#[inline] |
|
173 |
fn cmp(&self, rhs: &Self) -> cmp::Ordering { |
|
15211 | 174 |
self.temp_i128().cmp(&(rhs.temp_i128())) |
13882 | 175 |
} |
176 |
} |
|
177 |
||
178 |
impl ops::Add for FPNum { |
|
179 |
type Output = Self; |
|
180 |
||
181 |
#[inline] |
|
182 |
fn add(self, rhs: Self) -> Self { |
|
15211 | 183 |
let tmp = self.temp_i128() + rhs.temp_i128(); |
184 |
let mask = bool_mask(tmp < 0); |
|
185 |
Self { |
|
186 |
sign_mask: mask, |
|
187 |
value: ((tmp as u64) ^ mask).wrapping_sub(mask), |
|
13882 | 188 |
} |
189 |
} |
|
190 |
} |
|
191 |
||
192 |
impl ops::Sub for FPNum { |
|
193 |
type Output = Self; |
|
194 |
||
195 |
#[inline] |
|
15211 | 196 |
fn sub(self, mut rhs: Self) -> Self { |
197 |
rhs.sign_mask = !rhs.sign_mask; |
|
198 |
self + rhs |
|
13882 | 199 |
} |
200 |
} |
|
201 |
||
202 |
impl ops::Neg for FPNum { |
|
203 |
type Output = Self; |
|
204 |
||
205 |
#[inline] |
|
206 |
fn neg(self) -> Self { |
|
207 |
Self { |
|
15211 | 208 |
sign_mask: !self.sign_mask, |
13882 | 209 |
value: self.value, |
210 |
} |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
impl ops::Mul for FPNum { |
|
215 |
type Output = Self; |
|
216 |
||
217 |
#[inline] |
|
218 |
fn mul(self, rhs: Self) -> Self { |
|
219 |
Self { |
|
15211 | 220 |
sign_mask: self.sign_mask ^ rhs.sign_mask, |
13882 | 221 |
value: ((self.value as u128 * rhs.value as u128) >> 32) as u64, |
222 |
} |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
impl ops::Mul<i32> for FPNum { |
|
227 |
type Output = Self; |
|
228 |
||
229 |
#[inline] |
|
230 |
fn mul(self, rhs: i32) -> Self { |
|
231 |
Self { |
|
15211 | 232 |
sign_mask: self.sign_mask ^ bool_mask(rhs < 0), |
13882 | 233 |
value: self.value * rhs.abs() as u64, |
234 |
} |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
impl ops::Div for FPNum { |
|
239 |
type Output = Self; |
|
240 |
||
241 |
#[inline] |
|
242 |
fn div(self, rhs: Self) -> Self { |
|
243 |
Self { |
|
15211 | 244 |
sign_mask: self.sign_mask ^ rhs.sign_mask, |
13882 | 245 |
value: (((self.value as u128) << 32) / rhs.value as u128) as u64, |
246 |
} |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
impl ops::Div<i32> for FPNum { |
|
251 |
type Output = Self; |
|
252 |
||
253 |
#[inline] |
|
254 |
fn div(self, rhs: i32) -> Self { |
|
255 |
Self { |
|
15211 | 256 |
sign_mask: self.sign_mask ^ bool_mask(rhs < 0), |
13882 | 257 |
value: self.value / rhs.abs() as u64, |
258 |
} |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
impl ops::Div<u32> for FPNum { |
|
263 |
type Output = Self; |
|
264 |
||
265 |
#[inline] |
|
266 |
fn div(self, rhs: u32) -> Self { |
|
267 |
Self { |
|
15211 | 268 |
sign_mask: self.sign_mask, |
13882 | 269 |
value: self.value / rhs as u64, |
270 |
} |
|
271 |
} |
|
272 |
} |
|
273 |
||
13928 | 274 |
#[macro_export] |
13891 | 275 |
macro_rules! fp { |
14701 | 276 |
($n: literal / $d: tt) => { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
277 |
FPNum::new($n, $d) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
278 |
}; |
14664 | 279 |
($n: literal) => { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
280 |
FPNum::from($n) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
281 |
}; |
13891 | 282 |
} |
283 |
||
13925 | 284 |
const LINEARIZE_TRESHOLD: u64 = 0x1_0000; |
285 |
||
286 |
#[derive(Clone, Copy, Debug)] |
|
13928 | 287 |
pub struct FPPoint { |
15211 | 288 |
x_sign_mask: u32, |
289 |
y_sign_mask: u32, |
|
13925 | 290 |
x_value: u64, |
291 |
y_value: u64, |
|
292 |
} |
|
293 |
||
294 |
impl FPPoint { |
|
295 |
#[inline] |
|
14627 | 296 |
pub const fn new(x: FPNum, y: FPNum) -> Self { |
13925 | 297 |
Self { |
15211 | 298 |
x_sign_mask: x.sign_mask as u32, |
299 |
y_sign_mask: y.sign_mask as u32, |
|
13925 | 300 |
x_value: x.value, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
301 |
y_value: y.value, |
13925 | 302 |
} |
303 |
} |
|
304 |
||
305 |
#[inline] |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
306 |
pub fn zero() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
307 |
FPPoint::new(fp!(0), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
308 |
} |
13925 | 309 |
|
310 |
#[inline] |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
311 |
pub fn unit_x() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
312 |
FPPoint::new(fp!(1), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
313 |
} |
13925 | 314 |
|
315 |
#[inline] |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
316 |
pub fn unit_y() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
317 |
FPPoint::new(fp!(0), fp!(1)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
318 |
} |
13925 | 319 |
|
320 |
#[inline] |
|
14627 | 321 |
pub const fn x(&self) -> FPNum { |
13925 | 322 |
FPNum { |
15211 | 323 |
sign_mask: self.x_sign_mask as i32 as u64, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
324 |
value: self.x_value, |
13925 | 325 |
} |
326 |
} |
|
327 |
||
328 |
#[inline] |
|
14627 | 329 |
pub const fn y(&self) -> FPNum { |
13925 | 330 |
FPNum { |
15211 | 331 |
sign_mask: self.y_sign_mask as i32 as u64, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
332 |
value: self.y_value, |
13925 | 333 |
} |
334 |
} |
|
335 |
||
336 |
#[inline] |
|
14178 | 337 |
pub fn is_zero(&self) -> bool { |
338 |
self.x().is_zero() && self.y().is_zero() |
|
339 |
} |
|
340 |
||
341 |
#[inline] |
|
13928 | 342 |
pub fn max_norm(&self) -> FPNum { |
13939 | 343 |
std::cmp::max(self.x().abs(), self.y().abs()) |
13928 | 344 |
} |
345 |
||
346 |
#[inline] |
|
347 |
pub fn sqr_distance(&self) -> FPNum { |
|
13925 | 348 |
self.x().sqr() + self.y().sqr() |
349 |
} |
|
350 |
||
351 |
#[inline] |
|
13928 | 352 |
pub fn distance(&self) -> FPNum { |
13925 | 353 |
let r = self.x_value | self.y_value; |
354 |
if r < LINEARIZE_TRESHOLD { |
|
355 |
FPNum::from(r as u32) |
|
356 |
} else { |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
357 |
let mut sqr: u128 = (self.x_value as u128).pow(2) + (self.y_value as u128).pow(2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
358 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
359 |
let mut t: u128 = 0x40000000_00000000_00000000_00000000; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
360 |
let mut r: u128 = 0; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
361 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
362 |
for _ in 0..64 { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
363 |
let s = r + t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
364 |
r >>= 1; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
365 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
366 |
if s <= sqr { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
367 |
sqr -= s; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
368 |
r += t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
369 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
370 |
t >>= 2; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
371 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
372 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
373 |
FPNum { |
15211 | 374 |
sign_mask: POSITIVE_MASK, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
375 |
value: r as u64, |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
376 |
} |
13925 | 377 |
} |
378 |
} |
|
379 |
||
380 |
#[inline] |
|
13928 | 381 |
pub fn is_in_range(&self, radius: FPNum) -> bool { |
382 |
self.max_norm() < radius && self.sqr_distance() < radius.sqr() |
|
13925 | 383 |
} |
384 |
||
385 |
#[inline] |
|
13928 | 386 |
pub fn dot(&self, other: &FPPoint) -> FPNum { |
13925 | 387 |
self.x() * other.x() + self.y() * other.y() |
388 |
} |
|
389 |
} |
|
390 |
||
391 |
impl PartialEq for FPPoint { |
|
392 |
#[inline] |
|
393 |
fn eq(&self, other: &Self) -> bool { |
|
394 |
self.x() == other.x() && self.y() == other.y() |
|
395 |
} |
|
396 |
} |
|
397 |
||
398 |
impl Eq for FPPoint {} |
|
399 |
||
400 |
impl ops::Neg for FPPoint { |
|
401 |
type Output = Self; |
|
402 |
||
403 |
#[inline] |
|
404 |
fn neg(self) -> Self { |
|
405 |
Self::new(-self.x(), -self.y()) |
|
406 |
} |
|
407 |
} |
|
408 |
||
409 |
macro_rules! bin_op_impl { |
|
13927 | 410 |
($op: ty, $name: tt) => { |
13925 | 411 |
impl $op for FPPoint { |
412 |
type Output = Self; |
|
413 |
||
414 |
#[inline] |
|
415 |
fn $name(self, rhs: Self) -> Self { |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
416 |
Self::new(self.x().$name(rhs.x()), self.y().$name(rhs.y())) |
13925 | 417 |
} |
418 |
} |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
419 |
}; |
13925 | 420 |
} |
421 |
||
13932 | 422 |
macro_rules! right_scalar_bin_op_impl { |
423 |
($($op: tt)::+, $name: tt) => { |
|
424 |
impl $($op)::+<FPNum> for FPPoint { |
|
13927 | 425 |
type Output = Self; |
426 |
||
427 |
#[inline] |
|
428 |
fn $name(self, rhs: FPNum) -> Self { |
|
429 |
Self::new(self.x().$name(rhs), |
|
430 |
self.y().$name(rhs)) |
|
431 |
} |
|
432 |
} |
|
14140 | 433 |
}; |
434 |
($($op: tt)::+<$arg: tt>, $name: tt) => { |
|
435 |
impl $($op)::+<$arg> for FPPoint { |
|
436 |
type Output = Self; |
|
437 |
||
438 |
#[inline] |
|
439 |
fn $name(self, rhs: $arg) -> Self { |
|
440 |
Self::new(self.x().$name(rhs), |
|
441 |
self.y().$name(rhs)) |
|
442 |
} |
|
443 |
} |
|
13927 | 444 |
} |
445 |
} |
|
446 |
||
13932 | 447 |
macro_rules! left_scalar_bin_op_impl { |
448 |
($($op: tt)::+, $name: tt) => { |
|
449 |
impl $($op)::+<FPPoint> for FPNum { |
|
450 |
type Output = FPPoint; |
|
451 |
||
452 |
#[inline] |
|
453 |
fn $name(self, rhs: FPPoint) -> Self::Output { |
|
454 |
Self::Output::new(self.$name(rhs.x()), |
|
455 |
self.$name(rhs.y())) |
|
456 |
} |
|
457 |
} |
|
458 |
} |
|
459 |
} |
|
460 |
||
13925 | 461 |
bin_op_impl!(ops::Add, add); |
462 |
bin_op_impl!(ops::Sub, sub); |
|
463 |
bin_op_impl!(ops::Mul, mul); |
|
13929 | 464 |
bin_op_impl!(ops::Div, div); |
13932 | 465 |
right_scalar_bin_op_impl!(ops::Add, add); |
466 |
right_scalar_bin_op_impl!(ops::Mul, mul); |
|
467 |
right_scalar_bin_op_impl!(ops::Sub, sub); |
|
468 |
right_scalar_bin_op_impl!(ops::Div, div); |
|
14140 | 469 |
right_scalar_bin_op_impl!(ops::Div<u32>, div); |
13932 | 470 |
left_scalar_bin_op_impl!(ops::Mul, mul); |
13925 | 471 |
|
13929 | 472 |
macro_rules! bin_assign_op_impl { |
473 |
($typ: tt, $($op: tt)::+, $name: tt, $delegate: tt) => { |
|
474 |
bin_assign_op_impl!($typ, $($op)::+<$typ>, $name, $delegate); |
|
475 |
}; |
|
476 |
($typ: tt, $($op: tt)::+<$arg: tt>, $name: tt, $delegate: tt) => { |
|
477 |
impl $($op)::+<$arg> for $typ { |
|
478 |
#[inline] |
|
479 |
fn $name(&mut self, rhs: $arg) { |
|
480 |
*self = *self $delegate rhs; |
|
481 |
} |
|
482 |
} |
|
483 |
} |
|
484 |
} |
|
485 |
||
486 |
bin_assign_op_impl!(FPNum, ops::AddAssign, add_assign, +); |
|
487 |
bin_assign_op_impl!(FPNum, ops::SubAssign, sub_assign, -); |
|
488 |
bin_assign_op_impl!(FPNum, ops::MulAssign, mul_assign, *); |
|
489 |
bin_assign_op_impl!(FPNum, ops::DivAssign, div_assign, /); |
|
490 |
bin_assign_op_impl!(FPNum, ops::MulAssign<i32>, mul_assign, *); |
|
491 |
bin_assign_op_impl!(FPNum, ops::DivAssign<i32>, div_assign, /); |
|
492 |
bin_assign_op_impl!(FPNum, ops::DivAssign<u32>, div_assign, /); |
|
493 |
||
494 |
bin_assign_op_impl!(FPPoint, ops::AddAssign, add_assign, +); |
|
495 |
bin_assign_op_impl!(FPPoint, ops::SubAssign, sub_assign, -); |
|
496 |
bin_assign_op_impl!(FPPoint, ops::MulAssign, mul_assign, *); |
|
497 |
bin_assign_op_impl!(FPPoint, ops::DivAssign, div_assign, /); |
|
498 |
bin_assign_op_impl!(FPPoint, ops::AddAssign<FPNum>, add_assign, +); |
|
499 |
bin_assign_op_impl!(FPPoint, ops::SubAssign<FPNum>, sub_assign, -); |
|
500 |
bin_assign_op_impl!(FPPoint, ops::MulAssign<FPNum>, mul_assign, *); |
|
501 |
bin_assign_op_impl!(FPPoint, ops::DivAssign<FPNum>, div_assign, /); |
|
502 |
||
13928 | 503 |
pub fn distance<T>(x: T, y: T) -> FPNum |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
504 |
where |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
505 |
T: Into<i64> + std::fmt::Debug, |
13925 | 506 |
{ |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
507 |
let mut sqr: u128 = (x.into().pow(2) as u128).shl(64) + (y.into().pow(2) as u128).shl(64); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
508 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
509 |
let mut t: u128 = 0x40000000_00000000_00000000_00000000; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
510 |
let mut r: u128 = 0; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
511 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
512 |
for _ in 0..64 { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
513 |
let s = r + t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
514 |
r >>= 1; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
515 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
516 |
if s <= sqr { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
517 |
sqr -= s; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
518 |
r += t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
519 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
520 |
t >>= 2; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
521 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
522 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
523 |
FPNum { |
15211 | 524 |
sign_mask: POSITIVE_MASK, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
525 |
value: r as u64, |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
526 |
} |
13925 | 527 |
} |
528 |
||
13882 | 529 |
/* TODO: |
530 |
AngleSin |
|
531 |
AngleCos |
|
532 |
*/ |
|
533 |
||
534 |
#[cfg(test)] |
|
535 |
#[test] |
|
536 |
fn basics() { |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
537 |
let n = fp!(15 / 2); |
13882 | 538 |
assert!(n.is_positive()); |
539 |
assert!(!n.is_negative()); |
|
540 |
||
541 |
assert!(!(-n).is_positive()); |
|
542 |
assert!((-n).is_negative()); |
|
543 |
||
544 |
assert_eq!(-(-n), n); |
|
545 |
assert_eq!((-n).abs(), n); |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
546 |
assert_eq!(-n, fp!(-15 / 2)); |
13882 | 547 |
|
548 |
assert_eq!(n.round(), 7); |
|
549 |
assert_eq!((-n).round(), -7); |
|
550 |
} |
|
551 |
||
552 |
#[test] |
|
553 |
fn zero() { |
|
13891 | 554 |
let z = fp!(0); |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
555 |
let n = fp!(15 / 2); |
13882 | 556 |
|
557 |
assert!(z.is_zero()); |
|
558 |
assert!(z.is_positive()); |
|
15211 | 559 |
assert!((-z).is_negative()); |
13883 | 560 |
assert_eq!(n - n, z); |
561 |
assert_eq!(-n + n, z); |
|
13926 | 562 |
assert_eq!(n.with_sign_as(-n), -n); |
13882 | 563 |
} |
564 |
||
565 |
#[test] |
|
13884 | 566 |
fn ord() { |
13891 | 567 |
let z = fp!(0); |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
568 |
let n1_5 = fp!(3 / 2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
569 |
let n2_25 = fp!(9 / 4); |
13884 | 570 |
|
571 |
assert!(!(z > z)); |
|
572 |
assert!(!(z < z)); |
|
573 |
assert!(n2_25 > n1_5); |
|
574 |
assert!(-n2_25 < n1_5); |
|
575 |
assert!(-n2_25 < -n1_5); |
|
15211 | 576 |
|
577 |
assert_eq!(n1_5.signum(), 1); |
|
578 |
assert_eq!((-n1_5).signum(), -1); |
|
13884 | 579 |
} |
580 |
||
581 |
#[test] |
|
13882 | 582 |
fn arith() { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
583 |
let n1_5 = fp!(3 / 2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
584 |
let n2_25 = fp!(9 / 4); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
585 |
let n_0_15 = fp!(-15 / 100); |
13882 | 586 |
|
13891 | 587 |
assert_eq!(n1_5 + n1_5, fp!(3)); |
588 |
assert_eq!(-n1_5 - n1_5, fp!(-3)); |
|
15211 | 589 |
assert_eq!(n1_5 - n1_5, fp!(0)); |
13882 | 590 |
|
591 |
assert_eq!(n1_5 * n1_5, n2_25); |
|
592 |
assert_eq!(-n1_5 * -n1_5, n2_25); |
|
593 |
assert_eq!(n1_5 * -n1_5, -n2_25); |
|
594 |
assert_eq!(-n1_5 * n1_5, -n2_25); |
|
595 |
||
13883 | 596 |
assert_eq!(-n2_25 / -n1_5, n1_5); |
597 |
assert_eq!(n1_5 / -10, n_0_15); |
|
598 |
||
13882 | 599 |
assert_eq!(n1_5.sqr(), n2_25); |
600 |
assert_eq!((-n1_5).sqr(), n2_25); |
|
601 |
||
602 |
assert_eq!(n2_25.sqrt(), n1_5); |
|
603 |
||
604 |
assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25); |
|
13929 | 605 |
|
606 |
let mut m = fp!(1); |
|
607 |
m += n1_5; |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
608 |
assert_eq!(m, fp!(5 / 2)); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
609 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
610 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
611 |
#[test] |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
612 |
fn test_distance_high_values() { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
613 |
assert_eq!(distance(1_000_000i32, 0), fp!(1_000_000)); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
614 |
assert_eq!( |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
615 |
FPPoint::new(fp!(1_000_000), fp!(0)).distance(), |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
616 |
fp!(1_000_000) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
617 |
); |
13882 | 618 |
} |
13925 | 619 |
|
620 |
#[test] |
|
621 |
fn point() { |
|
622 |
let z = FPPoint::zero(); |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
623 |
let n = fp!(16 / 9); |
13925 | 624 |
let p = FPPoint::new(fp!(1), fp!(-2)); |
625 |
||
626 |
assert_eq!(p.sqr_distance(), fp!(5)); |
|
627 |
assert_eq!(p + -p, FPPoint::zero()); |
|
628 |
assert_eq!(p * z, z); |
|
629 |
assert_eq!(p.dot(&z), fp!(0)); |
|
13932 | 630 |
assert_eq!(n * p, p * n); |
13925 | 631 |
assert_eq!(distance(4, 3), fp!(5)); |
13927 | 632 |
assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6))); |
13939 | 633 |
assert_eq!(p.max_norm(), fp!(2)); |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
634 |
} |