forpy 2

exponentials.h
Go to the documentation of this file.
1 /* Author: Christoph Lassner */
2 #pragma once
3 #ifndef FORPY_UTIL_EXPONENTIALS_H_
4 #define FORPY_UTIL_EXPONENTIALS_H_
5
6 #include <cmath>
7
8 #include "../global.h"
9
10 namespace forpy {
12 static const double D_PI = 4. * atan(1.);
13
16 static const float TWO_PI = static_cast<float>(2. * D_PI);
17
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wunused-variable"
20
22 static const float TWO_PI_E = TWO_PI * expf(1.f);
23 #pragma clang diagnostic pop
24
35 inline int ipow(int base, unsigned int exp) {
36 int result = 1;
37 while (exp) {
38 if (exp & 1)
39 result *= base;
40 exp >>= 1;
41 base *= base;
42 }
43
44 return result;
45 };
46
56 inline float fpowi(float base, unsigned int exp) {
57 switch (exp) {
58 case 0:
59 return 1.f;
60 case 1:
61 return base;
62 case 2:
63 return base * base;
64 case 3:
65 return base * base * base;
66 case 4:
67 return base * base * base * base;
68 case 5:
69 return base * base * base * base * base;
70 default:
71 // This version is already a lot faster than fpow.
72 float result = 1.f;
73 while (exp) {
74 if (exp & 1)
75 result *= base;
76 exp >>= 1;
77 base *= base;
78 }
79 return result;
80 }
81 };
82 }; // namespace forpy
83 #endif // FORPY_UTIL_EXPONENTIALS_H_
forpy::D_PI
static const double D_PI
Definition: exponentials.h:12
forpy::fpowi
float fpowi(float base, unsigned int exp)
Computes a float power by an unsigned int.
Definition: exponentials.h:56
forpy::TWO_PI_E
static const float TWO_PI_E
Definition: exponentials.h:22
forpy::TWO_PI
static const float TWO_PI
Definition: exponentials.h:16
forpy::ipow
int ipow(int base, unsigned int exp)
Computes an int power by an int.
Definition: exponentials.h:35
forpy
Definition: fastdprov.h:13