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