unsigned.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #pragma once
  2. namespace nall {
  3. template<typename T, enable_if_t<is_unsigned<T>::value>>
  4. inline auto upper(T value) -> T {
  5. return value >> sizeof(T) * 4;
  6. }
  7. template<typename T, enable_if_t<is_unsigned<T>::value>>
  8. inline auto lower(T value) -> T {
  9. static const T Mask = ~T(0) >> sizeof(T) * 4;
  10. return value & Mask;
  11. }
  12. template<typename T, typename U, enable_if_t<is_unsigned<T>::value>, enable_if_t<is_unsigned<U>::value>>
  13. inline auto mul(T lhs, U rhs) -> uintmax {
  14. return lhs * rhs;
  15. }
  16. template<typename T, enable_if_t<is_unsigned<T>::value>>
  17. inline auto square(T value) -> uintmax {
  18. return value * value;
  19. }
  20. template<typename T, typename U>
  21. inline auto rol(T lhs, U rhs, enable_if_t<is_unsigned<T>::value>* = 0) -> T {
  22. return lhs << rhs | lhs >> sizeof(T) * 8 - rhs;
  23. }
  24. template<typename T, typename U>
  25. inline auto ror(T lhs, U rhs, enable_if_t<is_unsigned<T>::value>* = 0) -> T {
  26. return lhs >> rhs | lhs << sizeof(T) * 8 - rhs;
  27. }
  28. #if INTMAX_BITS >= 128
  29. inline auto operator"" _u128(const char* s) -> uint128_t {
  30. uint128_t p = 0;
  31. if(s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
  32. s += 2;
  33. while(*s) {
  34. auto c = *s++;
  35. if(c == '\'');
  36. else if(c >= '0' && c <= '9') p = (p << 4) + (c - '0');
  37. else if(c >= 'a' && c <= 'f') p = (p << 4) + (c - 'a' + 10);
  38. else if(c >= 'A' && c <= 'F') p = (p << 4) + (c - 'A' + 10);
  39. else break;
  40. }
  41. } else {
  42. while(*s) {
  43. auto c = *s++;
  44. if(c == '\'');
  45. else if(c >= '0' && c <= '9') p = (p << 3) + (p << 1) + (c - '0');
  46. else break;
  47. }
  48. }
  49. return p;
  50. }
  51. #endif
  52. }