153d9c9c8SScott Branden /* 29f85f9e3SJoel Hutton * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved. 353d9c9c8SScott Branden * 482cb2c1aSdp-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 19155a1006SJulius Werner #define BIT(nr) (ULL(1) << (nr)) 2053d9c9c8SScott Branden 21ebf1ca10SSoby Mathew /* 22ebf1ca10SSoby Mathew * This variant of div_round_up can be used in macro definition but should not 23ebf1ca10SSoby Mathew * be used in C code as the `div` parameter is evaluated twice. 24ebf1ca10SSoby Mathew */ 25ebf1ca10SSoby Mathew #define DIV_ROUND_UP_2EVAL(n, d) (((n) + (d) - 1) / (d)) 26ebf1ca10SSoby Mathew 277baa7bcaSJulius Werner #define div_round_up(val, div) __extension__ ({ \ 287baa7bcaSJulius Werner __typeof__(div) _div = (div); \ 297baa7bcaSJulius Werner ((val) + _div - 1) / _div; \ 307baa7bcaSJulius Werner }) 317baa7bcaSJulius Werner 3253d9c9c8SScott Branden #define MIN(x, y) __extension__ ({ \ 3353d9c9c8SScott Branden __typeof__(x) _x = (x); \ 3453d9c9c8SScott Branden __typeof__(y) _y = (y); \ 3553d9c9c8SScott Branden (void)(&_x == &_y); \ 3653d9c9c8SScott Branden _x < _y ? _x : _y; \ 3753d9c9c8SScott Branden }) 3853d9c9c8SScott Branden 3953d9c9c8SScott Branden #define MAX(x, y) __extension__ ({ \ 4053d9c9c8SScott Branden __typeof__(x) _x = (x); \ 4153d9c9c8SScott Branden __typeof__(y) _y = (y); \ 4253d9c9c8SScott Branden (void)(&_x == &_y); \ 4353d9c9c8SScott Branden _x > _y ? _x : _y; \ 4453d9c9c8SScott Branden }) 4553d9c9c8SScott Branden 4653d9c9c8SScott Branden /* 4753d9c9c8SScott Branden * The round_up() macro rounds up a value to the given boundary in a 4853d9c9c8SScott Branden * type-agnostic yet type-safe manner. The boundary must be a power of two. 4953d9c9c8SScott Branden * In other words, it computes the smallest multiple of boundary which is 5053d9c9c8SScott Branden * greater than or equal to value. 5153d9c9c8SScott Branden * 5253d9c9c8SScott Branden * round_down() is similar but rounds the value down instead. 5353d9c9c8SScott Branden */ 5453d9c9c8SScott Branden #define round_boundary(value, boundary) \ 5553d9c9c8SScott Branden ((__typeof__(value))((boundary) - 1)) 5653d9c9c8SScott Branden 5753d9c9c8SScott Branden #define round_up(value, boundary) \ 5853d9c9c8SScott Branden ((((value) - 1) | round_boundary(value, boundary)) + 1) 5953d9c9c8SScott Branden 6053d9c9c8SScott Branden #define round_down(value, boundary) \ 6153d9c9c8SScott Branden ((value) & ~round_boundary(value, boundary)) 6253d9c9c8SScott Branden 6353d9c9c8SScott Branden /* 6453d9c9c8SScott Branden * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise. 6553d9c9c8SScott Branden * Both arguments must be unsigned pointer values (i.e. uintptr_t). 6653d9c9c8SScott Branden */ 6753d9c9c8SScott Branden #define check_uptr_overflow(ptr, inc) \ 6853d9c9c8SScott Branden (((ptr) > UINTPTR_MAX - (inc)) ? 1 : 0) 6953d9c9c8SScott Branden 7053d9c9c8SScott Branden /* 71*30d81c36SJeenu Viswambharan * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise. 72*30d81c36SJeenu Viswambharan * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t). 73*30d81c36SJeenu Viswambharan */ 74*30d81c36SJeenu Viswambharan #define check_u32_overflow(u32, inc) \ 75*30d81c36SJeenu Viswambharan ((u32) > (UINT32_MAX - (inc)) ? 1 : 0) 76*30d81c36SJeenu Viswambharan 77*30d81c36SJeenu Viswambharan /* 786176b4fcSVarun Wadekar * For those constants to be shared between C and other sources, apply a 'u' 796176b4fcSVarun Wadekar * or 'ull' suffix to the argument only in C, to avoid undefined or unintended 806176b4fcSVarun Wadekar * behaviour. 8153d9c9c8SScott Branden * 826176b4fcSVarun Wadekar * The GNU assembler and linker do not support the 'u' and 'ull' suffix (it 836176b4fcSVarun Wadekar * causes the build process to fail) therefore the suffix is omitted when used 846176b4fcSVarun Wadekar * in linker scripts and assembler files. 8553d9c9c8SScott Branden */ 8653d9c9c8SScott Branden #if defined(__LINKER__) || defined(__ASSEMBLY__) 876176b4fcSVarun Wadekar # define U(_x) (_x) 8853d9c9c8SScott Branden # define ULL(_x) (_x) 8953d9c9c8SScott Branden #else 905724481fSDavid Cunado # define U(_x) (_x##U) 915724481fSDavid Cunado # define ULL(_x) (_x##ULL) 9253d9c9c8SScott Branden #endif 9353d9c9c8SScott Branden 94155a1006SJulius Werner /* Register size of the current architecture. */ 95155a1006SJulius Werner #ifdef AARCH32 96155a1006SJulius Werner #define REGSZ U(4) 97155a1006SJulius Werner #else 98155a1006SJulius Werner #define REGSZ U(8) 99155a1006SJulius Werner #endif 100155a1006SJulius Werner 101f45e232aSJeenu Viswambharan /* 102f45e232aSJeenu Viswambharan * Test for the current architecture version to be at least the version 103f45e232aSJeenu Viswambharan * expected. 104f45e232aSJeenu Viswambharan */ 105f45e232aSJeenu Viswambharan #define ARM_ARCH_AT_LEAST(_maj, _min) \ 106f45e232aSJeenu Viswambharan ((ARM_ARCH_MAJOR > _maj) || \ 107f45e232aSJeenu Viswambharan ((ARM_ARCH_MAJOR == _maj) && (ARM_ARCH_MINOR >= _min))) 108f45e232aSJeenu Viswambharan 1099f85f9e3SJoel Hutton /* 1109f85f9e3SJoel Hutton * Import an assembly or linker symbol as a C expression with the specified 1119f85f9e3SJoel Hutton * type 1129f85f9e3SJoel Hutton */ 1139f85f9e3SJoel Hutton #define IMPORT_SYM(type, sym, name) \ 1149f85f9e3SJoel Hutton extern char sym[];\ 1159f85f9e3SJoel Hutton static const __attribute__((unused)) type name = (type) sym; 1169f85f9e3SJoel Hutton 1179f85f9e3SJoel Hutton /* 1189f85f9e3SJoel Hutton * When the symbol is used to hold a pointer, its alignment can be asserted 1199f85f9e3SJoel Hutton * with this macro. For example, if there is a linker symbol that is going to 1209f85f9e3SJoel Hutton * be used as a 64-bit pointer, the value of the linker symbol must also be 1219f85f9e3SJoel Hutton * aligned to 64 bit. This macro makes sure this is the case. 1229f85f9e3SJoel Hutton */ 1239f85f9e3SJoel Hutton #define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0) 1249f85f9e3SJoel Hutton 1259f85f9e3SJoel Hutton 12653d9c9c8SScott Branden #endif /* __UTILS_DEF_H__ */ 127