forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_extras_impl.h
227 lines (190 loc) · 5.22 KB
/
math_extras_impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (c) 2019 Facebook.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Inline implementation of functions declared in math_extras.h.
*/
#ifndef ZEPHYR_INCLUDE_SYS_MATH_EXTRAS_H_
#error "please include <sys/math_extras.h> instead of this file"
#endif
#include <toolchain.h>
/*
* Force the use of portable C code (no builtins) by defining
* PORTABLE_MISC_MATH_EXTRAS before including <misc/math_extras.h>.
* This is primarily for use by tests.
*
* We'll #undef use_builtin again at the end of the file.
*/
#ifdef PORTABLE_MISC_MATH_EXTRAS
#define use_builtin(x) 0
#else
#define use_builtin(x) HAS_BUILTIN(x)
#endif
#if use_builtin(__builtin_add_overflow)
static inline bool u32_add_overflow(u32_t a, u32_t b, u32_t *result)
{
return __builtin_add_overflow(a, b, result);
}
static inline bool u64_add_overflow(u64_t a, u64_t b, u64_t *result)
{
return __builtin_add_overflow(a, b, result);
}
static inline bool size_add_overflow(size_t a, size_t b, size_t *result)
{
return __builtin_add_overflow(a, b, result);
}
#else /* !use_builtin(__builtin_add_overflow) */
static inline bool u32_add_overflow(u32_t a, u32_t b, u32_t *result)
{
u32_t c = a + b;
*result = c;
return c < a;
}
static inline bool u64_add_overflow(u64_t a, u64_t b, u64_t *result)
{
u64_t c = a + b;
*result = c;
return c < a;
}
static inline bool size_add_overflow(size_t a, size_t b, size_t *result)
{
size_t c = a + b;
*result = c;
return c < a;
}
#endif /* use_builtin(__builtin_add_overflow) */
#if use_builtin(__builtin_mul_overflow)
static inline bool u32_mul_overflow(u32_t a, u32_t b, u32_t *result)
{
return __builtin_mul_overflow(a, b, result);
}
static inline bool u64_mul_overflow(u64_t a, u64_t b, u64_t *result)
{
return __builtin_mul_overflow(a, b, result);
}
static inline bool size_mul_overflow(size_t a, size_t b, size_t *result)
{
return __builtin_mul_overflow(a, b, result);
}
#else /* !use_builtin(__builtin_mul_overflow) */
static inline bool u32_mul_overflow(u32_t a, u32_t b, u32_t *result)
{
u32_t c = a * b;
*result = c;
return a != 0 && (c / a) != b;
}
static inline bool u64_mul_overflow(u64_t a, u64_t b, u64_t *result)
{
u64_t c = a * b;
*result = c;
return a != 0 && (c / a) != b;
}
static inline bool size_mul_overflow(size_t a, size_t b, size_t *result)
{
size_t c = a * b;
*result = c;
return a != 0 && (c / a) != b;
}
#endif /* use_builtin(__builtin_mul_overflow) */
/*
* The GCC builtins __builtin_clz(), __builtin_ctz(), and 64-bit
* variants are described by the GCC documentation as having undefined
* behavior when the argument is zero. See
* https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html.
*
* The undefined behavior applies to all architectures, regardless of
* the behavior of the instruction used to implement the builtin.
*
* We don't want to expose users of this API to the undefined behavior,
* so we use a conditional to explicitly provide the correct result when
* x=0.
*
* Most instruction set architectures have a CLZ instruction or similar
* that already computes the correct result for x=0. Both GCC and Clang
* know this and simply generate a CLZ instruction, optimizing away the
* conditional.
*
* For x86, and for compilers that fail to eliminate the conditional,
* there is often another opportunity for optimization since code using
* these functions tends to contain a zero check already. For example,
* from kernel/sched.c:
*
* struct k_thread *z_priq_mq_best(struct _priq_mq *pq)
* {
* if (!pq->bitmask) {
* return NULL;
* }
*
* struct k_thread *thread = NULL;
* sys_dlist_t *l =
* &pq->queues[u32_count_trailing_zeros(pq->bitmask)];
*
* ...
*
* The compiler will often be able to eliminate the redundant x == 0
* check after inlining the call to u32_count_trailing_zeros().
*/
#if use_builtin(__builtin_clz)
static inline int u32_count_leading_zeros(u32_t x)
{
return x == 0 ? 32 : __builtin_clz(x);
}
#else /* !use_builtin(__builtin_clz) */
static inline int u32_count_leading_zeros(u32_t x)
{
int b;
for (b = 0; b < 32 && (x >> 31) == 0; b++) {
x <<= 1;
}
return b;
}
#endif /* use_builtin(__builtin_clz) */
#if use_builtin(__builtin_clzll)
static inline int u64_count_leading_zeros(u64_t x)
{
return x == 0 ? 64 : __builtin_clzll(x);
}
#else /* !use_builtin(__builtin_clzll) */
static inline int u64_count_leading_zeros(u64_t x)
{
if (x == (u32_t)x) {
return 32 + u32_count_leading_zeros((u32_t)x);
} else {
return u32_count_leading_zeros(x >> 32);
}
}
#endif /* use_builtin(__builtin_clzll) */
#if use_builtin(__builtin_ctz)
static inline int u32_count_trailing_zeros(u32_t x)
{
return x == 0 ? 32 : __builtin_ctz(x);
}
#else /* !use_builtin(__builtin_ctz) */
static inline int u32_count_trailing_zeros(u32_t x)
{
int b;
for (b = 0; b < 32 && (x & 1) == 0; b++) {
x >>= 1;
}
return b;
}
#endif /* use_builtin(__builtin_ctz) */
#if use_builtin(__builtin_ctzll)
static inline int u64_count_trailing_zeros(u64_t x)
{
return x == 0 ? 64 : __builtin_ctzll(x);
}
#else /* !use_builtin(__builtin_ctzll) */
static inline int u64_count_trailing_zeros(u64_t x)
{
if ((u32_t)x) {
return u32_count_trailing_zeros((u32_t)x);
} else {
return 32 + u32_count_trailing_zeros(x >> 32);
}
}
#endif /* use_builtin(__builtin_ctzll) */
#undef use_builtin