OpenWareLaboratory
basicmaths.h
Go to the documentation of this file.
1 #ifndef __basicmaths_h__
2 #define __basicmaths_h__
3 
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include "heap.h"
7 
8 #define _USE_MATH_DEFINES
9 /* Definitions of useful mathematical constants
10  * M_E - e
11  * M_LOG2E - log2(e)
12  * M_LOG10E - log10(e)
13  * M_LN2 - ln(2)
14  * M_LN10 - ln(10)
15  * M_PI - pi
16  * M_PI_2 - pi/2
17  * M_PI_4 - pi/4
18  * M_1_PI - 1/pi
19  * M_2_PI - 2/pi
20  * M_2_SQRTPI - 2/sqrt(pi)
21  * M_SQRT2 - sqrt(2)
22  * M_SQRT1_2 - 1/sqrt(2)
23  */
24 #ifdef ARM_CORTEX
25 #include "arm_math.h"
26 #endif //ARM_CORTEX
27 
28 #ifdef __cplusplus
29 #include <cmath>
30 #include <algorithm>
31 using std::min;
32 using std::max;
33 using std::abs;
34 using std::clamp;
35 #else
36 #include <math.h>
37 #ifndef min
38 #define min(a,b) ((a)<(b)?(a):(b))
39 #endif
40 #ifndef max
41 #define max(a,b) ((a)>(b)?(a):(b))
42 #endif
43 #ifndef abs
44 #define abs(x) ((x)>0?(x):-(x))
45 #endif
46 #ifndef clamp
47 #define clamp(x, lo, hi) ((x)>(hi)?(hi):((x)<(lo)?(lo):(x)))
48 #endif
49 #endif
50 
51 #ifndef M_PI
52 #define M_PI 3.14159265358979323846
53 #endif
54 #ifndef M_SQRT2
55 #define M_SQRT2 1.41421356237309504880
56 #endif
57 
58 
59 #ifdef __cplusplus
60  extern "C" {
61 #endif
62 
63  float arm_sqrtf(float in);
64  void arm_srand32(uint32_t s);
65  uint32_t arm_rand32();
66 
67  // fast lookup-based exponentials
68  float fast_powf(float x, float y);
69  float fast_expf(float x);
70  float fast_exp2f(float x);
71  float fast_exp10f(float x);
72  void fast_pow_set_table(const uint32_t* table, int size);
73 
74  // fast lookup-based logarithmics
75  float fast_logf(float x);
76  float fast_log2f(float x);
77  float fast_log10f(float x);
78  uint32_t fast_log2i(uint32_t x);
79  void fast_log_set_table(const float* table, int size);
80 
81  // fast approximations
82  float fast_atan2f(float a, float b);
83 
85  float randf();
86 
87  float fast_fmodf(float x, float y);
88 
89 #ifdef ARM_CORTEX
90 #define malloc(x) pvPortMalloc(x)
91 #define calloc(x, y) pvPortCalloc(x, y)
92 #define free(x) vPortFree(x)
93 #define realloc(x, y) pvPortRealloc(x, y);
94 void* pvPortCalloc(size_t nmemb, size_t size);
95 void* pvPortRealloc(void *pv, size_t xWantedSize);
96 #endif
97 
98 #ifdef __cplusplus
99 }
100 #endif
101 
102 #ifdef ARM_CORTEX
103 #define sin(x) arm_sin_f32(x)
104 #define sinf(x) arm_sin_f32(x)
105 #define cos(x) arm_cos_f32(x)
106 #define cosf(x) arm_cos_f32(x)
107 #define sqrt(x) sqrtf(x)
108 /* #define sqrtf(x) arm_sqrtf(x) */
109 #define rand() arm_rand32()
110 
111 #ifdef __FAST_MATH__ /* set by gcc option -ffast-math */
112 
113 // fast lookup-based exponentials
114 #define pow(x, y) fast_powf(x, y)
115 #define powf(x, y) fast_powf(x, y)
116 #define exp(x) fast_expf(x)
117 #define expf(x) fast_expf(x)
118 #define exp2(x) fast_exp2f(x)
119 #define exp2f(x) fast_exp2f(x)
120 #define exp10(x) fast_exp10f(x)
121 #define exp10f(x) fast_exp10f(x)
122 
123 // fast lookup-based logarithmics
124 #ifdef log2
125 #undef log2 /* defined in math.h */
126 #endif
127 #define log(x) fast_logf(x)
128 #define logf(x) fast_logf(x)
129 #define log2(x) fast_log2f(x)
130 #define log2f(x) fast_log2f(x)
131 #define log10(x) fast_log10f(x)
132 #define log10f(x) fast_log10f(x)
133 
134 #else /* __FAST_MATH__ */
135 
136 #define exp10(x) powf(10, x)
137 #define exp10f(x) powf(10, x)
138 
139 #endif /* __FAST_MATH__ */
140 
141 #undef RAND_MAX
142 #define RAND_MAX UINT32_MAX
143 #endif //ARM_CORTEX
144 
145 #endif // __basicmaths_h__
float fast_powf(float x, float y)
Definition: basicmaths.c:121
uint32_t fast_log2i(uint32_t x)
Definition: basicmaths.c:166
float fast_expf(float x)
Definition: basicmaths.c:125
#define abs(x)
Definition: basicmaths.h:44
float fast_fmodf(float x, float y)
Definition: basicmaths.c:161
void fast_log_set_table(const float *table, int size)
Definition: basicmaths.c:156
float randf()
generate a random number between 0 and 1
Definition: basicmaths.c:74
float fast_logf(float x)
Definition: basicmaths.c:137
#define clamp(x, lo, hi)
Definition: basicmaths.h:47
float fast_exp2f(float x)
Definition: basicmaths.c:129
uint32_t arm_rand32()
Generate an unsigned 32bit pseudo-random number using xorshifter algorithm.
Definition: basicmaths.c:67
void arm_srand32(uint32_t s)
Definition: basicmaths.c:58
float fast_exp10f(float x)
Definition: basicmaths.c:133
float fast_log2f(float x)
Definition: basicmaths.c:146
void fast_pow_set_table(const uint32_t *table, int size)
Definition: basicmaths.c:151
#define min(a, b)
Definition: basicmaths.h:38
float fast_atan2f(float a, float b)
Definition: basicmaths.c:91
float fast_log10f(float x)
Definition: basicmaths.c:141
float arm_sqrtf(float in)
Definition: basicmaths.c:78
#define max(a, b)
Definition: basicmaths.h:41