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