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