1*1392385eSFrançois Tigeot /*- 2*1392385eSFrançois Tigeot * Copyright (c) 2010 Isilon Systems, Inc. 3*1392385eSFrançois Tigeot * Copyright (c) 2010 iX Systems, Inc. 4*1392385eSFrançois Tigeot * Copyright (c) 2010 Panasas, Inc. 5*1392385eSFrançois Tigeot * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. 6*1392385eSFrançois Tigeot * All rights reserved. 7*1392385eSFrançois Tigeot * 8*1392385eSFrançois Tigeot * Redistribution and use in source and binary forms, with or without 9*1392385eSFrançois Tigeot * modification, are permitted provided that the following conditions 10*1392385eSFrançois Tigeot * are met: 11*1392385eSFrançois Tigeot * 1. Redistributions of source code must retain the above copyright 12*1392385eSFrançois Tigeot * notice unmodified, this list of conditions, and the following 13*1392385eSFrançois Tigeot * disclaimer. 14*1392385eSFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright 15*1392385eSFrançois Tigeot * notice, this list of conditions and the following disclaimer in the 16*1392385eSFrançois Tigeot * documentation and/or other materials provided with the distribution. 17*1392385eSFrançois Tigeot * 18*1392385eSFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19*1392385eSFrançois Tigeot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20*1392385eSFrançois Tigeot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21*1392385eSFrançois Tigeot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22*1392385eSFrançois Tigeot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23*1392385eSFrançois Tigeot * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24*1392385eSFrançois Tigeot * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25*1392385eSFrançois Tigeot * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26*1392385eSFrançois Tigeot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27*1392385eSFrançois Tigeot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*1392385eSFrançois Tigeot */ 29*1392385eSFrançois Tigeot 30*1392385eSFrançois Tigeot #ifndef _LINUX_LOG2_H_ 31*1392385eSFrançois Tigeot #define _LINUX_LOG2_H_ 32*1392385eSFrançois Tigeot 33*1392385eSFrançois Tigeot #include <linux/types.h> 34*1392385eSFrançois Tigeot 35*1392385eSFrançois Tigeot #include <sys/libkern.h> 36*1392385eSFrançois Tigeot 37*1392385eSFrançois Tigeot static inline unsigned long 38*1392385eSFrançois Tigeot roundup_pow_of_two(unsigned long x) 39*1392385eSFrançois Tigeot { 40*1392385eSFrançois Tigeot return (1UL << flsl(x - 1)); 41*1392385eSFrançois Tigeot } 42*1392385eSFrançois Tigeot 43*1392385eSFrançois Tigeot static inline int 44*1392385eSFrançois Tigeot is_power_of_2(unsigned long n) 45*1392385eSFrançois Tigeot { 46*1392385eSFrançois Tigeot return (n == roundup_pow_of_two(n)); 47*1392385eSFrançois Tigeot } 48*1392385eSFrançois Tigeot 49*1392385eSFrançois Tigeot static inline unsigned long 50*1392385eSFrançois Tigeot rounddown_pow_of_two(unsigned long x) 51*1392385eSFrançois Tigeot { 52*1392385eSFrançois Tigeot return (1UL << (flsl(x) - 1)); 53*1392385eSFrançois Tigeot } 54*1392385eSFrançois Tigeot 55*1392385eSFrançois Tigeot 56*1392385eSFrançois Tigeot /* 57*1392385eSFrançois Tigeot * deal with unrepresentable constant logarithms 58*1392385eSFrançois Tigeot */ 59*1392385eSFrançois Tigeot extern __attribute__((const, noreturn)) 60*1392385eSFrançois Tigeot int ____ilog2_NaN(void); 61*1392385eSFrançois Tigeot 62*1392385eSFrançois Tigeot /* 63*1392385eSFrançois Tigeot * non-constant log of base 2 calculators 64*1392385eSFrançois Tigeot * - the arch may override these in asm/bitops.h if they can be implemented 65*1392385eSFrançois Tigeot * more efficiently than using fls() and fls64() 66*1392385eSFrançois Tigeot * - the arch is not required to handle n==0 if implementing the fallback 67*1392385eSFrançois Tigeot */ 68*1392385eSFrançois Tigeot #ifndef CONFIG_ARCH_HAS_ILOG2_U32 69*1392385eSFrançois Tigeot static inline __attribute__((const)) 70*1392385eSFrançois Tigeot int __ilog2_u32(u32 n) 71*1392385eSFrançois Tigeot { 72*1392385eSFrançois Tigeot return flsl(n) - 1; 73*1392385eSFrançois Tigeot } 74*1392385eSFrançois Tigeot #endif 75*1392385eSFrançois Tigeot 76*1392385eSFrançois Tigeot #ifndef CONFIG_ARCH_HAS_ILOG2_U64 77*1392385eSFrançois Tigeot static inline __attribute__((const)) 78*1392385eSFrançois Tigeot int __ilog2_u64(u64 n) 79*1392385eSFrançois Tigeot { 80*1392385eSFrançois Tigeot return flsl(n) - 1; 81*1392385eSFrançois Tigeot } 82*1392385eSFrançois Tigeot #endif 83*1392385eSFrançois Tigeot 84*1392385eSFrançois Tigeot 85*1392385eSFrançois Tigeot /** 86*1392385eSFrançois Tigeot * ilog2 - log of base 2 of 32-bit or a 64-bit unsigned value 87*1392385eSFrançois Tigeot * @n - parameter 88*1392385eSFrançois Tigeot * 89*1392385eSFrançois Tigeot * constant-capable log of base 2 calculation 90*1392385eSFrançois Tigeot * - this can be used to initialise global variables from constant data, hence 91*1392385eSFrançois Tigeot * the massive ternary operator construction 92*1392385eSFrançois Tigeot * 93*1392385eSFrançois Tigeot * selects the appropriately-sized optimised version depending on sizeof(n) 94*1392385eSFrançois Tigeot */ 95*1392385eSFrançois Tigeot #define ilog2(n) \ 96*1392385eSFrançois Tigeot ( \ 97*1392385eSFrançois Tigeot __builtin_constant_p(n) ? ( \ 98*1392385eSFrançois Tigeot (n) < 1 ? ____ilog2_NaN() : \ 99*1392385eSFrançois Tigeot (n) & (1ULL << 63) ? 63 : \ 100*1392385eSFrançois Tigeot (n) & (1ULL << 62) ? 62 : \ 101*1392385eSFrançois Tigeot (n) & (1ULL << 61) ? 61 : \ 102*1392385eSFrançois Tigeot (n) & (1ULL << 60) ? 60 : \ 103*1392385eSFrançois Tigeot (n) & (1ULL << 59) ? 59 : \ 104*1392385eSFrançois Tigeot (n) & (1ULL << 58) ? 58 : \ 105*1392385eSFrançois Tigeot (n) & (1ULL << 57) ? 57 : \ 106*1392385eSFrançois Tigeot (n) & (1ULL << 56) ? 56 : \ 107*1392385eSFrançois Tigeot (n) & (1ULL << 55) ? 55 : \ 108*1392385eSFrançois Tigeot (n) & (1ULL << 54) ? 54 : \ 109*1392385eSFrançois Tigeot (n) & (1ULL << 53) ? 53 : \ 110*1392385eSFrançois Tigeot (n) & (1ULL << 52) ? 52 : \ 111*1392385eSFrançois Tigeot (n) & (1ULL << 51) ? 51 : \ 112*1392385eSFrançois Tigeot (n) & (1ULL << 50) ? 50 : \ 113*1392385eSFrançois Tigeot (n) & (1ULL << 49) ? 49 : \ 114*1392385eSFrançois Tigeot (n) & (1ULL << 48) ? 48 : \ 115*1392385eSFrançois Tigeot (n) & (1ULL << 47) ? 47 : \ 116*1392385eSFrançois Tigeot (n) & (1ULL << 46) ? 46 : \ 117*1392385eSFrançois Tigeot (n) & (1ULL << 45) ? 45 : \ 118*1392385eSFrançois Tigeot (n) & (1ULL << 44) ? 44 : \ 119*1392385eSFrançois Tigeot (n) & (1ULL << 43) ? 43 : \ 120*1392385eSFrançois Tigeot (n) & (1ULL << 42) ? 42 : \ 121*1392385eSFrançois Tigeot (n) & (1ULL << 41) ? 41 : \ 122*1392385eSFrançois Tigeot (n) & (1ULL << 40) ? 40 : \ 123*1392385eSFrançois Tigeot (n) & (1ULL << 39) ? 39 : \ 124*1392385eSFrançois Tigeot (n) & (1ULL << 38) ? 38 : \ 125*1392385eSFrançois Tigeot (n) & (1ULL << 37) ? 37 : \ 126*1392385eSFrançois Tigeot (n) & (1ULL << 36) ? 36 : \ 127*1392385eSFrançois Tigeot (n) & (1ULL << 35) ? 35 : \ 128*1392385eSFrançois Tigeot (n) & (1ULL << 34) ? 34 : \ 129*1392385eSFrançois Tigeot (n) & (1ULL << 33) ? 33 : \ 130*1392385eSFrançois Tigeot (n) & (1ULL << 32) ? 32 : \ 131*1392385eSFrançois Tigeot (n) & (1ULL << 31) ? 31 : \ 132*1392385eSFrançois Tigeot (n) & (1ULL << 30) ? 30 : \ 133*1392385eSFrançois Tigeot (n) & (1ULL << 29) ? 29 : \ 134*1392385eSFrançois Tigeot (n) & (1ULL << 28) ? 28 : \ 135*1392385eSFrançois Tigeot (n) & (1ULL << 27) ? 27 : \ 136*1392385eSFrançois Tigeot (n) & (1ULL << 26) ? 26 : \ 137*1392385eSFrançois Tigeot (n) & (1ULL << 25) ? 25 : \ 138*1392385eSFrançois Tigeot (n) & (1ULL << 24) ? 24 : \ 139*1392385eSFrançois Tigeot (n) & (1ULL << 23) ? 23 : \ 140*1392385eSFrançois Tigeot (n) & (1ULL << 22) ? 22 : \ 141*1392385eSFrançois Tigeot (n) & (1ULL << 21) ? 21 : \ 142*1392385eSFrançois Tigeot (n) & (1ULL << 20) ? 20 : \ 143*1392385eSFrançois Tigeot (n) & (1ULL << 19) ? 19 : \ 144*1392385eSFrançois Tigeot (n) & (1ULL << 18) ? 18 : \ 145*1392385eSFrançois Tigeot (n) & (1ULL << 17) ? 17 : \ 146*1392385eSFrançois Tigeot (n) & (1ULL << 16) ? 16 : \ 147*1392385eSFrançois Tigeot (n) & (1ULL << 15) ? 15 : \ 148*1392385eSFrançois Tigeot (n) & (1ULL << 14) ? 14 : \ 149*1392385eSFrançois Tigeot (n) & (1ULL << 13) ? 13 : \ 150*1392385eSFrançois Tigeot (n) & (1ULL << 12) ? 12 : \ 151*1392385eSFrançois Tigeot (n) & (1ULL << 11) ? 11 : \ 152*1392385eSFrançois Tigeot (n) & (1ULL << 10) ? 10 : \ 153*1392385eSFrançois Tigeot (n) & (1ULL << 9) ? 9 : \ 154*1392385eSFrançois Tigeot (n) & (1ULL << 8) ? 8 : \ 155*1392385eSFrançois Tigeot (n) & (1ULL << 7) ? 7 : \ 156*1392385eSFrançois Tigeot (n) & (1ULL << 6) ? 6 : \ 157*1392385eSFrançois Tigeot (n) & (1ULL << 5) ? 5 : \ 158*1392385eSFrançois Tigeot (n) & (1ULL << 4) ? 4 : \ 159*1392385eSFrançois Tigeot (n) & (1ULL << 3) ? 3 : \ 160*1392385eSFrançois Tigeot (n) & (1ULL << 2) ? 2 : \ 161*1392385eSFrançois Tigeot (n) & (1ULL << 1) ? 1 : \ 162*1392385eSFrançois Tigeot (n) & (1ULL << 0) ? 0 : \ 163*1392385eSFrançois Tigeot ____ilog2_NaN() \ 164*1392385eSFrançois Tigeot ) : \ 165*1392385eSFrançois Tigeot (sizeof(n) <= 4) ? \ 166*1392385eSFrançois Tigeot __ilog2_u32(n) : \ 167*1392385eSFrançois Tigeot __ilog2_u64(n) \ 168*1392385eSFrançois Tigeot ) 169*1392385eSFrançois Tigeot 170*1392385eSFrançois Tigeot #endif /* _LINUX_LOG2_H_ */ 171