1 #ifndef _CO_MATH_H_
2 #define _CO_MATH_H_
3 
4 /**
5  *****************************************************************************************
6  * @defgroup CO_MATH Math functions
7  * @ingroup COMMON
8  * @brief  Optimized math functions and other computations.
9  *
10  * @{
11  *****************************************************************************************
12  */
13 
14 /*
15  * INCLUDE FILES
16  ****************************************************************************************
17  */
18 #include <stdint.h>        // standard integer definitions
19 #include <stdbool.h>       // boolean definitions
20 #include <stdlib.h>        // standard library
21 #include "compiler.h"      // for __INLINE
22 
23 extern void srand (unsigned int seed);
24 extern int rand (void);
25 
26 /*
27  * MACROS
28  ****************************************************************************************
29  */
30 /**
31  ****************************************************************************************
32  * @brief Return value with one bit set.
33  *
34  * @param[in] pos Position of the bit to set.
35  *
36  * @return Value with one bit set.  There is no return type since this is a macro and this
37  * will be resolved by the compiler upon assignment to an l-value.
38  ****************************************************************************************
39  */
40 #define CO_BIT(pos) (1UL<<(pos))
41 
42 /**
43  ****************************************************************************************
44  * @brief Align val on the multiple of 4 equal or nearest higher.
45  * @param[in] val Value to align.
46  * @return Value aligned.
47  ****************************************************************************************
48  */
49 #define CO_ALIGN4_HI(val) (((val)+3)&~3)
50 
51 
52 /**
53  ****************************************************************************************
54  * @brief Align val on the multiple of 4 equal or nearest lower.
55  * @param[in] val Value to align.
56  * @return Value aligned.
57  ****************************************************************************************
58  */
59 #define CO_ALIGN4_LO(val) ((val)&~3)
60 
61 /**
62  ****************************************************************************************
63  * @brief Align val on the multiple of 2 equal or nearest higher.
64  * @param[in] val Value to align.
65  * @return Value aligned.
66  ****************************************************************************************
67  */
68 #define CO_ALIGN2_HI(val) (((val)+1)&~1)
69 
70 
71 /**
72  ****************************************************************************************
73  * @brief Align val on the multiple of 2 equal or nearest lower.
74  * @param[in] val Value to align.
75  * @return Value aligned.
76  ****************************************************************************************
77  */
78 #define CO_ALIGN2_LO(val) ((val)&~1)
79 
80 
81 /*
82  * FUNCTION DEFINTIONS
83  ****************************************************************************************
84  */
85 /**
86  ****************************************************************************************
87  * @brief Count leading zeros.
88  * @param[in] val Value to count the number of leading zeros on.
89  * @return Number of leading zeros when value is written as 32 bits.
90  ****************************************************************************************
91  */
co_clz(uint32_t val)92 __STATIC __INLINE uint32_t co_clz(uint32_t val)
93 {
94     #if defined(__arm__)
95     return __builtin_clz(val);
96     #elif defined(__GNUC__)
97     if (val == 0)
98     {
99         return 32;
100     }
101     return __builtin_clz(val);
102     #else
103     uint32_t i;
104     for (i = 0; i < 32; i++)
105     {
106         if (val & CO_BIT(31 - i))
107             break;
108     }
109     return i;
110     #endif // defined(__arm__)
111 }
112 
113 /**
114  ****************************************************************************************
115  * @brief Function to initialize the random seed.
116  * @param[in] seed The seed number to use to generate the random sequence.
117  ****************************************************************************************
118  */
co_random_init(uint32_t seed)119 __STATIC __INLINE void co_random_init(uint32_t seed)
120 {
121     srand(seed);
122 }
123 
124 /**
125  ****************************************************************************************
126  * @brief Function to get an 8 bit random number.
127  * @return Random byte value.
128  ****************************************************************************************
129  */
co_rand_byte(void)130 __STATIC __INLINE uint8_t co_rand_byte(void)
131 {
132     return (uint8_t)(rand() & 0xFF);
133 }
134 
135 /**
136  ****************************************************************************************
137  * @brief Function to get an 16 bit random number.
138  * @return Random half word value.
139  ****************************************************************************************
140  */
co_rand_hword(void)141 __STATIC __INLINE uint16_t co_rand_hword(void)
142 {
143     return (uint16_t)(rand() & 0xFFFF);
144 }
145 
146 /**
147  ****************************************************************************************
148  * @brief Function to get an 32 bit random number.
149  * @return Random word value.
150  ****************************************************************************************
151  */
co_rand_word(void)152 __STATIC __INLINE uint32_t co_rand_word(void)
153 {
154     return (uint32_t)rand();
155 }
156 
157 /**
158  ****************************************************************************************
159  * @brief Function to return the smallest of 2 unsigned 32 bits words.
160  * @return The smallest value.
161  ****************************************************************************************
162  */
co_min(uint32_t a,uint32_t b)163 __STATIC __INLINE uint32_t co_min(uint32_t a, uint32_t b)
164 {
165     return a < b ? a : b;
166 }
167 
168 /**
169  ****************************************************************************************
170  * @brief Function to return the greatest of 2 unsigned 32 bits words.
171  * @return The greatest value.
172  ****************************************************************************************
173  */
co_max(uint32_t a,uint32_t b)174 __STATIC __INLINE uint32_t co_max(uint32_t a, uint32_t b)
175 {
176     return a > b ? a : b;
177 }
178 
179 /**
180  ****************************************************************************************
181  * @brief Function to return the absolute value of a signed integer.
182  * @return The absolute value.
183  ****************************************************************************************
184  */
co_abs(int val)185 __STATIC __INLINE int co_abs(int val)
186 {
187     return val < 0 ? val*(-1) : val;
188 }
189 
190 /// @} CO_MATH
191 
192 
193 #endif // _CO_MATH_H_
194