xref: /rk3399_ARM-atf/include/lib/utils.h (revision 0146ae64c006956a281865f5688858d4846c781e)
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 
41*0146ae64SSandrine Bailleux /*
42*0146ae64SSandrine Bailleux  * The round_up() macro rounds up a value to the given boundary in a
43*0146ae64SSandrine Bailleux  * type-agnostic yet type-safe manner. The boundary must be a power of two.
44*0146ae64SSandrine Bailleux  * In other words, it computes the smallest multiple of boundary which is
45*0146ae64SSandrine Bailleux  * greater than or equal to value.
46*0146ae64SSandrine Bailleux  *
47*0146ae64SSandrine Bailleux  * round_down() is similar but rounds the value down instead.
48*0146ae64SSandrine Bailleux  */
49*0146ae64SSandrine Bailleux #define round_boundary(value, boundary)		\
50*0146ae64SSandrine Bailleux 	((__typeof__(value))((boundary) - 1))
51*0146ae64SSandrine Bailleux 
52*0146ae64SSandrine Bailleux #define round_up(value, boundary)		\
53*0146ae64SSandrine Bailleux 	((((value) - 1) | round_boundary(value, boundary)) + 1)
54*0146ae64SSandrine Bailleux 
55*0146ae64SSandrine Bailleux #define round_down(value, boundary)		\
56*0146ae64SSandrine Bailleux 	((value) & ~round_boundary(value, boundary))
57*0146ae64SSandrine Bailleux 
58ed81f3ebSSandrine Bailleux #endif /* __UTILS_H__ */
59