• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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  * Description: OS Abstract Layer.
15  */
16 
17 /**
18  * @defgroup osal_math osal_math
19  */
20 #ifndef __OSAL_MATH_H__
21 #define __OSAL_MATH_H__
22 
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28 
29 /**
30  * @ingroup osal_math
31  * @brief Unsigned 64bit divide with 32bit divisor.
32  *
33  * @par Description:
34  * Unsigned 64bit divide with 32bit divisor.
35  *
36  * @param dividend [in] unsigned 64bit dividend.
37  * @param divisor [in] unsigned 32bit divisor.
38  *
39  * @return Returns dividend / divisor.
40  *
41  * @attention Caller should ensure divisor not be zero
42  *
43  * @par Support System:
44  * linux liteos.
45  */
46 unsigned long long osal_div_u64(unsigned long long dividend, unsigned int divisor);
47 
48 /**
49  * @ingroup osal_math
50  * @brief Signed 64bit divide with 32bit divisor.
51  *
52  * @par Description:
53  * Signed 64bit divide with 32bit divisor.
54  *
55  * @param dividend [in] signed 64bit dividend.
56  * @param divisor [in] signed 32bit divisor.
57  *
58  * @return Returns dividend / divisor.
59  *
60  * @attention Caller should ensure divisor not be zero
61  *
62  * @par Support System:
63  * linux liteos.
64  */
65 long long osal_div_s64(long long dividend, int divisor);
66 
67 /**
68  * @ingroup osal_math
69  * @brief Unsigned 64bit divide with Unsigned 64bit divisor.
70  *
71  * @par Description:
72  * Unsigned 64bit divide with Unsigned 64bit divisor.
73  *
74  * @param dividend [in] unsigned 64bit dividend.
75  * @param divisor [in] unsigned 64bit divisor.
76  *
77  * @return Returns dividend / divisor.
78  *
79  * @attention Caller should ensure divisor not be zero
80  *
81  * @par Support System:
82  * linux liteos.
83  */
84 unsigned long long osal_div64_u64(unsigned long long dividend, unsigned long long divisor);
85 
86 /**
87  * @ingroup osal_math
88  * @brief Signed 64bit divide with 64bit divisor.
89  *
90  * @par Description:
91  * Signed 64bit divide with 64bit divisor.
92  *
93  * @param dividend [in] signed 64bit dividend.
94  * @param divisor [in] signed 64bit divisor.
95  *
96  * @return Returns dividend / divisor.
97  *
98  * @attention Caller should ensure divisor not be zero
99  *
100  * @par Support System:
101  * linux liteos.
102  */
103 long long osal_div64_s64(long long dividend, long long divisor);
104 
105 /**
106  * @ingroup osal_math
107  * @brief Unsigned 64bit divide with 32bit divisor with remainder.
108  *
109  * @par Description:
110  * Unsigned 64bit divide with 32bit divisor with remainder.
111  *
112  * @param dividend [in] unsigned 64bit dividend.
113  * @param divisor [in] unsigned 32bit divisor.
114  *
115  * @return Returns the remainder of dividend / divisor.
116  *
117  * @attention Caller should ensure divisor not be zero
118  *
119  * @par Support System:
120  * linux liteos.
121  */
122 unsigned long long osal_div_u64_rem(unsigned long long dividend, unsigned int divisor);
123 
124 /**
125  * @ingroup osal_math
126  * @brief Signed 64bit divide with 32bit divisor with remainder.
127  *
128  * @par Description:
129  * Signed 64bit divide with 32bit divisor with remainder.
130  *
131  * @param dividend [in] signed 64bit dividend.
132  * @param divisor [in] signed 32bit divisor.
133  *
134  * @return Returns the remainder of dividend / divisor.
135  *
136  * @attention Caller should ensure divisor not be zero
137  *
138  * @par Support System:
139  * linux liteos.
140  */
141 long long osal_div_s64_rem(long long dividend, int divisor);
142 
143 /**
144  * @ingroup osal_math
145  * @brief Unsigned 64bit divide with unsigned 64bit divisor with remainder.
146  *
147  * @par Description:
148  * Unsigned 64bit divide with unsigned 64bit divisor with remainder.
149  *
150  * @param dividend [in] unsigned 64bit dividend.
151  * @param divisor [in] unsigned 64bit divisor.
152  *
153  * @return Returns the remainder of dividend / divisor.
154  *
155  * @attention Caller should ensure divisor not be zero
156  *
157  * @par Support System:
158  * linux liteos.
159  */
160 unsigned long long osal_div64_u64_rem(unsigned long long dividend, unsigned long long divisor);
161 
162 /**
163  * @ingroup osal_math
164  * @brief Random number generator.
165  *
166  * @par Description:
167  * Random number generator.
168  *
169  * @return Returns random number.
170  *
171  * @par Support System:
172  * linux liteos.
173  */
174 unsigned int osal_get_random_int(void);
175 
176 #define osal_max(x, y)                 \
177     ({                                 \
178         __typeof__(x)_max1 = (x);      \
179         __typeof__(y)_max2 = (y);      \
180         (void)(&_max1 == &_max2);      \
181         _max1 > _max2 ? _max1 : _max2; \
182     })
183 
184 #define osal_min(x, y)                 \
185     ({                                 \
186         __typeof__(x)_min1 = (x);      \
187         __typeof__(y)_min2 = (y);      \
188         (void)(&_min1 == &_min2);      \
189         _min1 < _min2 ? _min1 : _min2; \
190     })
191 
192 #define osal_abs(x)                           \
193     ({                                        \
194         long ret;                             \
195         if (sizeof(x) == sizeof(long)) {      \
196             long __x = (x);                   \
197             ret = (__x < 0) ? (-__x) : (__x); \
198         } else {                              \
199             int __x = (x);                    \
200             ret = (__x < 0) ? (-__x) : (__x); \
201         }                                     \
202         ret;                                  \
203     })
204 
205 #ifdef __cplusplus
206 #if __cplusplus
207 }
208 #endif
209 #endif
210 #endif /* __OSAL_MATH_H__ */