about summary refs log tree commit diff
path: root/absl/random/internal/wide_multiply_test.cc
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2019-10-16T22·51-0700
committerAndy Soffer <asoffer@google.com>2019-10-17T18·43-0400
commita15364ce4d88534ae2295127e5d8e32aefb6b446 (patch)
treec01afa5f859f563b7bfd8cfdafa6b6fd1fed3aa6 /absl/random/internal/wide_multiply_test.cc
parentab3552a18964e7063c8324f45b3896a6a20b08a8 (diff)
Export of internal Abseil changes
--
d35c72d705155dcd89a92835103540f14c643d10 by Gennadiy Rozental <rogeeff@google.com>:

helpxml changed to report types of flags with built-in value type.

PiperOrigin-RevId: 275131440

--
f3478792943d7dd40a6ef6083a8e5d374f43a65e by Abseil Team <absl-team@google.com>:

Add space padding tests for hex conversions.

PiperOrigin-RevId: 275120155

--
7c2e4725403e173660f33f94af686a75d3722936 by Andy Soffer <asoffer@google.com>:

Fix https://github.com/abseil/abseil-cpp/issues/379
by renaming `roundup` to `round_up`

PiperOrigin-RevId: 275106110

--
84cb30d6ee509961ac4359cfdda1360973b9527d by Laramie Leavitt <lar@google.com>:

Move random_internal::wide_multiply into a file by the same name.

PiperOrigin-RevId: 275059359

--
06d691a8c187b5d899e7863784b23bdcfd580cb2 by Abseil Team <absl-team@google.com>:

Add missing "return" keyword.

PiperOrigin-RevId: 275036408
GitOrigin-RevId: d35c72d705155dcd89a92835103540f14c643d10
Change-Id: Id837b4de6c9cfe18f0a088363754bfe389df985b
Diffstat (limited to 'absl/random/internal/wide_multiply_test.cc')
-rw-r--r--absl/random/internal/wide_multiply_test.cc66
1 files changed, 66 insertions, 0 deletions
diff --git a/absl/random/internal/wide_multiply_test.cc b/absl/random/internal/wide_multiply_test.cc
new file mode 100644
index 000000000000..922603f22629
--- /dev/null
+++ b/absl/random/internal/wide_multiply_test.cc
@@ -0,0 +1,66 @@
+// Copyright 2017 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/random/internal/wide_multiply.h"
+
+#include "gtest/gtest.h"
+#include "absl/base/internal/bits.h"
+#include "absl/numeric/int128.h"
+
+using absl::random_internal::MultiplyU64ToU128;
+
+namespace {
+
+TEST(WideMultiplyTest, MultiplyU64ToU128Test) {
+  constexpr uint64_t k1 = 1;
+  constexpr uint64_t kMax = ~static_cast<uint64_t>(0);
+
+  EXPECT_EQ(absl::uint128(0), MultiplyU64ToU128(0, 0));
+
+  // Max uint64
+  EXPECT_EQ(MultiplyU64ToU128(kMax, kMax),
+            absl::MakeUint128(0xfffffffffffffffe, 0x0000000000000001));
+  EXPECT_EQ(absl::MakeUint128(0, kMax), MultiplyU64ToU128(kMax, 1));
+  EXPECT_EQ(absl::MakeUint128(0, kMax), MultiplyU64ToU128(1, kMax));
+  for (int i = 0; i < 64; ++i) {
+    EXPECT_EQ(absl::MakeUint128(0, kMax) << i,
+              MultiplyU64ToU128(kMax, k1 << i));
+    EXPECT_EQ(absl::MakeUint128(0, kMax) << i,
+              MultiplyU64ToU128(k1 << i, kMax));
+  }
+
+  // 1-bit x 1-bit.
+  for (int i = 0; i < 64; ++i) {
+    for (int j = 0; j < 64; ++j) {
+      EXPECT_EQ(absl::MakeUint128(0, 1) << (i + j),
+                MultiplyU64ToU128(k1 << i, k1 << j));
+      EXPECT_EQ(absl::MakeUint128(0, 1) << (i + j),
+                MultiplyU64ToU128(k1 << i, k1 << j));
+    }
+  }
+
+  // Verified multiplies
+  EXPECT_EQ(MultiplyU64ToU128(0xffffeeeeddddcccc, 0xbbbbaaaa99998888),
+            absl::MakeUint128(0xbbbb9e2692c5dddc, 0xc28f7531048d2c60));
+  EXPECT_EQ(MultiplyU64ToU128(0x0123456789abcdef, 0xfedcba9876543210),
+            absl::MakeUint128(0x0121fa00ad77d742, 0x2236d88fe5618cf0));
+  EXPECT_EQ(MultiplyU64ToU128(0x0123456789abcdef, 0xfdb97531eca86420),
+            absl::MakeUint128(0x0120ae99d26725fc, 0xce197f0ecac319e0));
+  EXPECT_EQ(MultiplyU64ToU128(0x97a87f4f261ba3f2, 0xfedcba9876543210),
+            absl::MakeUint128(0x96fbf1a8ae78d0ba, 0x5a6dd4b71f278320));
+  EXPECT_EQ(MultiplyU64ToU128(0xfedcba9876543210, 0xfdb97531eca86420),
+            absl::MakeUint128(0xfc98c6981a413e22, 0x342d0bbf48948200));
+}
+
+}  // namespace