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