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
15 #include "absl/random/internal/wide_multiply.h"
16
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 #include "absl/numeric/int128.h"
20
21 using absl::random_internal::MultiplyU128ToU256;
22 using absl::random_internal::U256;
23
24 namespace {
25
LeftShift(U256 v,int s)26 U256 LeftShift(U256 v, int s) {
27 if (s == 0) {
28 return v;
29 } else if (s < 128) {
30 return {(v.hi << s) | (v.lo >> (128 - s)), v.lo << s};
31 } else {
32 return {v.lo << (s - 128), 0};
33 }
34 }
35
36 MATCHER_P2(Eq256, hi, lo, "") { return arg.hi == hi && arg.lo == lo; }
37 MATCHER_P(Eq256, v, "") { return arg.hi == v.hi && arg.lo == v.lo; }
38
TEST(WideMultiplyTest,MultiplyU128ToU256Test)39 TEST(WideMultiplyTest, MultiplyU128ToU256Test) {
40 using absl::uint128;
41 constexpr uint128 k1 = 1;
42 constexpr uint128 kMax = ~static_cast<uint128>(0);
43
44 EXPECT_THAT(MultiplyU128ToU256(0, 0), Eq256(0, 0));
45
46 // Max uin128_t
47 EXPECT_THAT(MultiplyU128ToU256(kMax, kMax), Eq256(kMax << 1, 1));
48 EXPECT_THAT(MultiplyU128ToU256(kMax, 1), Eq256(0, kMax));
49 EXPECT_THAT(MultiplyU128ToU256(1, kMax), Eq256(0, kMax));
50 for (int i = 0; i < 64; ++i) {
51 SCOPED_TRACE(i);
52 EXPECT_THAT(MultiplyU128ToU256(kMax, k1 << i),
53 Eq256(LeftShift({0, kMax}, i)));
54 EXPECT_THAT(MultiplyU128ToU256(k1 << i, kMax),
55 Eq256(LeftShift({0, kMax}, i)));
56 }
57
58 // 1-bit x 1-bit.
59 for (int i = 0; i < 64; ++i) {
60 for (int j = 0; j < 64; ++j) {
61 EXPECT_THAT(MultiplyU128ToU256(k1 << i, k1 << j),
62 Eq256(LeftShift({0, 1}, i + j)));
63 }
64 }
65
66 // Verified multiplies
67 EXPECT_THAT(MultiplyU128ToU256(
68 absl::MakeUint128(0xc502da0d6ea99fe8, 0xfa3c9141a1f50912),
69 absl::MakeUint128(0x96bcf1ac37f97bd6, 0x27e2cdeb5fb2299e)),
70 Eq256(absl::MakeUint128(0x740113d838f96a64, 0x22e8cfa4d71f89ea),
71 absl::MakeUint128(0x19184a345c62e993, 0x237871b630337b1c)));
72 EXPECT_THAT(MultiplyU128ToU256(
73 absl::MakeUint128(0x6f29e670cee07230, 0xc3d8e6c3e4d86759),
74 absl::MakeUint128(0x3227d29fa6386db1, 0x231682bb1e4b764f)),
75 Eq256(absl::MakeUint128(0x15c779d9d5d3b07c, 0xd7e6c827f0c81cbe),
76 absl::MakeUint128(0xf88e3914f7fa287a, 0x15b79975137dea77)));
77 EXPECT_THAT(MultiplyU128ToU256(
78 absl::MakeUint128(0xafb77107215646e1, 0x3b844cb1ac5769e7),
79 absl::MakeUint128(0x1ff7b2d888b62479, 0x92f758ae96fcba0b)),
80 Eq256(absl::MakeUint128(0x15f13b70181f6985, 0x2adb36bbabce7d02),
81 absl::MakeUint128(0x6c470d72e13aad04, 0x63fba3f5841762ed)));
82 EXPECT_THAT(MultiplyU128ToU256(
83 absl::MakeUint128(0xd85d5558d67ac905, 0xf88c70654dae19b1),
84 absl::MakeUint128(0x17252c6727db3738, 0x399ff658c511eedc)),
85 Eq256(absl::MakeUint128(0x138fcdaf8b0421ee, 0x1b465ddf2a0d03f6),
86 absl::MakeUint128(0x8f573ba68296860f, 0xf327d2738741a21c)));
87 EXPECT_THAT(MultiplyU128ToU256(
88 absl::MakeUint128(0x46f0421a37ff6bee, 0xa61df89f09d140b1),
89 absl::MakeUint128(0x3d712ec9f37ca2e1, 0x9658a2cba47ef4b1)),
90 Eq256(absl::MakeUint128(0x11069cc48ee7c95d, 0xd35fb1c7aa91c978),
91 absl::MakeUint128(0xbe2f4a6de874b015, 0xd2f7ac1b76746e61)));
92 EXPECT_THAT(MultiplyU128ToU256(
93 absl::MakeUint128(0x730d27c72d58fa49, 0x3ebeda7498f8827c),
94 absl::MakeUint128(0xa2c959eca9f503af, 0x189c687eb842bbd8)),
95 Eq256(absl::MakeUint128(0x4928d0ea356ba022, 0x1546d34a2963393),
96 absl::MakeUint128(0x7481531e1e0a16d1, 0xdd8025015cf6aca0)));
97 EXPECT_THAT(MultiplyU128ToU256(
98 absl::MakeUint128(0x6ca41020f856d2f1, 0xb9b0838c04a7f4aa),
99 absl::MakeUint128(0x9cf41d28a8396f54, 0x1d681695e377ffe6)),
100 Eq256(absl::MakeUint128(0x429b92934d9be6f1, 0xea182877157c1e7),
101 absl::MakeUint128(0x7135c23f0a4a475, 0xc1adc366f4a126bc)));
102 EXPECT_THAT(MultiplyU128ToU256(
103 absl::MakeUint128(0x57472833797c332, 0x6c79272fdec4687a),
104 absl::MakeUint128(0xb5f022ea3838e46b, 0x16face2f003e27a6)),
105 Eq256(absl::MakeUint128(0x3e072e0962b3400, 0x5d9fe8fdc3d0e1f4),
106 absl::MakeUint128(0x7dc0df47cedafd62, 0xbe6501f1acd2551c)));
107 EXPECT_THAT(MultiplyU128ToU256(
108 absl::MakeUint128(0xf0fb4198322eb1c2, 0xfe7f5f31f3885938),
109 absl::MakeUint128(0xd99012b71bb7aa31, 0xac7a6f9eb190789)),
110 Eq256(absl::MakeUint128(0xcccc998cf075ca01, 0x642d144322fb873a),
111 absl::MakeUint128(0xc79dc12b69d91ed4, 0xa83459132ce046f8)));
112 EXPECT_THAT(MultiplyU128ToU256(
113 absl::MakeUint128(0xb5c04120848cdb47, 0x8aa62a827bf52635),
114 absl::MakeUint128(0x8d07a359be2f1380, 0x467bb90d59da0dea)),
115 Eq256(absl::MakeUint128(0x64205019d139a9ce, 0x99425c5fb6e7a977),
116 absl::MakeUint128(0xd3e99628a9e5fca7, 0x9c7824cb7279d72)));
117 }
118
119 } // namespace
120