forked from zf-lab/SoftFM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastatan2.h
72 lines (67 loc) · 2.95 KB
/
fastatan2.h
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
///////////////////////////////////////////////////////////////////////////////////
// SoftFM - Software decoder for FM broadcast radio with stereo support //
// //
// Copyright (C) 2015 Edouard Griffiths, F4EXB //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_FASTATAN2_H_
#define INCLUDE_FASTATAN2_H_
#include <cmath>
#include <cstdint>
// Fast arctan2
// For the algorithms, see:
// https://www.dsprelated.com/showarticle/1052.php
inline float fastatan2(float y, float x)
{
const float n1 = 0.97239411f;
const float n2 = -0.19194795f;
const float pi = (float)(M_PI);
const float pi_2 = pi/2.0f;
float result = 0.0f;
if (x != 0.0f)
{
const union { float flVal; uint32_t nVal; } tYSign = { y };
const union { float flVal; uint32_t nVal; } tXSign = { x };
if (fabsf(x) >= fabsf(y))
{
union { float flVal; uint32_t nVal; } tOffset = { pi };
// Add or subtract PI based on y's sign.
tOffset.nVal |= tYSign.nVal & 0x80000000u;
// No offset if x is positive, so multiply by 0 or based on x's sign.
tOffset.nVal *= tXSign.nVal >> 31;
result = tOffset.flVal;
const float z = y / x;
result += (n1 + n2 * z * z) * z;
}
else // Use atan(y/x) = pi/2 - atan(x/y) if |y/x| > 1.
{
union { float flVal; uint32_t nVal; } tOffset = { pi_2 };
// Add or subtract PI/2 based on y's sign.
tOffset.nVal |= tYSign.nVal & 0x80000000u;
result = tOffset.flVal;
const float z = x / y;
result -= (n1 + n2 * z * z) * z;
}
}
else if (y > 0.0f)
{
result = pi_2;
}
else if (y < 0.0f)
{
result = -pi_2;
}
return result;
}
#endif /* INCLUDE_FASTATAN2_H_ */