log2.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #ifndef _KERN_LOG2_H
  21. #define _KERN_LOG2_H
  22. #include <kern/assert.h>
  23. #ifdef __LP64__
  24. #define LONG_BIT 64
  25. #else /* __LP64__ */
  26. #define LONG_BIT 32
  27. #endif /* __LP64__ */
  28. static inline unsigned int
  29. gnumach_ilog2(unsigned long x)
  30. {
  31. assert(x != 0);
  32. return LONG_BIT - __builtin_clzl(x) - 1;
  33. }
  34. static inline unsigned int
  35. gnumach_iorder2(unsigned long size)
  36. {
  37. assert(size != 0);
  38. if (size == 1)
  39. return 0;
  40. return gnumach_ilog2(size - 1) + 1;
  41. }
  42. #endif /* _KERN_LOG2_H */