wide_multiply.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ABSL_NAMESPACE_BEGIN
  30. namespace random_internal {
  31. // Helper object to multiply two 64-bit values to a 128-bit value.
  32. // MultiplyU64ToU128 multiplies two 64-bit values to a 128-bit value.
  33. // If an intrinsic is available, it is used, otherwise use native 32-bit
  34. // multiplies to construct the result.
  35. inline absl::uint128 MultiplyU64ToU128(uint64_t a, uint64_t b) {
  36. #if defined(ABSL_HAVE_INTRINSIC_INT128)
  37. return absl::uint128(static_cast<__uint128_t>(a) * b);
  38. #elif defined(ABSL_INTERNAL_USE_UMUL128)
  39. // uint64_t * uint64_t => uint128 multiply using imul intrinsic on MSVC.
  40. uint64_t high = 0;
  41. const uint64_t low = _umul128(a, b, &high);
  42. return absl::MakeUint128(high, low);
  43. #else
  44. // uint128(a) * uint128(b) in emulated mode computes a full 128-bit x 128-bit
  45. // multiply. However there are many cases where that is not necessary, and it
  46. // is only necessary to support a 64-bit x 64-bit = 128-bit multiply. This is
  47. // for those cases.
  48. const uint64_t a00 = static_cast<uint32_t>(a);
  49. const uint64_t a32 = a >> 32;
  50. const uint64_t b00 = static_cast<uint32_t>(b);
  51. const uint64_t b32 = b >> 32;
  52. const uint64_t c00 = a00 * b00;
  53. const uint64_t c32a = a00 * b32;
  54. const uint64_t c32b = a32 * b00;
  55. const uint64_t c64 = a32 * b32;
  56. const uint32_t carry =
  57. static_cast<uint32_t>(((c00 >> 32) + static_cast<uint32_t>(c32a) +
  58. static_cast<uint32_t>(c32b)) >>
  59. 32);
  60. return absl::MakeUint128(c64 + (c32a >> 32) + (c32b >> 32) + carry,
  61. c00 + (c32a << 32) + (c32b << 32));
  62. #endif
  63. }
  64. // wide_multiply<T> multiplies two N-bit values to a 2N-bit result.
  65. template <typename UIntType>
  66. struct wide_multiply {
  67. static constexpr size_t kN = std::numeric_limits<UIntType>::digits;
  68. using input_type = UIntType;
  69. using result_type = typename random_internal::unsigned_bits<kN * 2>::type;
  70. static result_type multiply(input_type a, input_type b) {
  71. return static_cast<result_type>(a) * b;
  72. }
  73. static input_type hi(result_type r) { return r >> kN; }
  74. static input_type lo(result_type r) { return r; }
  75. static_assert(std::is_unsigned<UIntType>::value,
  76. "Class-template wide_multiply<> argument must be unsigned.");
  77. };
  78. #ifndef ABSL_HAVE_INTRINSIC_INT128
  79. template <>
  80. struct wide_multiply<uint64_t> {
  81. using input_type = uint64_t;
  82. using result_type = absl::uint128;
  83. static result_type multiply(uint64_t a, uint64_t b) {
  84. return MultiplyU64ToU128(a, b);
  85. }
  86. static uint64_t hi(result_type r) { return absl::Uint128High64(r); }
  87. static uint64_t lo(result_type r) { return absl::Uint128Low64(r); }
  88. };
  89. #endif
  90. } // namespace random_internal
  91. ABSL_NAMESPACE_END
  92. } // namespace absl
  93. #endif // ABSL_RANDOM_INTERNAL_WIDE_MULTIPLY_H_