Go to the documentation of this file.
25 #define _IC_MAKE_SINGLE_EVAL_1(macro, arg1) \
26 __builtin_constant_p(arg1) ? macro(arg1) : ({ \
27 __auto_type const _##macro##_1_ = (arg1); \
28 macro(_##macro##_1_); \
32 #define _IC_MAKE_SINGLE_EVAL_2(macro, arg1, arg2) \
33 __builtin_constant_p(arg1) && __builtin_constant_p(arg2) ? macro(arg1, arg2) : ({ \
34 __auto_type const _##macro##_1_ = (arg1); \
35 __auto_type const _##macro##_2_ = (arg2); \
36 macro(_##macro##_1_, _##macro##_2_); \
40 #define _IC_MAKE_SINGLE_EVAL_3(macro, arg1, arg2, arg3) \
41 __builtin_constant_p(arg1) && __builtin_constant_p(arg2) && __builtin_constant_p(arg3) \
42 ? macro(arg1, arg2, arg3) \
44 __auto_type const _##macro##_1_ = (arg1); \
45 __auto_type const _##macro##_2_ = (arg2); \
46 __auto_type const _##macro##_3_ = (arg3); \
47 macro(_##macro##_1_, _##macro##_2_, _##macro##_3_); \
52 #define IC_NUM_BITS(t) (sizeof(t) * 8)
55 #define IC_POINTER_TO_UINT(x) ((uintptr_t) (x))
57 #define IC_UINT_TO_POINTER(x) ((void *) (uintptr_t) (x))
59 #define IC_POINTER_TO_INT(x) ((intptr_t) (x))
61 #define IC_INT_TO_POINTER(x) ((void *) (intptr_t) (x))
67 #define IC_GENMASK32(h, l) (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (32UL - 1 - (h))))
73 #define IC_GENMASK64(h, l) (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (64ULL - 1 - (h))))
77 #define IC_G_LSB_GET(value) ((value) & -(value))
80 #define IC_LSB_GET(value) _IC_MAKE_SINGLE_EVAL_1(value)
86 #define IC_G_FIELD_GET(mask, value) (((value) & (mask)) / IC_LSB_GET(mask))
92 #define IC_FIELD_GET(mask, value) _IC_MAKE_SINGLE_EVAL_2(IC_G_FIELD_GET, mask, value)
99 #define IC_G_FIELD_PREP(mask, value) (((value) * IC_LSB_GET(mask)) & (mask))
106 #define IC_FIELD_PREP(mask, value) _IC_MAKE_SINGLE_EVAL_2(IC_G_FIELD_PREP, mask, value)
109 #define IC_ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
111 #if !defined(__cplusplus)
117 # define _IC_IS_ARRAY(array) \
118 IC_ZERO_OR_COMPILE_ERROR( \
119 !__builtin_types_compatible_p(__typeof__(array), __typeof__(&(array)[0])) \
130 # define IC_ARRAY_LENGTH(array) \
131 (unsigned int) (_IC_IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0])))
141 # define IC_ARRAY_SIZE(array) (size_t)(_IC_IS_ARRAY(array) + sizeof(array))
159 #define IC_IS_ARRAY_ELEMENT(array, ptr) \
160 ((ptr) && IC_POINTER_TO_UINT(array) <= IC_POINTER_TO_UINT(ptr) \
161 && IC_POINTER_TO_UINT(ptr) < IC_POINTER_TO_UINT(&(array)[IC_ARRAY_LENGTH(array)]) \
162 && (IC_POINTER_TO_UINT(ptr) - IC_POINTER_TO_UINT(array)) % sizeof((array)[0]) == 0)
178 #define IC_ARRAY_INDEX(array, ptr) \
179 IC_IS_ARRAY_ELEMENT(array, ptr) ? (__typeof__((array)[0]) *) (ptr) - (array) : -1
191 #define IC_PART_OF_ARRAY(array, ptr) \
192 ((ptr) && IC_POINTER_TO_UINT(array) <= IC_POINTER_TO_UINT(ptr) \
193 && IC_POINTER_TO_UINT(ptr) < IC_POINTER_TO_UINT(&(array)[IC_ARRAY_LENGTH(array)]))
212 #define IC_ARRAY_INDEX_FLOOR(array, ptr) \
213 IC_PART_OF_ARRAY(array, ptr) \
214 ? (IC_POINTER_TO_UINT(ptr) - IC_POINTER_TO_UINT(array)) / sizeof((array)[0]) : -1
223 #define IC_SAME_TYPE(a, b) __builtin_types_compatible_p(__typeof__(a), __typeof__(b))
229 # define _IC_CONTAINER_OF_VALIDATE(ptr, type, field) \
231 IC_SAME_TYPE(*(ptr), ((type *) 0)->field) || IC_SAME_TYPE(*(ptr), void), \
232 "pointer type mismatch in IC_CONTAINER_OF" \
235 # define _IC_CONTAINER_OF_VALIDATE(ptr, type, field)
259 #define IC_CONTAINER_OF(ptr, type, field) \
261 _IC_CONTAINER_OF_VALIDATE(ptr, type, field) \
262 ((type *) (((char *) (ptr)) - offsetof(type, field))); \
271 #define IC_G_IROUND_UP(x, align) \
272 ((((unsigned long) (x) + ((unsigned long) (align) -1)) / (unsigned long) (align)) \
273 * (unsigned long) (align))
280 #define IC_IROUND_UP(x, align) _IC_MAKE_SINGLE_EVAL_2(IC_G_IROUND_UP, x, align)
288 #define IC_G_IROUND_DOWN(x, align) \
289 (((unsigned long) (x) / (unsigned long) (align)) * (unsigned long) (align));
296 #define IC_IROUND_DOWN(x, align) _IC_MAKE_SINGLE_EVAL_2(IC_IROUND_DOWN, x, align)
299 #define IC_WB_UP(x) IC_IROUND_UP(x, sizeof(void *))
302 #define IC_WB_DN(x) IC_IROUND_DOWN(x, sizeof(void *))
313 #define IC_G_CEIL(x) ceil(x)
328 IC_SAME_TYPE(x, float) || IC_SAME_TYPE(x, double) || IC_SAME_TYPE(x, long double), \
329 "Argument must be a floating point number!" \
332 IC_REQUIRE_CONSTANT(ceil(x)); \
345 #define IC_G_FLOOR(x) floor(x)
356 #define IC_FLOOR(x) \
359 IC_SAME_TYPE(x, float) || IC_SAME_TYPE(x, double) || IC_SAME_TYPE(x, long double), \
360 "Argument must be a floating point number!" \
363 IC_REQUIRE_CONSTANT(floor(x)); \
375 #define IC_G_ROUND(x) (__typeof__(x)) (int64_t) ((x) + ((x) >= 0 ? 0.5 : -0.5))
388 #define IC_ROUND(x) \
391 IC_SAME_TYPE(x, float) || IC_SAME_TYPE(x, double) || IC_SAME_TYPE(x, long double), \
392 "Argument must be a floating point number!" \
394 IC_BUILD_ASSERT((x) >= INT64_MIN || (x) <= INT64_MAX); \
395 IC_REQUIRE_CONSTANT(IC_G_ROUND(x)); \
412 #define IC_G_DIV_ROUND_UP(n, d) (((n) + (d) -1) / (d))
430 #define IC_DIV_ROUND_UP(n, d) _IC_MAKE_SINGLE_EVAL_2(IC_G_DIV_ROUND_UP, n, d)
447 #define IC_G_DIV_ROUND_CLOSEST(n, d) \
448 ((((n) < 0) ^ ((d) < 0)) ? ((n) - ((d) / 2)) / (d) : ((n) + ((d) / 2)) / (d))
467 #define IC_DIV_ROUND_CLOSEST(n, d) _IC_MAKE_SINGLE_EVAL_2(IC_G_DIV_ROUND_CLOSEST, n, d)
477 #define IC_G_MAX(a, b) (((a) > (b)) ? (a) : (b))
489 #define IC_MAX(a, b) _IC_MAKE_SINGLE_EVAL_2(IC_G_MAX, a, b)
500 #define IC_G_MIN(a, b) (((a) < (b)) ? (a) : (b))
512 #define IC_MIN(a, b) _IC_MAKE_SINGLE_EVAL_2(IC_G_MIN, a, b)
523 #define IC_G_CLAMP(val, low, high) (((val) <= (low)) ? (low) : IC_MIN(val, high))
536 #define IC_CLAMP(val, low, high) _IC_MAKE_SINGLE_EVAL_3(IC_G_CLAMP, val, low, high)
548 #define IC_G_IN_RANGE(val, min, max) \
549 ((__typeof__(val)) -1) > 0 ? ((val) <= (max)) : ((val) >= (min) && (val) <= (max))
563 #define IC_IN_RANGE(val, min, max) _IC_MAKE_SINGLE_EVAL_3(IC_G_IN_RANGE, val, min, max)
572 #define IC_G_ILOG2(x) \
573 (sizeof(__typeof__(x)) > 4 ? (64 - __builtin_clzll(x) - 1) : (32 - __builtin_clz(x) - 1))
582 #define IC_ILOG2(x) \
583 __builtin_constant_p(x) ? IC_G_ILOG2(x) : ({ \
584 __auto_type const _ic_z_ilog2_x_ = (IC_Z_REQUIRE_UINT(x)); \
585 _ic_z_ilog2_x_ < 1 ? -1 : IC_G_ILOG2(_ic_z_ilog2_x_); \
595 #define IC_G_LOG2CEIL(x) (x) < 1 ? 0 : IC_G_ILOG2((x) -1) + 1
604 #define IC_LOG2CEIL(x) _IC_MAKE_SINGLE_EVAL_1(IC_G_LOG2CEIL, x)
615 #define IC_G_NHPOT(x) ((x) < 1 ? 1 : ((x) > (1ULL << 63) ? 0 : 1ULL << IC_G_LOG2CEIL(x)))
629 #define IC_NHPOT(x) _IC_MAKE_SINGLE_EVAL_1(IC_G_NHPOT, x)
637 #define IC_G_POW2_CEIL(x) ((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) -1))))
645 #define IC_POW2_CEIL(x) _IC_MAKE_SINGLE_EVAL_1(IC_G_POW2_CEIL, x)
653 #define IC_G_IS_POW2(x) (((x) != 0) && (((x) & ((x) -1)) == 0))
661 #define IC_IS_POW2(x) _IC_MAKE_SINGLE_EVAL_1(IC_G_IS_POW2)
664 #define IC_BIT(n) (1UL << (n))
667 #define IC_BIT64(n) (1ULL << (n))
678 #define IC_G_WRITE_BIT(var, bit, set) (set) ? ((var) | IC_BIT(bit)) : ((var) & ~IC_BIT(bit))
689 #define IC_WRITE_BIT(var, bit, set) _IC_MAKE_SINGLE_EVAL_3(IC_G_WRITE_BIT, var, bit, set)
695 #define IC_BIT_MASK(n) (IC_BIT(n) - 1UL)
701 #define IC_BIT64_MASK(n) (IC_BIT64(n) - 1ULL)
711 #define IC_G_IS_SHIFTED_BIT_MASK(m, s) (!(((m) >> (s)) & (((m) >> (s)) + 1U)))
721 #define IC_IS_SHIFTED_BIT_MASK(m, s) _IC_MAKE_SINGLE_EVAL_2(IC_G_IS_SHIFTED_BIT_MASK, m, s)
728 #define IC_G_IS_BIT_MASK(m) IC_G_IS_SHIFTED_BIT_MASK(m, 0)
735 #define IC_IS_BIT_MASK(m) IC_IS_SHIFTED_BIT_MASK(m, 0)
742 #define IC_G_BSWAP24(x) \
743 (((x) >> 16) & 0x0000FF) | (((x) >> 00) & 0x00FF00) | ((((x) & 0x0000FF) << 16))
750 #define IC_BSWAP24(x) _IC_MAKE_SINGLE_EVAL_1(IC_G_BSWAP24, x)