author | Wuzzy <Wuzzy2@mail.ru> |
Mon, 20 May 2019 14:59:26 +0200 | |
changeset 15019 | f877fbeaee6c |
parent 14706 | 5e2c892b0222 |
child 15216 | 924f7e38815e |
permissions | -rw-r--r-- |
14632 | 1 |
use std::{cmp, ops, ops::Shl}; |
13887 | 2 |
|
3 |
#[derive(Clone, Debug, Copy)] |
|
4 |
pub struct FPNum { |
|
5 |
is_negative: bool, |
|
6 |
value: u64, |
|
7 |
} |
|
8 |
||
9 |
impl FPNum { |
|
13932 | 10 |
#[inline] |
13933 | 11 |
pub fn new(numerator: i32, denominator: u32) -> Self { |
13887 | 12 |
FPNum::from(numerator) / denominator |
13 |
} |
|
14 |
||
13890 | 15 |
#[inline] |
13933 | 16 |
pub fn signum(&self) -> i8 { |
13887 | 17 |
if self.is_negative { |
18 |
-1 |
|
19 |
} else { |
|
20 |
1 |
|
21 |
} |
|
22 |
} |
|
23 |
||
13890 | 24 |
#[inline] |
14632 | 25 |
pub const fn is_negative(&self) -> bool { |
13887 | 26 |
self.is_negative |
27 |
} |
|
28 |
||
13890 | 29 |
#[inline] |
14632 | 30 |
pub const fn is_positive(&self) -> bool { |
13887 | 31 |
!self.is_negative |
32 |
} |
|
33 |
||
13890 | 34 |
#[inline] |
14632 | 35 |
pub const fn is_zero(&self) -> bool { |
13887 | 36 |
self.value == 0 |
37 |
} |
|
38 |
||
13890 | 39 |
#[inline] |
14632 | 40 |
pub const fn abs(&self) -> Self { |
13887 | 41 |
Self { |
42 |
is_negative: false, |
|
43 |
value: self.value, |
|
44 |
} |
|
45 |
} |
|
46 |
||
13890 | 47 |
#[inline] |
14144 | 48 |
pub fn round(&self) -> i32 { |
13887 | 49 |
if self.is_negative { |
14144 | 50 |
-((self.value >> 32) as i32) |
13887 | 51 |
} else { |
14144 | 52 |
(self.value >> 32) as i32 |
13887 | 53 |
} |
54 |
} |
|
55 |
||
13890 | 56 |
#[inline] |
14632 | 57 |
pub const fn abs_round(&self) -> u32 { |
14086 | 58 |
(self.value >> 32) as u32 |
59 |
} |
|
60 |
||
61 |
#[inline] |
|
13933 | 62 |
pub fn sqr(&self) -> Self { |
13887 | 63 |
Self { |
64 |
is_negative: false, |
|
65 |
value: ((self.value as u128).pow(2) >> 32) as u64, |
|
66 |
} |
|
67 |
} |
|
68 |
||
13933 | 69 |
pub fn sqrt(&self) -> Self { |
13887 | 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 |
} |
|
13930 | 92 |
|
13932 | 93 |
#[inline] |
14632 | 94 |
pub const fn with_sign(&self, is_negative: bool) -> FPNum { |
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
95 |
FPNum { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
96 |
is_negative, |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
97 |
..*self |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
98 |
} |
13930 | 99 |
} |
100 |
||
101 |
#[inline] |
|
14632 | 102 |
pub const fn with_sign_as(self, other: FPNum) -> FPNum { |
13930 | 103 |
self.with_sign(other.is_negative) |
104 |
} |
|
13932 | 105 |
|
106 |
#[inline] |
|
14632 | 107 |
pub const fn point(self) -> FPPoint { |
13932 | 108 |
FPPoint::new(self, self) |
109 |
} |
|
13887 | 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 { |
|
13890 | 144 |
#[inline] |
13887 | 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 { |
|
13890 | 153 |
#[inline] |
13887 | 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 { |
|
13889 | 162 |
#[inline] |
163 |
fn extend(n: &FPNum) -> i128 { |
|
164 |
if n.is_negative { |
|
165 |
-(n.value as i128) |
|
13887 | 166 |
} else { |
13889 | 167 |
n.value as i128 |
13887 | 168 |
} |
169 |
} |
|
13889 | 170 |
extend(self).cmp(&(extend(rhs))) |
13887 | 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 |
||
13933 | 296 |
#[macro_export] |
13896 | 297 |
macro_rules! fp { |
14706 | 298 |
($n: literal / $d: tt) => { |
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
299 |
FPNum::new($n, $d) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
300 |
}; |
14669 | 301 |
($n: literal) => { |
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
302 |
FPNum::from($n) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
303 |
}; |
13896 | 304 |
} |
305 |
||
13930 | 306 |
const LINEARIZE_TRESHOLD: u64 = 0x1_0000; |
307 |
||
308 |
#[derive(Clone, Copy, Debug)] |
|
13933 | 309 |
pub struct FPPoint { |
13930 | 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] |
|
14632 | 318 |
pub const fn new(x: FPNum, y: FPNum) -> Self { |
13930 | 319 |
Self { |
320 |
x_is_negative: x.is_negative, |
|
321 |
y_is_negative: y.is_negative, |
|
322 |
x_value: x.value, |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
323 |
y_value: y.value, |
13930 | 324 |
} |
325 |
} |
|
326 |
||
327 |
#[inline] |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
328 |
pub fn zero() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
329 |
FPPoint::new(fp!(0), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
330 |
} |
13930 | 331 |
|
332 |
#[inline] |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
333 |
pub fn unit_x() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
334 |
FPPoint::new(fp!(1), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
335 |
} |
13930 | 336 |
|
337 |
#[inline] |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
338 |
pub fn unit_y() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
339 |
FPPoint::new(fp!(0), fp!(1)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
340 |
} |
13930 | 341 |
|
342 |
#[inline] |
|
14632 | 343 |
pub const fn x(&self) -> FPNum { |
13930 | 344 |
FPNum { |
345 |
is_negative: self.x_is_negative, |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
346 |
value: self.x_value, |
13930 | 347 |
} |
348 |
} |
|
349 |
||
350 |
#[inline] |
|
14632 | 351 |
pub const fn y(&self) -> FPNum { |
13930 | 352 |
FPNum { |
353 |
is_negative: self.y_is_negative, |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
354 |
value: self.y_value, |
13930 | 355 |
} |
356 |
} |
|
357 |
||
358 |
#[inline] |
|
14183 | 359 |
pub fn is_zero(&self) -> bool { |
360 |
self.x().is_zero() && self.y().is_zero() |
|
361 |
} |
|
362 |
||
363 |
#[inline] |
|
13933 | 364 |
pub fn max_norm(&self) -> FPNum { |
13944 | 365 |
std::cmp::max(self.x().abs(), self.y().abs()) |
13933 | 366 |
} |
367 |
||
368 |
#[inline] |
|
369 |
pub fn sqr_distance(&self) -> FPNum { |
|
13930 | 370 |
self.x().sqr() + self.y().sqr() |
371 |
} |
|
372 |
||
373 |
#[inline] |
|
13933 | 374 |
pub fn distance(&self) -> FPNum { |
13930 | 375 |
let r = self.x_value | self.y_value; |
376 |
if r < LINEARIZE_TRESHOLD { |
|
377 |
FPNum::from(r as u32) |
|
378 |
} else { |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
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:
14086
diff
changeset
|
380 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
381 |
let mut t: u128 = 0x40000000_00000000_00000000_00000000; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
382 |
let mut r: u128 = 0; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
383 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
384 |
for _ in 0..64 { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
385 |
let s = r + t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
386 |
r >>= 1; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
387 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
388 |
if s <= sqr { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
389 |
sqr -= s; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
390 |
r += t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
391 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
392 |
t >>= 2; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
393 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
394 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
395 |
FPNum { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
396 |
is_negative: false, |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
397 |
value: r as u64, |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
398 |
} |
13930 | 399 |
} |
400 |
} |
|
401 |
||
402 |
#[inline] |
|
13933 | 403 |
pub fn is_in_range(&self, radius: FPNum) -> bool { |
404 |
self.max_norm() < radius && self.sqr_distance() < radius.sqr() |
|
13930 | 405 |
} |
406 |
||
407 |
#[inline] |
|
13933 | 408 |
pub fn dot(&self, other: &FPPoint) -> FPNum { |
13930 | 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 { |
|
13932 | 432 |
($op: ty, $name: tt) => { |
13930 | 433 |
impl $op for FPPoint { |
434 |
type Output = Self; |
|
435 |
||
436 |
#[inline] |
|
437 |
fn $name(self, rhs: Self) -> Self { |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
438 |
Self::new(self.x().$name(rhs.x()), self.y().$name(rhs.y())) |
13930 | 439 |
} |
440 |
} |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
441 |
}; |
13930 | 442 |
} |
443 |
||
13937 | 444 |
macro_rules! right_scalar_bin_op_impl { |
445 |
($($op: tt)::+, $name: tt) => { |
|
446 |
impl $($op)::+<FPNum> for FPPoint { |
|
13932 | 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 |
} |
|
14145 | 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 |
} |
|
13932 | 466 |
} |
467 |
} |
|
468 |
||
13937 | 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 |
||
13930 | 483 |
bin_op_impl!(ops::Add, add); |
484 |
bin_op_impl!(ops::Sub, sub); |
|
485 |
bin_op_impl!(ops::Mul, mul); |
|
13934 | 486 |
bin_op_impl!(ops::Div, div); |
13937 | 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); |
|
14145 | 491 |
right_scalar_bin_op_impl!(ops::Div<u32>, div); |
13937 | 492 |
left_scalar_bin_op_impl!(ops::Mul, mul); |
13930 | 493 |
|
13934 | 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 |
||
13933 | 525 |
pub fn distance<T>(x: T, y: T) -> FPNum |
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
526 |
where |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
527 |
T: Into<i64> + std::fmt::Debug, |
13930 | 528 |
{ |
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
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:
14086
diff
changeset
|
530 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
531 |
let mut t: u128 = 0x40000000_00000000_00000000_00000000; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
532 |
let mut r: u128 = 0; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
533 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
534 |
for _ in 0..64 { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
535 |
let s = r + t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
536 |
r >>= 1; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
537 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
538 |
if s <= sqr { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
539 |
sqr -= s; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
540 |
r += t; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
541 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
542 |
t >>= 2; |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
543 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
544 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
545 |
FPNum { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
546 |
is_negative: false, |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
547 |
value: r as u64, |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
548 |
} |
13930 | 549 |
} |
550 |
||
13887 | 551 |
/* TODO: |
552 |
AngleSin |
|
553 |
AngleCos |
|
554 |
*/ |
|
555 |
||
556 |
#[cfg(test)] |
|
557 |
#[test] |
|
558 |
fn basics() { |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
559 |
let n = fp!(15 / 2); |
13887 | 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); |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
568 |
assert_eq!(-n, fp!(-15 / 2)); |
13887 | 569 |
|
570 |
assert_eq!(n.round(), 7); |
|
571 |
assert_eq!((-n).round(), -7); |
|
572 |
} |
|
573 |
||
574 |
#[test] |
|
575 |
fn zero() { |
|
13896 | 576 |
let z = fp!(0); |
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
577 |
let n = fp!(15 / 2); |
13887 | 578 |
|
579 |
assert!(z.is_zero()); |
|
580 |
assert!(z.is_positive()); |
|
581 |
assert!((-z).is_negative); |
|
13888 | 582 |
assert_eq!(n - n, z); |
583 |
assert_eq!(-n + n, z); |
|
13931 | 584 |
assert_eq!(n.with_sign_as(-n), -n); |
13887 | 585 |
} |
586 |
||
587 |
#[test] |
|
13889 | 588 |
fn ord() { |
13896 | 589 |
let z = fp!(0); |
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
590 |
let n1_5 = fp!(3 / 2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
591 |
let n2_25 = fp!(9 / 4); |
13889 | 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] |
|
13887 | 601 |
fn arith() { |
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
602 |
let n1_5 = fp!(3 / 2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
603 |
let n2_25 = fp!(9 / 4); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
604 |
let n_0_15 = fp!(-15 / 100); |
13887 | 605 |
|
13896 | 606 |
assert_eq!(n1_5 + n1_5, fp!(3)); |
607 |
assert_eq!(-n1_5 - n1_5, fp!(-3)); |
|
13887 | 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 |
||
13888 | 614 |
assert_eq!(-n2_25 / -n1_5, n1_5); |
615 |
assert_eq!(n1_5 / -10, n_0_15); |
|
616 |
||
13887 | 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); |
|
13934 | 623 |
|
624 |
let mut m = fp!(1); |
|
625 |
m += n1_5; |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
626 |
assert_eq!(m, fp!(5 / 2)); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
627 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
628 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
629 |
#[test] |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
630 |
fn test_distance_high_values() { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
631 |
assert_eq!(distance(1_000_000i32, 0), fp!(1_000_000)); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
632 |
assert_eq!( |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
633 |
FPPoint::new(fp!(1_000_000), fp!(0)).distance(), |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
634 |
fp!(1_000_000) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
635 |
); |
13887 | 636 |
} |
13930 | 637 |
|
638 |
#[test] |
|
639 |
fn point() { |
|
640 |
let z = FPPoint::zero(); |
|
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
641 |
let n = fp!(16 / 9); |
13930 | 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)); |
|
13937 | 648 |
assert_eq!(n * p, p * n); |
13930 | 649 |
assert_eq!(distance(4, 3), fp!(5)); |
13932 | 650 |
assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6))); |
13944 | 651 |
assert_eq!(p.max_norm(), fp!(2)); |
14127
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14086
diff
changeset
|
652 |
} |