blob: 26ac44097d077d56b99699831fb97e74ad95519c [file] [log] [blame]
Scott Brandenbf404c02017-04-10 11:45:52 -07001/*
2 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
3 *
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
19#define BIT(nr) (1UL << (nr))
20
21#define MIN(x, y) __extension__ ({ \
22 __typeof__(x) _x = (x); \
23 __typeof__(y) _y = (y); \
24 (void)(&_x == &_y); \
25 _x < _y ? _x : _y; \
26})
27
28#define MAX(x, y) __extension__ ({ \
29 __typeof__(x) _x = (x); \
30 __typeof__(y) _y = (y); \
31 (void)(&_x == &_y); \
32 _x > _y ? _x : _y; \
33})
34
35/*
36 * The round_up() macro rounds up a value to the given boundary in a
37 * type-agnostic yet type-safe manner. The boundary must be a power of two.
38 * In other words, it computes the smallest multiple of boundary which is
39 * greater than or equal to value.
40 *
41 * round_down() is similar but rounds the value down instead.
42 */
43#define round_boundary(value, boundary) \
44 ((__typeof__(value))((boundary) - 1))
45
46#define round_up(value, boundary) \
47 ((((value) - 1) | round_boundary(value, boundary)) + 1)
48
49#define round_down(value, boundary) \
50 ((value) & ~round_boundary(value, boundary))
51
52/*
53 * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
54 * Both arguments must be unsigned pointer values (i.e. uintptr_t).
55 */
56#define check_uptr_overflow(ptr, inc) \
57 (((ptr) > UINTPTR_MAX - (inc)) ? 1 : 0)
58
59/*
Varun Wadekar05597a92017-05-18 10:32:51 -070060 * For those constants to be shared between C and other sources, apply a 'u'
61 * or 'ull' suffix to the argument only in C, to avoid undefined or unintended
62 * behaviour.
Scott Brandenbf404c02017-04-10 11:45:52 -070063 *
Varun Wadekar05597a92017-05-18 10:32:51 -070064 * The GNU assembler and linker do not support the 'u' and 'ull' suffix (it
65 * causes the build process to fail) therefore the suffix is omitted when used
66 * in linker scripts and assembler files.
Scott Brandenbf404c02017-04-10 11:45:52 -070067*/
68#if defined(__LINKER__) || defined(__ASSEMBLY__)
Varun Wadekar05597a92017-05-18 10:32:51 -070069# define U(_x) (_x)
Scott Brandenbf404c02017-04-10 11:45:52 -070070# define ULL(_x) (_x)
71#else
Varun Wadekar05597a92017-05-18 10:32:51 -070072# define U(_x) (_x##u)
Scott Brandenbf404c02017-04-10 11:45:52 -070073# define ULL(_x) (_x##ull)
74#endif
75
Jeenu Viswambharan0bc79d92017-08-16 11:44:25 +010076/*
77 * Test for the current architecture version to be at least the version
78 * expected.
79 */
80#define ARM_ARCH_AT_LEAST(_maj, _min) \
81 ((ARM_ARCH_MAJOR > _maj) || \
82 ((ARM_ARCH_MAJOR == _maj) && (ARM_ARCH_MINOR >= _min)))
83
Scott Brandenbf404c02017-04-10 11:45:52 -070084#endif /* __UTILS_DEF_H__ */