blob: e276cb51cf194a44f062e428231a5d686713d87f [file] [log] [blame]
Austin Schuhb4691e92020-12-31 12:37:18 -08001// 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 "gtest/gtest.h"
18#include "absl/numeric/int128.h"
19
20using absl::random_internal::MultiplyU64ToU128;
21
22namespace {
23
24TEST(WideMultiplyTest, MultiplyU64ToU128Test) {
25 constexpr uint64_t k1 = 1;
26 constexpr uint64_t kMax = ~static_cast<uint64_t>(0);
27
28 EXPECT_EQ(absl::uint128(0), MultiplyU64ToU128(0, 0));
29
30 // Max uint64_t
31 EXPECT_EQ(MultiplyU64ToU128(kMax, kMax),
32 absl::MakeUint128(0xfffffffffffffffe, 0x0000000000000001));
33 EXPECT_EQ(absl::MakeUint128(0, kMax), MultiplyU64ToU128(kMax, 1));
34 EXPECT_EQ(absl::MakeUint128(0, kMax), MultiplyU64ToU128(1, kMax));
35 for (int i = 0; i < 64; ++i) {
36 EXPECT_EQ(absl::MakeUint128(0, kMax) << i,
37 MultiplyU64ToU128(kMax, k1 << i));
38 EXPECT_EQ(absl::MakeUint128(0, kMax) << i,
39 MultiplyU64ToU128(k1 << i, kMax));
40 }
41
42 // 1-bit x 1-bit.
43 for (int i = 0; i < 64; ++i) {
44 for (int j = 0; j < 64; ++j) {
45 EXPECT_EQ(absl::MakeUint128(0, 1) << (i + j),
46 MultiplyU64ToU128(k1 << i, k1 << j));
47 EXPECT_EQ(absl::MakeUint128(0, 1) << (i + j),
48 MultiplyU64ToU128(k1 << i, k1 << j));
49 }
50 }
51
52 // Verified multiplies
53 EXPECT_EQ(MultiplyU64ToU128(0xffffeeeeddddcccc, 0xbbbbaaaa99998888),
54 absl::MakeUint128(0xbbbb9e2692c5dddc, 0xc28f7531048d2c60));
55 EXPECT_EQ(MultiplyU64ToU128(0x0123456789abcdef, 0xfedcba9876543210),
56 absl::MakeUint128(0x0121fa00ad77d742, 0x2236d88fe5618cf0));
57 EXPECT_EQ(MultiplyU64ToU128(0x0123456789abcdef, 0xfdb97531eca86420),
58 absl::MakeUint128(0x0120ae99d26725fc, 0xce197f0ecac319e0));
59 EXPECT_EQ(MultiplyU64ToU128(0x97a87f4f261ba3f2, 0xfedcba9876543210),
60 absl::MakeUint128(0x96fbf1a8ae78d0ba, 0x5a6dd4b71f278320));
61 EXPECT_EQ(MultiplyU64ToU128(0xfedcba9876543210, 0xfdb97531eca86420),
62 absl::MakeUint128(0xfc98c6981a413e22, 0x342d0bbf48948200));
63}
64
65} // namespace