author | alfadur |
Fri, 05 Jul 2019 21:16:33 +0300 | |
changeset 15214 | 58a0f2a6527b |
parent 15213 | 517f3a1dd5c2 |
child 15218 | b2c086629fb8 |
permissions | -rw-r--r-- |
14627 | 1 |
use std::{cmp, ops, ops::Shl}; |
13882 | 2 |
|
15211 | 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 |
||
13882 | 15 |
#[derive(Clone, Debug, Copy)] |
16 |
pub struct FPNum { |
|
15211 | 17 |
sign_mask: u64, |
13882 | 18 |
value: u64, |
19 |
} |
|
20 |
||
21 |
impl FPNum { |
|
13927 | 22 |
#[inline] |
13928 | 23 |
pub fn new(numerator: i32, denominator: u32) -> Self { |
13882 | 24 |
FPNum::from(numerator) / denominator |
25 |
} |
|
26 |
||
13885 | 27 |
#[inline] |
13928 | 28 |
pub fn signum(&self) -> i8 { |
15213 | 29 |
(1u64 ^ self.sign_mask).wrapping_sub(self.sign_mask) as i8 |
13882 | 30 |
} |
31 |
||
13885 | 32 |
#[inline] |
14627 | 33 |
pub const fn is_negative(&self) -> bool { |
15211 | 34 |
self.sign_mask != POSITIVE_MASK |
13882 | 35 |
} |
36 |
||
13885 | 37 |
#[inline] |
14627 | 38 |
pub const fn is_positive(&self) -> bool { |
15211 | 39 |
self.sign_mask == POSITIVE_MASK |
13882 | 40 |
} |
41 |
||
13885 | 42 |
#[inline] |
14627 | 43 |
pub const fn is_zero(&self) -> bool { |
13882 | 44 |
self.value == 0 |
45 |
} |
|
46 |
||
13885 | 47 |
#[inline] |
14627 | 48 |
pub const fn abs(&self) -> Self { |
13882 | 49 |
Self { |
15211 | 50 |
sign_mask: POSITIVE_MASK, |
13882 | 51 |
value: self.value, |
52 |
} |
|
53 |
} |
|
54 |
||
13885 | 55 |
#[inline] |
14139 | 56 |
pub fn round(&self) -> i32 { |
15211 | 57 |
((self.value >> 32) as i32 ^ self.sign_mask as i32).wrapping_sub(self.sign_mask as i32) |
13882 | 58 |
} |
59 |
||
13885 | 60 |
#[inline] |
14627 | 61 |
pub const fn abs_round(&self) -> u32 { |
14081 | 62 |
(self.value >> 32) as u32 |
63 |
} |
|
64 |
||
65 |
#[inline] |
|
13928 | 66 |
pub fn sqr(&self) -> Self { |
13882 | 67 |
Self { |
15211 | 68 |
sign_mask: 0, |
13882 | 69 |
value: ((self.value as u128).pow(2) >> 32) as u64, |
70 |
} |
|
71 |
} |
|
72 |
||
13928 | 73 |
pub fn sqrt(&self) -> Self { |
15211 | 74 |
debug_assert!(self.is_positive()); |
13882 | 75 |
|
76 |
Self { |
|
15211 | 77 |
sign_mask: POSITIVE_MASK, |
15214 | 78 |
value: integral_sqrt(self.value) << 16, |
13882 | 79 |
} |
80 |
} |
|
13925 | 81 |
|
13927 | 82 |
#[inline] |
15211 | 83 |
pub fn with_sign(&self, is_negative: bool) -> FPNum { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
84 |
FPNum { |
15211 | 85 |
sign_mask: bool_mask(is_negative), |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
86 |
..*self |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
87 |
} |
13925 | 88 |
} |
89 |
||
90 |
#[inline] |
|
14627 | 91 |
pub const fn with_sign_as(self, other: FPNum) -> FPNum { |
15211 | 92 |
FPNum { |
93 |
sign_mask: other.sign_mask, |
|
94 |
..self |
|
95 |
} |
|
13925 | 96 |
} |
13927 | 97 |
|
98 |
#[inline] |
|
14627 | 99 |
pub const fn point(self) -> FPPoint { |
13927 | 100 |
FPPoint::new(self, self) |
101 |
} |
|
15211 | 102 |
|
103 |
#[inline] |
|
104 |
const fn temp_i128(self) -> i128 { |
|
15212 | 105 |
((self.value ^ self.sign_mask) as i128).wrapping_sub(self.sign_mask as i128) |
15211 | 106 |
} |
13882 | 107 |
} |
108 |
||
109 |
impl From<i32> for FPNum { |
|
110 |
#[inline] |
|
111 |
fn from(n: i32) -> Self { |
|
112 |
FPNum { |
|
15211 | 113 |
sign_mask: bool_mask(n < 0), |
13882 | 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 { |
|
15212 | 123 |
sign_mask: POSITIVE_MASK, |
13882 | 124 |
value: (n as u64) << 32, |
125 |
} |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
impl From<FPNum> for f64 { |
|
130 |
#[inline] |
|
131 |
fn from(n: FPNum) -> Self { |
|
15211 | 132 |
if n.is_negative() { |
13882 | 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 { |
|
13885 | 141 |
#[inline] |
13882 | 142 |
fn eq(&self, other: &Self) -> bool { |
15211 | 143 |
self.value == other.value && (self.sign_mask == other.sign_mask || self.value == 0) |
13882 | 144 |
} |
145 |
} |
|
146 |
||
147 |
impl Eq for FPNum {} |
|
148 |
||
149 |
impl PartialOrd for FPNum { |
|
13885 | 150 |
#[inline] |
13882 | 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 { |
|
15211 | 159 |
self.temp_i128().cmp(&(rhs.temp_i128())) |
13882 | 160 |
} |
161 |
} |
|
162 |
||
163 |
impl ops::Add for FPNum { |
|
164 |
type Output = Self; |
|
165 |
||
166 |
#[inline] |
|
167 |
fn add(self, rhs: Self) -> Self { |
|
15211 | 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), |
|
13882 | 173 |
} |
174 |
} |
|
175 |
} |
|
176 |
||
177 |
impl ops::Sub for FPNum { |
|
178 |
type Output = Self; |
|
179 |
||
180 |
#[inline] |
|
15211 | 181 |
fn sub(self, mut rhs: Self) -> Self { |
182 |
rhs.sign_mask = !rhs.sign_mask; |
|
183 |
self + rhs |
|
13882 | 184 |
} |
185 |
} |
|
186 |
||
187 |
impl ops::Neg for FPNum { |
|
188 |
type Output = Self; |
|
189 |
||
190 |
#[inline] |
|
191 |
fn neg(self) -> Self { |
|
192 |
Self { |
|
15211 | 193 |
sign_mask: !self.sign_mask, |
13882 | 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 { |
|
15211 | 205 |
sign_mask: self.sign_mask ^ rhs.sign_mask, |
13882 | 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 { |
|
15211 | 217 |
sign_mask: self.sign_mask ^ bool_mask(rhs < 0), |
13882 | 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 { |
|
15211 | 229 |
sign_mask: self.sign_mask ^ rhs.sign_mask, |
13882 | 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 { |
|
15211 | 241 |
sign_mask: self.sign_mask ^ bool_mask(rhs < 0), |
13882 | 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 { |
|
15211 | 253 |
sign_mask: self.sign_mask, |
13882 | 254 |
value: self.value / rhs as u64, |
255 |
} |
|
256 |
} |
|
257 |
} |
|
258 |
||
13928 | 259 |
#[macro_export] |
13891 | 260 |
macro_rules! fp { |
14701 | 261 |
($n: literal / $d: tt) => { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
262 |
FPNum::new($n, $d) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
263 |
}; |
14664 | 264 |
($n: literal) => { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
265 |
FPNum::from($n) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
266 |
}; |
13891 | 267 |
} |
268 |
||
13925 | 269 |
const LINEARIZE_TRESHOLD: u64 = 0x1_0000; |
270 |
||
271 |
#[derive(Clone, Copy, Debug)] |
|
13928 | 272 |
pub struct FPPoint { |
15211 | 273 |
x_sign_mask: u32, |
274 |
y_sign_mask: u32, |
|
13925 | 275 |
x_value: u64, |
276 |
y_value: u64, |
|
277 |
} |
|
278 |
||
279 |
impl FPPoint { |
|
280 |
#[inline] |
|
14627 | 281 |
pub const fn new(x: FPNum, y: FPNum) -> Self { |
13925 | 282 |
Self { |
15211 | 283 |
x_sign_mask: x.sign_mask as u32, |
284 |
y_sign_mask: y.sign_mask as u32, |
|
13925 | 285 |
x_value: x.value, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
286 |
y_value: y.value, |
13925 | 287 |
} |
288 |
} |
|
289 |
||
290 |
#[inline] |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
291 |
pub fn zero() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
292 |
FPPoint::new(fp!(0), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
293 |
} |
13925 | 294 |
|
295 |
#[inline] |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
296 |
pub fn unit_x() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
297 |
FPPoint::new(fp!(1), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
298 |
} |
13925 | 299 |
|
300 |
#[inline] |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
301 |
pub fn unit_y() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
302 |
FPPoint::new(fp!(0), fp!(1)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
303 |
} |
13925 | 304 |
|
305 |
#[inline] |
|
14627 | 306 |
pub const fn x(&self) -> FPNum { |
13925 | 307 |
FPNum { |
15211 | 308 |
sign_mask: self.x_sign_mask as i32 as u64, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
309 |
value: self.x_value, |
13925 | 310 |
} |
311 |
} |
|
312 |
||
313 |
#[inline] |
|
14627 | 314 |
pub const fn y(&self) -> FPNum { |
13925 | 315 |
FPNum { |
15211 | 316 |
sign_mask: self.y_sign_mask as i32 as u64, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
317 |
value: self.y_value, |
13925 | 318 |
} |
319 |
} |
|
320 |
||
321 |
#[inline] |
|
14178 | 322 |
pub fn is_zero(&self) -> bool { |
323 |
self.x().is_zero() && self.y().is_zero() |
|
324 |
} |
|
325 |
||
326 |
#[inline] |
|
13928 | 327 |
pub fn max_norm(&self) -> FPNum { |
13939 | 328 |
std::cmp::max(self.x().abs(), self.y().abs()) |
13928 | 329 |
} |
330 |
||
331 |
#[inline] |
|
332 |
pub fn sqr_distance(&self) -> FPNum { |
|
13925 | 333 |
self.x().sqr() + self.y().sqr() |
334 |
} |
|
335 |
||
336 |
#[inline] |
|
13928 | 337 |
pub fn distance(&self) -> FPNum { |
13925 | 338 |
let r = self.x_value | self.y_value; |
339 |
if r < LINEARIZE_TRESHOLD { |
|
340 |
FPNum::from(r as u32) |
|
341 |
} else { |
|
15214 | 342 |
let sqr: u128 = (self.x_value as u128).pow(2) + (self.y_value as u128).pow(2); |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
343 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
344 |
FPNum { |
15211 | 345 |
sign_mask: POSITIVE_MASK, |
15214 | 346 |
value: integral_sqrt_ext(sqr) as u64, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
347 |
} |
13925 | 348 |
} |
349 |
} |
|
350 |
||
351 |
#[inline] |
|
13928 | 352 |
pub fn is_in_range(&self, radius: FPNum) -> bool { |
353 |
self.max_norm() < radius && self.sqr_distance() < radius.sqr() |
|
13925 | 354 |
} |
355 |
||
356 |
#[inline] |
|
13928 | 357 |
pub fn dot(&self, other: &FPPoint) -> FPNum { |
13925 | 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 { |
|
13927 | 381 |
($op: ty, $name: tt) => { |
13925 | 382 |
impl $op for FPPoint { |
383 |
type Output = Self; |
|
384 |
||
385 |
#[inline] |
|
386 |
fn $name(self, rhs: Self) -> Self { |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
387 |
Self::new(self.x().$name(rhs.x()), self.y().$name(rhs.y())) |
13925 | 388 |
} |
389 |
} |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
390 |
}; |
13925 | 391 |
} |
392 |
||
13932 | 393 |
macro_rules! right_scalar_bin_op_impl { |
394 |
($($op: tt)::+, $name: tt) => { |
|
395 |
impl $($op)::+<FPNum> for FPPoint { |
|
13927 | 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 |
} |
|
14140 | 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 |
} |
|
13927 | 415 |
} |
416 |
} |
|
417 |
||
13932 | 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 |
||
13925 | 432 |
bin_op_impl!(ops::Add, add); |
433 |
bin_op_impl!(ops::Sub, sub); |
|
434 |
bin_op_impl!(ops::Mul, mul); |
|
13929 | 435 |
bin_op_impl!(ops::Div, div); |
13932 | 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); |
|
14140 | 440 |
right_scalar_bin_op_impl!(ops::Div<u32>, div); |
13932 | 441 |
left_scalar_bin_op_impl!(ops::Mul, mul); |
13925 | 442 |
|
13929 | 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 |
||
15214 | 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 |
||
13928 | 509 |
pub fn distance<T>(x: T, y: T) -> FPNum |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
510 |
where |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
511 |
T: Into<i64> + std::fmt::Debug, |
13925 | 512 |
{ |
15214 | 513 |
let sqr: u128 = (x.into().pow(2) as u128).shl(64) + (y.into().pow(2) as u128).shl(64); |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
514 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
515 |
FPNum { |
15211 | 516 |
sign_mask: POSITIVE_MASK, |
15214 | 517 |
value: integral_sqrt_ext(sqr) as u64, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
518 |
} |
13925 | 519 |
} |
520 |
||
13882 | 521 |
/* TODO: |
522 |
AngleSin |
|
523 |
AngleCos |
|
524 |
*/ |
|
525 |
||
526 |
#[cfg(test)] |
|
527 |
#[test] |
|
528 |
fn basics() { |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
529 |
let n = fp!(15 / 2); |
13882 | 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); |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
538 |
assert_eq!(-n, fp!(-15 / 2)); |
13882 | 539 |
|
540 |
assert_eq!(n.round(), 7); |
|
541 |
assert_eq!((-n).round(), -7); |
|
542 |
} |
|
543 |
||
544 |
#[test] |
|
545 |
fn zero() { |
|
13891 | 546 |
let z = fp!(0); |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
547 |
let n = fp!(15 / 2); |
13882 | 548 |
|
549 |
assert!(z.is_zero()); |
|
550 |
assert!(z.is_positive()); |
|
15211 | 551 |
assert!((-z).is_negative()); |
13883 | 552 |
assert_eq!(n - n, z); |
553 |
assert_eq!(-n + n, z); |
|
13926 | 554 |
assert_eq!(n.with_sign_as(-n), -n); |
13882 | 555 |
} |
556 |
||
557 |
#[test] |
|
13884 | 558 |
fn ord() { |
13891 | 559 |
let z = fp!(0); |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
560 |
let n1_5 = fp!(3 / 2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
561 |
let n2_25 = fp!(9 / 4); |
13884 | 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); |
|
15211 | 568 |
|
569 |
assert_eq!(n1_5.signum(), 1); |
|
570 |
assert_eq!((-n1_5).signum(), -1); |
|
13884 | 571 |
} |
572 |
||
573 |
#[test] |
|
13882 | 574 |
fn arith() { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
575 |
let n1_5 = fp!(3 / 2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
576 |
let n2_25 = fp!(9 / 4); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
577 |
let n_0_15 = fp!(-15 / 100); |
13882 | 578 |
|
13891 | 579 |
assert_eq!(n1_5 + n1_5, fp!(3)); |
580 |
assert_eq!(-n1_5 - n1_5, fp!(-3)); |
|
15211 | 581 |
assert_eq!(n1_5 - n1_5, fp!(0)); |
13882 | 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 |
||
13883 | 588 |
assert_eq!(-n2_25 / -n1_5, n1_5); |
589 |
assert_eq!(n1_5 / -10, n_0_15); |
|
590 |
||
13882 | 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); |
|
13929 | 597 |
|
598 |
let mut m = fp!(1); |
|
599 |
m += n1_5; |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
600 |
assert_eq!(m, fp!(5 / 2)); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
601 |
} |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
602 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
603 |
#[test] |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
604 |
fn test_distance_high_values() { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
605 |
assert_eq!(distance(1_000_000i32, 0), fp!(1_000_000)); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
606 |
assert_eq!( |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
607 |
FPPoint::new(fp!(1_000_000), fp!(0)).distance(), |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
608 |
fp!(1_000_000) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
609 |
); |
13882 | 610 |
} |
13925 | 611 |
|
612 |
#[test] |
|
613 |
fn point() { |
|
614 |
let z = FPPoint::zero(); |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
615 |
let n = fp!(16 / 9); |
13925 | 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)); |
|
13932 | 622 |
assert_eq!(n * p, p * n); |
13925 | 623 |
assert_eq!(distance(4, 3), fp!(5)); |
13927 | 624 |
assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6))); |
13939 | 625 |
assert_eq!(p.max_norm(), fp!(2)); |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
626 |
} |