diff options
author | lukebhan <luke.bhan@vanderbilt.edu> | 2021-08-06 18:06:11 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-08-12 21:45:10 +0000 |
commit | 691d4c3db4b13c60a1c4cd3e210de4c983f186b3 (patch) | |
tree | e49487cb7bf9ca4971baee58bdbeeaa611335a54 /src | |
parent | 4cdb2c6340c7c13be51ba12e0f3d5437e27c9bf0 (diff) | |
download | mongo-691d4c3db4b13c60a1c4cd3e210de4c983f186b3.tar.gz |
SERVER-59139 refactor to use abseil for int128
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/bson/util/simple8b_test.cpp | 10 | ||||
-rw-r--r-- | src/mongo/bson/util/simple8b_type_util.cpp | 2 | ||||
-rw-r--r-- | src/mongo/platform/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/platform/int128.h | 6 | ||||
-rw-r--r-- | src/mongo/platform/int128_test.cpp | 59 | ||||
-rw-r--r-- | src/third_party/SConscript | 2 | ||||
-rw-r--r-- | src/third_party/abseil-cpp-master/SConscript | 7 |
7 files changed, 78 insertions, 9 deletions
diff --git a/src/mongo/bson/util/simple8b_test.cpp b/src/mongo/bson/util/simple8b_test.cpp index 9c7be2eefba..bcf792be995 100644 --- a/src/mongo/bson/util/simple8b_test.cpp +++ b/src/mongo/bson/util/simple8b_test.cpp @@ -979,7 +979,7 @@ TEST(Simple8b, EightSelectorLargeBase) { // second value of 20 is the nibble shift of 4*20. The first value is 0111 because we store at // least 4 values. This should be encoded as [(0111) (10100)] x6 [1000] [1000] = // // 81E8F47A3D1E8F48 - uint128_t val("8462480737302404222943232"); + uint128_t val = absl::MakeUint128(0x70000, 0x0); std::vector<boost::optional<uint128_t>> expectedInts = {val, val, val, val, val, val}; std::vector<uint8_t> expectedBinary = {0x88, 0xF4, 0xE8, 0xD1, 0xA3, 0x47, 0x8F, 0x1E}; testSimple8b(expectedInts, expectedBinary); @@ -990,7 +990,7 @@ TEST(Simple8b, Selector8LargeBaseTestAndSkip) { // second value of 20 is the nibble shift of 4*20. The first value is 0111 because we store at // least 4 values. With skip this should be encoded as: // [(1111) (11111) (0111) (10100)] x3 [1000] = 83FEF4FFBD3FEF48 - uint128_t val("8462480737302404222943232"); + uint128_t val = absl::MakeUint128(0x70000, 0x0); std::vector<boost::optional<uint128_t>> expectedInts; for (uint32_t i = 0; i < 3; i++) { expectedInts.push_back(val); @@ -1005,7 +1005,7 @@ TEST(Simple8b, Selector8LargeSkipEncoding) { // that. // This should be encoded as // [(001111 11111) x 5] [1001] [1000] = 1FF3FE7FCFF9FF98 - uint128_t val("319014718988379809496913694467282698240"); + uint128_t val = absl::MakeUint128(0xF000000000000000, 0x0); std::vector<boost::optional<uint128_t>> expectedInts = {val, val, val, val, val}; std::vector<uint8_t> expectedBinary = {0x98, 0xFF, 0xF9, 0xCF, 0x7F, 0xFE, 0xF3, 0x1F}; testSimple8b(expectedInts, expectedBinary); @@ -1015,7 +1015,7 @@ TEST(Simple8b, Selector8LargeNibbleShift) { // 170141183460469231731687303715884105728= 1 + 127 zeros. This is a value that should have 3 // trailing zeros due to nibble. So we should encode as: // [(1000) (11111)] x6 [1000] [1000] = 23F1F8FC7E3F1F88 - uint128_t val("170141183460469231731687303715884105728"); + uint128_t val = absl::MakeUint128(0x8000000000000000, 0x0); std::vector<boost::optional<uint128_t>> expectedInts = {val, val, val, val, val, val}; std::vector<uint8_t> expectedBinary = {0x88, 0x1F, 0x3F, 0x7E, 0xFC, 0xF8, 0xF1, 0x23}; testSimple8b(expectedInts, expectedBinary); @@ -1074,7 +1074,7 @@ TEST(Simple8b, RleEightSelectorLarge) { // 8462480737302404222943232= 111 + 80 zeros // This should be encoded as // [(0111) (10000)] x6 [1000] [1000] = 1E8F47A3D1E8F488 + 0xF (rle) + repeat eight selector - uint128_t val("8462480737302404222943232"); + uint128_t val = absl::MakeUint128(0x70000, 0x0); std::vector<boost::optional<uint128_t>> expectedInts(132, val); std::vector<uint8_t> expectedBinary = {0x88, 0xF4, 0xE8, 0xD1, 0xA3, 0x47, 0x8F, 0x1E, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, diff --git a/src/mongo/bson/util/simple8b_type_util.cpp b/src/mongo/bson/util/simple8b_type_util.cpp index 8d408261acc..9b4f4f25186 100644 --- a/src/mongo/bson/util/simple8b_type_util.cpp +++ b/src/mongo/bson/util/simple8b_type_util.cpp @@ -37,7 +37,7 @@ namespace mongo { uint64_t Simple8bTypeUtil::encodeInt64(int64_t val) { - return (uint64_t(val) << 1) ^ (val >> 63); + return (static_cast<uint64_t>(val) << 1) ^ (val >> 63); } int64_t Simple8bTypeUtil::decodeInt64(uint64_t val) { diff --git a/src/mongo/platform/SConscript b/src/mongo/platform/SConscript index 68ab49dad4c..71a0a57772d 100644 --- a/src/mongo/platform/SConscript +++ b/src/mongo/platform/SConscript @@ -13,6 +13,7 @@ env.CppUnitTest( 'atomic_word_test.cpp', 'bits_test.cpp', 'endian_test.cpp', + 'int128_test.cpp', 'mutex_test.cpp', 'process_id_test.cpp', 'random_test.cpp', diff --git a/src/mongo/platform/int128.h b/src/mongo/platform/int128.h index 67c3b27232a..093c33cfe6d 100644 --- a/src/mongo/platform/int128.h +++ b/src/mongo/platform/int128.h @@ -29,7 +29,7 @@ #pragma once -#include <boost/multiprecision/cpp_int.hpp> +#include <absl/numeric/int128.h> -using uint128_t = boost::multiprecision::uint128_t; -using int128_t = boost::multiprecision::int128_t; +using uint128_t = absl::uint128; +using int128_t = absl::int128; diff --git a/src/mongo/platform/int128_test.cpp b/src/mongo/platform/int128_test.cpp new file mode 100644 index 00000000000..be36cd3c4ed --- /dev/null +++ b/src/mongo/platform/int128_test.cpp @@ -0,0 +1,59 @@ +/** + * Copyright (C) 2021-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/platform/int128.h" +#include "mongo/unittest/unittest.h" + +using namespace mongo; + +// Asserts that we can cast to uint128_t and cast back as well that the expected high and low bits +// after cast are following 2's complement. +void assertCastIsValid(int128_t val, uint64_t expectedHi, uint64_t expectedLo) { + uint128_t castedInt = static_cast<uint128_t>(val); + ASSERT_EQUALS(absl::Uint128High64(castedInt), expectedHi); + ASSERT_EQUALS(absl::Uint128Low64(castedInt), expectedLo); + int128_t backToSigned = static_cast<int128_t>(castedInt); + ASSERT_EQUALS(val, backToSigned); +} + +TEST(Int128, TestCastingPositive) { + assertCastIsValid(12345, 0, 12345); +} + +TEST(Int128, TestCastingNegative) { + assertCastIsValid(-12345, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFCFC7); +} + +TEST(Int128, MaxPositiveInt) { + assertCastIsValid(std::numeric_limits<int128_t>::max(), 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF); +} + +TEST(Int128, MaxNegativeInt) { + assertCastIsValid(std::numeric_limits<int128_t>::min(), 0x8000000000000000, 0); +} diff --git a/src/third_party/SConscript b/src/third_party/SConscript index c5b21681406..62d47b749e8 100644 --- a/src/third_party/SConscript +++ b/src/third_party/SConscript @@ -372,6 +372,7 @@ if use_system_version_of_library("abseil-cpp"): SYSLIBDEPS=[ env['LIBDEPS_ABSL_CONTAINER_SYSLIBDEP'], env['LIBDEPS_ABSL_HASH_SYSLIBDEP'], + env['LIBDEPS_ABSL_NUMERIC_SYSLIBDEP'], ]) else: abseilDirectory = 'abseil-cpp-master' @@ -382,6 +383,7 @@ else: LIBDEPS_INTERFACE=[ abseilDirectory + '/absl_container', abseilDirectory + '/absl_hash', + abseilDirectory + '/absl_numeric', ]) abseilEnv.ShimLibrary(name="abseil") diff --git a/src/third_party/abseil-cpp-master/SConscript b/src/third_party/abseil-cpp-master/SConscript index ad6caeb01d1..3e960562068 100644 --- a/src/third_party/abseil-cpp-master/SConscript +++ b/src/third_party/abseil-cpp-master/SConscript @@ -5,6 +5,13 @@ env = env.Clone() env.InjectThirdParty(libraries=['abseil-cpp']) env.Library( + target="absl_numeric", + source = [ + "abseil-cpp/absl/numeric/int128.cc", + ] +) + +env.Library( target="absl_hash", source=[ "abseil-cpp/absl/hash/internal/city.cc", |