File: unsigned.hpp

package info (click to toggle)
higan 106-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 9,640 kB
  • sloc: cpp: 108,736; ansic: 809; makefile: 22; sh: 7
file content (39 lines) | stat: -rw-r--r-- 1,056 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once

namespace nall {

template<typename T, enable_if_t<is_unsigned<T>::value>> alwaysinline auto upper(T value) -> T {
  return value >> sizeof(T) * 4;
}

template<typename T, enable_if_t<is_unsigned<T>::value>> alwaysinline auto lower(T value) -> T {
  static const T Mask = T(0) - 1 >> sizeof(T) * 4;
  return value & Mask;
}

alwaysinline auto square(uintmax value) -> uintmax {
  return value * value;
}

template<typename T, typename U> alwaysinline auto rol(const T& lhs, const U& rhs, enable_if_t<is_unsigned<T>::value>* = 0) -> T {
  return lhs << rhs | lhs >> (sizeof(T) * 8 - rhs);
}

template<typename T, typename U> alwaysinline auto ror(const T& lhs, const U& rhs, enable_if_t<is_unsigned<T>::value>* = 0) -> T {
  return lhs >> rhs | lhs << (sizeof(T) * 8 - rhs);
}

#if INTMAX_BITS >= 128
inline auto operator"" _u128(const char* s) -> uint128_t {
  uint128_t p = 0;
  while(*s) {
    auto c = *s++;
    if(c == '\'') continue;
    if(c < '0' || c > '9') break;
    p = (p << 3) + (p << 1) + (c - '0');
  }
  return p;
}
#endif

}