traits.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_RANDOM_INTERNAL_TRAITS_H_
  15. #define ABSL_RANDOM_INTERNAL_TRAITS_H_
  16. #include <cstdint>
  17. #include <limits>
  18. #include <type_traits>
  19. #include "absl/base/config.h"
  20. #include "absl/numeric/bits.h"
  21. #include "absl/numeric/int128.h"
  22. namespace absl {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace random_internal {
  25. // random_internal::is_widening_convertible<A, B>
  26. //
  27. // Returns whether a type A is widening-convertible to a type B.
  28. //
  29. // A is widening-convertible to B means:
  30. // A a = <any number>;
  31. // B b = a;
  32. // A c = b;
  33. // EXPECT_EQ(a, c);
  34. template <typename A, typename B>
  35. class is_widening_convertible {
  36. // As long as there are enough bits in the exact part of a number:
  37. // - unsigned can fit in float, signed, unsigned
  38. // - signed can fit in float, signed
  39. // - float can fit in float
  40. // So we define rank to be:
  41. // - rank(float) -> 2
  42. // - rank(signed) -> 1
  43. // - rank(unsigned) -> 0
  44. template <class T>
  45. static constexpr int rank() {
  46. return !std::numeric_limits<T>::is_integer +
  47. std::numeric_limits<T>::is_signed;
  48. }
  49. public:
  50. // If an arithmetic-type B can represent at least as many digits as a type A,
  51. // and B belongs to a rank no lower than A, then A can be safely represented
  52. // by B through a widening-conversion.
  53. static constexpr bool value =
  54. std::numeric_limits<A>::digits <= std::numeric_limits<B>::digits &&
  55. rank<A>() <= rank<B>();
  56. };
  57. template <typename T>
  58. struct IsIntegral : std::is_integral<T> {};
  59. template <>
  60. struct IsIntegral<absl::int128> : std::true_type {};
  61. template <>
  62. struct IsIntegral<absl::uint128> : std::true_type {};
  63. template <typename T>
  64. struct MakeUnsigned : std::make_unsigned<T> {};
  65. template <>
  66. struct MakeUnsigned<absl::int128> {
  67. using type = absl::uint128;
  68. };
  69. template <>
  70. struct MakeUnsigned<absl::uint128> {
  71. using type = absl::uint128;
  72. };
  73. template <typename T>
  74. struct IsUnsigned : std::is_unsigned<T> {};
  75. template <>
  76. struct IsUnsigned<absl::int128> : std::false_type {};
  77. template <>
  78. struct IsUnsigned<absl::uint128> : std::true_type {};
  79. // unsigned_bits<N>::type returns the unsigned int type with the indicated
  80. // number of bits.
  81. template <size_t N>
  82. struct unsigned_bits;
  83. template <>
  84. struct unsigned_bits<8> {
  85. using type = uint8_t;
  86. };
  87. template <>
  88. struct unsigned_bits<16> {
  89. using type = uint16_t;
  90. };
  91. template <>
  92. struct unsigned_bits<32> {
  93. using type = uint32_t;
  94. };
  95. template <>
  96. struct unsigned_bits<64> {
  97. using type = uint64_t;
  98. };
  99. template <>
  100. struct unsigned_bits<128> {
  101. using type = absl::uint128;
  102. };
  103. // 256-bit wrapper for wide multiplications.
  104. struct U256 {
  105. uint128 hi;
  106. uint128 lo;
  107. };
  108. template <>
  109. struct unsigned_bits<256> {
  110. using type = U256;
  111. };
  112. template <typename IntType>
  113. struct make_unsigned_bits {
  114. using type = typename unsigned_bits<
  115. std::numeric_limits<typename MakeUnsigned<IntType>::type>::digits>::type;
  116. };
  117. template <typename T>
  118. int BitWidth(T v) {
  119. // Workaround for bit_width not supporting int128.
  120. // Don't hardcode `64` to make sure this code does not trigger compiler
  121. // warnings in smaller types.
  122. constexpr int half_bits = sizeof(T) * 8 / 2;
  123. if (sizeof(T) == 16 && (v >> half_bits) != 0) {
  124. return bit_width(static_cast<uint64_t>(v >> half_bits)) + half_bits;
  125. } else {
  126. return bit_width(static_cast<uint64_t>(v));
  127. }
  128. }
  129. } // namespace random_internal
  130. ABSL_NAMESPACE_END
  131. } // namespace absl
  132. #endif // ABSL_RANDOM_INTERNAL_TRAITS_H_