-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathquickmath.cpp
More file actions
59 lines (47 loc) · 1.16 KB
/
quickmath.cpp
File metadata and controls
59 lines (47 loc) · 1.16 KB
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
/*
* Flybrix Flight Controller -- Copyright 2018 Flying Selfie Inc. d/b/a Flybrix
*
* http://www.flybrix.com
*/
#include "quickmath.h"
#include <cmath>
#ifndef SE_NON_IEEE_STANDARD_FLOATS
/*
* Fast inverse square-root
* See: http://en.wikipedia.org/wiki/Fast_inverse_square_root
*/
float quick::invSqrt(float x) {
float halfx = 0.5f * (float)x;
float y = (float)x;
long i = *(long *)&y;
i = 0x5f3759df - (i >> 1);
y = *(float *)&i;
y = y * (1.5f - (halfx * y * y));
return fabs(y);
}
#else
float quick::invSqrt(float x) {
return 1.0f / sqrt(x);
}
#endif
namespace {
constexpr float PI = 4 * std::atan(1.0);
constexpr float TWO_PI = 2 * PI;
constexpr float PI_HALF = PI / 2;
constexpr float _4__PI = 1.2732395447;
constexpr float _4__PIPI = 0.4052847346;
} // namespace
float quick::sin(float x) {
// always wrap input angle to -PI..PI
while (x < -PI) {
x += TWO_PI;
}
while (x > PI) {
x -= TWO_PI;
}
float helper = x * (_4__PI - _4__PIPI * std::fabs(x));
return helper * (0.225 * (std::fabs(helper) - 1) + 1);
}
float quick::cos(float x) {
return quick::sin(x + PI_HALF);
}