util.h (1532B)
1 // Copyright 2022 Stefan Kerkmann (KarlK90) 2 // Copyright 2011 Jun Wako <wakojun@gmail.com> 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 5 #pragma once 6 7 #include "bits.h" 8 #include "bitwise.h" 9 10 // convert to string 11 #define STR(s) XSTR(s) 12 #define XSTR(s) #s 13 14 #if !defined(MIN) 15 # define MIN(x, y) (((x) < (y)) ? (x) : (y)) 16 #endif 17 18 #if !defined(MAX) 19 # define MAX(x, y) (((x) > (y)) ? (x) : (y)) 20 #endif 21 22 #if !defined(CEILING) 23 /** 24 * @brief Computes the rounded up result of a division of two integers at 25 * compile time. 26 */ 27 # define CEILING(dividend, divisor) (((dividend) + (divisor) - 1) / (divisor)) 28 #endif 29 30 #if !defined(IS_ARRAY) 31 /** 32 * @brief Returns true if the value is an array, false if it's a pointer. 33 * 34 * This macro is ill-formed for scalars, which is OK for its intended use in 35 * ARRAY_SIZE. 36 */ 37 # define IS_ARRAY(value) (!__builtin_types_compatible_p(typeof((value)), typeof(&(value)[0]))) 38 #endif 39 40 #if !defined(ARRAY_SIZE) 41 /** 42 * @brief Computes the number of elements of the given array at compile time. 43 * 44 * This Macro can only be used for statically allocated arrays that have not 45 * been decayed into a pointer. This is detected at compile time, though the 46 * error message for scalar values is poor. 47 */ 48 # define ARRAY_SIZE(array) (__builtin_choose_expr(IS_ARRAY((array)), sizeof((array)) / sizeof((array)[0]), (void)0)) 49 #endif 50 51 #if !defined(PACKED) 52 # define PACKED __attribute__((__packed__)) 53 #endif 54 55 #if __has_include("_util.h") 56 # include "_util.h" /* Include the platform's _util.h */ 57 #endif