src/cx/common.h

changeset 970
c9b02747cfc5
parent 959
0e1bf3c199bf
equal deleted inserted replaced
969:72e5432f6b42 970:c9b02747cfc5
124 * @param arr the array identifier 124 * @param arr the array identifier
125 * @return the number of elements 125 * @return the number of elements
126 */ 126 */
127 #define cx_nmemb(arr) (sizeof(arr)/sizeof((arr)[0])) 127 #define cx_nmemb(arr) (sizeof(arr)/sizeof((arr)[0]))
128 128
129 // cx_szmul() definition
130 #if (__GNUC__ >= 5 || defined(__clang__)) && !defined(CX_NO_SZMUL_BUILTIN)
131 #define CX_SZMUL_BUILTIN
132
133 /**
134 * Alias for \c __builtin_mul_overflow.
135 *
136 * Performs a multiplication of size_t values and checks for overflow.
137 *
138 * @param a first operand
139 * @param b second operand
140 * @param result a pointer to a size_t, where the result should
141 * be stored
142 * @return zero, if no overflow occurred and the result is correct, non-zero
143 * otherwise
144 */
145 #define cx_szmul(a, b, result) __builtin_mul_overflow(a, b, result)
146
147 #else // no GNUC or clang bultin
148
149 /**
150 * Performs a multiplication of size_t values and checks for overflow.
151 *
152 * @param a first operand
153 * @param b second operand
154 * @param result a pointer to a size_t, where the result should
155 * be stored
156 * @return zero, if no overflow occurred and the result is correct, non-zero
157 * otherwise
158 */
159 #define cx_szmul(a, b, result) cx_szmul_impl(a, b, result)
160
161 /**
162 * Performs a multiplication of size_t values and checks for overflow.
163 *
164 * This is a custom implementation in case there is no compiler builtin
165 * available.
166 *
167 * @param a first operand
168 * @param b second operand
169 * @param result a pointer to a size_t where the result should be stored
170 * @return zero, if no overflow occurred and the result is correct, non-zero
171 * otherwise
172 */
173 int cx_szmul_impl(size_t a, size_t b, size_t *result);
174
175 #endif // cx_szmul
176
129 // Compiler specific stuff 177 // Compiler specific stuff
130 178
131 #ifndef __GNUC__ 179 #ifndef __GNUC__
132 /** 180 /**
133 * Removes GNU C attributes where they are not supported. 181 * Removes GNU C attributes where they are not supported.

mercurial