xref: /rk3399_rockchip-uboot/arch/arm/include/asm/utils.h (revision 2c451f7831208741d0ff7ca6046cffcd9ee49def)
1*2c451f78SAneesh V /*
2*2c451f78SAneesh V  * (C) Copyright 2010
3*2c451f78SAneesh V  * Texas Instruments, <www.ti.com>
4*2c451f78SAneesh V  * Aneesh V <aneesh@ti.com>
5*2c451f78SAneesh V  *
6*2c451f78SAneesh V  * See file CREDITS for list of people who contributed to this
7*2c451f78SAneesh V  * project.
8*2c451f78SAneesh V  *
9*2c451f78SAneesh V  * This program is free software; you can redistribute it and/or
10*2c451f78SAneesh V  * modify it under the terms of the GNU General Public License as
11*2c451f78SAneesh V  * published by the Free Software Foundation; either version 2 of
12*2c451f78SAneesh V  * the License, or (at your option) any later version.
13*2c451f78SAneesh V  *
14*2c451f78SAneesh V  * This program is distributed in the hope that it will be useful,
15*2c451f78SAneesh V  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*2c451f78SAneesh V  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17*2c451f78SAneesh V  * GNU General Public License for more details.
18*2c451f78SAneesh V  *
19*2c451f78SAneesh V  * You should have received a copy of the GNU General Public License
20*2c451f78SAneesh V  * along with this program; if not, write to the Free Software
21*2c451f78SAneesh V  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22*2c451f78SAneesh V  * MA 02111-1307 USA
23*2c451f78SAneesh V  */
24*2c451f78SAneesh V #ifndef _UTILS_H_
25*2c451f78SAneesh V #define _UTILS_H_
26*2c451f78SAneesh V 
27*2c451f78SAneesh V static inline s32 log_2_n_round_up(u32 n)
28*2c451f78SAneesh V {
29*2c451f78SAneesh V 	s32 log2n = -1;
30*2c451f78SAneesh V 	u32 temp = n;
31*2c451f78SAneesh V 
32*2c451f78SAneesh V 	while (temp) {
33*2c451f78SAneesh V 		log2n++;
34*2c451f78SAneesh V 		temp >>= 1;
35*2c451f78SAneesh V 	}
36*2c451f78SAneesh V 
37*2c451f78SAneesh V 	if (n & (n - 1))
38*2c451f78SAneesh V 		return log2n + 1; /* not power of 2 - round up */
39*2c451f78SAneesh V 	else
40*2c451f78SAneesh V 		return log2n; /* power of 2 */
41*2c451f78SAneesh V }
42*2c451f78SAneesh V 
43*2c451f78SAneesh V static inline s32 log_2_n_round_down(u32 n)
44*2c451f78SAneesh V {
45*2c451f78SAneesh V 	s32 log2n = -1;
46*2c451f78SAneesh V 	u32 temp = n;
47*2c451f78SAneesh V 
48*2c451f78SAneesh V 	while (temp) {
49*2c451f78SAneesh V 		log2n++;
50*2c451f78SAneesh V 		temp >>= 1;
51*2c451f78SAneesh V 	}
52*2c451f78SAneesh V 
53*2c451f78SAneesh V 	return log2n;
54*2c451f78SAneesh V }
55*2c451f78SAneesh V 
56*2c451f78SAneesh V #endif
57