author | alfadur |
Thu, 04 Jul 2019 21:40:50 +0300 | |
changeset 15234 | 517f3a1dd5c2 |
parent 15233 | 293250953317 |
child 15235 | 58a0f2a6527b |
permissions | -rw-r--r-- |
14648 | 1 |
use std::{cmp, ops, ops::Shl}; |
13905 | 2 |
|
15232 | 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 |
||
13905 | 15 |
#[derive(Clone, Debug, Copy)] |
16 |
pub struct FPNum { |
|
15232 | 17 |
sign_mask: u64, |
13905 | 18 |
value: u64, |
19 |
} |
|
20 |
||
21 |
impl FPNum { |
|
13948 | 22 |
#[inline] |
13949 | 23 |
pub fn new(numerator: i32, denominator: u32) -> Self { |
13905 | 24 |
FPNum::from(numerator) / denominator |
25 |
} |
|
26 |
||
13909 | 27 |
#[inline] |
13949 | 28 |
pub fn signum(&self) -> i8 { |
15234 | 29 |
(1u64 ^ self.sign_mask).wrapping_sub(self.sign_mask) as i8 |
13905 | 30 |
} |
31 |
||
13909 | 32 |
#[inline] |
14648 | 33 |
pub const fn is_negative(&self) -> bool { |
15232 | 34 |
self.sign_mask != POSITIVE_MASK |
13905 | 35 |
} |
36 |
||
13909 | 37 |
#[inline] |
14648 | 38 |
pub const fn is_positive(&self) -> bool { |
15232 | 39 |
self.sign_mask == POSITIVE_MASK |
13905 | 40 |
} |
41 |
||
13909 | 42 |
#[inline] |
14648 | 43 |
pub const fn is_zero(&self) -> bool { |
13905 | 44 |
self.value == 0 |
45 |
} |
|
46 |
||
13909 | 47 |
#[inline] |
14648 | 48 |
pub const fn abs(&self) -> Self { |
13905 | 49 |
Self { |
15232 | 50 |
sign_mask: POSITIVE_MASK, |
13905 | 51 |
value: self.value, |
52 |
} |
|
53 |
} |
|
54 |
||
13909 | 55 |
#[inline] |
14160 | 56 |
pub fn round(&self) -> i32 { |
15232 | 57 |
((self.value >> 32) as i32 ^ self.sign_mask as i32).wrapping_sub(self.sign_mask as i32) |
13905 | 58 |
} |
59 |
||
13909 | 60 |
#[inline] |
14648 | 61 |
pub const fn abs_round(&self) -> u32 { |
14102 | 62 |
(self.value >> 32) as u32 |
63 |
} |
|
64 |
||
65 |
#[inline] |
|
13949 | 66 |
pub fn sqr(&self) -> Self { |
13905 | 67 |
Self { |
15232 | 68 |
sign_mask: 0, |
13905 | 69 |
value: ((self.value as u128).pow(2) >> 32) as u64, |
70 |
} |
|
71 |
} |
|
72 |
||
13949 | 73 |
pub fn sqrt(&self) -> Self { |
15232 | 74 |
debug_assert!(self.is_positive()); |
13905 | 75 |
|
15232 | 76 |
let mut t: u64 = 0x4000_0000_0000_0000; |
13905 | 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 { |
|
15232 | 92 |
sign_mask: POSITIVE_MASK, |
13905 | 93 |
value: r << 16, |
94 |
} |
|
95 |
} |
|
13946 | 96 |
|
13948 | 97 |
#[inline] |
15232 | 98 |
pub fn with_sign(&self, is_negative: bool) -> FPNum { |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
99 |
FPNum { |
15232 | 100 |
sign_mask: bool_mask(is_negative), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
101 |
..*self |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
102 |
} |
13946 | 103 |
} |
104 |
||
105 |
#[inline] |
|
14648 | 106 |
pub const fn with_sign_as(self, other: FPNum) -> FPNum { |
15232 | 107 |
FPNum { |
108 |
sign_mask: other.sign_mask, |
|
109 |
..self |
|
110 |
} |
|
13946 | 111 |
} |
13948 | 112 |
|
113 |
#[inline] |
|
14648 | 114 |
pub const fn point(self) -> FPPoint { |
13948 | 115 |
FPPoint::new(self, self) |
116 |
} |
|
15232 | 117 |
|
118 |
#[inline] |
|
119 |
const fn temp_i128(self) -> i128 { |
|
15233 | 120 |
((self.value ^ self.sign_mask) as i128).wrapping_sub(self.sign_mask as i128) |
15232 | 121 |
} |
13905 | 122 |
} |
123 |
||
124 |
impl From<i32> for FPNum { |
|
125 |
#[inline] |
|
126 |
fn from(n: i32) -> Self { |
|
127 |
FPNum { |
|
15232 | 128 |
sign_mask: bool_mask(n < 0), |
13905 | 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 { |
|
15233 | 138 |
sign_mask: POSITIVE_MASK, |
13905 | 139 |
value: (n as u64) << 32, |
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
impl From<FPNum> for f64 { |
|
145 |
#[inline] |
|
146 |
fn from(n: FPNum) -> Self { |
|
15232 | 147 |
if n.is_negative() { |
13905 | 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 { |
|
13909 | 156 |
#[inline] |
13905 | 157 |
fn eq(&self, other: &Self) -> bool { |
15232 | 158 |
self.value == other.value && (self.sign_mask == other.sign_mask || self.value == 0) |
13905 | 159 |
} |
160 |
} |
|
161 |
||
162 |
impl Eq for FPNum {} |
|
163 |
||
164 |
impl PartialOrd for FPNum { |
|
13909 | 165 |
#[inline] |
13905 | 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 { |
|
15232 | 174 |
self.temp_i128().cmp(&(rhs.temp_i128())) |
13905 | 175 |
} |
176 |
} |
|
177 |
||
178 |
impl ops::Add for FPNum { |
|
179 |
type Output = Self; |
|
180 |
||
181 |
#[inline] |
|
182 |
fn add(self, rhs: Self) -> Self { |
|
15232 | 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), |
|
13905 | 188 |
} |
189 |
} |
|
190 |
} |
|
191 |
||
192 |
impl ops::Sub for FPNum { |
|
193 |
type Output = Self; |
|
194 |
||
195 |
#[inline] |
|
15232 | 196 |
fn sub(self, mut rhs: Self) -> Self { |
197 |
rhs.sign_mask = !rhs.sign_mask; |
|
198 |
self + rhs |
|
13905 | 199 |
} |
200 |
} |
|
201 |
||
202 |
impl ops::Neg for FPNum { |
|
203 |
type Output = Self; |
|
204 |
||
205 |
#[inline] |
|
206 |
fn neg(self) -> Self { |
|
207 |
Self { |
|
15232 | 208 |
sign_mask: !self.sign_mask, |
13905 | 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 { |
|
15232 | 220 |
sign_mask: self.sign_mask ^ rhs.sign_mask, |
13905 | 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 { |
|
15232 | 232 |
sign_mask: self.sign_mask ^ bool_mask(rhs < 0), |
13905 | 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 { |
|
15232 | 244 |
sign_mask: self.sign_mask ^ rhs.sign_mask, |
13905 | 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 { |
|
15232 | 256 |
sign_mask: self.sign_mask ^ bool_mask(rhs < 0), |
13905 | 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 { |
|
15232 | 268 |
sign_mask: self.sign_mask, |
13905 | 269 |
value: self.value / rhs as u64, |
270 |
} |
|
271 |
} |
|
272 |
} |
|
273 |
||
13949 | 274 |
#[macro_export] |
13911 | 275 |
macro_rules! fp { |
14722 | 276 |
($n: literal / $d: tt) => { |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
277 |
FPNum::new($n, $d) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
278 |
}; |
14685 | 279 |
($n: literal) => { |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
280 |
FPNum::from($n) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
281 |
}; |
13911 | 282 |
} |
283 |
||
13946 | 284 |
const LINEARIZE_TRESHOLD: u64 = 0x1_0000; |
285 |
||
286 |
#[derive(Clone, Copy, Debug)] |
|
13949 | 287 |
pub struct FPPoint { |
15232 | 288 |
x_sign_mask: u32, |
289 |
y_sign_mask: u32, |
|
13946 | 290 |
x_value: u64, |
291 |
y_value: u64, |
|
292 |
} |
|
293 |
||
294 |
impl FPPoint { |
|
295 |
#[inline] |
|
14648 | 296 |
pub const fn new(x: FPNum, y: FPNum) -> Self { |
13946 | 297 |
Self { |
15232 | 298 |
x_sign_mask: x.sign_mask as u32, |
299 |
y_sign_mask: y.sign_mask as u32, |
|
13946 | 300 |
x_value: x.value, |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
301 |
y_value: y.value, |
13946 | 302 |
} |
303 |
} |
|
304 |
||
305 |
#[inline] |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
306 |
pub fn zero() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
307 |
FPPoint::new(fp!(0), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
308 |
} |
13946 | 309 |
|
310 |
#[inline] |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
311 |
pub fn unit_x() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
312 |
FPPoint::new(fp!(1), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
313 |
} |
13946 | 314 |
|
315 |
#[inline] |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
316 |
pub fn unit_y() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
317 |
FPPoint::new(fp!(0), fp!(1)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
318 |
} |
13946 | 319 |
|
320 |
#[inline] |
|
14648 | 321 |
pub const fn x(&self) -> FPNum { |
13946 | 322 |
FPNum { |
15232 | 323 |
sign_mask: self.x_sign_mask as i32 as u64, |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
324 |
value: self.x_value, |
13946 | 325 |
} |
326 |
} |
|
327 |
||
328 |
#[inline] |
|
14648 | 329 |
pub const fn y(&self) -> FPNum { |
13946 | 330 |
FPNum { |
15232 | 331 |
sign_mask: self.y_sign_mask as i32 as u64, |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
332 |
value: self.y_value, |
13946 | 333 |
} |
334 |
} |
|
335 |
||
336 |
#[inline] |
|
14199 | 337 |
pub fn is_zero(&self) -> bool { |
338 |
self.x().is_zero() && self.y().is_zero() |
|
339 |
} |
|
340 |
||
341 |
#[inline] |
|
13949 | 342 |
pub fn max_norm(&self) -> FPNum { |
13960 | 343 |
std::cmp::max(self.x().abs(), self.y().abs()) |
13949 | 344 |
} |
345 |
||
346 |
#[inline] |
|
347 |
pub fn sqr_distance(&self) -> FPNum { |
|
13946 | 348 |
self.x().sqr() + self.y().sqr() |
349 |
} |
|
350 |
||
351 |
#[inline] |
|
13949 | 352 |
pub fn distance(&self) -> FPNum { |
13946 | 353 |
let r = self.x_value | self.y_value; |
354 |
if r < LINEARIZE_TRESHOLD { |
|
355 |
FPNum::from(r as u32) |
|
356 |
} else { |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
357 |
let mut sqr: u128 = (self.x_value as u128).pow(2) + (self.y_value as u128).pow(2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
358 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
359 |
let mut t: u128 = 0x40000000_00000000_00000000_00000000; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
360 |
let mut r: u128 = 0; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
361 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
362 |
for _ in 0..64 { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
363 |
let s = r + t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
364 |
r >>= 1; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
365 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
366 |
if s <= sqr { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
367 |
sqr -= s; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
368 |
r += t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
369 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
370 |
t >>= 2; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
371 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
372 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
373 |
FPNum { |
15232 | 374 |
sign_mask: POSITIVE_MASK, |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
375 |
value: r as u64, |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
376 |
} |
13946 | 377 |
} |
378 |
} |
|
379 |
||
380 |
#[inline] |
|
13949 | 381 |
pub fn is_in_range(&self, radius: FPNum) -> bool { |
382 |
self.max_norm() < radius && self.sqr_distance() < radius.sqr() |
|
13946 | 383 |
} |
384 |
||
385 |
#[inline] |
|
13949 | 386 |
pub fn dot(&self, other: &FPPoint) -> FPNum { |
13946 | 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 { |
|
13948 | 410 |
($op: ty, $name: tt) => { |
13946 | 411 |
impl $op for FPPoint { |
412 |
type Output = Self; |
|
413 |
||
414 |
#[inline] |
|
415 |
fn $name(self, rhs: Self) -> Self { |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
416 |
Self::new(self.x().$name(rhs.x()), self.y().$name(rhs.y())) |
13946 | 417 |
} |
418 |
} |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
419 |
}; |
13946 | 420 |
} |
421 |
||
13953 | 422 |
macro_rules! right_scalar_bin_op_impl { |
423 |
($($op: tt)::+, $name: tt) => { |
|
424 |
impl $($op)::+<FPNum> for FPPoint { |
|
13948 | 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 |
} |
|
14161 | 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 |
} |
|
13948 | 444 |
} |
445 |
} |
|
446 |
||
13953 | 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 |
||
13946 | 461 |
bin_op_impl!(ops::Add, add); |
462 |
bin_op_impl!(ops::Sub, sub); |
|
463 |
bin_op_impl!(ops::Mul, mul); |
|
13950 | 464 |
bin_op_impl!(ops::Div, div); |
13953 | 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); |
|
14161 | 469 |
right_scalar_bin_op_impl!(ops::Div<u32>, div); |
13953 | 470 |
left_scalar_bin_op_impl!(ops::Mul, mul); |
13946 | 471 |
|
13950 | 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 |
||
13949 | 503 |
pub fn distance<T>(x: T, y: T) -> FPNum |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
504 |
where |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
505 |
T: Into<i64> + std::fmt::Debug, |
13946 | 506 |
{ |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
507 |
let mut sqr: u128 = (x.into().pow(2) as u128).shl(64) + (y.into().pow(2) as u128).shl(64); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
508 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
509 |
let mut t: u128 = 0x40000000_00000000_00000000_00000000; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
510 |
let mut r: u128 = 0; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
511 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
512 |
for _ in 0..64 { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
513 |
let s = r + t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
514 |
r >>= 1; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
515 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
516 |
if s <= sqr { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
517 |
sqr -= s; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
518 |
r += t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
519 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
520 |
t >>= 2; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
521 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
522 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
523 |
FPNum { |
15232 | 524 |
sign_mask: POSITIVE_MASK, |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
525 |
value: r as u64, |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
526 |
} |
13946 | 527 |
} |
528 |
||
13905 | 529 |
/* TODO: |
530 |
AngleSin |
|
531 |
AngleCos |
|
532 |
*/ |
|
533 |
||
534 |
#[cfg(test)] |
|
535 |
#[test] |
|
536 |
fn basics() { |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
537 |
let n = fp!(15 / 2); |
13905 | 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); |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
546 |
assert_eq!(-n, fp!(-15 / 2)); |
13905 | 547 |
|
548 |
assert_eq!(n.round(), 7); |
|
549 |
assert_eq!((-n).round(), -7); |
|
550 |
} |
|
551 |
||
552 |
#[test] |
|
553 |
fn zero() { |
|
13911 | 554 |
let z = fp!(0); |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
555 |
let n = fp!(15 / 2); |
13905 | 556 |
|
557 |
assert!(z.is_zero()); |
|
558 |
assert!(z.is_positive()); |
|
15232 | 559 |
assert!((-z).is_negative()); |
13907 | 560 |
assert_eq!(n - n, z); |
561 |
assert_eq!(-n + n, z); |
|
13947 | 562 |
assert_eq!(n.with_sign_as(-n), -n); |
13905 | 563 |
} |
564 |
||
565 |
#[test] |
|
13908 | 566 |
fn ord() { |
13911 | 567 |
let z = fp!(0); |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
568 |
let n1_5 = fp!(3 / 2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
569 |
let n2_25 = fp!(9 / 4); |
13908 | 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); |
|
15232 | 576 |
|
577 |
assert_eq!(n1_5.signum(), 1); |
|
578 |
assert_eq!((-n1_5).signum(), -1); |
|
13908 | 579 |
} |
580 |
||
581 |
#[test] |
|
13905 | 582 |
fn arith() { |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
583 |
let n1_5 = fp!(3 / 2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
584 |
let n2_25 = fp!(9 / 4); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
585 |
let n_0_15 = fp!(-15 / 100); |
13905 | 586 |
|
13911 | 587 |
assert_eq!(n1_5 + n1_5, fp!(3)); |
588 |
assert_eq!(-n1_5 - n1_5, fp!(-3)); |
|
15232 | 589 |
assert_eq!(n1_5 - n1_5, fp!(0)); |
13905 | 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 |
||
13907 | 596 |
assert_eq!(-n2_25 / -n1_5, n1_5); |
597 |
assert_eq!(n1_5 / -10, n_0_15); |
|
598 |
||
13905 | 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); |
|
13950 | 605 |
|
606 |
let mut m = fp!(1); |
|
607 |
m += n1_5; |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
608 |
assert_eq!(m, fp!(5 / 2)); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
609 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
610 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
611 |
#[test] |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
612 |
fn test_distance_high_values() { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
613 |
assert_eq!(distance(1_000_000i32, 0), fp!(1_000_000)); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
614 |
assert_eq!( |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
615 |
FPPoint::new(fp!(1_000_000), fp!(0)).distance(), |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
616 |
fp!(1_000_000) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
617 |
); |
13905 | 618 |
} |
13946 | 619 |
|
620 |
#[test] |
|
621 |
fn point() { |
|
622 |
let z = FPPoint::zero(); |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
623 |
let n = fp!(16 / 9); |
13946 | 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)); |
|
13953 | 630 |
assert_eq!(n * p, p * n); |
13946 | 631 |
assert_eq!(distance(4, 3), fp!(5)); |
13948 | 632 |
assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6))); |
13960 | 633 |
assert_eq!(p.max_norm(), fp!(2)); |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
634 |
} |