153d9c9c8SScott Branden /* 253d9c9c8SScott Branden * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. 353d9c9c8SScott Branden * 4*82cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 553d9c9c8SScott Branden */ 653d9c9c8SScott Branden 753d9c9c8SScott Branden #ifndef __UTILS_DEF_H__ 853d9c9c8SScott Branden #define __UTILS_DEF_H__ 953d9c9c8SScott Branden 1053d9c9c8SScott Branden /* Compute the number of elements in the given array */ 1153d9c9c8SScott Branden #define ARRAY_SIZE(a) \ 1253d9c9c8SScott Branden (sizeof(a) / sizeof((a)[0])) 1353d9c9c8SScott Branden 1453d9c9c8SScott Branden #define IS_POWER_OF_TWO(x) \ 1553d9c9c8SScott Branden (((x) & ((x) - 1)) == 0) 1653d9c9c8SScott Branden 1753d9c9c8SScott Branden #define SIZE_FROM_LOG2_WORDS(n) (4 << (n)) 1853d9c9c8SScott Branden 1953d9c9c8SScott Branden #define BIT(nr) (1UL << (nr)) 2053d9c9c8SScott Branden 2153d9c9c8SScott Branden #define MIN(x, y) __extension__ ({ \ 2253d9c9c8SScott Branden __typeof__(x) _x = (x); \ 2353d9c9c8SScott Branden __typeof__(y) _y = (y); \ 2453d9c9c8SScott Branden (void)(&_x == &_y); \ 2553d9c9c8SScott Branden _x < _y ? _x : _y; \ 2653d9c9c8SScott Branden }) 2753d9c9c8SScott Branden 2853d9c9c8SScott Branden #define MAX(x, y) __extension__ ({ \ 2953d9c9c8SScott Branden __typeof__(x) _x = (x); \ 3053d9c9c8SScott Branden __typeof__(y) _y = (y); \ 3153d9c9c8SScott Branden (void)(&_x == &_y); \ 3253d9c9c8SScott Branden _x > _y ? _x : _y; \ 3353d9c9c8SScott Branden }) 3453d9c9c8SScott Branden 3553d9c9c8SScott Branden /* 3653d9c9c8SScott Branden * The round_up() macro rounds up a value to the given boundary in a 3753d9c9c8SScott Branden * type-agnostic yet type-safe manner. The boundary must be a power of two. 3853d9c9c8SScott Branden * In other words, it computes the smallest multiple of boundary which is 3953d9c9c8SScott Branden * greater than or equal to value. 4053d9c9c8SScott Branden * 4153d9c9c8SScott Branden * round_down() is similar but rounds the value down instead. 4253d9c9c8SScott Branden */ 4353d9c9c8SScott Branden #define round_boundary(value, boundary) \ 4453d9c9c8SScott Branden ((__typeof__(value))((boundary) - 1)) 4553d9c9c8SScott Branden 4653d9c9c8SScott Branden #define round_up(value, boundary) \ 4753d9c9c8SScott Branden ((((value) - 1) | round_boundary(value, boundary)) + 1) 4853d9c9c8SScott Branden 4953d9c9c8SScott Branden #define round_down(value, boundary) \ 5053d9c9c8SScott Branden ((value) & ~round_boundary(value, boundary)) 5153d9c9c8SScott Branden 5253d9c9c8SScott Branden /* 5353d9c9c8SScott Branden * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise. 5453d9c9c8SScott Branden * Both arguments must be unsigned pointer values (i.e. uintptr_t). 5553d9c9c8SScott Branden */ 5653d9c9c8SScott Branden #define check_uptr_overflow(ptr, inc) \ 5753d9c9c8SScott Branden (((ptr) > UINTPTR_MAX - (inc)) ? 1 : 0) 5853d9c9c8SScott Branden 5953d9c9c8SScott Branden /* 6053d9c9c8SScott Branden * For those constants to be shared between C and other sources, apply a 'ull' 6153d9c9c8SScott Branden * suffix to the argument only in C, to avoid undefined or unintended behaviour. 6253d9c9c8SScott Branden * 6353d9c9c8SScott Branden * The GNU assembler and linker do not support the 'ull' suffix (it causes the 6453d9c9c8SScott Branden * build process to fail) therefore the suffix is omitted when used in linker 6553d9c9c8SScott Branden * scripts and assembler files. 6653d9c9c8SScott Branden */ 6753d9c9c8SScott Branden #if defined(__LINKER__) || defined(__ASSEMBLY__) 6853d9c9c8SScott Branden # define ULL(_x) (_x) 6953d9c9c8SScott Branden #else 7053d9c9c8SScott Branden # define ULL(_x) (_x##ull) 7153d9c9c8SScott Branden #endif 7253d9c9c8SScott Branden 7353d9c9c8SScott Branden #endif /* __UTILS_DEF_H__ */ 74