Scott Branden | 53d9c9c | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 1 | /* |
Joel Hutton | 9f85f9e | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved. |
Scott Branden | 53d9c9c | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 3 | * |
dp-arm | 82cb2c1 | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Scott Branden | 53d9c9c | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 5 | */ |
| 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 Werner | 155a100 | 2017-12-12 14:23:26 -0800 | [diff] [blame] | 19 | #define BIT(nr) (ULL(1) << (nr)) |
Scott Branden | 53d9c9c | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 20 | |
Soby Mathew | ebf1ca1 | 2017-07-13 15:19:51 +0100 | [diff] [blame] | 21 | /* |
| 22 | * This variant of div_round_up can be used in macro definition but should not |
| 23 | * be used in C code as the `div` parameter is evaluated twice. |
| 24 | */ |
| 25 | #define DIV_ROUND_UP_2EVAL(n, d) (((n) + (d) - 1) / (d)) |
| 26 | |
Julius Werner | 7baa7bc | 2018-01-22 13:56:13 -0800 | [diff] [blame] | 27 | #define div_round_up(val, div) __extension__ ({ \ |
| 28 | __typeof__(div) _div = (div); \ |
| 29 | ((val) + _div - 1) / _div; \ |
| 30 | }) |
| 31 | |
Scott Branden | 53d9c9c | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 32 | #define MIN(x, y) __extension__ ({ \ |
| 33 | __typeof__(x) _x = (x); \ |
| 34 | __typeof__(y) _y = (y); \ |
| 35 | (void)(&_x == &_y); \ |
| 36 | _x < _y ? _x : _y; \ |
| 37 | }) |
| 38 | |
| 39 | #define MAX(x, y) __extension__ ({ \ |
| 40 | __typeof__(x) _x = (x); \ |
| 41 | __typeof__(y) _y = (y); \ |
| 42 | (void)(&_x == &_y); \ |
| 43 | _x > _y ? _x : _y; \ |
| 44 | }) |
| 45 | |
| 46 | /* |
| 47 | * The round_up() macro rounds up a value to the given boundary in a |
| 48 | * type-agnostic yet type-safe manner. The boundary must be a power of two. |
| 49 | * In other words, it computes the smallest multiple of boundary which is |
| 50 | * greater than or equal to value. |
| 51 | * |
| 52 | * round_down() is similar but rounds the value down instead. |
| 53 | */ |
| 54 | #define round_boundary(value, boundary) \ |
| 55 | ((__typeof__(value))((boundary) - 1)) |
| 56 | |
| 57 | #define round_up(value, boundary) \ |
| 58 | ((((value) - 1) | round_boundary(value, boundary)) + 1) |
| 59 | |
| 60 | #define round_down(value, boundary) \ |
| 61 | ((value) & ~round_boundary(value, boundary)) |
| 62 | |
| 63 | /* |
| 64 | * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise. |
| 65 | * Both arguments must be unsigned pointer values (i.e. uintptr_t). |
| 66 | */ |
| 67 | #define check_uptr_overflow(ptr, inc) \ |
| 68 | (((ptr) > UINTPTR_MAX - (inc)) ? 1 : 0) |
| 69 | |
| 70 | /* |
Jeenu Viswambharan | 30d81c3 | 2017-12-07 08:43:05 +0000 | [diff] [blame^] | 71 | * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise. |
| 72 | * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t). |
| 73 | */ |
| 74 | #define check_u32_overflow(u32, inc) \ |
| 75 | ((u32) > (UINT32_MAX - (inc)) ? 1 : 0) |
| 76 | |
| 77 | /* |
Varun Wadekar | 6176b4f | 2017-05-18 10:32:51 -0700 | [diff] [blame] | 78 | * For those constants to be shared between C and other sources, apply a 'u' |
| 79 | * or 'ull' suffix to the argument only in C, to avoid undefined or unintended |
| 80 | * behaviour. |
Scott Branden | 53d9c9c | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 81 | * |
Varun Wadekar | 6176b4f | 2017-05-18 10:32:51 -0700 | [diff] [blame] | 82 | * The GNU assembler and linker do not support the 'u' and 'ull' suffix (it |
| 83 | * causes the build process to fail) therefore the suffix is omitted when used |
| 84 | * in linker scripts and assembler files. |
Scott Branden | 53d9c9c | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 85 | */ |
| 86 | #if defined(__LINKER__) || defined(__ASSEMBLY__) |
Varun Wadekar | 6176b4f | 2017-05-18 10:32:51 -0700 | [diff] [blame] | 87 | # define U(_x) (_x) |
Scott Branden | 53d9c9c | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 88 | # define ULL(_x) (_x) |
| 89 | #else |
David Cunado | 5724481 | 2018-02-16 21:12:58 +0000 | [diff] [blame] | 90 | # define U(_x) (_x##U) |
| 91 | # define ULL(_x) (_x##ULL) |
Scott Branden | 53d9c9c | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 92 | #endif |
| 93 | |
Julius Werner | 155a100 | 2017-12-12 14:23:26 -0800 | [diff] [blame] | 94 | /* Register size of the current architecture. */ |
| 95 | #ifdef AARCH32 |
| 96 | #define REGSZ U(4) |
| 97 | #else |
| 98 | #define REGSZ U(8) |
| 99 | #endif |
| 100 | |
Jeenu Viswambharan | f45e232 | 2017-08-16 11:44:25 +0100 | [diff] [blame] | 101 | /* |
| 102 | * Test for the current architecture version to be at least the version |
| 103 | * expected. |
| 104 | */ |
| 105 | #define ARM_ARCH_AT_LEAST(_maj, _min) \ |
| 106 | ((ARM_ARCH_MAJOR > _maj) || \ |
| 107 | ((ARM_ARCH_MAJOR == _maj) && (ARM_ARCH_MINOR >= _min))) |
| 108 | |
Joel Hutton | 9f85f9e | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 109 | /* |
| 110 | * Import an assembly or linker symbol as a C expression with the specified |
| 111 | * type |
| 112 | */ |
| 113 | #define IMPORT_SYM(type, sym, name) \ |
| 114 | extern char sym[];\ |
| 115 | static const __attribute__((unused)) type name = (type) sym; |
| 116 | |
| 117 | /* |
| 118 | * When the symbol is used to hold a pointer, its alignment can be asserted |
| 119 | * with this macro. For example, if there is a linker symbol that is going to |
| 120 | * be used as a 64-bit pointer, the value of the linker symbol must also be |
| 121 | * aligned to 64 bit. This macro makes sure this is the case. |
| 122 | */ |
| 123 | #define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0) |
| 124 | |
| 125 | |
Scott Branden | 53d9c9c | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 126 | #endif /* __UTILS_DEF_H__ */ |