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