Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Scott Branden | bf404c0 | 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 | 02eb727 | 2017-12-12 14:23:26 -0800 | [diff] [blame] | 19 | #define BIT(nr) (ULL(1) << (nr)) |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 20 | |
Soby Mathew | 327548c | 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 | |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 27 | #define MIN(x, y) __extension__ ({ \ |
| 28 | __typeof__(x) _x = (x); \ |
| 29 | __typeof__(y) _y = (y); \ |
| 30 | (void)(&_x == &_y); \ |
| 31 | _x < _y ? _x : _y; \ |
| 32 | }) |
| 33 | |
| 34 | #define MAX(x, y) __extension__ ({ \ |
| 35 | __typeof__(x) _x = (x); \ |
| 36 | __typeof__(y) _y = (y); \ |
| 37 | (void)(&_x == &_y); \ |
| 38 | _x > _y ? _x : _y; \ |
| 39 | }) |
| 40 | |
| 41 | /* |
| 42 | * The round_up() macro rounds up a value to the given boundary in a |
| 43 | * type-agnostic yet type-safe manner. The boundary must be a power of two. |
| 44 | * In other words, it computes the smallest multiple of boundary which is |
| 45 | * greater than or equal to value. |
| 46 | * |
| 47 | * round_down() is similar but rounds the value down instead. |
| 48 | */ |
| 49 | #define round_boundary(value, boundary) \ |
| 50 | ((__typeof__(value))((boundary) - 1)) |
| 51 | |
| 52 | #define round_up(value, boundary) \ |
| 53 | ((((value) - 1) | round_boundary(value, boundary)) + 1) |
| 54 | |
| 55 | #define round_down(value, boundary) \ |
| 56 | ((value) & ~round_boundary(value, boundary)) |
| 57 | |
Soby Mathew | 327548c | 2017-07-13 15:19:51 +0100 | [diff] [blame] | 58 | #define div_round_up(val, div) __extension__ ({ \ |
| 59 | __typeof__(div) _div = (div); \ |
| 60 | round_up((val), _div)/_div; \ |
| 61 | }) |
| 62 | |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 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 | /* |
Varun Wadekar | 05597a9 | 2017-05-18 10:32:51 -0700 | [diff] [blame] | 71 | * For those constants to be shared between C and other sources, apply a 'u' |
| 72 | * or 'ull' suffix to the argument only in C, to avoid undefined or unintended |
| 73 | * behaviour. |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 74 | * |
Varun Wadekar | 05597a9 | 2017-05-18 10:32:51 -0700 | [diff] [blame] | 75 | * The GNU assembler and linker do not support the 'u' and 'ull' suffix (it |
| 76 | * causes the build process to fail) therefore the suffix is omitted when used |
| 77 | * in linker scripts and assembler files. |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 78 | */ |
| 79 | #if defined(__LINKER__) || defined(__ASSEMBLY__) |
Varun Wadekar | 05597a9 | 2017-05-18 10:32:51 -0700 | [diff] [blame] | 80 | # define U(_x) (_x) |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 81 | # define ULL(_x) (_x) |
| 82 | #else |
Varun Wadekar | 05597a9 | 2017-05-18 10:32:51 -0700 | [diff] [blame] | 83 | # define U(_x) (_x##u) |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 84 | # define ULL(_x) (_x##ull) |
| 85 | #endif |
| 86 | |
Julius Werner | 02eb727 | 2017-12-12 14:23:26 -0800 | [diff] [blame] | 87 | /* Register size of the current architecture. */ |
| 88 | #ifdef AARCH32 |
| 89 | #define REGSZ U(4) |
| 90 | #else |
| 91 | #define REGSZ U(8) |
| 92 | #endif |
| 93 | |
Jeenu Viswambharan | 0bc79d9 | 2017-08-16 11:44:25 +0100 | [diff] [blame] | 94 | /* |
| 95 | * Test for the current architecture version to be at least the version |
| 96 | * expected. |
| 97 | */ |
| 98 | #define ARM_ARCH_AT_LEAST(_maj, _min) \ |
| 99 | ((ARM_ARCH_MAJOR > _maj) || \ |
| 100 | ((ARM_ARCH_MAJOR == _maj) && (ARM_ARCH_MINOR >= _min))) |
| 101 | |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 102 | #endif /* __UTILS_DEF_H__ */ |