• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef _WB_CO_MATH_H
16 #define _WB_CO_MATH_H
17 
18 #include "wb_co_int.h"        // standard integer definitions
19 #include "wb_co_bool.h"       // boolean definitions
20 #include <stdlib.h>        // standard library
21 #include "compiler.h"      // for __STATIC_INLINE
22 
23 /**
24  ****************************************************************************************
25  * @brief Return value with one bit set.
26  *
27  * @param[in] pos Position of the bit to set.
28  *
29  * @return Value with one bit set.  There is no return type since this is a macro and this
30  * will be resolved by the compiler upon assignment to an l-value.
31  ****************************************************************************************
32  */
33 #define CO_BIT(pos) (1UL<<(pos))
34 
35 /**
36  ****************************************************************************************
37  * @brief Align val on the multiple of 4 equal or nearest higher.
38  * @param[in] val Value to align.
39  * @return Value aligned.
40  ****************************************************************************************
41  */
42 #define CO_ALIGN4_HI(val) (((val)+3)&~3)
43 
44 /**
45  ****************************************************************************************
46  * @brief Align val on the multiple of a given number equal or nearest higher.
47  *
48  * x value should be a power of 2.
49  *
50  * @param[in] val  Value to align.
51  * @param[in] x    Multiple value.
52  * @return Value   aligned.
53  ****************************************************************************************
54  */
55 #define CO_ALIGNx_HI(val, x) (((val)+((x)-1))&~((x)-1))
56 
57 /**
58  ****************************************************************************************
59  * @brief Align val on the multiple of 4 equal or nearest lower.
60  * @param[in] val Value to align.
61  * @return Value aligned.
62  ****************************************************************************************
63  */
64 #define CO_ALIGN4_LO(val) ((val)&~3)
65 
66 /**
67  ****************************************************************************************
68  * @brief Align val on the multiple of 2 equal or nearest higher.
69  * @param[in] val Value to align.
70  * @return Value aligned.
71  ****************************************************************************************
72  */
73 #define CO_ALIGN2_HI(val) (((val)+1)&~1)
74 
75 
76 /**
77  ****************************************************************************************
78  * @brief Align val on the multiple of 2 equal or nearest lower.
79  * @param[in] val Value to align.
80  * @return Value aligned.
81  ****************************************************************************************
82  */
83 #define CO_ALIGN2_LO(val) ((val)&~1)
84 
85 
86 /*
87  * FUNCTION DEFINTIONS
88  ****************************************************************************************
89  */
90 /**
91  ****************************************************************************************
92  * @brief Count leading zeros.
93  * @param[in] val Value to count the number of leading zeros on.
94  * @return Number of leading zeros when value is written as 32 bits.
95  ****************************************************************************************
96  */
co_clz(uint32_t val)97 __STATIC_INLINE uint32_t co_clz(uint32_t val)
98 {
99     return __CLZ(val);
100 }
101 
102 /**
103  ****************************************************************************************
104  * @brief Function to initialize the random seed.
105  * @param[in] seed The seed number to use to generate the random sequence.
106  ****************************************************************************************
107  */
co_random_init(uint32_t seed)108 __STATIC_INLINE void co_random_init(uint32_t seed)
109 {
110     srand(seed);
111 }
112 
113 /**
114  ****************************************************************************************
115  * @brief Function to get an 8 bit random number.
116  * @return Random byte value.
117  ****************************************************************************************
118  */
co_rand_byte(void)119 __STATIC_INLINE uint8_t co_rand_byte(void)
120 {
121     return (uint8_t)(rand() & 0xFF);
122 }
123 
124 /**
125  ****************************************************************************************
126  * @brief Function to get an 16 bit random number.
127  * @return Random half word value.
128  ****************************************************************************************
129  */
co_rand_hword(void)130 __STATIC_INLINE uint16_t co_rand_hword(void)
131 {
132     return (uint16_t)(rand() & 0xFFFF);
133 }
134 
135 /**
136  ****************************************************************************************
137  * @brief Function to get an 32 bit random number.
138  * @return Random word value.
139  ****************************************************************************************
140  */
co_rand_word(void)141 __STATIC_INLINE uint32_t co_rand_word(void)
142 {
143     return (uint32_t)rand();
144 }
145 
146 /**
147  ****************************************************************************************
148  * @brief Function to return the smallest of 2 unsigned 32 bits words.
149  * @param[in] a First value
150  * @param[in] b Second value
151  * @return The smallest value between a and b
152  ****************************************************************************************
153  */
co_min(uint32_t a,uint32_t b)154 __STATIC_INLINE uint32_t co_min(uint32_t a, uint32_t b)
155 {
156     return a < b ? a : b;
157 }
158 
159 /**
160  ****************************************************************************************
161  * @brief Function to return the greatest of 2 unsigned 32 bits words.
162  * @param[in] a First value
163  * @param[in] b Second value
164  * @return The greatest value between a and b
165  ****************************************************************************************
166  */
co_max(uint32_t a,uint32_t b)167 __STATIC_INLINE uint32_t co_max(uint32_t a, uint32_t b)
168 {
169     return a > b ? a : b;
170 }
171 
172 /**
173  ****************************************************************************************
174  * @brief Function to return the absolute value of a signed integer.
175  * @param[in] val Value
176  * @return The absolute value of val.
177  ****************************************************************************************
178  */
co_abs(int val)179 __STATIC_INLINE int co_abs(int val)
180 {
181     return val < 0 ? val*(-1) : val;
182 }
183 
184 /**
185  ****************************************************************************************
186  * @brief Compute a CRC32 on the buffer passed as parameter. The initial value of the
187  * computation is taken from crc parameter, allowing for incremental computation.
188  *
189  * @param[in] addr   Pointer to the buffer on which the CRC has to be computed
190  * @param[in] len    Length of the buffer
191  * @param[in] crc    The initial value of the CRC computation
192  *
193  * @return The CRC computed on the buffer.
194  ****************************************************************************************
195  */
196 uint32_t co_crc32(uint32_t addr, uint32_t len, uint32_t crc);
197 
198 uint8_t co_crc8(uint8_t *pdata, uint32_t len, uint8_t crc);
199 
200 #endif // _WB_CO_MATH_H
201