Debian Codegraph @

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

raw symbols SCIP
1 #ifndef AWS_COMMON_MATH_H
2 #define AWS_COMMON_MATH_H
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
12 #include <limits.h>
13 #include <stdlib.h>
14
15 AWS_PUSH_SANE_WARNING_LEVEL
16
17 /* The number of bits in a size_t variable */
18 #if SIZE_MAX == UINT32_MAX
19 # define SIZE_BITS 32
20 #elif SIZE_MAX == UINT64_MAX
21 # define SIZE_BITS 64
22 #else
23 # error "Target not supported"
24 #endif
25
26 /* The largest power of two that can be stored in a size_t */
27 #define SIZE_MAX_POWER_OF_TWO (((size_t)1) << (SIZE_BITS - 1))
28
29 AWS_EXTERN_C_BEGIN
30
31 /**
32 * Multiplies a * b. If the result overflows, returns 2^64 - 1.
33 */
34 AWS_STATIC_IMPL uint64_t aws_mul_u64_saturating(uint64_t a, uint64_t b);
35
36 /**
37 * If a * b overflows, returns AWS_OP_ERR; otherwise multiplies
38 * a * b, returns the result in *r, and returns AWS_OP_SUCCESS.
39 */
40 AWS_STATIC_IMPL int aws_mul_u64_checked(uint64_t a, uint64_t b, uint64_t *r);
41
42 /**
43 * Multiplies a * b. If the result overflows, returns 2^32 - 1.
44 */
45 AWS_STATIC_IMPL uint32_t aws_mul_u32_saturating(uint32_t a, uint32_t b);
46
47 /**
48 * If a * b overflows, returns AWS_OP_ERR; otherwise multiplies
49 * a * b, returns the result in *r, and returns AWS_OP_SUCCESS.
50 */
51 AWS_STATIC_IMPL int aws_mul_u32_checked(uint32_t a, uint32_t b, uint32_t *r);
52
53 /**
54 * Adds a + b. If the result overflows returns 2^64 - 1.
55 */
56 AWS_STATIC_IMPL uint64_t aws_add_u64_saturating(uint64_t a, uint64_t b);
57
58 /**
59 * If a + b overflows, returns AWS_OP_ERR; otherwise adds
60 * a + b, returns the result in *r, and returns AWS_OP_SUCCESS.
61 */
62 AWS_STATIC_IMPL int aws_add_u64_checked(uint64_t a, uint64_t b, uint64_t *r);
63
64 /**
65 * Adds a + b. If the result overflows returns 2^32 - 1.
66 */
67 AWS_STATIC_IMPL uint32_t aws_add_u32_saturating(uint32_t a, uint32_t b);
68
69 /**
70 * If a + b overflows, returns AWS_OP_ERR; otherwise adds
71 * a + b, returns the result in *r, and returns AWS_OP_SUCCESS.
72 */
73 AWS_STATIC_IMPL int aws_add_u32_checked(uint32_t a, uint32_t b, uint32_t *r);
74
75 /**
76 * Subtracts a - b. If the result overflows returns 0.
77 */
78 AWS_STATIC_IMPL uint64_t aws_sub_u64_saturating(uint64_t a, uint64_t b);
79
80 /**
81 * If a - b overflows, returns AWS_OP_ERR; otherwise subtracts
82 * a - b, returns the result in *r, and returns AWS_OP_SUCCESS.
83 */
84 AWS_STATIC_IMPL int aws_sub_u64_checked(uint64_t a, uint64_t b, uint64_t *r);
85
86 /**
87 * Subtracts a - b. If the result overflows returns 0.
88 */
89 AWS_STATIC_IMPL uint32_t aws_sub_u32_saturating(uint32_t a, uint32_t b);
90
91 /**
92 * If a - b overflows, returns AWS_OP_ERR; otherwise subtracts
93 * a - b, returns the result in *r, and returns AWS_OP_SUCCESS.
94 */
95 AWS_STATIC_IMPL int aws_sub_u32_checked(uint32_t a, uint32_t b, uint32_t *r);
96
97 /**
98 * Multiplies a * b. If the result overflows, returns SIZE_MAX.
99 */
100 AWS_STATIC_IMPL size_t aws_mul_size_saturating(size_t a, size_t b);
101
102 /**
103 * Multiplies a * b and returns the result in *r. If the result
104 * overflows, returns AWS_OP_ERR; otherwise returns AWS_OP_SUCCESS.
105 */
106 AWS_STATIC_IMPL int aws_mul_size_checked(size_t a, size_t b, size_t *r);
107
108 /**
109 * Adds a + b. If the result overflows returns SIZE_MAX.
110 */
111 AWS_STATIC_IMPL size_t aws_add_size_saturating(size_t a, size_t b);
112
113 /**
114 * Adds a + b and returns the result in *r. If the result
115 * overflows, returns AWS_OP_ERR; otherwise returns AWS_OP_SUCCESS.
116 */
117 AWS_STATIC_IMPL int aws_add_size_checked(size_t a, size_t b, size_t *r);
118
119 /**
120 * Adds [num] arguments (expected to be of size_t), and returns the result in *r.
121 * If the result overflows, returns AWS_OP_ERR; otherwise returns AWS_OP_SUCCESS.
122 */
123 AWS_COMMON_API int aws_add_size_checked_varargs(size_t num, size_t *r, ...);
124
125 /**
126 * Subtracts a - b. If the result overflows returns 0.
127 */
128 AWS_STATIC_IMPL size_t aws_sub_size_saturating(size_t a, size_t b);
129
130 /**
131 * If a - b overflows, returns AWS_OP_ERR; otherwise subtracts
132 * a - b, returns the result in *r, and returns AWS_OP_SUCCESS.
133 */
134 AWS_STATIC_IMPL int aws_sub_size_checked(size_t a, size_t b, size_t *r);
135
136 /**
137 * Function to check if x is power of 2
138 */
139 AWS_STATIC_IMPL bool aws_is_power_of_two(const size_t x);
140 /**
141 * Function to find the smallest result that is power of 2 >= n. Returns AWS_OP_ERR if this cannot
142 * be done without overflow
143 */
144 AWS_STATIC_IMPL int aws_round_up_to_power_of_two(size_t n, size_t *result);
145
146 /**
147 * Counts the number of leading 0 bits in an integer. 0 will return the size of the integer in bits.
148 */
149 AWS_STATIC_IMPL size_t aws_clz_u32(uint32_t n);
150 AWS_STATIC_IMPL size_t aws_clz_i32(int32_t n);
151 AWS_STATIC_IMPL size_t aws_clz_u64(uint64_t n);
152 AWS_STATIC_IMPL size_t aws_clz_i64(int64_t n);
153 AWS_STATIC_IMPL size_t aws_clz_size(size_t n);
154
155 /**
156 * Counts the number of trailing 0 bits in an integer. 0 will return the size of the integer in bits.
157 */
158 AWS_STATIC_IMPL size_t aws_ctz_u32(uint32_t n);
159 AWS_STATIC_IMPL size_t aws_ctz_i32(int32_t n);
160 AWS_STATIC_IMPL size_t aws_ctz_u64(uint64_t n);
161 AWS_STATIC_IMPL size_t aws_ctz_i64(int64_t n);
162 AWS_STATIC_IMPL size_t aws_ctz_size(size_t n);
163
164 AWS_STATIC_IMPL uint8_t aws_min_u8(uint8_t a, uint8_t b);
165 AWS_STATIC_IMPL uint8_t aws_max_u8(uint8_t a, uint8_t b);
166 AWS_STATIC_IMPL int8_t aws_min_i8(int8_t a, int8_t b);
167 AWS_STATIC_IMPL int8_t aws_max_i8(int8_t a, int8_t b);
168 AWS_STATIC_IMPL uint16_t aws_min_u16(uint16_t a, uint16_t b);
169 AWS_STATIC_IMPL uint16_t aws_max_u16(uint16_t a, uint16_t b);
170 AWS_STATIC_IMPL int16_t aws_min_i16(int16_t a, int16_t b);
171 AWS_STATIC_IMPL int16_t aws_max_i16(int16_t a, int16_t b);
172 AWS_STATIC_IMPL uint32_t aws_min_u32(uint32_t a, uint32_t b);
173 AWS_STATIC_IMPL uint32_t aws_max_u32(uint32_t a, uint32_t b);
174 AWS_STATIC_IMPL int32_t aws_min_i32(int32_t a, int32_t b);
175 AWS_STATIC_IMPL int32_t aws_max_i32(int32_t a, int32_t b);
176 AWS_STATIC_IMPL uint64_t aws_min_u64(uint64_t a, uint64_t b);
177 AWS_STATIC_IMPL uint64_t aws_max_u64(uint64_t a, uint64_t b);
178 AWS_STATIC_IMPL int64_t aws_min_i64(int64_t a, int64_t b);
179 AWS_STATIC_IMPL int64_t aws_max_i64(int64_t a, int64_t b);
180 AWS_STATIC_IMPL size_t aws_min_size(size_t a, size_t b);
181 AWS_STATIC_IMPL size_t aws_max_size(size_t a, size_t b);
182 AWS_STATIC_IMPL int aws_min_int(int a, int b);
183 AWS_STATIC_IMPL int aws_max_int(int a, int b);
184 AWS_STATIC_IMPL float aws_min_float(float a, float b);
185 AWS_STATIC_IMPL float aws_max_float(float a, float b);
186 AWS_STATIC_IMPL double aws_min_double(double a, double b);
187 AWS_STATIC_IMPL double aws_max_double(double a, double b);
188
189 AWS_EXTERN_C_END
190 #ifndef AWS_NO_STATIC_IMPL
191 # include <aws/common/math.inl>
192 #endif /* AWS_NO_STATIC_IMPL */
193
194 AWS_POP_SANE_WARNING_LEVEL
195
196 #endif /* AWS_COMMON_MATH_H */