Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2010 |
| 3 | * Texas Instruments, <www.ti.com> |
| 4 | * Aneesh V <aneesh@ti.com> |
| 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 7 | */ |
| 8 | #ifndef _UTILS_H_ |
| 9 | #define _UTILS_H_ |
| 10 | |
| 11 | static inline s32 log_2_n_round_up(u32 n) |
| 12 | { |
| 13 | s32 log2n = -1; |
| 14 | u32 temp = n; |
| 15 | |
| 16 | while (temp) { |
| 17 | log2n++; |
| 18 | temp >>= 1; |
| 19 | } |
| 20 | |
| 21 | if (n & (n - 1)) |
| 22 | return log2n + 1; /* not power of 2 - round up */ |
| 23 | else |
| 24 | return log2n; /* power of 2 */ |
| 25 | } |
| 26 | |
| 27 | static inline s32 log_2_n_round_down(u32 n) |
| 28 | { |
| 29 | s32 log2n = -1; |
| 30 | u32 temp = n; |
| 31 | |
| 32 | while (temp) { |
| 33 | log2n++; |
| 34 | temp >>= 1; |
| 35 | } |
| 36 | |
| 37 | return log2n; |
| 38 | } |
| 39 | |
| 40 | #endif |