log2.h (219820) | log2.h (255932) |
---|---|
1/*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 37 unchanged lines hidden (view full) --- 46} 47 48static inline unsigned long 49rounddown_pow_of_two(unsigned long x) 50{ 51 return (1UL << (flsl(x) - 1)); 52} 53 | 1/*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 37 unchanged lines hidden (view full) --- 46} 47 48static inline unsigned long 49rounddown_pow_of_two(unsigned long x) 50{ 51 return (1UL << (flsl(x) - 1)); 52} 53 |
54static inline unsigned long 55ilog2(unsigned long x) | 54 55/* 56 * deal with unrepresentable constant logarithms 57 */ 58extern __attribute__((const, noreturn)) 59int ____ilog2_NaN(void); 60 61/* 62 * non-constant log of base 2 calculators 63 * - the arch may override these in asm/bitops.h if they can be implemented 64 * more efficiently than using fls() and fls64() 65 * - the arch is not required to handle n==0 if implementing the fallback 66 */ 67#ifndef CONFIG_ARCH_HAS_ILOG2_U32 68static inline __attribute__((const)) 69int __ilog2_u32(u32 n) |
56{ | 70{ |
57 return (flsl(x) - 1); | 71 return flsl(n) - 1; |
58} | 72} |
73#endif |
|
59 | 74 |
75#ifndef CONFIG_ARCH_HAS_ILOG2_U64 76static inline __attribute__((const)) 77int __ilog2_u64(u64 n) 78{ 79 return flsl(n) - 1; 80} 81#endif 82 83 84/** 85 * ilog2 - log of base 2 of 32-bit or a 64-bit unsigned value 86 * @n - parameter 87 * 88 * constant-capable log of base 2 calculation 89 * - this can be used to initialise global variables from constant data, hence 90 * the massive ternary operator construction 91 * 92 * selects the appropriately-sized optimised version depending on sizeof(n) 93 */ 94#define ilog2(n) \ 95( \ 96 __builtin_constant_p(n) ? ( \ 97 (n) < 1 ? ____ilog2_NaN() : \ 98 (n) & (1ULL << 63) ? 63 : \ 99 (n) & (1ULL << 62) ? 62 : \ 100 (n) & (1ULL << 61) ? 61 : \ 101 (n) & (1ULL << 60) ? 60 : \ 102 (n) & (1ULL << 59) ? 59 : \ 103 (n) & (1ULL << 58) ? 58 : \ 104 (n) & (1ULL << 57) ? 57 : \ 105 (n) & (1ULL << 56) ? 56 : \ 106 (n) & (1ULL << 55) ? 55 : \ 107 (n) & (1ULL << 54) ? 54 : \ 108 (n) & (1ULL << 53) ? 53 : \ 109 (n) & (1ULL << 52) ? 52 : \ 110 (n) & (1ULL << 51) ? 51 : \ 111 (n) & (1ULL << 50) ? 50 : \ 112 (n) & (1ULL << 49) ? 49 : \ 113 (n) & (1ULL << 48) ? 48 : \ 114 (n) & (1ULL << 47) ? 47 : \ 115 (n) & (1ULL << 46) ? 46 : \ 116 (n) & (1ULL << 45) ? 45 : \ 117 (n) & (1ULL << 44) ? 44 : \ 118 (n) & (1ULL << 43) ? 43 : \ 119 (n) & (1ULL << 42) ? 42 : \ 120 (n) & (1ULL << 41) ? 41 : \ 121 (n) & (1ULL << 40) ? 40 : \ 122 (n) & (1ULL << 39) ? 39 : \ 123 (n) & (1ULL << 38) ? 38 : \ 124 (n) & (1ULL << 37) ? 37 : \ 125 (n) & (1ULL << 36) ? 36 : \ 126 (n) & (1ULL << 35) ? 35 : \ 127 (n) & (1ULL << 34) ? 34 : \ 128 (n) & (1ULL << 33) ? 33 : \ 129 (n) & (1ULL << 32) ? 32 : \ 130 (n) & (1ULL << 31) ? 31 : \ 131 (n) & (1ULL << 30) ? 30 : \ 132 (n) & (1ULL << 29) ? 29 : \ 133 (n) & (1ULL << 28) ? 28 : \ 134 (n) & (1ULL << 27) ? 27 : \ 135 (n) & (1ULL << 26) ? 26 : \ 136 (n) & (1ULL << 25) ? 25 : \ 137 (n) & (1ULL << 24) ? 24 : \ 138 (n) & (1ULL << 23) ? 23 : \ 139 (n) & (1ULL << 22) ? 22 : \ 140 (n) & (1ULL << 21) ? 21 : \ 141 (n) & (1ULL << 20) ? 20 : \ 142 (n) & (1ULL << 19) ? 19 : \ 143 (n) & (1ULL << 18) ? 18 : \ 144 (n) & (1ULL << 17) ? 17 : \ 145 (n) & (1ULL << 16) ? 16 : \ 146 (n) & (1ULL << 15) ? 15 : \ 147 (n) & (1ULL << 14) ? 14 : \ 148 (n) & (1ULL << 13) ? 13 : \ 149 (n) & (1ULL << 12) ? 12 : \ 150 (n) & (1ULL << 11) ? 11 : \ 151 (n) & (1ULL << 10) ? 10 : \ 152 (n) & (1ULL << 9) ? 9 : \ 153 (n) & (1ULL << 8) ? 8 : \ 154 (n) & (1ULL << 7) ? 7 : \ 155 (n) & (1ULL << 6) ? 6 : \ 156 (n) & (1ULL << 5) ? 5 : \ 157 (n) & (1ULL << 4) ? 4 : \ 158 (n) & (1ULL << 3) ? 3 : \ 159 (n) & (1ULL << 2) ? 2 : \ 160 (n) & (1ULL << 1) ? 1 : \ 161 (n) & (1ULL << 0) ? 0 : \ 162 ____ilog2_NaN() \ 163 ) : \ 164 (sizeof(n) <= 4) ? \ 165 __ilog2_u32(n) : \ 166 __ilog2_u64(n) \ 167 ) 168 |
|
60#endif /* _LINUX_LOG2_H_ */ | 169#endif /* _LINUX_LOG2_H_ */ |