wide_multiply.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Y_ABSL_RANDOM_INTERNAL_WIDE_MULTIPLY_H_
  15. #define Y_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 Y_ABSL_INTERNAL_USE_UMUL128 1
  23. #endif
  24. #include "y_absl/base/config.h"
  25. #include "y_absl/numeric/bits.h"
  26. #include "y_absl/numeric/int128.h"
  27. #include "y_absl/random/internal/traits.h"
  28. namespace y_absl {
  29. Y_ABSL_NAMESPACE_BEGIN
  30. namespace random_internal {
  31. // wide_multiply<T> multiplies two N-bit values to a 2N-bit result.
  32. template <typename UIntType>
  33. struct wide_multiply {
  34. static constexpr size_t kN = std::numeric_limits<UIntType>::digits;
  35. using input_type = UIntType;
  36. using result_type = typename random_internal::unsigned_bits<kN * 2>::type;
  37. static result_type multiply(input_type a, input_type b) {
  38. return static_cast<result_type>(a) * b;
  39. }
  40. static input_type hi(result_type r) {
  41. return static_cast<input_type>(r >> kN);
  42. }
  43. static input_type lo(result_type r) { return static_cast<input_type>(r); }
  44. static_assert(std::is_unsigned<UIntType>::value,
  45. "Class-template wide_multiply<> argument must be unsigned.");
  46. };
  47. // MultiplyU128ToU256 multiplies two 128-bit values to a 256-bit value.
  48. inline U256 MultiplyU128ToU256(uint128 a, uint128 b) {
  49. const uint128 a00 = static_cast<uint64_t>(a);
  50. const uint128 a64 = a >> 64;
  51. const uint128 b00 = static_cast<uint64_t>(b);
  52. const uint128 b64 = b >> 64;
  53. const uint128 c00 = a00 * b00;
  54. const uint128 c64a = a00 * b64;
  55. const uint128 c64b = a64 * b00;
  56. const uint128 c128 = a64 * b64;
  57. const uint64_t carry =
  58. static_cast<uint64_t>(((c00 >> 64) + static_cast<uint64_t>(c64a) +
  59. static_cast<uint64_t>(c64b)) >>
  60. 64);
  61. return {c128 + (c64a >> 64) + (c64b >> 64) + carry,
  62. c00 + (c64a << 64) + (c64b << 64)};
  63. }
  64. template <>
  65. struct wide_multiply<uint128> {
  66. using input_type = uint128;
  67. using result_type = U256;
  68. static result_type multiply(input_type a, input_type b) {
  69. return MultiplyU128ToU256(a, b);
  70. }
  71. static input_type hi(result_type r) { return r.hi; }
  72. static input_type lo(result_type r) { return r.lo; }
  73. };
  74. } // namespace random_internal
  75. Y_ABSL_NAMESPACE_END
  76. } // namespace y_absl
  77. #endif // Y_ABSL_RANDOM_INTERNAL_WIDE_MULTIPLY_H_