xref: /rk3399_ARM-atf/include/lib/utils_def.h (revision 53d9c9c85bc49845c4c40315e1ab29d627a1f8c3)
1*53d9c9c8SScott Branden /*
2*53d9c9c8SScott Branden  * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
3*53d9c9c8SScott Branden  *
4*53d9c9c8SScott Branden  * Redistribution and use in source and binary forms, with or without
5*53d9c9c8SScott Branden  * modification, are permitted provided that the following conditions are met:
6*53d9c9c8SScott Branden  *
7*53d9c9c8SScott Branden  * Redistributions of source code must retain the above copyright notice, this
8*53d9c9c8SScott Branden  * list of conditions and the following disclaimer.
9*53d9c9c8SScott Branden  *
10*53d9c9c8SScott Branden  * Redistributions in binary form must reproduce the above copyright notice,
11*53d9c9c8SScott Branden  * this list of conditions and the following disclaimer in the documentation
12*53d9c9c8SScott Branden  * and/or other materials provided with the distribution.
13*53d9c9c8SScott Branden  *
14*53d9c9c8SScott Branden  * Neither the name of ARM nor the names of its contributors may be used
15*53d9c9c8SScott Branden  * to endorse or promote products derived from this software without specific
16*53d9c9c8SScott Branden  * prior written permission.
17*53d9c9c8SScott Branden  *
18*53d9c9c8SScott Branden  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*53d9c9c8SScott Branden  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*53d9c9c8SScott Branden  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*53d9c9c8SScott Branden  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*53d9c9c8SScott Branden  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*53d9c9c8SScott Branden  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*53d9c9c8SScott Branden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*53d9c9c8SScott Branden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*53d9c9c8SScott Branden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*53d9c9c8SScott Branden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*53d9c9c8SScott Branden  * POSSIBILITY OF SUCH DAMAGE.
29*53d9c9c8SScott Branden  */
30*53d9c9c8SScott Branden 
31*53d9c9c8SScott Branden #ifndef __UTILS_DEF_H__
32*53d9c9c8SScott Branden #define __UTILS_DEF_H__
33*53d9c9c8SScott Branden 
34*53d9c9c8SScott Branden /* Compute the number of elements in the given array */
35*53d9c9c8SScott Branden #define ARRAY_SIZE(a)				\
36*53d9c9c8SScott Branden 	(sizeof(a) / sizeof((a)[0]))
37*53d9c9c8SScott Branden 
38*53d9c9c8SScott Branden #define IS_POWER_OF_TWO(x)			\
39*53d9c9c8SScott Branden 	(((x) & ((x) - 1)) == 0)
40*53d9c9c8SScott Branden 
41*53d9c9c8SScott Branden #define SIZE_FROM_LOG2_WORDS(n)		(4 << (n))
42*53d9c9c8SScott Branden 
43*53d9c9c8SScott Branden #define BIT(nr)				(1UL << (nr))
44*53d9c9c8SScott Branden 
45*53d9c9c8SScott Branden #define MIN(x, y) __extension__ ({	\
46*53d9c9c8SScott Branden 	__typeof__(x) _x = (x);		\
47*53d9c9c8SScott Branden 	__typeof__(y) _y = (y);		\
48*53d9c9c8SScott Branden 	(void)(&_x == &_y);		\
49*53d9c9c8SScott Branden 	_x < _y ? _x : _y;		\
50*53d9c9c8SScott Branden })
51*53d9c9c8SScott Branden 
52*53d9c9c8SScott Branden #define MAX(x, y) __extension__ ({	\
53*53d9c9c8SScott Branden 	__typeof__(x) _x = (x);		\
54*53d9c9c8SScott Branden 	__typeof__(y) _y = (y);		\
55*53d9c9c8SScott Branden 	(void)(&_x == &_y);		\
56*53d9c9c8SScott Branden 	_x > _y ? _x : _y;		\
57*53d9c9c8SScott Branden })
58*53d9c9c8SScott Branden 
59*53d9c9c8SScott Branden /*
60*53d9c9c8SScott Branden  * The round_up() macro rounds up a value to the given boundary in a
61*53d9c9c8SScott Branden  * type-agnostic yet type-safe manner. The boundary must be a power of two.
62*53d9c9c8SScott Branden  * In other words, it computes the smallest multiple of boundary which is
63*53d9c9c8SScott Branden  * greater than or equal to value.
64*53d9c9c8SScott Branden  *
65*53d9c9c8SScott Branden  * round_down() is similar but rounds the value down instead.
66*53d9c9c8SScott Branden  */
67*53d9c9c8SScott Branden #define round_boundary(value, boundary)		\
68*53d9c9c8SScott Branden 	((__typeof__(value))((boundary) - 1))
69*53d9c9c8SScott Branden 
70*53d9c9c8SScott Branden #define round_up(value, boundary)		\
71*53d9c9c8SScott Branden 	((((value) - 1) | round_boundary(value, boundary)) + 1)
72*53d9c9c8SScott Branden 
73*53d9c9c8SScott Branden #define round_down(value, boundary)		\
74*53d9c9c8SScott Branden 	((value) & ~round_boundary(value, boundary))
75*53d9c9c8SScott Branden 
76*53d9c9c8SScott Branden /*
77*53d9c9c8SScott Branden  * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
78*53d9c9c8SScott Branden  * Both arguments must be unsigned pointer values (i.e. uintptr_t).
79*53d9c9c8SScott Branden  */
80*53d9c9c8SScott Branden #define check_uptr_overflow(ptr, inc)		\
81*53d9c9c8SScott Branden 	(((ptr) > UINTPTR_MAX - (inc)) ? 1 : 0)
82*53d9c9c8SScott Branden 
83*53d9c9c8SScott Branden /*
84*53d9c9c8SScott Branden  * For those constants to be shared between C and other sources, apply a 'ull'
85*53d9c9c8SScott Branden  * suffix to the argument only in C, to avoid undefined or unintended behaviour.
86*53d9c9c8SScott Branden  *
87*53d9c9c8SScott Branden  * The GNU assembler and linker do not support the 'ull' suffix (it causes the
88*53d9c9c8SScott Branden  * build process to fail) therefore the suffix is omitted when used in linker
89*53d9c9c8SScott Branden  * scripts and assembler files.
90*53d9c9c8SScott Branden */
91*53d9c9c8SScott Branden #if defined(__LINKER__) || defined(__ASSEMBLY__)
92*53d9c9c8SScott Branden # define ULL(_x)	(_x)
93*53d9c9c8SScott Branden #else
94*53d9c9c8SScott Branden # define ULL(_x)	(_x##ull)
95*53d9c9c8SScott Branden #endif
96*53d9c9c8SScott Branden 
97*53d9c9c8SScott Branden #endif /* __UTILS_DEF_H__ */
98