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.

wide_multiply.h 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_WIDE_MULTIPLY_H_
  15. #define ABSL_RANDOM_INTERNAL_WIDE_MULTIPLY_H_
  16. #include <cstdint>
  17. #include <limits>
  18. #include <type_traits>
  19. #if (defined(_WIN32) || defined(_WIN64)) && defined(_M_IA64)
  20. #include <intrin.h> // NOLINT(build/include_order)
  21. #pragma intrinsic(_umul128)
  22. #define ABSL_INTERNAL_USE_UMUL128 1
  23. #endif
  24. #include "absl/base/config.h"
  25. #include "absl/numeric/bits.h"
  26. #include "absl/numeric/int128.h"
  27. #include "absl/random/internal/traits.h"
  28. namespace absl
  29. {
  30. ABSL_NAMESPACE_BEGIN
  31. namespace random_internal
  32. {
  33. // wide_multiply<T> multiplies two N-bit values to a 2N-bit result.
  34. template<typename UIntType>
  35. struct wide_multiply
  36. {
  37. static constexpr size_t kN = std::numeric_limits<UIntType>::digits;
  38. using input_type = UIntType;
  39. using result_type = typename random_internal::unsigned_bits<kN * 2>::type;
  40. static result_type multiply(input_type a, input_type b)
  41. {
  42. return static_cast<result_type>(a) * b;
  43. }
  44. static input_type hi(result_type r)
  45. {
  46. return static_cast<input_type>(r >> kN);
  47. }
  48. static input_type lo(result_type r)
  49. {
  50. return static_cast<input_type>(r);
  51. }
  52. static_assert(std::is_unsigned<UIntType>::value, "Class-template wide_multiply<> argument must be unsigned.");
  53. };
  54. // MultiplyU128ToU256 multiplies two 128-bit values to a 256-bit value.
  55. inline U256 MultiplyU128ToU256(uint128 a, uint128 b)
  56. {
  57. const uint128 a00 = static_cast<uint64_t>(a);
  58. const uint128 a64 = a >> 64;
  59. const uint128 b00 = static_cast<uint64_t>(b);
  60. const uint128 b64 = b >> 64;
  61. const uint128 c00 = a00 * b00;
  62. const uint128 c64a = a00 * b64;
  63. const uint128 c64b = a64 * b00;
  64. const uint128 c128 = a64 * b64;
  65. const uint64_t carry =
  66. static_cast<uint64_t>(((c00 >> 64) + static_cast<uint64_t>(c64a) + static_cast<uint64_t>(c64b)) >> 64);
  67. return {c128 + (c64a >> 64) + (c64b >> 64) + carry, c00 + (c64a << 64) + (c64b << 64)};
  68. }
  69. template<>
  70. struct wide_multiply<uint128>
  71. {
  72. using input_type = uint128;
  73. using result_type = U256;
  74. static result_type multiply(input_type a, input_type b)
  75. {
  76. return MultiplyU128ToU256(a, b);
  77. }
  78. static input_type hi(result_type r)
  79. {
  80. return r.hi;
  81. }
  82. static input_type lo(result_type r)
  83. {
  84. return r.lo;
  85. }
  86. };
  87. } // namespace random_internal
  88. ABSL_NAMESPACE_END
  89. } // namespace absl
  90. #endif // ABSL_RANDOM_INTERNAL_WIDE_MULTIPLY_H_