1ed81f3ebSSandrine Bailleux /* 2*9edac047SDavid Cunado * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. 3ed81f3ebSSandrine Bailleux * 4ed81f3ebSSandrine Bailleux * Redistribution and use in source and binary forms, with or without 5ed81f3ebSSandrine Bailleux * modification, are permitted provided that the following conditions are met: 6ed81f3ebSSandrine Bailleux * 7ed81f3ebSSandrine Bailleux * Redistributions of source code must retain the above copyright notice, this 8ed81f3ebSSandrine Bailleux * list of conditions and the following disclaimer. 9ed81f3ebSSandrine Bailleux * 10ed81f3ebSSandrine Bailleux * Redistributions in binary form must reproduce the above copyright notice, 11ed81f3ebSSandrine Bailleux * this list of conditions and the following disclaimer in the documentation 12ed81f3ebSSandrine Bailleux * and/or other materials provided with the distribution. 13ed81f3ebSSandrine Bailleux * 14ed81f3ebSSandrine Bailleux * Neither the name of ARM nor the names of its contributors may be used 15ed81f3ebSSandrine Bailleux * to endorse or promote products derived from this software without specific 16ed81f3ebSSandrine Bailleux * prior written permission. 17ed81f3ebSSandrine Bailleux * 18ed81f3ebSSandrine Bailleux * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19ed81f3ebSSandrine Bailleux * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20ed81f3ebSSandrine Bailleux * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21ed81f3ebSSandrine Bailleux * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22ed81f3ebSSandrine Bailleux * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23ed81f3ebSSandrine Bailleux * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24ed81f3ebSSandrine Bailleux * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25ed81f3ebSSandrine Bailleux * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26ed81f3ebSSandrine Bailleux * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27ed81f3ebSSandrine Bailleux * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28ed81f3ebSSandrine Bailleux * POSSIBILITY OF SUCH DAMAGE. 29ed81f3ebSSandrine Bailleux */ 30ed81f3ebSSandrine Bailleux 31ed81f3ebSSandrine Bailleux #ifndef __UTILS_H__ 32ed81f3ebSSandrine Bailleux #define __UTILS_H__ 33ed81f3ebSSandrine Bailleux 34ed81f3ebSSandrine Bailleux /* Compute the number of elements in the given array */ 35ed81f3ebSSandrine Bailleux #define ARRAY_SIZE(a) \ 36ed81f3ebSSandrine Bailleux (sizeof(a) / sizeof((a)[0])) 37ed81f3ebSSandrine Bailleux 38ed81f3ebSSandrine Bailleux #define IS_POWER_OF_TWO(x) \ 39ed81f3ebSSandrine Bailleux (((x) & ((x) - 1)) == 0) 40ed81f3ebSSandrine Bailleux 41c45f627dSSoby Mathew #define SIZE_FROM_LOG2_WORDS(n) (4 << (n)) 42c45f627dSSoby Mathew 43152c8c11SMasahiro Yamada #define BIT(nr) (1UL << (nr)) 44152c8c11SMasahiro Yamada 450146ae64SSandrine Bailleux /* 460146ae64SSandrine Bailleux * The round_up() macro rounds up a value to the given boundary in a 470146ae64SSandrine Bailleux * type-agnostic yet type-safe manner. The boundary must be a power of two. 480146ae64SSandrine Bailleux * In other words, it computes the smallest multiple of boundary which is 490146ae64SSandrine Bailleux * greater than or equal to value. 500146ae64SSandrine Bailleux * 510146ae64SSandrine Bailleux * round_down() is similar but rounds the value down instead. 520146ae64SSandrine Bailleux */ 530146ae64SSandrine Bailleux #define round_boundary(value, boundary) \ 540146ae64SSandrine Bailleux ((__typeof__(value))((boundary) - 1)) 550146ae64SSandrine Bailleux 560146ae64SSandrine Bailleux #define round_up(value, boundary) \ 570146ae64SSandrine Bailleux ((((value) - 1) | round_boundary(value, boundary)) + 1) 580146ae64SSandrine Bailleux 590146ae64SSandrine Bailleux #define round_down(value, boundary) \ 600146ae64SSandrine Bailleux ((value) & ~round_boundary(value, boundary)) 610146ae64SSandrine Bailleux 627b6d330cSSandrine Bailleux /* 637b6d330cSSandrine Bailleux * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise. 647b6d330cSSandrine Bailleux * Both arguments must be unsigned pointer values (i.e. uintptr_t). 657b6d330cSSandrine Bailleux */ 667b6d330cSSandrine Bailleux #define check_uptr_overflow(ptr, inc) \ 677b6d330cSSandrine Bailleux (((ptr) > UINTPTR_MAX - (inc)) ? 1 : 0) 687b6d330cSSandrine Bailleux 69*9edac047SDavid Cunado /* 70*9edac047SDavid Cunado * For those constants to be shared between C and other sources, apply a 'ull' 71*9edac047SDavid Cunado * suffix to the argument only in C, to avoid undefined or unintended behaviour. 72*9edac047SDavid Cunado * 73*9edac047SDavid Cunado * The GNU assembler and linker do not support the 'ull' suffix (it causes the 74*9edac047SDavid Cunado * build process to fail) therefore the suffix is omitted when used in linker 75*9edac047SDavid Cunado * scripts and assembler files. 76*9edac047SDavid Cunado */ 77*9edac047SDavid Cunado #if defined(__LINKER__) || defined(__ASSEMBLY__) 78*9edac047SDavid Cunado # define ULL(_x) (_x) 79*9edac047SDavid Cunado #else 80*9edac047SDavid Cunado # define ULL(_x) (_x##ull) 81*9edac047SDavid Cunado #endif 82*9edac047SDavid Cunado 83ed81f3ebSSandrine Bailleux #endif /* __UTILS_H__ */ 84