1ed81f3ebSSandrine Bailleux /* 2ed81f3ebSSandrine Bailleux * Copyright (c) 2016, 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 410146ae64SSandrine Bailleux /* 420146ae64SSandrine Bailleux * The round_up() macro rounds up a value to the given boundary in a 430146ae64SSandrine Bailleux * type-agnostic yet type-safe manner. The boundary must be a power of two. 440146ae64SSandrine Bailleux * In other words, it computes the smallest multiple of boundary which is 450146ae64SSandrine Bailleux * greater than or equal to value. 460146ae64SSandrine Bailleux * 470146ae64SSandrine Bailleux * round_down() is similar but rounds the value down instead. 480146ae64SSandrine Bailleux */ 490146ae64SSandrine Bailleux #define round_boundary(value, boundary) \ 500146ae64SSandrine Bailleux ((__typeof__(value))((boundary) - 1)) 510146ae64SSandrine Bailleux 520146ae64SSandrine Bailleux #define round_up(value, boundary) \ 530146ae64SSandrine Bailleux ((((value) - 1) | round_boundary(value, boundary)) + 1) 540146ae64SSandrine Bailleux 550146ae64SSandrine Bailleux #define round_down(value, boundary) \ 560146ae64SSandrine Bailleux ((value) & ~round_boundary(value, boundary)) 570146ae64SSandrine Bailleux 58*7b6d330cSSandrine Bailleux /* 59*7b6d330cSSandrine Bailleux * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise. 60*7b6d330cSSandrine Bailleux * Both arguments must be unsigned pointer values (i.e. uintptr_t). 61*7b6d330cSSandrine Bailleux */ 62*7b6d330cSSandrine Bailleux #define check_uptr_overflow(ptr, inc) \ 63*7b6d330cSSandrine Bailleux (((ptr) > UINTPTR_MAX - (inc)) ? 1 : 0) 64*7b6d330cSSandrine Bailleux 65ed81f3ebSSandrine Bailleux #endif /* __UTILS_H__ */ 66