General Purpose library for Freestanding C++ and POSIX systems
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

76 行
1.7 KiB

  1. #pragma once
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. namespace gp {
  5. namespace math {
  6. template<typename word_t>
  7. size_t log2(word_t v);
  8. /**
  9. Sean Eron Anderson
  10. seander@cs.stanford.edu
  11. **/
  12. template<>
  13. constexpr size_t log2<uint32_t>(uint32_t v)
  14. {
  15. constexpr int MultiplyDeBruijnBitPosition[32] =
  16. {
  17. 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
  18. 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
  19. };
  20. v |= v >> 1;
  21. v |= v >> 2;
  22. v |= v >> 4;
  23. v |= v >> 8;
  24. v |= v >> 16;
  25. return MultiplyDeBruijnBitPosition[(uint32_t)(v * 0x07C4ACDDU) >> 27];
  26. }
  27. template<>
  28. constexpr size_t log2<uint64_t>(uint64_t v)
  29. {
  30. constexpr int MultiplyDeBruijnBitPosition[64] =
  31. {
  32. 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61,
  33. 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62,
  34. 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21, 56,
  35. 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5, 63
  36. };
  37. v |= v >> 1;
  38. v |= v >> 2;
  39. v |= v >> 4;
  40. v |= v >> 8;
  41. v |= v >> 16;
  42. v |= v >> 32;
  43. return MultiplyDeBruijnBitPosition[(uint64_t)(v * 0x03f6eaf2cd271461) >> 58];
  44. }
  45. static_assert(log2<uint32_t>(7) == 2, "bad log2");
  46. static_assert(log2<uint32_t>(8) == 3, "bad log2");
  47. template<typename word_t>
  48. constexpr size_t msb(word_t v);
  49. template<>
  50. constexpr size_t msb<uint32_t>(uint32_t v)
  51. {
  52. auto l = log2(v);
  53. return l + (((1 << l) ^ v) != 0);
  54. }
  55. template<>
  56. constexpr size_t msb<uint64_t>(uint64_t v)
  57. {
  58. auto l = log2(v);
  59. return l + (((1 << l) ^ v) != 0);
  60. }
  61. static_assert(msb<uint32_t>(7) == 3, "bad log2");
  62. static_assert(msb<uint32_t>(8) == 3, "bad log2");
  63. }
  64. }