blob: 2975103f5e14629b598897c52eecc2f9cd4458a1 [file] [log] [blame]
Scott Brandenbf404c02017-04-10 11:45:52 -07001/*
Joel Hutton5cc3bc82018-03-21 11:40:57 +00002 * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
Scott Brandenbf404c02017-04-10 11:45:52 -07003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Scott Brandenbf404c02017-04-10 11:45:52 -07005 */
6
7#ifndef __UTILS_DEF_H__
8#define __UTILS_DEF_H__
9
10/* Compute the number of elements in the given array */
11#define ARRAY_SIZE(a) \
12 (sizeof(a) / sizeof((a)[0]))
13
14#define IS_POWER_OF_TWO(x) \
15 (((x) & ((x) - 1)) == 0)
16
17#define SIZE_FROM_LOG2_WORDS(n) (4 << (n))
18
Julius Werner02eb7272017-12-12 14:23:26 -080019#define BIT(nr) (ULL(1) << (nr))
Scott Brandenbf404c02017-04-10 11:45:52 -070020
Soby Mathew327548c2017-07-13 15:19:51 +010021/*
Yann Gautier953e94d2018-06-14 18:35:33 +020022 * Create a contiguous bitmask starting at bit position @l and ending at
23 * position @h. For example
24 * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
25 */
26#define GENMASK_32(h, l) \
27 (((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h))))
28
29#define GENMASK_64(h, l) \
30 (((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h))))
31
32#ifdef AARCH32
33#define GENMASK GENMASK_32
34#else
35#define GENMASK GENMASK_64
36#endif
37
38/*
Soby Mathew327548c2017-07-13 15:19:51 +010039 * This variant of div_round_up can be used in macro definition but should not
40 * be used in C code as the `div` parameter is evaluated twice.
41 */
42#define DIV_ROUND_UP_2EVAL(n, d) (((n) + (d) - 1) / (d))
43
Julius Werner54b6d202018-01-22 13:56:13 -080044#define div_round_up(val, div) __extension__ ({ \
45 __typeof__(div) _div = (div); \
46 ((val) + _div - 1) / _div; \
47})
48
Scott Brandenbf404c02017-04-10 11:45:52 -070049#define MIN(x, y) __extension__ ({ \
50 __typeof__(x) _x = (x); \
51 __typeof__(y) _y = (y); \
52 (void)(&_x == &_y); \
53 _x < _y ? _x : _y; \
54})
55
56#define MAX(x, y) __extension__ ({ \
57 __typeof__(x) _x = (x); \
58 __typeof__(y) _y = (y); \
59 (void)(&_x == &_y); \
60 _x > _y ? _x : _y; \
61})
62
63/*
64 * The round_up() macro rounds up a value to the given boundary in a
65 * type-agnostic yet type-safe manner. The boundary must be a power of two.
66 * In other words, it computes the smallest multiple of boundary which is
67 * greater than or equal to value.
68 *
69 * round_down() is similar but rounds the value down instead.
70 */
71#define round_boundary(value, boundary) \
72 ((__typeof__(value))((boundary) - 1))
73
74#define round_up(value, boundary) \
75 ((((value) - 1) | round_boundary(value, boundary)) + 1)
76
77#define round_down(value, boundary) \
78 ((value) & ~round_boundary(value, boundary))
79
80/*
81 * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
82 * Both arguments must be unsigned pointer values (i.e. uintptr_t).
83 */
84#define check_uptr_overflow(ptr, inc) \
85 (((ptr) > UINTPTR_MAX - (inc)) ? 1 : 0)
86
87/*
Jeenu Viswambharan19f6cf22017-12-07 08:43:05 +000088 * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise.
89 * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t).
90 */
91#define check_u32_overflow(u32, inc) \
92 ((u32) > (UINT32_MAX - (inc)) ? 1 : 0)
93
94/*
Varun Wadekar05597a92017-05-18 10:32:51 -070095 * For those constants to be shared between C and other sources, apply a 'u'
96 * or 'ull' suffix to the argument only in C, to avoid undefined or unintended
97 * behaviour.
Scott Brandenbf404c02017-04-10 11:45:52 -070098 *
Varun Wadekar05597a92017-05-18 10:32:51 -070099 * The GNU assembler and linker do not support the 'u' and 'ull' suffix (it
100 * causes the build process to fail) therefore the suffix is omitted when used
101 * in linker scripts and assembler files.
Scott Brandenbf404c02017-04-10 11:45:52 -0700102*/
103#if defined(__LINKER__) || defined(__ASSEMBLY__)
Varun Wadekar05597a92017-05-18 10:32:51 -0700104# define U(_x) (_x)
Scott Brandenbf404c02017-04-10 11:45:52 -0700105# define ULL(_x) (_x)
106#else
David Cunadoc1503122018-02-16 21:12:58 +0000107# define U(_x) (_x##U)
108# define ULL(_x) (_x##ULL)
Scott Brandenbf404c02017-04-10 11:45:52 -0700109#endif
110
Julius Werner02eb7272017-12-12 14:23:26 -0800111/* Register size of the current architecture. */
112#ifdef AARCH32
113#define REGSZ U(4)
114#else
115#define REGSZ U(8)
116#endif
117
Jeenu Viswambharan0bc79d92017-08-16 11:44:25 +0100118/*
119 * Test for the current architecture version to be at least the version
120 * expected.
121 */
122#define ARM_ARCH_AT_LEAST(_maj, _min) \
123 ((ARM_ARCH_MAJOR > _maj) || \
124 ((ARM_ARCH_MAJOR == _maj) && (ARM_ARCH_MINOR >= _min)))
125
Joel Hutton5cc3bc82018-03-21 11:40:57 +0000126/*
127 * Import an assembly or linker symbol as a C expression with the specified
128 * type
129 */
130#define IMPORT_SYM(type, sym, name) \
131 extern char sym[];\
132 static const __attribute__((unused)) type name = (type) sym;
133
134/*
135 * When the symbol is used to hold a pointer, its alignment can be asserted
136 * with this macro. For example, if there is a linker symbol that is going to
137 * be used as a 64-bit pointer, the value of the linker symbol must also be
138 * aligned to 64 bit. This macro makes sure this is the case.
139 */
140#define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0)
141
142
Scott Brandenbf404c02017-04-10 11:45:52 -0700143#endif /* __UTILS_DEF_H__ */