author | unC0Rr |
Sat, 28 Sep 2024 22:27:13 +0200 | |
changeset 16066 | 2b4f361e3891 |
parent 15860 | c910381d1ea9 |
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 |
||
15239 | 73 |
#[inline] |
13949 | 74 |
pub fn sqrt(&self) -> Self { |
15232 | 75 |
debug_assert!(self.is_positive()); |
13905 | 76 |
|
77 |
Self { |
|
15232 | 78 |
sign_mask: POSITIVE_MASK, |
15235 | 79 |
value: integral_sqrt(self.value) << 16, |
13905 | 80 |
} |
81 |
} |
|
13946 | 82 |
|
13948 | 83 |
#[inline] |
15232 | 84 |
pub fn with_sign(&self, is_negative: bool) -> FPNum { |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
85 |
FPNum { |
15232 | 86 |
sign_mask: bool_mask(is_negative), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
87 |
..*self |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
88 |
} |
13946 | 89 |
} |
90 |
||
91 |
#[inline] |
|
14648 | 92 |
pub const fn with_sign_as(self, other: FPNum) -> FPNum { |
15232 | 93 |
FPNum { |
94 |
sign_mask: other.sign_mask, |
|
95 |
..self |
|
96 |
} |
|
13946 | 97 |
} |
13948 | 98 |
|
99 |
#[inline] |
|
14648 | 100 |
pub const fn point(self) -> FPPoint { |
13948 | 101 |
FPPoint::new(self, self) |
102 |
} |
|
15232 | 103 |
|
104 |
#[inline] |
|
105 |
const fn temp_i128(self) -> i128 { |
|
15233 | 106 |
((self.value ^ self.sign_mask) as i128).wrapping_sub(self.sign_mask as i128) |
15232 | 107 |
} |
13905 | 108 |
} |
109 |
||
110 |
impl From<i32> for FPNum { |
|
111 |
#[inline] |
|
112 |
fn from(n: i32) -> Self { |
|
113 |
FPNum { |
|
15232 | 114 |
sign_mask: bool_mask(n < 0), |
13905 | 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 { |
|
15233 | 124 |
sign_mask: POSITIVE_MASK, |
13905 | 125 |
value: (n as u64) << 32, |
126 |
} |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
impl From<FPNum> for f64 { |
|
131 |
#[inline] |
|
132 |
fn from(n: FPNum) -> Self { |
|
15232 | 133 |
if n.is_negative() { |
15240 | 134 |
n.value as f64 / -0x1_0000_0000i64 as f64 |
13905 | 135 |
} else { |
15240 | 136 |
n.value as f64 / 0x1_0000_0000i64 as f64 |
13905 | 137 |
} |
138 |
} |
|
139 |
} |
|
140 |
||
141 |
impl PartialEq for FPNum { |
|
13909 | 142 |
#[inline] |
13905 | 143 |
fn eq(&self, other: &Self) -> bool { |
15232 | 144 |
self.value == other.value && (self.sign_mask == other.sign_mask || self.value == 0) |
13905 | 145 |
} |
146 |
} |
|
147 |
||
148 |
impl Eq for FPNum {} |
|
149 |
||
150 |
impl PartialOrd for FPNum { |
|
13909 | 151 |
#[inline] |
13905 | 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 { |
|
15232 | 160 |
self.temp_i128().cmp(&(rhs.temp_i128())) |
13905 | 161 |
} |
162 |
} |
|
163 |
||
164 |
impl ops::Add for FPNum { |
|
165 |
type Output = Self; |
|
166 |
||
167 |
#[inline] |
|
168 |
fn add(self, rhs: Self) -> Self { |
|
15232 | 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), |
|
13905 | 174 |
} |
175 |
} |
|
176 |
} |
|
177 |
||
178 |
impl ops::Sub for FPNum { |
|
179 |
type Output = Self; |
|
180 |
||
181 |
#[inline] |
|
15232 | 182 |
fn sub(self, mut rhs: Self) -> Self { |
183 |
rhs.sign_mask = !rhs.sign_mask; |
|
184 |
self + rhs |
|
13905 | 185 |
} |
186 |
} |
|
187 |
||
188 |
impl ops::Neg for FPNum { |
|
189 |
type Output = Self; |
|
190 |
||
191 |
#[inline] |
|
192 |
fn neg(self) -> Self { |
|
193 |
Self { |
|
15232 | 194 |
sign_mask: !self.sign_mask, |
13905 | 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 { |
|
15232 | 206 |
sign_mask: self.sign_mask ^ rhs.sign_mask, |
13905 | 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 { |
|
15232 | 218 |
sign_mask: self.sign_mask ^ bool_mask(rhs < 0), |
13905 | 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 { |
|
15232 | 230 |
sign_mask: self.sign_mask ^ rhs.sign_mask, |
13905 | 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 { |
|
15232 | 242 |
sign_mask: self.sign_mask ^ bool_mask(rhs < 0), |
13905 | 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 { |
|
15232 | 254 |
sign_mask: self.sign_mask, |
13905 | 255 |
value: self.value / rhs as u64, |
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
||
13949 | 260 |
#[macro_export] |
13911 | 261 |
macro_rules! fp { |
14722 | 262 |
($n: literal / $d: tt) => { |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
263 |
FPNum::new($n, $d) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
264 |
}; |
14685 | 265 |
($n: literal) => { |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
266 |
FPNum::from($n) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
267 |
}; |
13911 | 268 |
} |
269 |
||
13946 | 270 |
const LINEARIZE_TRESHOLD: u64 = 0x1_0000; |
271 |
||
272 |
#[derive(Clone, Copy, Debug)] |
|
13949 | 273 |
pub struct FPPoint { |
15232 | 274 |
x_sign_mask: u32, |
275 |
y_sign_mask: u32, |
|
13946 | 276 |
x_value: u64, |
277 |
y_value: u64, |
|
278 |
} |
|
279 |
||
280 |
impl FPPoint { |
|
281 |
#[inline] |
|
14648 | 282 |
pub const fn new(x: FPNum, y: FPNum) -> Self { |
13946 | 283 |
Self { |
15232 | 284 |
x_sign_mask: x.sign_mask as u32, |
285 |
y_sign_mask: y.sign_mask as u32, |
|
13946 | 286 |
x_value: x.value, |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
287 |
y_value: y.value, |
13946 | 288 |
} |
289 |
} |
|
290 |
||
291 |
#[inline] |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
292 |
pub fn zero() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
293 |
FPPoint::new(fp!(0), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
294 |
} |
13946 | 295 |
|
296 |
#[inline] |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
297 |
pub fn unit_x() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
298 |
FPPoint::new(fp!(1), fp!(0)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
299 |
} |
13946 | 300 |
|
301 |
#[inline] |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
302 |
pub fn unit_y() -> FPPoint { |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
303 |
FPPoint::new(fp!(0), fp!(1)) |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
304 |
} |
13946 | 305 |
|
306 |
#[inline] |
|
14648 | 307 |
pub const fn x(&self) -> FPNum { |
13946 | 308 |
FPNum { |
15232 | 309 |
sign_mask: self.x_sign_mask as i32 as u64, |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
310 |
value: self.x_value, |
13946 | 311 |
} |
312 |
} |
|
313 |
||
314 |
#[inline] |
|
14648 | 315 |
pub const fn y(&self) -> FPNum { |
13946 | 316 |
FPNum { |
15232 | 317 |
sign_mask: self.y_sign_mask as i32 as u64, |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
318 |
value: self.y_value, |
13946 | 319 |
} |
320 |
} |
|
321 |
||
322 |
#[inline] |
|
14199 | 323 |
pub fn is_zero(&self) -> bool { |
324 |
self.x().is_zero() && self.y().is_zero() |
|
325 |
} |
|
326 |
||
327 |
#[inline] |
|
13949 | 328 |
pub fn max_norm(&self) -> FPNum { |
13960 | 329 |
std::cmp::max(self.x().abs(), self.y().abs()) |
13949 | 330 |
} |
331 |
||
332 |
#[inline] |
|
333 |
pub fn sqr_distance(&self) -> FPNum { |
|
13946 | 334 |
self.x().sqr() + self.y().sqr() |
335 |
} |
|
336 |
||
337 |
#[inline] |
|
13949 | 338 |
pub fn distance(&self) -> FPNum { |
13946 | 339 |
let r = self.x_value | self.y_value; |
340 |
if r < LINEARIZE_TRESHOLD { |
|
341 |
FPNum::from(r as u32) |
|
342 |
} else { |
|
15235 | 343 |
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
|
344 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
345 |
FPNum { |
15232 | 346 |
sign_mask: POSITIVE_MASK, |
15860 | 347 |
value: integral_sqrt_ext(sqr), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
348 |
} |
13946 | 349 |
} |
350 |
} |
|
351 |
||
352 |
#[inline] |
|
13949 | 353 |
pub fn is_in_range(&self, radius: FPNum) -> bool { |
354 |
self.max_norm() < radius && self.sqr_distance() < radius.sqr() |
|
13946 | 355 |
} |
356 |
||
357 |
#[inline] |
|
13949 | 358 |
pub fn dot(&self, other: &FPPoint) -> FPNum { |
13946 | 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 { |
|
13948 | 382 |
($op: ty, $name: tt) => { |
13946 | 383 |
impl $op for FPPoint { |
384 |
type Output = Self; |
|
385 |
||
386 |
#[inline] |
|
387 |
fn $name(self, rhs: Self) -> Self { |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
388 |
Self::new(self.x().$name(rhs.x()), self.y().$name(rhs.y())) |
13946 | 389 |
} |
390 |
} |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
391 |
}; |
13946 | 392 |
} |
393 |
||
13953 | 394 |
macro_rules! right_scalar_bin_op_impl { |
395 |
($($op: tt)::+, $name: tt) => { |
|
396 |
impl $($op)::+<FPNum> for FPPoint { |
|
13948 | 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 |
} |
|
14161 | 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 |
} |
|
13948 | 416 |
} |
417 |
} |
|
418 |
||
13953 | 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 |
||
13946 | 433 |
bin_op_impl!(ops::Add, add); |
434 |
bin_op_impl!(ops::Sub, sub); |
|
435 |
bin_op_impl!(ops::Mul, mul); |
|
13950 | 436 |
bin_op_impl!(ops::Div, div); |
13953 | 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); |
|
14161 | 441 |
right_scalar_bin_op_impl!(ops::Div<u32>, div); |
13953 | 442 |
left_scalar_bin_op_impl!(ops::Mul, mul); |
13946 | 443 |
|
13950 | 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 |
||
15239 | 475 |
pub fn integral_sqrt(value: u64) -> u64 { |
15859 | 476 |
let mut digits = (64u32 - 1).saturating_sub(value.leading_zeros()) & 0xFE; |
15239 | 477 |
let mut result = if value == 0 { 0u64 } else { 1u64 }; |
15235 | 478 |
|
15239 | 479 |
while digits != 0 { |
480 |
result <<= 1; |
|
481 |
if (result + 1).pow(2) <= value >> (digits - 2) { |
|
482 |
result += 1; |
|
483 |
} |
|
484 |
digits -= 2; |
|
485 |
} |
|
15235 | 486 |
|
487 |
result |
|
488 |
} |
|
489 |
||
15860 | 490 |
pub fn integral_sqrt_ext(value: u128) -> u64 { |
15859 | 491 |
let mut digits = (128u32 - 1).saturating_sub(value.leading_zeros()) & 0xFE; |
15860 | 492 |
let mut result = if value == 0 { 0u64 } else { 1u64 }; |
15235 | 493 |
|
15859 | 494 |
while digits != 0 { |
495 |
result <<= 1; |
|
15860 | 496 |
if ((result + 1) as u128).pow(2) <= value >> (digits - 2) { |
15859 | 497 |
result += 1; |
498 |
} |
|
499 |
digits -= 2; |
|
500 |
} |
|
15235 | 501 |
|
502 |
result |
|
503 |
} |
|
504 |
||
15239 | 505 |
#[inline] |
13949 | 506 |
pub fn distance<T>(x: T, y: T) -> FPNum |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
507 |
where |
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
508 |
T: Into<i64> + std::fmt::Debug, |
13946 | 509 |
{ |
15235 | 510 |
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
|
511 |
|
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
512 |
FPNum { |
15232 | 513 |
sign_mask: POSITIVE_MASK, |
15860 | 514 |
value: integral_sqrt_ext(sqr), |
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
515 |
} |
13946 | 516 |
} |
517 |
||
13905 | 518 |
/* TODO: |
519 |
AngleSin |
|
520 |
AngleCos |
|
521 |
*/ |
|
522 |
||
523 |
#[test] |
|
524 |
fn basics() { |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
525 |
let n = fp!(15 / 2); |
13905 | 526 |
assert!(n.is_positive()); |
527 |
assert!(!n.is_negative()); |
|
528 |
||
529 |
assert!(!(-n).is_positive()); |
|
530 |
assert!((-n).is_negative()); |
|
531 |
||
532 |
assert_eq!(-(-n), n); |
|
533 |
assert_eq!((-n).abs(), n); |
|
14143
c27461e6a9eb
Implement non-overflowing calculations for high values
unc0rr
parents:
14102
diff
changeset
|
534 |
assert_eq!(-n, fp!(-15 / 2)); |
13905 | 535 |
|
536 |
assert_eq!(n.round(), 7); |
|
537 |
assert_eq!((-n).round(), -7); |
|
15240 | 538 |
|
539 |
assert_eq!(f64::from(fp!(5/2)), 2.5f64); |
|
15860 | 540 |
|
541 |
assert_eq!(integral_sqrt_ext(0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF), 0xFFFF_FFFF_FFFF_FFFF); |
|
13905 | 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 |
} |