blob: a305df0cd55c877e8579d05c61f02f238088bc46 [file] [log] [blame]
Julius Werner98eeb962019-12-11 15:47:42 -08001/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only */
2
3#ifndef COMMONLIB_BSD_HELPERS_H
4#define COMMONLIB_BSD_HELPERS_H
5
6#ifndef __ASSEMBLER__
7#include <commonlib/bsd/compiler.h>
8#include <stddef.h>
9#endif
10
11#ifndef ARRAY_SIZE
12#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
13#endif
14
15#define ALIGN(x, a) __ALIGN_MASK(x, (__typeof__(x))(a)-1UL)
16#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
17#define ALIGN_UP(x, a) ALIGN((x), (a))
18#define ALIGN_DOWN(x, a) ((x) & ~((__typeof__(x))(a)-1UL))
19#define IS_ALIGNED(x, a) (((x) & ((__typeof__(x))(a)-1UL)) == 0)
20
21/* Double-evaluation unsafe min/max, for bitfields and outside of functions */
22#define __CMP_UNSAFE(a, b, op) ((a) op (b) ? (a) : (b))
23#define MIN_UNSAFE(a, b) __CMP_UNSAFE(a, b, <)
24#define MAX_UNSAFE(a, b) __CMP_UNSAFE(a, b, >)
25
26#define __CMP_SAFE(a, b, op, var_a, var_b) ({ \
27 __TYPEOF_UNLESS_CONST(a, b) var_a = (a); \
28 __TYPEOF_UNLESS_CONST(b, a) var_b = (b); \
29 var_a op var_b ? var_a : var_b; \
30})
31
32#define __CMP(a, b, op) __builtin_choose_expr( \
33 __builtin_constant_p(a) && __builtin_constant_p(b), \
34 __CMP_UNSAFE(a, b, op), __CMP_SAFE(a, b, op, __TMPNAME, __TMPNAME))
35
36#ifndef MIN
37#define MIN(a, b) __CMP(a, b, <)
38#endif
39#ifndef MAX
40#define MAX(a, b) __CMP(a, b, >)
41#endif
42
43#ifndef ABS
44#define ABS(a) ({ \
45 __typeof__(a) _abs_local_a = (a); \
46 (_abs_local_a < 0) ? (-_abs_local_a) : _abs_local_a; \
47})
48#endif
49
50#define IS_POWER_OF_2(x) ({ \
51 __typeof__(x) _power_local_x = (x); \
52 (_power_local_x & (_power_local_x - 1)) == 0; \
53})
54
55#define DIV_ROUND_UP(x, y) ({ \
56 __typeof__(x) _div_local_x = (x); \
57 __typeof__(y) _div_local_y = (y); \
58 (_div_local_x + _div_local_y - 1) / _div_local_y; \
59})
60
61#define SWAP(a, b) do { \
62 __typeof__(&(a)) _swap_local_a = &(a); \
63 __typeof__(&(b)) _swap_local_b = &(b); \
64 __typeof__(a) _swap_local_tmp = *_swap_local_a; \
65 *_swap_local_a = *_swap_local_b; \
66 *_swap_local_b = _swap_local_tmp; \
67} while (0)
68
69/* Standard units. */
70#define KiB (1<<10)
71#define MiB (1<<20)
72#define GiB (1<<30)
73
74#define KHz (1000)
75#define MHz (1000 * KHz)
76#define GHz (1000 * MHz)
77
78#ifndef offsetof
79#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
80#endif
81
82#define check_member(structure, member, offset) _Static_assert( \
83 offsetof(struct structure, member) == offset, \
84 "`struct " #structure "` offset for `" #member "` is not " #offset)
85
86/* Calculate size of structure member. */
87#define member_size(type, member) (sizeof(((type *)0)->member))
88
89#endif /* COMMONLIB_BSD_HELPERS_H */