xref: /rk3399_rockchip-uboot/arch/arm/include/asm/utils.h (revision 326ea986ac150acdc7656d57fca647db80b50158)
12c451f78SAneesh V /*
22c451f78SAneesh V  * (C) Copyright 2010
32c451f78SAneesh V  * Texas Instruments, <www.ti.com>
42c451f78SAneesh V  * Aneesh V <aneesh@ti.com>
52c451f78SAneesh V  *
6*1a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
72c451f78SAneesh V  */
82c451f78SAneesh V #ifndef _UTILS_H_
92c451f78SAneesh V #define _UTILS_H_
102c451f78SAneesh V 
log_2_n_round_up(u32 n)112c451f78SAneesh V static inline s32 log_2_n_round_up(u32 n)
122c451f78SAneesh V {
132c451f78SAneesh V 	s32 log2n = -1;
142c451f78SAneesh V 	u32 temp = n;
152c451f78SAneesh V 
162c451f78SAneesh V 	while (temp) {
172c451f78SAneesh V 		log2n++;
182c451f78SAneesh V 		temp >>= 1;
192c451f78SAneesh V 	}
202c451f78SAneesh V 
212c451f78SAneesh V 	if (n & (n - 1))
222c451f78SAneesh V 		return log2n + 1; /* not power of 2 - round up */
232c451f78SAneesh V 	else
242c451f78SAneesh V 		return log2n; /* power of 2 */
252c451f78SAneesh V }
262c451f78SAneesh V 
log_2_n_round_down(u32 n)272c451f78SAneesh V static inline s32 log_2_n_round_down(u32 n)
282c451f78SAneesh V {
292c451f78SAneesh V 	s32 log2n = -1;
302c451f78SAneesh V 	u32 temp = n;
312c451f78SAneesh V 
322c451f78SAneesh V 	while (temp) {
332c451f78SAneesh V 		log2n++;
342c451f78SAneesh V 		temp >>= 1;
352c451f78SAneesh V 	}
362c451f78SAneesh V 
372c451f78SAneesh V 	return log2n;
382c451f78SAneesh V }
392c451f78SAneesh V 
402c451f78SAneesh V #endif
41