bit.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #include <nall/stdint.hpp>
  3. namespace nall {
  4. template<uint bits> inline auto uclamp(const uintmax x) -> uintmax {
  5. enum : uintmax { b = 1ull << (bits - 1), y = b * 2 - 1 };
  6. return y + ((x - y) & -(x < y)); //min(x, y);
  7. }
  8. template<uint bits> inline auto uclip(const uintmax x) -> uintmax {
  9. enum : uintmax { b = 1ull << (bits - 1), m = b * 2 - 1 };
  10. return (x & m);
  11. }
  12. template<uint bits> inline auto sclamp(const intmax x) -> intmax {
  13. enum : intmax { b = 1ull << (bits - 1), m = b - 1 };
  14. return (x > m) ? m : (x < -b) ? -b : x;
  15. }
  16. template<uint bits> inline auto sclip(const intmax x) -> intmax {
  17. enum : uintmax { b = 1ull << (bits - 1), m = b * 2 - 1 };
  18. return ((x & m) ^ b) - b;
  19. }
  20. namespace bit {
  21. constexpr inline auto mask(const char* s, uintmax sum = 0) -> uintmax {
  22. return (
  23. *s == '0' || *s == '1' ? mask(s + 1, (sum << 1) | 1) :
  24. *s == ' ' || *s == '_' ? mask(s + 1, sum) :
  25. *s ? mask(s + 1, sum << 1) :
  26. sum
  27. );
  28. }
  29. constexpr inline auto test(const char* s, uintmax sum = 0) -> uintmax {
  30. return (
  31. *s == '0' || *s == '1' ? test(s + 1, (sum << 1) | (*s - '0')) :
  32. *s == ' ' || *s == '_' ? test(s + 1, sum) :
  33. *s ? test(s + 1, sum << 1) :
  34. sum
  35. );
  36. }
  37. //lowest(0b1110) == 0b0010
  38. constexpr inline auto lowest(const uintmax x) -> uintmax {
  39. return x & -x;
  40. }
  41. //clear_lowest(0b1110) == 0b1100
  42. constexpr inline auto clearLowest(const uintmax x) -> uintmax {
  43. return x & (x - 1);
  44. }
  45. //set_lowest(0b0101) == 0b0111
  46. constexpr inline auto setLowest(const uintmax x) -> uintmax {
  47. return x | (x + 1);
  48. }
  49. //count number of bits set in a byte
  50. constexpr inline auto count(uintmax x) -> uint {
  51. uint count = 0;
  52. while(x) x &= x - 1, count++; //clear the least significant bit
  53. return count;
  54. }
  55. //return index of the first bit set (or zero of no bits are set)
  56. //first(0b1000) == 3
  57. constexpr inline auto first(uintmax x) -> uint {
  58. uint first = 0;
  59. while(x) { if(x & 1) break; x >>= 1; first++; }
  60. return first;
  61. }
  62. //round up to next highest single bit:
  63. //round(15) == 16, round(16) == 16, round(17) == 32
  64. constexpr inline auto round(uintmax x) -> uintmax {
  65. if((x & (x - 1)) == 0) return x;
  66. while(x & (x - 1)) x &= x - 1;
  67. return x << 1;
  68. }
  69. }
  70. }