-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrig.rs
507 lines (463 loc) · 15 KB
/
trig.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Trigonometric functions for fixed numbers.
//!
//! ## Implementation
//!
//! We use the CORDIC algorithm, following this paper:
//! Efficient Fixed-Point Trigonometry Using CORDIC Functions For PIC16F - Jose Benavides Microchip Technology Inc.
//! (2007)
//! <https://ww1.microchip.com/downloads/en/AppNotes/01061A.pdf>
use fixed::traits::{Fixed, FixedSigned};
use crate::{
tables::{ATAN_2P_DEG, ATAN_2P_RAD},
traits::FixedRadians,
};
pub(crate) mod cordic_constants {
use fixed::types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64, LeEqU8};
use fixed::{
FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64,
FixedU8,
};
use typenum::*;
/// Constants used by the cordic algorithm
pub trait CordicConstants {
/// WolframAlpha: Product[1/Sqrt[1 + 2^-2j],{j,0,128}]
/// "0.60725293500888125616944675250492826311239085215008977245697601311014788120842578"
const KN: Self;
}
impl CordicConstants for f32 {
const KN: Self =
0.60725293500888125616944675250492826311239085215008977245697601311014788120842578f32;
}
impl CordicConstants for f64 {
const KN: Self =
0.60725293500888125616944675250492826311239085215008977245697601311014788120842578f64;
}
macro_rules! impl_cordic_constants {
($f:ident, $leq:ident, $f0:ty) => {
impl<N> CordicConstants for $f<N>
where
N: $leq + IsLess<$f0, Output = True>,
{
const KN: Self = Self::lit(
"0.60725293500888125616944675250492826311239085215008977245697601311014788120842578",
);
}
};
}
macro_rules! impl_cordic_constants_u {
($f:ident, $leq:ident) => {
impl<N> CordicConstants for $f<N>
where
N: $leq
{
const KN: Self = Self::lit(
"0.60725293500888125616944675250492826311239085215008977245697601311014788120842578",
);
}
};
}
impl_cordic_constants!(FixedI8, LeEqU8, U8);
impl_cordic_constants!(FixedI16, LeEqU16, U16);
impl_cordic_constants!(FixedI32, LeEqU32, U32);
impl_cordic_constants!(FixedI64, LeEqU64, U64);
impl_cordic_constants!(FixedI128, LeEqU128, U128);
impl_cordic_constants_u!(FixedU8, LeEqU8);
impl_cordic_constants_u!(FixedU16, LeEqU16);
impl_cordic_constants_u!(FixedU32, LeEqU32);
impl_cordic_constants_u!(FixedU64, LeEqU64);
impl_cordic_constants_u!(FixedU128, LeEqU128);
}
pub use cordic_constants::CordicConstants;
/// Setup traits for the cordic sincos.
/// Maps an angle to the interval of -90 to 90 degrees.
/// Normalization outputs a correct starting value and a bool meaning if the result will have to be negated.
pub(crate) mod normalization {
use fixed::types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64};
use fixed::{FixedI128, FixedI16, FixedI32, FixedI64, FixedI8};
use seq_macro::seq;
use typenum::*;
/// Constant number 90.
pub trait Fixed90 {
const F_90: Self;
}
/// Constant number 180.
pub trait Fixed180 {
const F_180: Self;
}
/// Constant number 270.
pub trait Fixed270 {
const F_270: Self;
}
/// Constant number 360.
pub trait Fixed360 {
const F_360: Self;
}
/// Get the angle in the range where cordic can run on it also returning
/// if the final result will need to be negated based on where it is on an interval.
pub trait NormalizeCordic: Sized {
fn normalize_cordic(angle_degs: Self) -> (Self, bool);
}
macro_rules! impl_f90 {
($f:ident, $leq:ident, $f0:ty) => {
impl<N> Fixed90 for $f<N>
where
N: $leq + IsLess<$f0, Output = True>,
{
const F_90: Self = Self::lit("90");
}
};
}
impl Fixed90 for FixedI8<U0> {
const F_90: Self = Self::lit("90");
}
impl_f90!(FixedI16, LeEqU16, U9);
impl_f90!(FixedI32, LeEqU32, U25);
impl_f90!(FixedI64, LeEqU64, U57);
impl_f90!(FixedI128, LeEqU128, U121);
macro_rules! impl_f180 {
($f:ident, $leq:ident, $f0:ty) => {
impl<N> Fixed180 for $f<N>
where
N: $leq + IsLess<$f0, Output = True>,
{
const F_180: Self = Self::lit("180");
}
};
}
impl_f180!(FixedI16, LeEqU16, U8);
impl_f180!(FixedI32, LeEqU32, U24);
impl_f180!(FixedI64, LeEqU64, U56);
impl_f180!(FixedI128, LeEqU128, U120);
macro_rules! impl_f270plus {
($f:ident, $leq:ident, $f0:ty) => {
impl<N> Fixed270 for $f<N>
where
N: $leq + IsLess<$f0, Output = True>,
{
const F_270: Self = Self::lit("270");
}
impl<N> Fixed360 for $f<N>
where
N: $leq + IsLess<$f0, Output = True>,
{
const F_360: Self = Self::lit("360");
}
};
}
impl_f270plus!(FixedI16, LeEqU16, U7);
impl_f270plus!(FixedI32, LeEqU32, U23);
impl_f270plus!(FixedI64, LeEqU64, U55);
impl_f270plus!(FixedI128, LeEqU128, U119);
macro_rules! impl_norm_small {
($f:ident, $frac:ident) => {
impl NormalizeCordic for $f<$frac> {
#[inline(always)]
fn normalize_cordic(angle_degs: Self) -> (Self, bool) {
(angle_degs, false)
}
}
};
}
seq!(N in 1..=8 {
impl_norm_small!(FixedI8, U~N);
});
seq!(N in 9..=16 {
impl_norm_small!(FixedI16, U~N);
});
seq!(N in 25..=32 {
impl_norm_small!(FixedI32, U~N);
});
seq!(N in 57..=64 {
impl_norm_small!(FixedI64, U~N);
});
seq!(N in 121..=128 {
impl_norm_small!(FixedI128, U~N);
});
// Note: it would overflow at 180 so 90 has to be added twice
macro_rules! impl_norm_i8 {
($f:ident, $frac:ident) => {
impl NormalizeCordic for $f<$frac> {
#[inline(always)]
fn normalize_cordic(mut angle_degs: Self) -> (Self, bool) {
let mut neg = false;
if angle_degs < -Self::F_90 {
angle_degs += Self::F_90;
angle_degs += Self::F_90;
neg = true;
} else if Self::F_90 < angle_degs {
angle_degs -= Self::F_90;
angle_degs -= Self::F_90;
neg = true;
}
(angle_degs, neg)
}
}
};
}
impl_norm_i8!(FixedI8, U0);
impl_norm_i8!(FixedI16, U8);
impl_norm_i8!(FixedI32, U24);
impl_norm_i8!(FixedI64, U56);
impl_norm_i8!(FixedI128, U120);
macro_rules! impl_norm_i9 {
($f:ident, $frac:ident) => {
impl NormalizeCordic for $f<$frac> {
#[inline(always)]
fn normalize_cordic(mut angle_degs: Self) -> (Self, bool) {
let mut neg = false;
if angle_degs < -Self::F_90 {
angle_degs += Self::F_180;
neg = true;
} else if Self::F_90 < angle_degs {
angle_degs -= Self::F_180;
neg = true;
}
(angle_degs, neg)
}
}
};
}
impl_norm_i9!(FixedI16, U7);
impl_norm_i9!(FixedI32, U23);
impl_norm_i9!(FixedI64, U55);
impl_norm_i9!(FixedI128, U119);
// Normalize any bigger number
macro_rules! impl_norm {
($f:ident, $frac:ident) => {
impl NormalizeCordic for $f<$frac> {
#[inline(always)]
fn normalize_cordic(mut angle_degs: Self) -> (Self, bool) {
let mut neg = false;
angle_degs %= Self::F_360;
if angle_degs < -Self::F_90 {
if -Self::F_270 <= angle_degs {
angle_degs += Self::F_180;
neg = true;
} else {
angle_degs += Self::F_360;
}
} else if Self::F_90 < angle_degs {
if angle_degs <= Self::F_270 {
angle_degs -= Self::F_180;
neg = true;
} else {
angle_degs -= Self::F_360;
}
}
(angle_degs, neg)
}
}
};
}
seq!(N in 0..=6 {
impl_norm!(FixedI16, U~N);
});
seq!(N in 0..=22 {
impl_norm!(FixedI32, U~N);
});
seq!(N in 0..=54 {
impl_norm!(FixedI64, U~N);
});
seq!(N in 0..=118 {
impl_norm!(FixedI128, U~N);
});
}
pub use normalization::*;
/// Only works with values in -90 to 90 degrees.
#[inline]
pub fn sin_cos_deg_unchecked<Val: FixedSigned + NormalizeCordic + CordicConstants>(
mut angle_degs: Val,
) -> (Val, Val) {
// debug_assert!(
// angle_degs <= Val::from_num(90) && Val::from_num(-90) <= angle_degs,
// "Can only take sin_cos of values in -90 to 90! Got: {}",
// angle_degs
// );
let mut x: Val = Val::KN;
let mut y: Val = Val::ZERO;
let mut at: Val = atan_table_deg::<Val>(0);
let mut i = 0u32;
while at != Val::ZERO {
// println!(
// "i={i} x={}, y={}, z={}, atan(2^-i)={}",
// x,
// y,
// angle_degs,
// atan_table_deg::<Val>(i)
// );
let xx = x;
if angle_degs < 0 {
x += y >> i;
y -= xx >> i;
angle_degs += at;
} else {
x -= y >> i;
y += xx >> i;
angle_degs -= at;
}
i += 1;
at = atan_table_deg::<Val>(i);
}
(y, x)
}
//TODO fix for small int bits, 90 overflows
#[inline]
fn sin_cos_right_angles<Val: FixedSigned + NormalizeCordic>(
angle_normalized: Val,
neg: bool,
) -> Option<(Val, Val)> {
return match angle_normalized {
angle_normalized if angle_normalized == Val::from_num(0) => {
if neg {
return Some((Val::from_num(0), Val::from_num(-1)));
} else {
return Some((Val::from_num(0), Val::from_num(1)));
}
}
angle_normalized if angle_normalized == Val::from_num(90) => {
if neg {
return Some((Val::from_num(-1), Val::from_num(0)));
} else {
return Some((Val::from_num(1), Val::from_num(0)));
}
}
angle_normalized if angle_normalized == Val::from_num(-90) => {
if neg {
return Some((Val::from_num(1), Val::from_num(0)));
} else {
return Some((Val::from_num(-1), Val::from_num(0)));
}
}
_ => None,
};
}
/// Compute sin and cos simultaneously of an angle in degrees.
///
/// Note: number representation needs at least 10 integer bits to fit 360.
#[inline]
pub fn sin_cos<Val: FixedSigned + NormalizeCordic + CordicConstants>(
angle_degs: Val,
) -> (Val, Val) {
let (angle_normalized, neg) = Val::normalize_cordic(angle_degs);
//TODO use constants instead of from_num
// get values for right angles
match sin_cos_right_angles(angle_normalized, neg) {
Some(res) => return res,
None => {}
}
let result = sin_cos_deg_unchecked(angle_normalized);
if neg {
(-result.0, -result.1)
} else {
result
}
}
/// Compute sin of an angle in degrees.
///
/// Note: use sin_cos when you need both for performance.
#[inline]
pub fn sin<Val: FixedSigned + NormalizeCordic + CordicConstants>(angle_degs: Val) -> Val {
sin_cos(angle_degs).0
}
/// Compute cos of an angle in degrees.
///
/// Note: use sin_cos when you need both for performance.
#[inline]
pub fn cos<Val: FixedSigned + NormalizeCordic + CordicConstants>(angle_degs: Val) -> Val {
sin_cos(angle_degs).1
}
/// Calculate tangent of an angle in degrees.
/// Returns None when the cos is 0 or on overflow.
#[inline]
pub fn tan<Val: FixedSigned + NormalizeCordic + CordicConstants>(angle_degs: Val) -> Option<Val> {
let (sin, cos) = sin_cos(angle_degs);
sin.checked_div(cos)
}
//TODO? explode the atan tables with macro to avoid from_fixed conversion here
/// Get atan(2^{-index}) in degrees.
#[inline]
fn atan_table_deg<Val>(index: u32) -> Val
where
Val: Fixed,
{
Val::from_fixed(ATAN_2P_DEG[index as usize])
}
/// Currently it is very imprecise.
/// You probably want to convert to degrees and use sin_cos instead.
#[inline]
pub fn sin_cos_rad<Val: FixedSigned + FixedRadians + CordicConstants>(
mut angle_rads: Val,
) -> (Val, Val) {
let mut neg = false;
// FIXME % is very imprecise here...
angle_rads %= <Val as FixedRadians>::TAU;
// FIXME FRAC_3_PI_2 should be a const; maybe even NEG_FRAC_3_PI_2
let frac_3_pi_2 = <Val as FixedRadians>::PI + <Val as FixedRadians>::FRAC_PI_2;
if angle_rads < -<Val as FixedRadians>::FRAC_PI_2 {
if -frac_3_pi_2 <= angle_rads {
angle_rads += <Val as FixedRadians>::PI;
neg = true;
} else {
angle_rads += <Val as FixedRadians>::TAU;
}
} else if <Val as FixedRadians>::FRAC_PI_2 < angle_rads {
if angle_rads <= frac_3_pi_2 {
angle_rads -= <Val as FixedRadians>::PI;
neg = true;
} else {
angle_rads -= <Val as FixedRadians>::TAU;
}
}
let c_res = sin_cos_rad_unchecked(angle_rads);
if neg {
(-c_res.0, -c_res.1)
} else {
c_res
}
}
/// Get atan(2^{-index}) in radians.
#[inline]
fn atan_table_rad<Val>(index: u32) -> Val
where
Val: Fixed,
{
Val::from_fixed(ATAN_2P_RAD[index as usize])
}
/// Only works for values in -π/2 to π/2 radians
#[inline]
pub fn sin_cos_rad_unchecked<Val: FixedSigned + FixedRadians + CordicConstants>(
mut angle_rads: Val,
) -> (Val, Val) {
debug_assert!(
angle_rads <= <Val as FixedRadians>::FRAC_PI_2
&& -<Val as FixedRadians>::FRAC_PI_2 <= angle_rads,
"Can only take sin_cos of values in -π/2 to π/2 (~ 1.57079)! Got: {}",
angle_rads
);
let mut x: Val = Val::KN;
let mut y: Val = Val::ZERO;
let mut at: Val = atan_table_rad::<Val>(0);
let mut i = 0u32;
while at != Val::ZERO {
// println!(
// "i={i} x={}, y={}, z={}, atan(2^-i)={}",
// x,
// y,
// angle_rads,
// atan_table_rad::<Val>(i)
// );
let xx = x;
if angle_rads < 0 {
x += y >> i;
y -= xx >> i;
angle_rads += at;
} else {
x -= y >> i;
y += xx >> i;
angle_rads -= at;
}
i += 1;
at = atan_table_rad::<Val>(i);
}
(y, x)
}