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