Debian Codegraph @

crt/aws-c-common/include/aws/common/math.inl

raw
1 #ifndef AWS_COMMON_MATH_INL
2 #define AWS_COMMON_MATH_INL
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/common.h>
10 #include <aws/common/config.h>
11 #include <aws/common/math.h>
12
13 #include <limits.h>
14 #include <stdlib.h>
15
16 #ifndef __has_builtin
17 # define __has_builtin(x) 0
18 #endif
19
20 /* CBMC is its own thing */
21 #if defined(CBMC)
22 # include <aws/common/math.cbmc.inl>
23
24 /* Prefer GCC-style overflow builtins */
25 #elif defined(AWS_HAVE_GCC_OVERFLOW_MATH_EXTENSIONS) || __has_builtin(__builtin_add_overflow)
26 # include <aws/common/math.gcc_builtin.inl>
27 # include <aws/common/math.gcc_overflow.inl>
28
29 /* Fall back on GCC-style assembly, and ancient builtins available in all versions of GCC and Clang we support */
30 #elif defined(__x86_64__) && defined(AWS_HAVE_GCC_INLINE_ASM)
31 # include <aws/common/math.gcc_builtin.inl>
32 # include <aws/common/math.gcc_x64_asm.inl>
33
34 /* Fall back on GCC-style assembly, and ancient builtins available in all versions of GCC and Clang we support */
35 #elif defined(__aarch64__) && defined(AWS_HAVE_GCC_INLINE_ASM)
36 # include <aws/common/math.gcc_arm64_asm.inl>
37 # include <aws/common/math.gcc_builtin.inl>
38
39 /* On MSVC, use intrinsics */
40 #elif defined(AWS_HAVE_MSVC_INTRINSICS_X64)
41 # include <aws/common/math.msvc_x64.inl>
42
43 /* Fall back to pure C implementation */
44 #else
45 # include <aws/common/math.fallback.inl>
46 #endif
47
48 AWS_EXTERN_C_BEGIN
49
50 #ifdef _MSC_VER
51 # pragma warning(push)
52 # pragma warning(disable : 4127) /*Disable "conditional expression is constant" */
53 #elif defined(__GNUC__)
54 # pragma GCC diagnostic push
55 # if defined(__cplusplus) && !defined(__clang__)
56 # pragma GCC diagnostic ignored "-Wuseless-cast" /* Warning is C++ only (not C), and GCC only (not clang) */
57 # endif
58 #endif
59
60 AWS_STATIC_IMPL uint64_t aws_sub_u64_saturating(uint64_t a, uint64_t b) {
61 return a <= b ? 0 : a - b;
62 }
63
64 AWS_STATIC_IMPL int aws_sub_u64_checked(uint64_t a, uint64_t b, uint64_t *r) {
65 if (a < b) {
66 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
67 }
68
69 *r = a - b;
70 return AWS_OP_SUCCESS;
71 }
72
73 AWS_STATIC_IMPL uint32_t aws_sub_u32_saturating(uint32_t a, uint32_t b) {
74 return a <= b ? 0 : a - b;
75 }
76
77 AWS_STATIC_IMPL int aws_sub_u32_checked(uint32_t a, uint32_t b, uint32_t *r) {
78 if (a < b) {
79 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
80 }
81
82 *r = a - b;
83 return AWS_OP_SUCCESS;
84 }
85
86 /**
87 * Multiplies a * b. If the result overflows, returns SIZE_MAX.
88 */
89 AWS_STATIC_IMPL size_t aws_mul_size_saturating(size_t a, size_t b) {
90 #if SIZE_BITS == 32
91 return (size_t)aws_mul_u32_saturating(a, b);
92 #elif SIZE_BITS == 64
93 return (size_t)aws_mul_u64_saturating(a, b);
94 #else
95 # error "Target not supported"
96 #endif
97 }
98
99 /**
100 * Multiplies a * b and returns the result in *r. If the result
101 * overflows, returns AWS_OP_ERR; otherwise returns AWS_OP_SUCCESS.
102 */
103 AWS_STATIC_IMPL int aws_mul_size_checked(size_t a, size_t b, size_t *r) {
104 #if SIZE_BITS == 32
105 return aws_mul_u32_checked(a, b, (uint32_t *)r);
106 #elif SIZE_BITS == 64
107 return aws_mul_u64_checked(a, b, (uint64_t *)r);
108 #else
109 # error "Target not supported"
110 #endif
111 }
112
113 /**
114 * Adds a + b. If the result overflows returns SIZE_MAX.
115 */
116 AWS_STATIC_IMPL size_t aws_add_size_saturating(size_t a, size_t b) {
117 #if SIZE_BITS == 32
118 return (size_t)aws_add_u32_saturating(a, b);
119 #elif SIZE_BITS == 64
120 return (size_t)aws_add_u64_saturating(a, b);
121 #else
122 # error "Target not supported"
123 #endif
124 }
125
126 /**
127 * Adds a + b and returns the result in *r. If the result
128 * overflows, returns AWS_OP_ERR; otherwise returns AWS_OP_SUCCESS.
129 */
130 AWS_STATIC_IMPL int aws_add_size_checked(size_t a, size_t b, size_t *r) {
131 #if SIZE_BITS == 32
132 return aws_add_u32_checked(a, b, (uint32_t *)r);
133 #elif SIZE_BITS == 64
134 return aws_add_u64_checked(a, b, (uint64_t *)r);
135 #else
136 # error "Target not supported"
137 #endif
138 }
139
140 AWS_STATIC_IMPL size_t aws_sub_size_saturating(size_t a, size_t b) {
141 #if SIZE_BITS == 32
142 return (size_t)aws_sub_u32_saturating(a, b);
143 #elif SIZE_BITS == 64
144 return (size_t)aws_sub_u64_saturating(a, b);
145 #else
146 # error "Target not supported"
147 #endif
148 }
149
150 AWS_STATIC_IMPL int aws_sub_size_checked(size_t a, size_t b, size_t *r) {
151 #if SIZE_BITS == 32
152 return aws_sub_u32_checked(a, b, (uint32_t *)r);
153 #elif SIZE_BITS == 64
154 return aws_sub_u64_checked(a, b, (uint64_t *)r);
155 #else
156 # error "Target not supported"
157 #endif
158 }
159
160 /**
161 * Function to check if x is power of 2
162 */
163 AWS_STATIC_IMPL bool aws_is_power_of_two(const size_t x) {
164 /* First x in the below expression is for the case when x is 0 */
165 return x && (!(x & (x - 1)));
166 }
167
168 /**
169 * Function to find the smallest result that is power of 2 >= n. Returns AWS_OP_ERR if this cannot
170 * be done without overflow
171 */
172 AWS_STATIC_IMPL int aws_round_up_to_power_of_two(size_t n, size_t *result) {
173 if (n == 0) {
174 *result = 1;
175 return AWS_OP_SUCCESS;
176 }
177 if (n > SIZE_MAX_POWER_OF_TWO) {
178 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
179 }
180
181 n--;
182 n |= n >> 1;
183 n |= n >> 2;
184 n |= n >> 4;
185 n |= n >> 8;
186 n |= n >> 16;
187 #if SIZE_BITS == 64
188 n |= n >> 32;
189 #endif
190 n++;
191 *result = n;
192 return AWS_OP_SUCCESS;
193 }
194
195 #ifdef _MSC_VER
196 # pragma warning(pop)
197 #elif defined(__GNUC__)
198 # pragma GCC diagnostic pop
199 #endif /* _MSC_VER */
200
201 AWS_STATIC_IMPL uint8_t aws_min_u8(uint8_t a, uint8_t b) {
202 return a < b ? a : b;
203 }
204
205 AWS_STATIC_IMPL uint8_t aws_max_u8(uint8_t a, uint8_t b) {
206 return a > b ? a : b;
207 }
208
209 AWS_STATIC_IMPL int8_t aws_min_i8(int8_t a, int8_t b) {
210 return a < b ? a : b;
211 }
212
213 AWS_STATIC_IMPL int8_t aws_max_i8(int8_t a, int8_t b) {
214 return a > b ? a : b;
215 }
216
217 AWS_STATIC_IMPL uint16_t aws_min_u16(uint16_t a, uint16_t b) {
218 return a < b ? a : b;
219 }
220
221 AWS_STATIC_IMPL uint16_t aws_max_u16(uint16_t a, uint16_t b) {
222 return a > b ? a : b;
223 }
224
225 AWS_STATIC_IMPL int16_t aws_min_i16(int16_t a, int16_t b) {
226 return a < b ? a : b;
227 }
228
229 AWS_STATIC_IMPL int16_t aws_max_i16(int16_t a, int16_t b) {
230 return a > b ? a : b;
231 }
232
233 AWS_STATIC_IMPL uint32_t aws_min_u32(uint32_t a, uint32_t b) {
234 return a < b ? a : b;
235 }
236
237 AWS_STATIC_IMPL uint32_t aws_max_u32(uint32_t a, uint32_t b) {
238 return a > b ? a : b;
239 }
240
241 AWS_STATIC_IMPL int32_t aws_min_i32(int32_t a, int32_t b) {
242 return a < b ? a : b;
243 }
244
245 AWS_STATIC_IMPL int32_t aws_max_i32(int32_t a, int32_t b) {
246 return a > b ? a : b;
247 }
248
249 AWS_STATIC_IMPL uint64_t aws_min_u64(uint64_t a, uint64_t b) {
250 return a < b ? a : b;
251 }
252
253 AWS_STATIC_IMPL uint64_t aws_max_u64(uint64_t a, uint64_t b) {
254 return a > b ? a : b;
255 }
256
257 AWS_STATIC_IMPL int64_t aws_min_i64(int64_t a, int64_t b) {
258 return a < b ? a : b;
259 }
260
261 AWS_STATIC_IMPL int64_t aws_max_i64(int64_t a, int64_t b) {
262 return a > b ? a : b;
263 }
264
265 AWS_STATIC_IMPL size_t aws_min_size(size_t a, size_t b) {
266 return a < b ? a : b;
267 }
268
269 AWS_STATIC_IMPL size_t aws_max_size(size_t a, size_t b) {
270 return a > b ? a : b;
271 }
272
273 AWS_STATIC_IMPL int aws_min_int(int a, int b) {
274 return a < b ? a : b;
275 }
276
277 AWS_STATIC_IMPL int aws_max_int(int a, int b) {
278 return a > b ? a : b;
279 }
280
281 AWS_STATIC_IMPL float aws_min_float(float a, float b) {
282 return a < b ? a : b;
283 }
284
285 AWS_STATIC_IMPL float aws_max_float(float a, float b) {
286 return a > b ? a : b;
287 }
288
289 AWS_STATIC_IMPL double aws_min_double(double a, double b) {
290 return a < b ? a : b;
291 }
292
293 AWS_STATIC_IMPL double aws_max_double(double a, double b) {
294 return a > b ? a : b;
295 }
296
297 AWS_EXTERN_C_END
298
299 #endif /* AWS_COMMON_MATH_INL */