13905
|
1 |
use std::cmp;
|
|
2 |
use std::ops;
|
|
3 |
|
|
4 |
#[derive(Clone, Debug, Copy)]
|
|
5 |
pub struct FPNum {
|
|
6 |
is_negative: bool,
|
|
7 |
value: u64,
|
|
8 |
}
|
|
9 |
|
|
10 |
impl FPNum {
|
13948
|
11 |
#[inline]
|
13905
|
12 |
fn new(numerator: i32, denominator: u32) -> Self {
|
|
13 |
FPNum::from(numerator) / denominator
|
|
14 |
}
|
|
15 |
|
13909
|
16 |
#[inline]
|
13905
|
17 |
fn signum(&self) -> i8 {
|
|
18 |
if self.is_negative {
|
|
19 |
-1
|
|
20 |
} else {
|
|
21 |
1
|
|
22 |
}
|
|
23 |
}
|
|
24 |
|
13909
|
25 |
#[inline]
|
13905
|
26 |
fn is_negative(&self) -> bool {
|
|
27 |
self.is_negative
|
|
28 |
}
|
|
29 |
|
13909
|
30 |
#[inline]
|
13905
|
31 |
fn is_positive(&self) -> bool {
|
|
32 |
!self.is_negative
|
|
33 |
}
|
|
34 |
|
13909
|
35 |
#[inline]
|
13905
|
36 |
fn is_zero(&self) -> bool {
|
|
37 |
self.value == 0
|
|
38 |
}
|
|
39 |
|
13909
|
40 |
#[inline]
|
13905
|
41 |
fn abs(&self) -> Self {
|
|
42 |
Self {
|
|
43 |
is_negative: false,
|
|
44 |
value: self.value,
|
|
45 |
}
|
|
46 |
}
|
|
47 |
|
13909
|
48 |
#[inline]
|
13905
|
49 |
fn round(&self) -> i64 {
|
|
50 |
if self.is_negative {
|
|
51 |
-((self.value >> 32) as i64)
|
|
52 |
} else {
|
|
53 |
(self.value >> 32) as i64
|
|
54 |
}
|
|
55 |
}
|
|
56 |
|
13909
|
57 |
#[inline]
|
13905
|
58 |
fn sqr(&self) -> Self {
|
|
59 |
Self {
|
|
60 |
is_negative: false,
|
|
61 |
value: ((self.value as u128).pow(2) >> 32) as u64,
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
fn sqrt(&self) -> Self {
|
|
66 |
debug_assert!(!self.is_negative);
|
|
67 |
|
|
68 |
let mut t: u64 = 0x4000000000000000;
|
|
69 |
let mut r: u64 = 0;
|
|
70 |
let mut q = self.value;
|
|
71 |
|
|
72 |
for _ in 0..32 {
|
|
73 |
let s = r + t;
|
|
74 |
r >>= 1;
|
|
75 |
|
|
76 |
if s <= q {
|
|
77 |
q -= s;
|
|
78 |
r += t;
|
|
79 |
}
|
|
80 |
t >>= 2;
|
|
81 |
}
|
|
82 |
|
|
83 |
Self {
|
|
84 |
is_negative: false,
|
|
85 |
value: r << 16,
|
|
86 |
}
|
|
87 |
}
|
13946
|
88 |
|
13948
|
89 |
#[inline]
|
13946
|
90 |
fn with_sign(&self, is_negative: bool) -> FPNum {
|
|
91 |
FPNum{ is_negative, ..*self}
|
|
92 |
}
|
|
93 |
|
|
94 |
#[inline]
|
|
95 |
fn with_sign_as(self, other: FPNum) -> FPNum {
|
|
96 |
self.with_sign(other.is_negative)
|
|
97 |
}
|
13948
|
98 |
|
|
99 |
#[inline]
|
|
100 |
fn point(self) -> FPPoint {
|
|
101 |
FPPoint::new(self, self)
|
|
102 |
}
|
13905
|
103 |
}
|
|
104 |
|
|
105 |
impl From<i32> for FPNum {
|
|
106 |
#[inline]
|
|
107 |
fn from(n: i32) -> Self {
|
|
108 |
FPNum {
|
|
109 |
is_negative: n < 0,
|
|
110 |
value: (n.abs() as u64) << 32,
|
|
111 |
}
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
impl From<u32> for FPNum {
|
|
116 |
#[inline]
|
|
117 |
fn from(n: u32) -> Self {
|
|
118 |
Self {
|
|
119 |
is_negative: false,
|
|
120 |
value: (n as u64) << 32,
|
|
121 |
}
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
impl From<FPNum> for f64 {
|
|
126 |
#[inline]
|
|
127 |
fn from(n: FPNum) -> Self {
|
|
128 |
if n.is_negative {
|
|
129 |
n.value as f64 / (-0x10000000 as f64)
|
|
130 |
} else {
|
|
131 |
n.value as f64 / 0x10000000 as f64
|
|
132 |
}
|
|
133 |
}
|
|
134 |
}
|
|
135 |
|
|
136 |
impl PartialEq for FPNum {
|
13909
|
137 |
#[inline]
|
13905
|
138 |
fn eq(&self, other: &Self) -> bool {
|
|
139 |
self.value == other.value && (self.is_negative == other.is_negative || self.value == 0)
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
impl Eq for FPNum {}
|
|
144 |
|
|
145 |
impl PartialOrd for FPNum {
|
13909
|
146 |
#[inline]
|
13905
|
147 |
fn partial_cmp(&self, rhs: &Self) -> std::option::Option<std::cmp::Ordering> {
|
|
148 |
Some(self.cmp(rhs))
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
impl Ord for FPNum {
|
|
153 |
#[inline]
|
|
154 |
fn cmp(&self, rhs: &Self) -> cmp::Ordering {
|
13908
|
155 |
#[inline]
|
|
156 |
fn extend(n: &FPNum) -> i128 {
|
|
157 |
if n.is_negative {
|
|
158 |
-(n.value as i128)
|
13905
|
159 |
} else {
|
13908
|
160 |
n.value as i128
|
13905
|
161 |
}
|
|
162 |
}
|
13908
|
163 |
extend(self).cmp(&(extend(rhs)))
|
13905
|
164 |
}
|
|
165 |
}
|
|
166 |
|
|
167 |
impl ops::Add for FPNum {
|
|
168 |
type Output = Self;
|
|
169 |
|
|
170 |
#[inline]
|
|
171 |
fn add(self, rhs: Self) -> Self {
|
|
172 |
if self.is_negative == rhs.is_negative {
|
|
173 |
Self {
|
|
174 |
is_negative: self.is_negative,
|
|
175 |
value: self.value + rhs.value,
|
|
176 |
}
|
|
177 |
} else if self.value > rhs.value {
|
|
178 |
Self {
|
|
179 |
is_negative: self.is_negative,
|
|
180 |
value: self.value - rhs.value,
|
|
181 |
}
|
|
182 |
} else {
|
|
183 |
Self {
|
|
184 |
is_negative: rhs.is_negative,
|
|
185 |
value: rhs.value - self.value,
|
|
186 |
}
|
|
187 |
}
|
|
188 |
}
|
|
189 |
}
|
|
190 |
|
|
191 |
impl ops::Sub for FPNum {
|
|
192 |
type Output = Self;
|
|
193 |
|
|
194 |
#[inline]
|
|
195 |
fn sub(self, rhs: Self) -> Self {
|
|
196 |
if self.is_negative == rhs.is_negative {
|
|
197 |
if self.value > rhs.value {
|
|
198 |
Self {
|
|
199 |
is_negative: self.is_negative,
|
|
200 |
value: self.value - rhs.value,
|
|
201 |
}
|
|
202 |
} else {
|
|
203 |
Self {
|
|
204 |
is_negative: !rhs.is_negative,
|
|
205 |
value: rhs.value - self.value,
|
|
206 |
}
|
|
207 |
}
|
|
208 |
} else {
|
|
209 |
Self {
|
|
210 |
is_negative: self.is_negative,
|
|
211 |
value: self.value + rhs.value,
|
|
212 |
}
|
|
213 |
}
|
|
214 |
}
|
|
215 |
}
|
|
216 |
|
|
217 |
impl ops::Neg for FPNum {
|
|
218 |
type Output = Self;
|
|
219 |
|
|
220 |
#[inline]
|
|
221 |
fn neg(self) -> Self {
|
|
222 |
Self {
|
|
223 |
is_negative: !self.is_negative,
|
|
224 |
value: self.value,
|
|
225 |
}
|
|
226 |
}
|
|
227 |
}
|
|
228 |
|
|
229 |
impl ops::Mul for FPNum {
|
|
230 |
type Output = Self;
|
|
231 |
|
|
232 |
#[inline]
|
|
233 |
fn mul(self, rhs: Self) -> Self {
|
|
234 |
Self {
|
|
235 |
is_negative: self.is_negative ^ rhs.is_negative,
|
|
236 |
value: ((self.value as u128 * rhs.value as u128) >> 32) as u64,
|
|
237 |
}
|
|
238 |
}
|
|
239 |
}
|
|
240 |
|
|
241 |
impl ops::Mul<i32> for FPNum {
|
|
242 |
type Output = Self;
|
|
243 |
|
|
244 |
#[inline]
|
|
245 |
fn mul(self, rhs: i32) -> Self {
|
|
246 |
Self {
|
|
247 |
is_negative: self.is_negative ^ (rhs < 0),
|
|
248 |
value: self.value * rhs.abs() as u64,
|
|
249 |
}
|
|
250 |
}
|
|
251 |
}
|
|
252 |
|
|
253 |
impl ops::Div for FPNum {
|
|
254 |
type Output = Self;
|
|
255 |
|
|
256 |
#[inline]
|
|
257 |
fn div(self, rhs: Self) -> Self {
|
|
258 |
Self {
|
|
259 |
is_negative: self.is_negative ^ rhs.is_negative,
|
|
260 |
value: (((self.value as u128) << 32) / rhs.value as u128) as u64,
|
|
261 |
}
|
|
262 |
}
|
|
263 |
}
|
|
264 |
|
|
265 |
impl ops::Div<i32> for FPNum {
|
|
266 |
type Output = Self;
|
|
267 |
|
|
268 |
#[inline]
|
|
269 |
fn div(self, rhs: i32) -> Self {
|
|
270 |
Self {
|
|
271 |
is_negative: self.is_negative ^ (rhs < 0),
|
|
272 |
value: self.value / rhs.abs() as u64,
|
|
273 |
}
|
|
274 |
}
|
|
275 |
}
|
|
276 |
|
|
277 |
impl ops::Div<u32> for FPNum {
|
|
278 |
type Output = Self;
|
|
279 |
|
|
280 |
#[inline]
|
|
281 |
fn div(self, rhs: u32) -> Self {
|
|
282 |
Self {
|
|
283 |
is_negative: self.is_negative,
|
|
284 |
value: self.value / rhs as u64,
|
|
285 |
}
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
13911
|
289 |
macro_rules! fp {
|
|
290 |
(-$n: tt / $d: tt) => { FPNum::new(-$n, $d) };
|
|
291 |
($n: tt / $d: tt) => { FPNum::new($n, $d) };
|
13926
|
292 |
(-$n: tt) => { FPNum::from(-$n) };
|
13911
|
293 |
($n: tt) => { FPNum::from($n) };
|
|
294 |
}
|
|
295 |
|
13946
|
296 |
const LINEARIZE_TRESHOLD: u64 = 0x1_0000;
|
|
297 |
|
|
298 |
#[derive(Clone, Copy, Debug)]
|
|
299 |
struct FPPoint {
|
|
300 |
x_is_negative: bool,
|
|
301 |
y_is_negative: bool,
|
|
302 |
x_value: u64,
|
|
303 |
y_value: u64,
|
|
304 |
}
|
|
305 |
|
|
306 |
impl FPPoint {
|
|
307 |
#[inline]
|
|
308 |
fn new(x: FPNum, y: FPNum) -> Self {
|
|
309 |
Self {
|
|
310 |
x_is_negative: x.is_negative,
|
|
311 |
y_is_negative: y.is_negative,
|
|
312 |
x_value: x.value,
|
|
313 |
y_value: y.value
|
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
#[inline]
|
|
318 |
fn zero() -> FPPoint { FPPoint::new(fp!(0), fp!(0)) }
|
|
319 |
|
|
320 |
#[inline]
|
|
321 |
fn unit_x() -> FPPoint { FPPoint::new(fp!(1), fp!(0)) }
|
|
322 |
|
|
323 |
#[inline]
|
|
324 |
fn unit_y() -> FPPoint { FPPoint::new(fp!(0), fp!(1)) }
|
|
325 |
|
|
326 |
#[inline]
|
|
327 |
fn x(&self) -> FPNum {
|
|
328 |
FPNum {
|
|
329 |
is_negative: self.x_is_negative,
|
|
330 |
value: self.x_value
|
|
331 |
}
|
|
332 |
}
|
|
333 |
|
|
334 |
#[inline]
|
|
335 |
fn y(&self) -> FPNum {
|
|
336 |
FPNum {
|
|
337 |
is_negative: self.y_is_negative,
|
|
338 |
value: self.y_value
|
|
339 |
}
|
|
340 |
}
|
|
341 |
|
|
342 |
#[inline]
|
|
343 |
fn sqr_distance(&self) -> FPNum {
|
|
344 |
self.x().sqr() + self.y().sqr()
|
|
345 |
}
|
|
346 |
|
|
347 |
#[inline]
|
|
348 |
fn distance(&self) -> FPNum {
|
|
349 |
let r = self.x_value | self.y_value;
|
|
350 |
if r < LINEARIZE_TRESHOLD {
|
|
351 |
FPNum::from(r as u32)
|
|
352 |
} else {
|
|
353 |
self.sqr_distance().sqrt()
|
|
354 |
}
|
|
355 |
}
|
|
356 |
|
|
357 |
#[inline]
|
|
358 |
fn is_in_range(&self, radius: FPNum) -> bool {
|
|
359 |
std::cmp::max(self.x(), self.y()) < radius
|
|
360 |
&& self.sqr_distance() < radius.sqr()
|
|
361 |
}
|
|
362 |
|
|
363 |
#[inline]
|
|
364 |
fn dot(&self, other: &FPPoint) -> FPNum {
|
|
365 |
self.x() * other.x() + self.y() * other.y()
|
|
366 |
}
|
|
367 |
}
|
|
368 |
|
|
369 |
impl PartialEq for FPPoint {
|
|
370 |
#[inline]
|
|
371 |
fn eq(&self, other: &Self) -> bool {
|
|
372 |
self.x() == other.x() && self.y() == other.y()
|
|
373 |
}
|
|
374 |
}
|
|
375 |
|
|
376 |
impl Eq for FPPoint {}
|
|
377 |
|
|
378 |
impl ops::Neg for FPPoint {
|
|
379 |
type Output = Self;
|
|
380 |
|
|
381 |
#[inline]
|
|
382 |
fn neg(self) -> Self {
|
|
383 |
Self::new(-self.x(), -self.y())
|
|
384 |
}
|
|
385 |
}
|
|
386 |
|
|
387 |
macro_rules! bin_op_impl {
|
13948
|
388 |
($op: ty, $name: tt) => {
|
13946
|
389 |
impl $op for FPPoint {
|
|
390 |
type Output = Self;
|
|
391 |
|
|
392 |
#[inline]
|
|
393 |
fn $name(self, rhs: Self) -> Self {
|
|
394 |
Self::new(self.x().$name(rhs.x()),
|
|
395 |
self.y().$name(rhs.y()))
|
|
396 |
}
|
|
397 |
}
|
|
398 |
}
|
|
399 |
}
|
|
400 |
|
13948
|
401 |
macro_rules! scalar_bin_op_impl {
|
|
402 |
($op: ty, $name: tt) => {
|
|
403 |
impl $op for FPPoint {
|
|
404 |
type Output = Self;
|
|
405 |
|
|
406 |
#[inline]
|
|
407 |
fn $name(self, rhs: FPNum) -> Self {
|
|
408 |
Self::new(self.x().$name(rhs),
|
|
409 |
self.y().$name(rhs))
|
|
410 |
}
|
|
411 |
}
|
|
412 |
}
|
|
413 |
}
|
|
414 |
|
13946
|
415 |
bin_op_impl!(ops::Add, add);
|
|
416 |
bin_op_impl!(ops::Sub, sub);
|
|
417 |
bin_op_impl!(ops::Mul, mul);
|
13948
|
418 |
scalar_bin_op_impl!(ops::Add<FPNum>, add);
|
|
419 |
scalar_bin_op_impl!(ops::Mul<FPNum>, mul);
|
|
420 |
scalar_bin_op_impl!(ops::Sub<FPNum>, sub);
|
|
421 |
scalar_bin_op_impl!(ops::Div<FPNum>, div);
|
13946
|
422 |
|
|
423 |
#[inline]
|
|
424 |
fn distance<T>(x: T, y: T) -> FPNum
|
|
425 |
where T: ops::Add + ops::Mul + Copy +
|
|
426 |
From<<T as ops::Add>::Output> +
|
|
427 |
From<<T as ops::Mul>::Output> +
|
|
428 |
Into<FPNum>
|
|
429 |
{
|
|
430 |
let sqr: FPNum = T::from(T::from(x * x) + T::from(y * y)).into();
|
|
431 |
sqr.sqrt()
|
|
432 |
}
|
|
433 |
|
13905
|
434 |
/* TODO:
|
|
435 |
AngleSin
|
|
436 |
AngleCos
|
|
437 |
*/
|
|
438 |
|
|
439 |
#[cfg(test)]
|
|
440 |
#[test]
|
|
441 |
fn basics() {
|
13911
|
442 |
let n = fp!(15/2);
|
13905
|
443 |
assert!(n.is_positive());
|
|
444 |
assert!(!n.is_negative());
|
|
445 |
|
|
446 |
assert!(!(-n).is_positive());
|
|
447 |
assert!((-n).is_negative());
|
|
448 |
|
|
449 |
assert_eq!(-(-n), n);
|
|
450 |
assert_eq!((-n).abs(), n);
|
13911
|
451 |
assert_eq!(-n, fp!(-15/2));
|
13905
|
452 |
|
|
453 |
assert_eq!(n.round(), 7);
|
|
454 |
assert_eq!((-n).round(), -7);
|
|
455 |
}
|
|
456 |
|
|
457 |
#[test]
|
|
458 |
fn zero() {
|
13911
|
459 |
let z = fp!(0);
|
|
460 |
let n = fp!(15/2);
|
13905
|
461 |
|
|
462 |
assert!(z.is_zero());
|
|
463 |
assert!(z.is_positive());
|
|
464 |
assert!((-z).is_negative);
|
13907
|
465 |
assert_eq!(n - n, z);
|
|
466 |
assert_eq!(-n + n, z);
|
13947
|
467 |
assert_eq!(n.with_sign_as(-n), -n);
|
13905
|
468 |
}
|
|
469 |
|
|
470 |
#[test]
|
13908
|
471 |
fn ord() {
|
13911
|
472 |
let z = fp!(0);
|
|
473 |
let n1_5 = fp!(3/2);
|
|
474 |
let n2_25 = fp!(9/4);
|
13908
|
475 |
|
|
476 |
assert!(!(z > z));
|
|
477 |
assert!(!(z < z));
|
|
478 |
assert!(n2_25 > n1_5);
|
|
479 |
assert!(-n2_25 < n1_5);
|
|
480 |
assert!(-n2_25 < -n1_5);
|
|
481 |
}
|
|
482 |
|
|
483 |
#[test]
|
13905
|
484 |
fn arith() {
|
13911
|
485 |
let n1_5 = fp!(3/2);
|
|
486 |
let n2_25 = fp!(9/4);
|
|
487 |
let n_0_15 = fp!(-15/100);
|
13905
|
488 |
|
13911
|
489 |
assert_eq!(n1_5 + n1_5, fp!(3));
|
|
490 |
assert_eq!(-n1_5 - n1_5, fp!(-3));
|
13905
|
491 |
|
|
492 |
assert_eq!(n1_5 * n1_5, n2_25);
|
|
493 |
assert_eq!(-n1_5 * -n1_5, n2_25);
|
|
494 |
assert_eq!(n1_5 * -n1_5, -n2_25);
|
|
495 |
assert_eq!(-n1_5 * n1_5, -n2_25);
|
|
496 |
|
13907
|
497 |
assert_eq!(-n2_25 / -n1_5, n1_5);
|
|
498 |
assert_eq!(n1_5 / -10, n_0_15);
|
|
499 |
|
13905
|
500 |
assert_eq!(n1_5.sqr(), n2_25);
|
|
501 |
assert_eq!((-n1_5).sqr(), n2_25);
|
|
502 |
|
|
503 |
assert_eq!(n2_25.sqrt(), n1_5);
|
|
504 |
|
|
505 |
assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25);
|
|
506 |
}
|
13946
|
507 |
|
|
508 |
#[test]
|
|
509 |
fn point() {
|
|
510 |
let z = FPPoint::zero();
|
|
511 |
let p = FPPoint::new(fp!(1), fp!(-2));
|
|
512 |
|
|
513 |
assert_eq!(p.sqr_distance(), fp!(5));
|
|
514 |
assert_eq!(p + -p, FPPoint::zero());
|
|
515 |
assert_eq!(p * z, z);
|
|
516 |
assert_eq!(p.dot(&z), fp!(0));
|
|
517 |
assert_eq!(distance(4, 3), fp!(5));
|
13948
|
518 |
assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
|
13946
|
519 |
} |