CatapultServer  v0.5.0.1 (Elephant)
IntegerMath.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include <cstdint>
23 #include <limits>
24 #include <type_traits>
25 
26 namespace catapult { namespace utils {
27 
29  template<typename T>
30  bool CheckedAdd(T& value, T delta) {
31  if (value > std::numeric_limits<T>::max() - delta)
32  return false;
33 
34  value += delta;
35  return true;
36  }
37 
39  template<typename T, typename X = std::enable_if_t<std::is_integral_v<T>>>
40  constexpr T GetNumBits() {
41  return static_cast<T>(8u * sizeof(T));
42  }
43 
45  template<typename T, typename X = std::enable_if_t<std::is_unsigned_v<T>>>
46  constexpr T Log2(T value) {
47 #ifdef _MSC_VER
48  unsigned long result;
49  if (!_BitScanReverse(&result, value))
50  return std::numeric_limits<T>::max();
51 
52  return static_cast<T>(result);
53 #else
54  if (!value)
55  return std::numeric_limits<T>::max();
56 
57  return static_cast<T>(63 - __builtin_clzll(value));
58 #endif
59  }
60 
62  uint64_t Log2TimesPowerOfTwo(uint64_t value, uint64_t n);
63 
65  template<typename T, typename X = std::enable_if_t<std::is_unsigned_v<T>>>
66  constexpr T Pow2(T value) {
67  return value >= GetNumBits<T>() ? 0 : static_cast<T>(static_cast<T>(1) << value);
68  }
69 
71  template<typename T, typename X = std::enable_if_t<std::is_unsigned_v<T>>>
72  constexpr T DivideAndGetRemainder(T& value, T divisor) {
73  auto remainder = static_cast<T>(value % divisor);
74  value /= divisor;
75  return remainder;
76  }
77 
79  template<typename T, typename X = std::enable_if_t<std::is_unsigned_v<T>>>
80  constexpr bool IsPowerMultiple(T lhs, T rhs, T base) {
81  if (lhs > rhs || 0 != rhs % lhs)
82  return false;
83 
84  T quotient = rhs / lhs;
85  while (quotient > 1) {
86  if (0 != DivideAndGetRemainder(quotient, base))
87  return false;
88  }
89 
90  return true;
91  }
92 }}
catapult::utils::Pow2
constexpr T Pow2(T value)
Calculates 2^(value).
Definition: IntegerMath.h:66
exceptions.h
catapult::utils::Log2TimesPowerOfTwo
uint64_t Log2TimesPowerOfTwo(uint64_t value, uint64_t n)
Calculates log2(value^(2^n)).
Definition: IntegerMath.cpp:48
IntegerMath.h
catapult::utils::GetNumBits
constexpr T GetNumBits()
Gets the number of bits in the specified type.
Definition: IntegerMath.h:40
CATAPULT_THROW_INVALID_ARGUMENT
#define CATAPULT_THROW_INVALID_ARGUMENT(MESSAGE)
Macro used to throw a catapult invalid argument.
Definition: exceptions.h:179
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::Log2
constexpr T Log2(T value)
Calculates log2(value).
Definition: IntegerMath.h:46
catapult::utils::IsPowerMultiple
constexpr bool IsPowerMultiple(T lhs, T rhs, T base)
Returns true if rhs is equal to lhs multipled by a power of base.
Definition: IntegerMath.h:80
catapult::utils::DivideAndGetRemainder
constexpr T DivideAndGetRemainder(T &value, T divisor)
Divides value by divisor and returns the remainder.
Definition: IntegerMath.h:72
catapult::utils::CheckedAdd
bool CheckedAdd(T &value, T delta)
Adds delta to value if and only if there is no overflow.
Definition: IntegerMath.h:30