Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 1 | #ifndef _LINUX_KERNEL_H |
| 2 | #define _LINUX_KERNEL_H |
| 3 | |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 4 | #include <linux/types.h> |
AKASHI Takahiro | 0037a3d | 2019-11-13 09:44:51 +0900 | [diff] [blame] | 5 | #include <linux/printk.h> /* for printf/pr_* utilities */ |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 6 | |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 7 | #define USHRT_MAX ((u16)(~0U)) |
| 8 | #define SHRT_MAX ((s16)(USHRT_MAX>>1)) |
| 9 | #define SHRT_MIN ((s16)(-SHRT_MAX - 1)) |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 10 | #define INT_MAX ((int)(~0U>>1)) |
| 11 | #define INT_MIN (-INT_MAX - 1) |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 12 | #define UINT_MAX (~0U) |
| 13 | #define LONG_MAX ((long)(~0UL>>1)) |
| 14 | #define LONG_MIN (-LONG_MAX - 1) |
| 15 | #define ULONG_MAX (~0UL) |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 16 | #define LLONG_MAX ((long long)(~0ULL>>1)) |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 17 | #define LLONG_MIN (-LLONG_MAX - 1) |
| 18 | #define ULLONG_MAX (~0ULL) |
Simon Glass | 9bd36bc | 2014-11-24 21:18:21 -0700 | [diff] [blame] | 19 | #ifndef SIZE_MAX |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 20 | #define SIZE_MAX (~(size_t)0) |
Simon Glass | 9bd36bc | 2014-11-24 21:18:21 -0700 | [diff] [blame] | 21 | #endif |
Heinrich Schuchardt | b314e4b | 2020-08-23 09:54:44 +0200 | [diff] [blame] | 22 | #ifndef SSIZE_MAX |
| 23 | #define SSIZE_MAX ((ssize_t)(SIZE_MAX >> 1)) |
| 24 | #endif |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 25 | |
| 26 | #define U8_MAX ((u8)~0U) |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 27 | #define S8_MAX ((s8)(U8_MAX>>1)) |
| 28 | #define S8_MIN ((s8)(-S8_MAX - 1)) |
| 29 | #define U16_MAX ((u16)~0U) |
| 30 | #define S16_MAX ((s16)(U16_MAX>>1)) |
| 31 | #define S16_MIN ((s16)(-S16_MAX - 1)) |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 32 | #define U32_MAX ((u32)~0U) |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 33 | #define S32_MAX ((s32)(U32_MAX>>1)) |
| 34 | #define S32_MIN ((s32)(-S32_MAX - 1)) |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 35 | #define U64_MAX ((u64)~0ULL) |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 36 | #define S64_MAX ((s64)(U64_MAX>>1)) |
| 37 | #define S64_MIN ((s64)(-S64_MAX - 1)) |
| 38 | |
Simon Glass | 0d477ae | 2018-11-23 21:29:42 -0700 | [diff] [blame] | 39 | /* Aliases defined by stdint.h */ |
| 40 | #define UINT32_MAX U32_MAX |
| 41 | #define UINT64_MAX U64_MAX |
| 42 | |
Simon Glass | 89117ac | 2019-10-27 09:47:39 -0600 | [diff] [blame] | 43 | #define INT32_MAX S32_MAX |
| 44 | |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 45 | #define STACK_MAGIC 0xdeadbeef |
| 46 | |
| 47 | #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 48 | |
| 49 | #define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1) |
Masahiro Yamada | b148ad8 | 2017-12-21 13:51:46 +0900 | [diff] [blame] | 50 | #define ALIGN_DOWN(x, a) ALIGN((x) - ((a) - 1), (a)) |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 51 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 52 | #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) |
| 53 | #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 54 | |
| 55 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 56 | |
| 57 | /* |
| 58 | * This looks more complex than it should be. But we need to |
| 59 | * get the type for the ~ right in round_down (it needs to be |
| 60 | * as wide as the result!), and we want to evaluate the macro |
| 61 | * arguments just once each. |
| 62 | */ |
| 63 | #define __round_mask(x, y) ((__typeof__(x))((y)-1)) |
| 64 | #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) |
| 65 | #define round_down(x, y) ((x) & ~__round_mask(x, y)) |
| 66 | |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 67 | #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 68 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
| 69 | |
Masahiro Yamada | 170b687 | 2017-09-13 19:16:44 +0900 | [diff] [blame] | 70 | #define DIV_ROUND_DOWN_ULL(ll, d) \ |
| 71 | ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) |
| 72 | |
| 73 | #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d)) |
| 74 | |
Simon Glass | 64b8fe1 | 2019-12-28 10:45:09 -0700 | [diff] [blame] | 75 | #define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) |
| 76 | |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 77 | #if BITS_PER_LONG == 32 |
| 78 | # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d) |
| 79 | #else |
| 80 | # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d) |
| 81 | #endif |
| 82 | |
Masahiro Yamada | b8f8921 | 2014-11-07 03:03:30 +0900 | [diff] [blame] | 83 | /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */ |
| 84 | #define roundup(x, y) ( \ |
| 85 | { \ |
| 86 | const typeof(y) __y = y; \ |
| 87 | (((x) + (__y - 1)) / __y) * __y; \ |
| 88 | } \ |
| 89 | ) |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 90 | #define rounddown(x, y) ( \ |
| 91 | { \ |
| 92 | typeof(x) __x = (x); \ |
| 93 | __x - (__x % (y)); \ |
| 94 | } \ |
| 95 | ) |
| 96 | |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 97 | /* |
| 98 | * Divide positive or negative dividend by positive divisor and round |
| 99 | * to closest integer. Result is undefined for negative divisors and |
| 100 | * for negative dividends if the divisor variable type is unsigned. |
| 101 | */ |
| 102 | #define DIV_ROUND_CLOSEST(x, divisor)( \ |
| 103 | { \ |
| 104 | typeof(x) __x = x; \ |
| 105 | typeof(divisor) __d = divisor; \ |
| 106 | (((typeof(x))-1) > 0 || \ |
| 107 | ((typeof(divisor))-1) > 0 || (__x) > 0) ? \ |
| 108 | (((__x) + ((__d) / 2)) / (__d)) : \ |
| 109 | (((__x) - ((__d) / 2)) / (__d)); \ |
| 110 | } \ |
| 111 | ) |
Masahiro Yamada | 8b21844 | 2018-12-19 20:03:16 +0900 | [diff] [blame] | 112 | /* |
| 113 | * Same as above but for u64 dividends. divisor must be a 32-bit |
| 114 | * number. |
| 115 | */ |
| 116 | #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \ |
| 117 | { \ |
| 118 | typeof(divisor) __d = divisor; \ |
| 119 | unsigned long long _tmp = (x) + (__d) / 2; \ |
| 120 | do_div(_tmp, __d); \ |
| 121 | _tmp; \ |
| 122 | } \ |
| 123 | ) |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * Multiplies an integer by a fraction, while avoiding unnecessary |
| 127 | * overflow or loss of precision. |
| 128 | */ |
| 129 | #define mult_frac(x, numer, denom)( \ |
| 130 | { \ |
| 131 | typeof(x) quot = (x) / (denom); \ |
| 132 | typeof(x) rem = (x) % (denom); \ |
| 133 | (quot * (numer)) + ((rem * (numer)) / (denom)); \ |
| 134 | } \ |
| 135 | ) |
| 136 | |
| 137 | /** |
| 138 | * upper_32_bits - return bits 32-63 of a number |
| 139 | * @n: the number we're accessing |
| 140 | * |
| 141 | * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress |
| 142 | * the "right shift count >= width of type" warning when that quantity is |
| 143 | * 32-bits. |
| 144 | */ |
| 145 | #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) |
| 146 | |
| 147 | /** |
| 148 | * lower_32_bits - return bits 0-31 of a number |
| 149 | * @n: the number we're accessing |
| 150 | */ |
| 151 | #define lower_32_bits(n) ((u32)(n)) |
| 152 | |
| 153 | /* |
| 154 | * abs() handles unsigned and signed longs, ints, shorts and chars. For all |
| 155 | * input types abs() returns a signed long. |
| 156 | * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64() |
| 157 | * for those. |
| 158 | */ |
| 159 | #define abs(x) ({ \ |
| 160 | long ret; \ |
| 161 | if (sizeof(x) == sizeof(long)) { \ |
| 162 | long __x = (x); \ |
| 163 | ret = (__x < 0) ? -__x : __x; \ |
| 164 | } else { \ |
| 165 | int __x = (x); \ |
| 166 | ret = (__x < 0) ? -__x : __x; \ |
| 167 | } \ |
| 168 | ret; \ |
| 169 | }) |
| 170 | |
| 171 | #define abs64(x) ({ \ |
| 172 | s64 __x = (x); \ |
| 173 | (__x < 0) ? -__x : __x; \ |
| 174 | }) |
| 175 | |
| 176 | /* |
| 177 | * min()/max()/clamp() macros that also do |
| 178 | * strict type-checking.. See the |
| 179 | * "unnecessary" pointer comparison. |
| 180 | */ |
| 181 | #define min(x, y) ({ \ |
| 182 | typeof(x) _min1 = (x); \ |
| 183 | typeof(y) _min2 = (y); \ |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 184 | (void) (&_min1 == &_min2); \ |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 185 | _min1 < _min2 ? _min1 : _min2; }) |
| 186 | |
| 187 | #define max(x, y) ({ \ |
| 188 | typeof(x) _max1 = (x); \ |
| 189 | typeof(y) _max2 = (y); \ |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 190 | (void) (&_max1 == &_max2); \ |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 191 | _max1 > _max2 ? _max1 : _max2; }) |
| 192 | |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 193 | #define min3(x, y, z) min((typeof(x))min(x, y), z) |
| 194 | #define max3(x, y, z) max((typeof(x))max(x, y), z) |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 195 | |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 196 | /** |
| 197 | * min_not_zero - return the minimum that is _not_ zero, unless both are zero |
| 198 | * @x: value1 |
| 199 | * @y: value2 |
| 200 | */ |
| 201 | #define min_not_zero(x, y) ({ \ |
| 202 | typeof(x) __x = (x); \ |
| 203 | typeof(y) __y = (y); \ |
| 204 | __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) |
| 205 | |
| 206 | /** |
| 207 | * clamp - return a value clamped to a given range with strict typechecking |
| 208 | * @val: current value |
| 209 | * @lo: lowest allowable value |
| 210 | * @hi: highest allowable value |
| 211 | * |
| 212 | * This macro does strict typechecking of lo/hi to make sure they are of the |
| 213 | * same type as val. See the unnecessary pointer comparisons. |
| 214 | */ |
| 215 | #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) |
| 216 | |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 217 | /* |
| 218 | * ..and if you can't take the strict |
| 219 | * types, you can specify one yourself. |
| 220 | * |
| 221 | * Or not use min/max/clamp at all, of course. |
| 222 | */ |
| 223 | #define min_t(type, x, y) ({ \ |
| 224 | type __min1 = (x); \ |
| 225 | type __min2 = (y); \ |
| 226 | __min1 < __min2 ? __min1: __min2; }) |
| 227 | |
| 228 | #define max_t(type, x, y) ({ \ |
| 229 | type __max1 = (x); \ |
| 230 | type __max2 = (y); \ |
| 231 | __max1 > __max2 ? __max1: __max2; }) |
| 232 | |
| 233 | /** |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 234 | * clamp_t - return a value clamped to a given range using a given type |
| 235 | * @type: the type of variable to use |
| 236 | * @val: current value |
| 237 | * @lo: minimum allowable value |
| 238 | * @hi: maximum allowable value |
| 239 | * |
| 240 | * This macro does no typechecking and uses temporary variables of type |
| 241 | * 'type' to make all the comparisons. |
| 242 | */ |
| 243 | #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) |
| 244 | |
| 245 | /** |
| 246 | * clamp_val - return a value clamped to a given range using val's type |
| 247 | * @val: current value |
| 248 | * @lo: minimum allowable value |
| 249 | * @hi: maximum allowable value |
| 250 | * |
| 251 | * This macro does no typechecking and uses temporary variables of whatever |
| 252 | * type the input argument 'val' is. This is useful when val is an unsigned |
| 253 | * type and min and max are literals that will otherwise be assigned a signed |
| 254 | * integer type. |
| 255 | */ |
| 256 | #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) |
| 257 | |
Masahiro Yamada | 76a8520 | 2014-11-07 03:03:29 +0900 | [diff] [blame] | 258 | /* |
| 259 | * swap - swap value of @a and @b |
| 260 | */ |
| 261 | #define swap(a, b) \ |
| 262 | do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) |
| 263 | |
| 264 | /** |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 265 | * container_of - cast a member of a structure out to the containing structure |
| 266 | * @ptr: the pointer to the member. |
| 267 | * @type: the type of the container struct this is embedded in. |
| 268 | * @member: the name of the member within the struct. |
| 269 | * |
| 270 | */ |
| 271 | #define container_of(ptr, type, member) ({ \ |
| 272 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 273 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 274 | |
Simon Glass | 84c48c3 | 2019-12-28 10:45:08 -0700 | [diff] [blame] | 275 | /* |
| 276 | * check_member() - Check the offset of a structure member |
| 277 | * |
| 278 | * @structure: Name of structure (e.g. global_data) |
| 279 | * @member: Name of member (e.g. baudrate) |
| 280 | * @offset: Expected offset in bytes |
| 281 | */ |
| 282 | #define check_member(structure, member, offset) _Static_assert( \ |
| 283 | offsetof(struct structure, member) == (offset), \ |
| 284 | "`struct " #structure "` offset for `" #member "` is not " #offset) |
| 285 | |
Chris Packham | b0a19b3 | 2023-03-20 10:23:43 +1300 | [diff] [blame] | 286 | #define __find_closest(x, a, as, op) \ |
| 287 | ({ \ |
| 288 | typeof(as) __fc_i, __fc_as = (as) - 1; \ |
| 289 | typeof(x) __fc_x = (x); \ |
| 290 | typeof(*a) const *__fc_a = (a); \ |
| 291 | for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \ |
| 292 | if (__fc_x op DIV_ROUND_CLOSEST(__fc_a[__fc_i] + \ |
| 293 | __fc_a[__fc_i + 1], 2)) \ |
| 294 | break; \ |
| 295 | } \ |
| 296 | (__fc_i); \ |
| 297 | }) |
| 298 | |
| 299 | /** |
| 300 | * find_closest - locate the closest element in a sorted array |
| 301 | * @x: The reference value. |
| 302 | * @a: The array in which to look for the closest element. Must be sorted |
| 303 | * in ascending order. |
| 304 | * @as: Size of 'a'. |
| 305 | * |
| 306 | * Returns the index of the element closest to 'x'. |
| 307 | */ |
| 308 | #define find_closest(x, a, as) __find_closest(x, a, as, <=) |
| 309 | |
Masahiro Yamada | bd42662 | 2014-11-07 03:03:28 +0900 | [diff] [blame] | 310 | #endif |