You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

traits.h 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. {
  24. ABSL_NAMESPACE_BEGIN
  25. namespace random_internal
  26. {
  27. // random_internal::is_widening_convertible<A, B>
  28. //
  29. // Returns whether a type A is widening-convertible to a type B.
  30. //
  31. // A is widening-convertible to B means:
  32. // A a = <any number>;
  33. // B b = a;
  34. // A c = b;
  35. // EXPECT_EQ(a, c);
  36. template<typename A, typename B>
  37. class is_widening_convertible
  38. {
  39. // As long as there are enough bits in the exact part of a number:
  40. // - unsigned can fit in float, signed, unsigned
  41. // - signed can fit in float, signed
  42. // - float can fit in float
  43. // So we define rank to be:
  44. // - rank(float) -> 2
  45. // - rank(signed) -> 1
  46. // - rank(unsigned) -> 0
  47. template<class T>
  48. static constexpr int rank()
  49. {
  50. return !std::numeric_limits<T>::is_integer +
  51. std::numeric_limits<T>::is_signed;
  52. }
  53. public:
  54. // If an arithmetic-type B can represent at least as many digits as a type A,
  55. // and B belongs to a rank no lower than A, then A can be safely represented
  56. // by B through a widening-conversion.
  57. static constexpr bool value =
  58. std::numeric_limits<A>::digits <= std::numeric_limits<B>::digits &&
  59. rank<A>() <= rank<B>();
  60. };
  61. template<typename T>
  62. struct IsIntegral : std::is_integral<T>
  63. {
  64. };
  65. template<>
  66. struct IsIntegral<absl::int128> : std::true_type
  67. {
  68. };
  69. template<>
  70. struct IsIntegral<absl::uint128> : std::true_type
  71. {
  72. };
  73. template<typename T>
  74. struct MakeUnsigned : std::make_unsigned<T>
  75. {
  76. };
  77. template<>
  78. struct MakeUnsigned<absl::int128>
  79. {
  80. using type = absl::uint128;
  81. };
  82. template<>
  83. struct MakeUnsigned<absl::uint128>
  84. {
  85. using type = absl::uint128;
  86. };
  87. template<typename T>
  88. struct IsUnsigned : std::is_unsigned<T>
  89. {
  90. };
  91. template<>
  92. struct IsUnsigned<absl::int128> : std::false_type
  93. {
  94. };
  95. template<>
  96. struct IsUnsigned<absl::uint128> : std::true_type
  97. {
  98. };
  99. // unsigned_bits<N>::type returns the unsigned int type with the indicated
  100. // number of bits.
  101. template<size_t N>
  102. struct unsigned_bits;
  103. template<>
  104. struct unsigned_bits<8>
  105. {
  106. using type = uint8_t;
  107. };
  108. template<>
  109. struct unsigned_bits<16>
  110. {
  111. using type = uint16_t;
  112. };
  113. template<>
  114. struct unsigned_bits<32>
  115. {
  116. using type = uint32_t;
  117. };
  118. template<>
  119. struct unsigned_bits<64>
  120. {
  121. using type = uint64_t;
  122. };
  123. template<>
  124. struct unsigned_bits<128>
  125. {
  126. using type = absl::uint128;
  127. };
  128. // 256-bit wrapper for wide multiplications.
  129. struct U256
  130. {
  131. uint128 hi;
  132. uint128 lo;
  133. };
  134. template<>
  135. struct unsigned_bits<256>
  136. {
  137. using type = U256;
  138. };
  139. template<typename IntType>
  140. struct make_unsigned_bits
  141. {
  142. using type = typename unsigned_bits<
  143. std::numeric_limits<typename MakeUnsigned<IntType>::type>::digits>::type;
  144. };
  145. template<typename T>
  146. int BitWidth(T v)
  147. {
  148. // Workaround for bit_width not supporting int128.
  149. // Don't hardcode `64` to make sure this code does not trigger compiler
  150. // warnings in smaller types.
  151. constexpr int half_bits = sizeof(T) * 8 / 2;
  152. if (sizeof(T) == 16 && (v >> half_bits) != 0)
  153. {
  154. return bit_width(static_cast<uint64_t>(v >> half_bits)) + half_bits;
  155. }
  156. else
  157. {
  158. return bit_width(static_cast<uint64_t>(v));
  159. }
  160. }
  161. } // namespace random_internal
  162. ABSL_NAMESPACE_END
  163. } // namespace absl
  164. #endif // ABSL_RANDOM_INTERNAL_TRAITS_H_