1 // © 2021 Qualcomm Innovation Center, Inc. All rights reserved. 2 // 3 // SPDX-License-Identifier: BSD-3-Clause 4 5 // Miscellaneous utility macros. 6 // 7 // These all have simple definitions - no compiler builtins or other language 8 // extensions. Look in compiler.h for those. 9 10 #define util_bit(b) ((uintmax_t)1U << (b)) 11 #define util_sbit(b) ((intmax_t)1 << (b)) 12 #define util_mask(n) (util_bit(n) - 1U) 13 14 #define util_max(x, y) (((x) > (y)) ? (x) : (y)) 15 #define util_min(x, y) (((x) < (y)) ? (x) : (y)) 16 17 // Arithmetic predicates with intent that is not obvious when open-coded 18 #define util_is_p2_or_zero(x) (((x) & ((x)-1U)) == 0U) 19 #define util_is_p2(x) (((x) != 0U) && util_is_p2_or_zero(x)) 20 #define util_is_baligned(x, a) (assert(util_is_p2(a)), (((x) & ((a)-1U)) == 0U)) 21 #define util_is_p2aligned(x, b) (((x) & (util_bit(b) - 1U)) == 0U) 22 #define util_add_overflows(a, b) ((a) > ~(__typeof__((a) + (b)))(b)) 23 24 // This version can be used in static asserts 25 #define util_is_baligned_assert(x, a) \ 26 (util_is_p2(a) && (((x) & ((a)-1U)) == 0U)) 27 28 // Align up or down to bytes (which must be a power of two) 29 #if defined(__TYPED_DSL__) 30 #define util_balign_down(x, a) ((x) & ~((a)-1U)) 31 #else 32 #define util_balign_down(x, a) \ 33 (assert(util_is_p2(a)), (x) & ~((__typeof__(x))(a)-1U)) 34 #endif 35 #define util_balign_up(x, a) util_balign_down((x) + ((a)-1U), a) 36 37 // Round up or down to a multiple of an unsigned constant, which may not be a 38 // power of two. Rounding to a non-constant at runtime should be avoided, 39 // because it will perform a slow divide operation. 40 #define util_round_down(x, a) ((x) - ((x) % (a))) 41 #define util_round_up(x, a) util_round_down((x) + ((a)-1U), (a)) 42 43 // Align up or down to a power-of-two size (in bits) 44 #define util_p2align_down(x, b) \ 45 (assert((sizeof(x) * 8U) > (b)), (((x) >> (b)) << (b))) 46 #define util_p2align_up(x, b) util_p2align_down((x) + util_bit(b) - 1U, b) 47 48 // Generate an identifier that can be declared inside a macro without 49 // shadowing anything else declared in the same file, given a base name to 50 // disambiguate uses within one macro expansion. Generally the name should be 51 // prefixed with the name of the macro it's being used in. 52 // 53 // Note that this should only ever be used as a macro parameter; otherwise 54 // it is difficult to determine what identifier it expanded to. 55 #define util_cpp_unique_ident(name) util_cpp_paste_expanded(name, __LINE__) 56 57 // Paste two tokens together, after macro-expansion of the arguments. 58 #define util_cpp_paste_expanded(name, suffix) util_cpp_paste(name, suffix) 59 60 // Paste two tokens together, before macro-expansion of the arguments. 61 // 62 // This is only really useful in util_cpp_paste_expanded(). In any other macro 63 // definition, use ## directly, which is equivalent and more concise. 64 #define util_cpp_paste(name, suffix) name##suffix 65 66 // Return the number of elements in an array. 67 #define util_array_size(a) (sizeof(a) / sizeof((a)[0])) 68 69 // Return the size of a structure member. 70 #define util_sizeof_member(type, member) sizeof(((type *)NULL)->member) 71 72 // Check whether a given offset is with the bounds of a structure member 73 #define util_offset_in_range(offset, type, member) \ 74 (((offset) >= offsetof(type, member)) && \ 75 ((offset) < \ 76 offsetof(type, member) + util_sizeof_member(type, member))) 77