author | alfadur |
Mon, 14 Mar 2022 22:08:31 +0300 | |
changeset 15837 | 42109eb6ef51 |
parent 15219 | b71bae455926 |
child 15838 | c910381d1ea9 |
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 |
||
15218 | 73 |
#[inline] |
13928 | 74 |
pub fn sqrt(&self) -> Self { |
15211 | 75 |
debug_assert!(self.is_positive()); |
13882 | 76 |
|
77 |
Self { |
|
15211 | 78 |
sign_mask: POSITIVE_MASK, |
15214 | 79 |
value: integral_sqrt(self.value) << 16, |
13882 | 80 |
} |
81 |
} |
|
13925 | 82 |
|
13927 | 83 |
#[inline] |
15211 | 84 |
pub fn with_sign(&self, is_negative: bool) -> FPNum { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
85 |
FPNum { |
15211 | 86 |
sign_mask: bool_mask(is_negative), |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
87 |
..*self |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
88 |
} |
13925 | 89 |
} |
90 |
||
91 |
#[inline] |
|
14627 | 92 |
pub const fn with_sign_as(self, other: FPNum) -> FPNum { |
15211 | 93 |
FPNum { |
94 |
sign_mask: other.sign_mask, |
|
95 |
..self |
|
96 |
} |
|
13925 | 97 |
} |
13927 | 98 |
|
99 |
#[inline] |
|
14627 | 100 |
pub const fn point(self) -> FPPoint { |
13927 | 101 |
FPPoint::new(self, self) |
102 |
} |
|
15211 | 103 |
|
104 |
#[inline] |
|
105 |
const fn temp_i128(self) -> i128 { |
|
15212 | 106 |
((self.value ^ self.sign_mask) as i128).wrapping_sub(self.sign_mask as i128) |
15211 | 107 |
} |
13882 | 108 |
} |
109 |
||
110 |
impl From<i32> for FPNum { |
|
111 |
#[inline] |
|
112 |
fn from(n: i32) -> Self { |
|
113 |
FPNum { |
|
15211 | 114 |
sign_mask: bool_mask(n < 0), |
13882 | 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 { |
|
15212 | 124 |
sign_mask: POSITIVE_MASK, |
13882 | 125 |
value: (n as u64) << 32, |
126 |
} |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
impl From<FPNum> for f64 { |
|
131 |
#[inline] |
|
132 |
fn from(n: FPNum) -> Self { |
|
15211 | 133 |
if n.is_negative() { |
15219 | 134 |
n.value as f64 / -0x1_0000_0000i64 as f64 |
13882 | 135 |
} else { |
15219 | 136 |
n.value as f64 / 0x1_0000_0000i64 as f64 |
13882 | 137 |
} |
138 |
} |
|
139 |
} |
|
140 |
||
141 |
impl PartialEq for FPNum { |
|
13885 | 142 |
#[inline] |
13882 | 143 |
fn eq(&self, other: &Self) -> bool { |
15211 | 144 |
self.value == other.value && (self.sign_mask == other.sign_mask || self.value == 0) |
13882 | 145 |
} |
146 |
} |
|
147 |
||
148 |
impl Eq for FPNum {} |
|
149 |
||
150 |
impl PartialOrd for FPNum { |
|
13885 | 151 |
#[inline] |
13882 | 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 { |
|
15211 | 160 |
self.temp_i128().cmp(&(rhs.temp_i128())) |
13882 | 161 |
} |
162 |
} |
|
163 |
||
164 |
impl ops::Add for FPNum { |
|
165 |
type Output = Self; |
|
166 |
||
167 |
#[inline] |
|
168 |
fn add(self, rhs: Self) -> Self { |
|
15211 | 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), |
|
13882 | 174 |
} |
175 |
} |
|
176 |
} |
|
177 |
||
178 |
impl ops::Sub for FPNum { |
|
179 |
type Output = Self; |
|
180 |
||
181 |
#[inline] |
|
15211 | 182 |
fn sub(self, mut rhs: Self) -> Self { |
183 |
rhs.sign_mask = !rhs.sign_mask; |
|
184 |
self + rhs |
|
13882 | 185 |
} |
186 |
} |
|
187 |
||
188 |
impl ops::Neg for FPNum { |
|
189 |
type Output = Self; |
|
190 |
||
191 |
#[inline] |
|
192 |
fn neg(self) -> Self { |
|
193 |
Self { |
|
15211 | 194 |
sign_mask: !self.sign_mask, |
13882 | 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 { |
|
15211 | 206 |
sign_mask: self.sign_mask ^ rhs.sign_mask, |
13882 | 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 { |
|
15211 | 218 |
sign_mask: self.sign_mask ^ bool_mask(rhs < 0), |
13882 | 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 { |
|
15211 | 230 |
sign_mask: self.sign_mask ^ rhs.sign_mask, |
13882 | 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 { |
|
15211 | 242 |
sign_mask: self.sign_mask ^ bool_mask(rhs < 0), |
13882 | 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 { |
|
15211 | 254 |
sign_mask: self.sign_mask, |
13882 | 255 |
value: self.value / rhs as u64, |
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
||
13928 | 260 |
#[macro_export] |
13891 | 261 |
macro_rules! fp { |
14701 | 262 |
($n: literal / $d: tt) => { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
263 |
FPNum::new($n, $d) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
264 |
}; |
14664 | 265 |
($n: literal) => { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
266 |
FPNum::from($n) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
267 |
}; |
13891 | 268 |
} |
269 |
||
13925 | 270 |
const LINEARIZE_TRESHOLD: u64 = 0x1_0000; |
271 |
||
272 |
#[derive(Clone, Copy, Debug)] |
|
13928 | 273 |
pub struct FPPoint { |
15211 | 274 |
x_sign_mask: u32, |
275 |
y_sign_mask: u32, |
|
13925 | 276 |
x_value: u64, |
277 |
y_value: u64, |
|
278 |
} |
|
279 |
||
280 |
impl FPPoint { |
|
281 |
#[inline] |
|
14627 | 282 |
pub const fn new(x: FPNum, y: FPNum) -> Self { |
13925 | 283 |
Self { |
15211 | 284 |
x_sign_mask: x.sign_mask as u32, |
285 |
y_sign_mask: y.sign_mask as u32, |
|
13925 | 286 |
x_value: x.value, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
287 |
y_value: y.value, |
13925 | 288 |
} |
289 |
} |
|
290 |
||
291 |
#[inline] |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
292 |
pub fn zero() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
293 |
FPPoint::new(fp!(0), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
294 |
} |
13925 | 295 |
|
296 |
#[inline] |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
297 |
pub fn unit_x() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
298 |
FPPoint::new(fp!(1), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
299 |
} |
13925 | 300 |
|
301 |
#[inline] |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
302 |
pub fn unit_y() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
303 |
FPPoint::new(fp!(0), fp!(1)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
304 |
} |
13925 | 305 |
|
306 |
#[inline] |
|
14627 | 307 |
pub const fn x(&self) -> FPNum { |
13925 | 308 |
FPNum { |
15211 | 309 |
sign_mask: self.x_sign_mask as i32 as u64, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
310 |
value: self.x_value, |
13925 | 311 |
} |
312 |
} |
|
313 |
||
314 |
#[inline] |
|
14627 | 315 |
pub const fn y(&self) -> FPNum { |
13925 | 316 |
FPNum { |
15211 | 317 |
sign_mask: self.y_sign_mask as i32 as u64, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
318 |
value: self.y_value, |
13925 | 319 |
} |
320 |
} |
|
321 |
||
322 |
#[inline] |
|
14178 | 323 |
pub fn is_zero(&self) -> bool { |
324 |
self.x().is_zero() && self.y().is_zero() |
|
325 |
} |
|
326 |
||
327 |
#[inline] |
|
13928 | 328 |
pub fn max_norm(&self) -> FPNum { |
13939 | 329 |
std::cmp::max(self.x().abs(), self.y().abs()) |
13928 | 330 |
} |
331 |
||
332 |
#[inline] |
|
333 |
pub fn sqr_distance(&self) -> FPNum { |
|
13925 | 334 |
self.x().sqr() + self.y().sqr() |
335 |
} |
|
336 |
||
337 |
#[inline] |
|
13928 | 338 |
pub fn distance(&self) -> FPNum { |
13925 | 339 |
let r = self.x_value | self.y_value; |
340 |
if r < LINEARIZE_TRESHOLD { |
|
341 |
FPNum::from(r as u32) |
|
342 |
} else { |
|
15214 | 343 |
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
|
344 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
345 |
FPNum { |
15211 | 346 |
sign_mask: POSITIVE_MASK, |
15214 | 347 |
value: integral_sqrt_ext(sqr) as u64, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
348 |
} |
13925 | 349 |
} |
350 |
} |
|
351 |
||
352 |
#[inline] |
|
13928 | 353 |
pub fn is_in_range(&self, radius: FPNum) -> bool { |
354 |
self.max_norm() < radius && self.sqr_distance() < radius.sqr() |
|
13925 | 355 |
} |
356 |
||
357 |
#[inline] |
|
13928 | 358 |
pub fn dot(&self, other: &FPPoint) -> FPNum { |
13925 | 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 { |
|
13927 | 382 |
($op: ty, $name: tt) => { |
13925 | 383 |
impl $op for FPPoint { |
384 |
type Output = Self; |
|
385 |
||
386 |
#[inline] |
|
387 |
fn $name(self, rhs: Self) -> Self { |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
388 |
Self::new(self.x().$name(rhs.x()), self.y().$name(rhs.y())) |
13925 | 389 |
} |
390 |
} |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
391 |
}; |
13925 | 392 |
} |
393 |
||
13932 | 394 |
macro_rules! right_scalar_bin_op_impl { |
395 |
($($op: tt)::+, $name: tt) => { |
|
396 |
impl $($op)::+<FPNum> for FPPoint { |
|
13927 | 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 |
} |
|
14140 | 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 |
} |
|
13927 | 416 |
} |
417 |
} |
|
418 |
||
13932 | 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 |
||
13925 | 433 |
bin_op_impl!(ops::Add, add); |
434 |
bin_op_impl!(ops::Sub, sub); |
|
435 |
bin_op_impl!(ops::Mul, mul); |
|
13929 | 436 |
bin_op_impl!(ops::Div, div); |
13932 | 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); |
|
14140 | 441 |
right_scalar_bin_op_impl!(ops::Div<u32>, div); |
13932 | 442 |
left_scalar_bin_op_impl!(ops::Mul, mul); |
13925 | 443 |
|
13929 | 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 |
||
15218 | 475 |
pub fn integral_sqrt(value: u64) -> u64 { |
15837 | 476 |
let mut digits = (64u32 - 1).saturating_sub(value.leading_zeros()) & 0xFE; |
15218 | 477 |
let mut result = if value == 0 { 0u64 } else { 1u64 }; |
15214 | 478 |
|
15218 | 479 |
while digits != 0 { |
480 |
result <<= 1; |
|
481 |
if (result + 1).pow(2) <= value >> (digits - 2) { |
|
482 |
result += 1; |
|
483 |
} |
|
484 |
digits -= 2; |
|
485 |
} |
|
15214 | 486 |
|
487 |
result |
|
488 |
} |
|
489 |
||
490 |
pub fn integral_sqrt_ext(mut value: u128) -> u128 { |
|
15837 | 491 |
let mut digits = (128u32 - 1).saturating_sub(value.leading_zeros()) & 0xFE; |
492 |
let mut result = if value == 0 { 0u128 } else { 1u128 }; |
|
15214 | 493 |
|
15837 | 494 |
while digits != 0 { |
495 |
result <<= 1; |
|
496 |
if (result + 1).pow(2) <= value >> (digits - 2) { |
|
497 |
result += 1; |
|
498 |
} |
|
499 |
digits -= 2; |
|
500 |
} |
|
15214 | 501 |
|
502 |
result |
|
503 |
} |
|
504 |
||
15218 | 505 |
#[inline] |
13928 | 506 |
pub fn distance<T>(x: T, y: T) -> FPNum |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
507 |
where |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
508 |
T: Into<i64> + std::fmt::Debug, |
13925 | 509 |
{ |
15214 | 510 |
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
|
511 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
512 |
FPNum { |
15211 | 513 |
sign_mask: POSITIVE_MASK, |
15214 | 514 |
value: integral_sqrt_ext(sqr) as u64, |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
515 |
} |
13925 | 516 |
} |
517 |
||
13882 | 518 |
/* TODO: |
519 |
AngleSin |
|
520 |
AngleCos |
|
521 |
*/ |
|
522 |
||
523 |
#[cfg(test)] |
|
524 |
#[test] |
|
525 |
fn basics() { |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
526 |
let n = fp!(15 / 2); |
13882 | 527 |
assert!(n.is_positive()); |
528 |
assert!(!n.is_negative()); |
|
529 |
||
530 |
assert!(!(-n).is_positive()); |
|
531 |
assert!((-n).is_negative()); |
|
532 |
||
533 |
assert_eq!(-(-n), n); |
|
534 |
assert_eq!((-n).abs(), n); |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
535 |
assert_eq!(-n, fp!(-15 / 2)); |
13882 | 536 |
|
537 |
assert_eq!(n.round(), 7); |
|
538 |
assert_eq!((-n).round(), -7); |
|
15219 | 539 |
|
540 |
assert_eq!(f64::from(fp!(5/2)), 2.5f64); |
|
13882 | 541 |
} |
542 |
||
543 |
#[test] |
|
544 |
fn zero() { |
|
13891 | 545 |
let z = fp!(0); |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
546 |
let n = fp!(15 / 2); |
13882 | 547 |
|
548 |
assert!(z.is_zero()); |
|
549 |
assert!(z.is_positive()); |
|
15211 | 550 |
assert!((-z).is_negative()); |
13883 | 551 |
assert_eq!(n - n, z); |
552 |
assert_eq!(-n + n, z); |
|
13926 | 553 |
assert_eq!(n.with_sign_as(-n), -n); |
13882 | 554 |
} |
555 |
||
556 |
#[test] |
|
13884 | 557 |
fn ord() { |
13891 | 558 |
let z = fp!(0); |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
559 |
let n1_5 = fp!(3 / 2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
560 |
let n2_25 = fp!(9 / 4); |
13884 | 561 |
|
562 |
assert!(!(z > z)); |
|
563 |
assert!(!(z < z)); |
|
564 |
assert!(n2_25 > n1_5); |
|
565 |
assert!(-n2_25 < n1_5); |
|
566 |
assert!(-n2_25 < -n1_5); |
|
15211 | 567 |
|
568 |
assert_eq!(n1_5.signum(), 1); |
|
569 |
assert_eq!((-n1_5).signum(), -1); |
|
13884 | 570 |
} |
571 |
||
572 |
#[test] |
|
13882 | 573 |
fn arith() { |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
574 |
let n1_5 = fp!(3 / 2); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
575 |
let n2_25 = fp!(9 / 4); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
576 |
let n_0_15 = fp!(-15 / 100); |
13882 | 577 |
|
13891 | 578 |
assert_eq!(n1_5 + n1_5, fp!(3)); |
579 |
assert_eq!(-n1_5 - n1_5, fp!(-3)); |
|
15211 | 580 |
assert_eq!(n1_5 - n1_5, fp!(0)); |
13882 | 581 |
|
582 |
assert_eq!(n1_5 * n1_5, n2_25); |
|
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 |
||
13883 | 587 |
assert_eq!(-n2_25 / -n1_5, n1_5); |
588 |
assert_eq!(n1_5 / -10, n_0_15); |
|
589 |
||
13882 | 590 |
assert_eq!(n1_5.sqr(), n2_25); |
591 |
assert_eq!((-n1_5).sqr(), n2_25); |
|
592 |
||
593 |
assert_eq!(n2_25.sqrt(), n1_5); |
|
594 |
||
595 |
assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25); |
|
13929 | 596 |
|
597 |
let mut m = fp!(1); |
|
598 |
m += n1_5; |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
599 |
assert_eq!(m, fp!(5 / 2)); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
600 |
} |
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 |
#[test] |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
603 |
fn test_distance_high_values() { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
604 |
assert_eq!(distance(1_000_000i32, 0), fp!(1_000_000)); |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
605 |
assert_eq!( |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
606 |
FPPoint::new(fp!(1_000_000), fp!(0)).distance(), |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
607 |
fp!(1_000_000) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
608 |
); |
13882 | 609 |
} |
13925 | 610 |
|
611 |
#[test] |
|
612 |
fn point() { |
|
613 |
let z = FPPoint::zero(); |
|
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
614 |
let n = fp!(16 / 9); |
13925 | 615 |
let p = FPPoint::new(fp!(1), fp!(-2)); |
616 |
||
617 |
assert_eq!(p.sqr_distance(), fp!(5)); |
|
618 |
assert_eq!(p + -p, FPPoint::zero()); |
|
619 |
assert_eq!(p * z, z); |
|
620 |
assert_eq!(p.dot(&z), fp!(0)); |
|
13932 | 621 |
assert_eq!(n * p, p * n); |
13925 | 622 |
assert_eq!(distance(4, 3), fp!(5)); |
13927 | 623 |
assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6))); |
13939 | 624 |
assert_eq!(p.max_norm(), fp!(2)); |
14122
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14081
diff
changeset
|
625 |
} |