Guest User

Untitled

a guest
Jan 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. template <typename T>
  2. constexpr unsigned BitsRequired(T min, T max)
  3. {
  4. template <unsigned x> struct PopCount
  5. {
  6. enum {
  7. a = x - ((x >> 1) & 0x55555555),
  8. b = (((a >> 2) & 0x33333333) + (a & 0x33333333)),
  9. c = (((b >> 4) + b) & 0x0f0f0f0f),
  10. d = c + (c >> 8),
  11. e = d + (d >> 16),
  12. result = e & 0x0000003f
  13. };
  14. };
  15.  
  16. template <unsigned x> struct Log2
  17. {
  18. enum {
  19. a = x | (x >> 1),
  20. b = a | (a >> 2),
  21. c = b | (b >> 4),
  22. d = c | (c >> 8),
  23. e = d | (d >> 16),
  24. f = e >> 1,
  25. result = PopCount<f>::result
  26. };
  27. };
  28.  
  29. return (min == max) ? 0 : (Log2<uint32_t(max - min)>::result + 1);
  30. }
Add Comment
Please, Sign In to add comment