1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2015 Mellanox Technologies, Ltd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 #ifndef _LINUXKPI_LINUX_LOG2_H_ 30 #define _LINUXKPI_LINUX_LOG2_H_ 31 32 #include <linux/types.h> 33 34 #include <sys/libkern.h> 35 36 static inline unsigned long 37 roundup_pow_of_two(unsigned long x) 38 { 39 return (1UL << flsl(x - 1)); 40 } 41 42 static inline int 43 is_power_of_2(unsigned long n) 44 { 45 return (n == roundup_pow_of_two(n)); 46 } 47 48 static inline unsigned long 49 rounddown_pow_of_two(unsigned long x) 50 { 51 return (1UL << (flsl(x) - 1)); 52 } 53 54 #define ilog2(n) \ 55 ( \ 56 __builtin_constant_p(n) ? ( \ 57 (n) < 1 ? -1 : \ 58 (n) & (1ULL << 63) ? 63 : \ 59 (n) & (1ULL << 62) ? 62 : \ 60 (n) & (1ULL << 61) ? 61 : \ 61 (n) & (1ULL << 60) ? 60 : \ 62 (n) & (1ULL << 59) ? 59 : \ 63 (n) & (1ULL << 58) ? 58 : \ 64 (n) & (1ULL << 57) ? 57 : \ 65 (n) & (1ULL << 56) ? 56 : \ 66 (n) & (1ULL << 55) ? 55 : \ 67 (n) & (1ULL << 54) ? 54 : \ 68 (n) & (1ULL << 53) ? 53 : \ 69 (n) & (1ULL << 52) ? 52 : \ 70 (n) & (1ULL << 51) ? 51 : \ 71 (n) & (1ULL << 50) ? 50 : \ 72 (n) & (1ULL << 49) ? 49 : \ 73 (n) & (1ULL << 48) ? 48 : \ 74 (n) & (1ULL << 47) ? 47 : \ 75 (n) & (1ULL << 46) ? 46 : \ 76 (n) & (1ULL << 45) ? 45 : \ 77 (n) & (1ULL << 44) ? 44 : \ 78 (n) & (1ULL << 43) ? 43 : \ 79 (n) & (1ULL << 42) ? 42 : \ 80 (n) & (1ULL << 41) ? 41 : \ 81 (n) & (1ULL << 40) ? 40 : \ 82 (n) & (1ULL << 39) ? 39 : \ 83 (n) & (1ULL << 38) ? 38 : \ 84 (n) & (1ULL << 37) ? 37 : \ 85 (n) & (1ULL << 36) ? 36 : \ 86 (n) & (1ULL << 35) ? 35 : \ 87 (n) & (1ULL << 34) ? 34 : \ 88 (n) & (1ULL << 33) ? 33 : \ 89 (n) & (1ULL << 32) ? 32 : \ 90 (n) & (1ULL << 31) ? 31 : \ 91 (n) & (1ULL << 30) ? 30 : \ 92 (n) & (1ULL << 29) ? 29 : \ 93 (n) & (1ULL << 28) ? 28 : \ 94 (n) & (1ULL << 27) ? 27 : \ 95 (n) & (1ULL << 26) ? 26 : \ 96 (n) & (1ULL << 25) ? 25 : \ 97 (n) & (1ULL << 24) ? 24 : \ 98 (n) & (1ULL << 23) ? 23 : \ 99 (n) & (1ULL << 22) ? 22 : \ 100 (n) & (1ULL << 21) ? 21 : \ 101 (n) & (1ULL << 20) ? 20 : \ 102 (n) & (1ULL << 19) ? 19 : \ 103 (n) & (1ULL << 18) ? 18 : \ 104 (n) & (1ULL << 17) ? 17 : \ 105 (n) & (1ULL << 16) ? 16 : \ 106 (n) & (1ULL << 15) ? 15 : \ 107 (n) & (1ULL << 14) ? 14 : \ 108 (n) & (1ULL << 13) ? 13 : \ 109 (n) & (1ULL << 12) ? 12 : \ 110 (n) & (1ULL << 11) ? 11 : \ 111 (n) & (1ULL << 10) ? 10 : \ 112 (n) & (1ULL << 9) ? 9 : \ 113 (n) & (1ULL << 8) ? 8 : \ 114 (n) & (1ULL << 7) ? 7 : \ 115 (n) & (1ULL << 6) ? 6 : \ 116 (n) & (1ULL << 5) ? 5 : \ 117 (n) & (1ULL << 4) ? 4 : \ 118 (n) & (1ULL << 3) ? 3 : \ 119 (n) & (1ULL << 2) ? 2 : \ 120 (n) & (1ULL << 1) ? 1 : \ 121 (n) & (1ULL << 0) ? 0 : \ 122 -1) : \ 123 (sizeof(n) <= 4) ? \ 124 fls((u32)(n)) - 1 : flsll((u64)(n)) - 1 \ 125 ) 126 127 #define order_base_2(x) ilog2(roundup_pow_of_two(x)) 128 129 #endif /* _LINUXKPI_LINUX_LOG2_H_ */ 130