integer.h 435 B

123456789101112131415161718192021222324252627282930
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "types.h"
  7. namespace crown
  8. {
  9. inline u32 next_pow_2(u32 x)
  10. {
  11. x--;
  12. x = (x >> 1) | x;
  13. x = (x >> 2) | x;
  14. x = (x >> 4) | x;
  15. x = (x >> 8) | x;
  16. x = (x >> 16) | x;
  17. return ++x;
  18. }
  19. inline bool is_pow_2(u32 x)
  20. {
  21. return !(x & (x - 1)) && x;
  22. }
  23. } // namespace crown