|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 2014 Richard Braun. ! 3: * ! 4: * This program is free software: you can redistribute it and/or modify ! 5: * it under the terms of the GNU General Public License as published by ! 6: * the Free Software Foundation, either version 2 of the License, or ! 7: * (at your option) any later version. ! 8: * ! 9: * This program is distributed in the hope that it will be useful, ! 10: * but WITHOUT ANY WARRANTY; without even the implied warranty of ! 11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ! 12: * GNU General Public License for more details. ! 13: * ! 14: * You should have received a copy of the GNU General Public License ! 15: * along with this program. If not, see <http://www.gnu.org/licenses/>. ! 16: * ! 17: * ! 18: * Integer base 2 logarithm operations. ! 19: */ ! 20: ! 21: #ifndef _KERN_LOG2_H ! 22: #define _KERN_LOG2_H ! 23: ! 24: #include <kern/assert.h> ! 25: ! 26: #ifdef __LP64__ ! 27: #define LONG_BIT 64 ! 28: #else /* __LP64__ */ ! 29: #define LONG_BIT 32 ! 30: #endif /* __LP64__ */ ! 31: ! 32: static inline unsigned int ! 33: ilog2(unsigned long x) ! 34: { ! 35: assert(x != 0); ! 36: return LONG_BIT - __builtin_clzl(x) - 1; ! 37: } ! 38: ! 39: static inline unsigned int ! 40: iorder2(unsigned long size) ! 41: { ! 42: assert(size != 0); ! 43: ! 44: if (size == 1) ! 45: return 0; ! 46: ! 47: return ilog2(size - 1) + 1; ! 48: } ! 49: ! 50: #endif /* _KERN_LOG2_H */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.