Changeset 615 for GPL/branches/uniaud32-next/include/linux/log2.h
- Timestamp:
- Jan 1, 2021, 5:31:48 AM (5 years ago)
- Location:
- GPL/branches/uniaud32-next
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
GPL/branches/uniaud32-next/include/linux/log2.h
r598 r615 2 2 #define _LINUX_LOG2_H 3 3 4 /* 5 * Determine whether some value is a power of two, where zero is 6 * *not* considered a power of two. 7 */ 8 9 static inline 10 bool is_power_of_2(unsigned long n) 11 { 12 return (n != 0 && ((n & (n - 1)) == 0)); 13 } 14 15 /* 16 * round up to nearest power of two 17 */ 18 static inline 19 unsigned long __roundup_pow_of_two(unsigned long n) 20 { 21 return 1UL << fls_long(n - 1); 22 } 23 24 /* 25 * round down to nearest power of two 26 */ 27 static inline 28 unsigned long __rounddown_pow_of_two(unsigned long n) 29 { 30 return 1UL << (fls_long(n) - 1); 31 } 32 33 /***********************************************/ 34 /* Locate the position of the highest bit set. */ 35 /* A binary search is used. The result is an */ 36 /* approximation of log2(n) [the integer part] */ 37 /***********************************************/ 38 static inline int ilog2(unsigned long n) 39 { 40 int i = (-1); 41 42 /* Is there a bit on in the high word? */ 43 /* Else, all the high bits are already zero. */ 44 if (n & 0xffff0000) { 45 i += 16; /* Update our search position */ 46 n >>= 16; /* Shift out lower (irrelevant) bits */ 47 } 48 /* Is there a bit on in the high byte of the current word? */ 49 /* Else, all the high bits are already zero. */ 50 if (n & 0xff00) { 51 i += 8; /* Update our search position */ 52 n >>= 8; /* Shift out lower (irrelevant) bits */ 53 } 54 /* Is there a bit on in the current nybble? */ 55 /* Else, all the high bits are already zero. */ 56 if (n & 0xf0) { 57 i += 4; /* Update our search position */ 58 n >>= 4; /* Shift out lower (irrelevant) bits */ 59 } 60 /* Is there a bit on in the high 2 bits of the current nybble? */ 61 /* 0xc is 1100 in binary... */ 62 /* Else, all the high bits are already zero. */ 63 if (n & 0xc) { 64 i += 2; /* Update our search position */ 65 n >>= 2; /* Shift out lower (irrelevant) bits */ 66 } 67 /* Is the 2nd bit on? [ 0x2 is 0010 in binary...] */ 68 /* Else, all the 2nd bit is already zero. */ 69 if (n & 0x2) { 70 i++; /* Update our search position */ 71 n >>= 1; /* Shift out lower (irrelevant) bit */ 72 } 73 /* Is the lowest bit set? */ 74 if (n) 75 i++; /* Update our search position */ 76 return i; 77 } 78 4 79 #endif /* _LINUX_LOG2_H */
Note:
See TracChangeset
for help on using the changeset viewer.