forked from zenmetsu/radioModem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharctanapprox.cpp
156 lines (118 loc) · 2.36 KB
/
arctanapprox.cpp
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
#include <stdint.h>
#include "configuration.h"
#ifdef ___MAPLE
#include <libmaple_types.h>
#endif
#include "arctanapprox.h"
#include "math.h"
F16 F16pi, F16pi1By2, atanLookupTable[64];
#ifndef atanf
#define atanf atan
#endif
void atan_init()
{
int i;
const float pi = 3.14159265f;
F16pi = floatToF16(pi);
F16pi1By2 = floatToF16(pi / 2.0f);
for(i = 0; i < 64; i++) atanLookupTable[i] = floatToF16(atanf(((float)i) / 4.0f));
}
uint8_t count_leading_unused_bits(uint16_t x)
{
uint8_t i = 0;
uint8_t sign = (x >> 15) & 0x1;
if(sign == 0x1) x = ~x;
x <<= 1;
while((x & 0x8000) == 0 && i < 16)
{
i++;
x <<= 1;
}
return i;
}
uint8_t count_trailing_unused_bits(uint16_t x)
{
uint8_t i = 0;
uint8_t sign = (x >> 15) & 0x1;
if(sign == 0x1) x = ~x;
while((x & 0x0001) == 0 && i < 16)
{
i++;
x >>= 1;
}
return i;
}
uint16_t remove_leading_bits(uint8_t bitcount, uint16_t x)
{
return (x & 0x8000) | ((x << bitcount) & 0x7FFF);
}
F16 atan_lookup(F15 xi, F15 yi)
{
F15 _x, _y;
//Handle the zeroes here
if(yi == 0)
{
if(xi >= 0)
{
return F16pi;
}
else
{
return F16neg(F16pi);
}
}
if(xi == 0)
{
if(yi > 0)
{
return F16pi1By2;
}
else
{
return F16neg(F16pi1By2);
}
}
_x = (xi < 0) ? F15neg(xi) : xi;
_y = (yi < 0) ? F15neg(yi) : yi;
F16 val = 0, valp = 0;
uint16_t i = _y / _x;
uint16_t r = _y % _x;
uint8_t us = count_leading_unused_bits(r);
uint8_t ls = count_leading_unused_bits(_x);
_x >>= (15 - ls - 7);
uint16_t c = 0xFFFF;
if(_x != 0) c = (r << us) / _x;
c = (c << (15 - (us + (15 - ls - 7)))) - 1;
F15 alpha = c, alphap;
i = (i << 2) | ((alpha & 0x6000) >> 13);
alpha = (alpha << 2) & 0x7FFF;
alphap = F15neg(F15inc(alpha));
if(i >= 64)
{
val = F16pi1By2;
}
else
{
val = atanLookupTable[i];
if(i + 1 < 64) valp = atanLookupTable[i + 1];
else valp = F16pi1By2;
val = F16add(F16unsafeMul(2, val, F15ToF16(alphap)), F16unsafeMul(2, valp, F15ToF16(alpha)));
}
if(xi > 0 && yi > 0)
{
return val;
}
else if(xi > 0 && yi < 0)
{
return F16neg(val);
}
else if(xi < 0 && yi > 0)
{
return F16add(val, F16pi1By2);
}
else if(xi < 0 && yi < 0)
{
return F16neg(F16add(val, F16pi1By2));
}
return 0;
}