Add vendored dependencies & cleanup script

This commit is contained in:
2022-02-11 14:01:25 -05:00
parent ea5ad06289
commit f55d064a0d
4315 changed files with 1296565 additions and 18 deletions

View File

@@ -0,0 +1,115 @@
# Copyright 2018 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.
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load(
"//absl:copts/configure_copts.bzl",
"ABSL_DEFAULT_COPTS",
"ABSL_DEFAULT_LINKOPTS",
"ABSL_TEST_COPTS",
)
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
cc_library(
name = "bits",
hdrs = [
"bits.h",
"internal/bits.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
"//absl/base:config",
"//absl/base:core_headers",
],
)
cc_test(
name = "bits_test",
size = "small",
srcs = [
"bits_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":bits",
"//absl/random",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "int128",
srcs = [
"int128.cc",
"int128_have_intrinsic.inc",
"int128_no_intrinsic.inc",
],
hdrs = ["int128.h"],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":bits",
"//absl/base:config",
"//absl/base:core_headers",
],
)
cc_test(
name = "int128_test",
size = "small",
srcs = [
"int128_stream_test.cc",
"int128_test.cc",
],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":int128",
"//absl/base",
"//absl/base:core_headers",
"//absl/hash:hash_testing",
"//absl/meta:type_traits",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "int128_benchmark",
srcs = ["int128_benchmark.cc"],
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
tags = ["benchmark"],
deps = [
":int128",
"//absl/base:config",
"@com_github_google_benchmark//:benchmark_main",
],
)
cc_library(
name = "representation",
hdrs = [
"internal/representation.h",
],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
"//absl/base:config",
],
)

View File

@@ -0,0 +1,100 @@
#
# 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.
#
absl_cc_library(
NAME
bits
HDRS
"bits.h"
"internal/bits.h"
COPTS
${ABSL_DEFAULT_COPTS}
DEPS
absl::core_headers
PUBLIC
)
absl_cc_test(
NAME
bits_test
SRCS
"bits_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::bits
absl::core_headers
absl::random_random
GTest::gmock_main
)
absl_cc_library(
NAME
int128
HDRS
"int128.h"
SRCS
"int128.cc"
"int128_have_intrinsic.inc"
"int128_no_intrinsic.inc"
COPTS
${ABSL_DEFAULT_COPTS}
DEPS
absl::config
absl::core_headers
absl::bits
PUBLIC
)
absl_cc_test(
NAME
int128_test
SRCS
"int128_stream_test.cc"
"int128_test.cc"
COPTS
${ABSL_TEST_COPTS}
DEPS
absl::int128
absl::base
absl::core_headers
absl::hash_testing
absl::type_traits
GTest::gmock_main
)
# component target
absl_cc_library(
NAME
numeric
COPTS
${ABSL_DEFAULT_COPTS}
DEPS
absl::int128
PUBLIC
)
absl_cc_library(
NAME
numeric_representation
HDRS
"internal/representation.h"
COPTS
${ABSL_DEFAULT_COPTS}
DEPS
absl::config
PUBLIC
)

View File

@@ -0,0 +1,177 @@
// Copyright 2020 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.
//
// -----------------------------------------------------------------------------
// File: bits.h
// -----------------------------------------------------------------------------
//
// This file contains implementations of C++20's bitwise math functions, as
// defined by:
//
// P0553R4:
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0553r4.html
// P0556R3:
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0556r3.html
// P1355R2:
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1355r2.html
// P1956R1:
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1956r1.pdf
//
// When using a standard library that implements these functions, we use the
// standard library's implementation.
#ifndef ABSL_NUMERIC_BITS_H_
#define ABSL_NUMERIC_BITS_H_
#include <cstdint>
#include <limits>
#include <type_traits>
#if (defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L) || \
(defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
#include <bit>
#endif
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/numeric/internal/bits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
#if !(defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L)
// rotating
template <class T>
ABSL_MUST_USE_RESULT constexpr
typename std::enable_if<std::is_unsigned<T>::value, T>::type
rotl(T x, int s) noexcept {
return numeric_internal::RotateLeft(x, s);
}
template <class T>
ABSL_MUST_USE_RESULT constexpr
typename std::enable_if<std::is_unsigned<T>::value, T>::type
rotr(T x, int s) noexcept {
return numeric_internal::RotateRight(x, s);
}
// Counting functions
//
// While these functions are typically constexpr, on some platforms, they may
// not be marked as constexpr due to constraints of the compiler/available
// intrinsics.
template <class T>
ABSL_INTERNAL_CONSTEXPR_CLZ inline
typename std::enable_if<std::is_unsigned<T>::value, int>::type
countl_zero(T x) noexcept {
return numeric_internal::CountLeadingZeroes(x);
}
template <class T>
ABSL_INTERNAL_CONSTEXPR_CLZ inline
typename std::enable_if<std::is_unsigned<T>::value, int>::type
countl_one(T x) noexcept {
// Avoid integer promotion to a wider type
return countl_zero(static_cast<T>(~x));
}
template <class T>
ABSL_INTERNAL_CONSTEXPR_CTZ inline
typename std::enable_if<std::is_unsigned<T>::value, int>::type
countr_zero(T x) noexcept {
return numeric_internal::CountTrailingZeroes(x);
}
template <class T>
ABSL_INTERNAL_CONSTEXPR_CTZ inline
typename std::enable_if<std::is_unsigned<T>::value, int>::type
countr_one(T x) noexcept {
// Avoid integer promotion to a wider type
return countr_zero(static_cast<T>(~x));
}
template <class T>
ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline
typename std::enable_if<std::is_unsigned<T>::value, int>::type
popcount(T x) noexcept {
return numeric_internal::Popcount(x);
}
#else // defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L
using std::countl_one;
using std::countl_zero;
using std::countr_one;
using std::countr_zero;
using std::popcount;
using std::rotl;
using std::rotr;
#endif
#if !(defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L)
// Returns: true if x is an integral power of two; false otherwise.
template <class T>
constexpr inline typename std::enable_if<std::is_unsigned<T>::value, bool>::type
has_single_bit(T x) noexcept {
return x != 0 && (x & (x - 1)) == 0;
}
// Returns: If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any
// fractional part discarded.
template <class T>
ABSL_INTERNAL_CONSTEXPR_CLZ inline
typename std::enable_if<std::is_unsigned<T>::value, T>::type
bit_width(T x) noexcept {
return std::numeric_limits<T>::digits - countl_zero(x);
}
// Returns: If x == 0, 0; otherwise the maximal value y such that
// has_single_bit(y) is true and y <= x.
template <class T>
ABSL_INTERNAL_CONSTEXPR_CLZ inline
typename std::enable_if<std::is_unsigned<T>::value, T>::type
bit_floor(T x) noexcept {
return x == 0 ? 0 : T{1} << (bit_width(x) - 1);
}
// Returns: N, where N is the smallest power of 2 greater than or equal to x.
//
// Preconditions: N is representable as a value of type T.
template <class T>
ABSL_INTERNAL_CONSTEXPR_CLZ inline
typename std::enable_if<std::is_unsigned<T>::value, T>::type
bit_ceil(T x) {
// If T is narrower than unsigned, T{1} << bit_width will be promoted. We
// want to force it to wraparound so that bit_ceil of an invalid value are not
// core constant expressions.
//
// BitCeilNonPowerOf2 triggers an overflow in constexpr contexts if we would
// undergo promotion to unsigned but not fit the result into T without
// truncation.
return has_single_bit(x) ? T{1} << (bit_width(x) - 1)
: numeric_internal::BitCeilNonPowerOf2(x);
}
#else // defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
using std::bit_ceil;
using std::bit_floor;
using std::bit_width;
using std::has_single_bit;
#endif
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_NUMERIC_BITS_H_

View File

@@ -0,0 +1,573 @@
// Copyright 2020 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/numeric/bits.h"
#include <limits>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/random/random.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace {
TEST(Rotate, Left) {
static_assert(rotl(uint8_t{0x12}, 0) == uint8_t{0x12}, "");
static_assert(rotl(uint16_t{0x1234}, 0) == uint16_t{0x1234}, "");
static_assert(rotl(uint32_t{0x12345678UL}, 0) == uint32_t{0x12345678UL}, "");
static_assert(rotl(uint64_t{0x12345678ABCDEF01ULL}, 0) ==
uint64_t{0x12345678ABCDEF01ULL},
"");
EXPECT_EQ(rotl(uint8_t{0x12}, 0), uint8_t{0x12});
EXPECT_EQ(rotl(uint16_t{0x1234}, 0), uint16_t{0x1234});
EXPECT_EQ(rotl(uint32_t{0x12345678UL}, 0), uint32_t{0x12345678UL});
EXPECT_EQ(rotl(uint64_t{0x12345678ABCDEF01ULL}, 0),
uint64_t{0x12345678ABCDEF01ULL});
EXPECT_EQ(rotl(uint8_t{0x12}, 8), uint8_t{0x12});
EXPECT_EQ(rotl(uint16_t{0x1234}, 16), uint16_t{0x1234});
EXPECT_EQ(rotl(uint32_t{0x12345678UL}, 32), uint32_t{0x12345678UL});
EXPECT_EQ(rotl(uint64_t{0x12345678ABCDEF01ULL}, 64),
uint64_t{0x12345678ABCDEF01ULL});
EXPECT_EQ(rotl(uint8_t{0x12}, -8), uint8_t{0x12});
EXPECT_EQ(rotl(uint16_t{0x1234}, -16), uint16_t{0x1234});
EXPECT_EQ(rotl(uint32_t{0x12345678UL}, -32), uint32_t{0x12345678UL});
EXPECT_EQ(rotl(uint64_t{0x12345678ABCDEF01ULL}, -64),
uint64_t{0x12345678ABCDEF01ULL});
EXPECT_EQ(rotl(uint8_t{0x12}, 4), uint8_t{0x21});
EXPECT_EQ(rotl(uint16_t{0x1234}, 4), uint16_t{0x2341});
EXPECT_EQ(rotl(uint32_t{0x12345678UL}, 4), uint32_t{0x23456781UL});
EXPECT_EQ(rotl(uint64_t{0x12345678ABCDEF01ULL}, 4),
uint64_t{0x2345678ABCDEF011ULL});
EXPECT_EQ(rotl(uint8_t{0x12}, -4), uint8_t{0x21});
EXPECT_EQ(rotl(uint16_t{0x1234}, -4), uint16_t{0x4123});
EXPECT_EQ(rotl(uint32_t{0x12345678UL}, -4), uint32_t{0x81234567UL});
EXPECT_EQ(rotl(uint64_t{0x12345678ABCDEF01ULL}, -4),
uint64_t{0x112345678ABCDEF0ULL});
}
TEST(Rotate, Right) {
static_assert(rotr(uint8_t{0x12}, 0) == uint8_t{0x12}, "");
static_assert(rotr(uint16_t{0x1234}, 0) == uint16_t{0x1234}, "");
static_assert(rotr(uint32_t{0x12345678UL}, 0) == uint32_t{0x12345678UL}, "");
static_assert(rotr(uint64_t{0x12345678ABCDEF01ULL}, 0) ==
uint64_t{0x12345678ABCDEF01ULL},
"");
EXPECT_EQ(rotr(uint8_t{0x12}, 0), uint8_t{0x12});
EXPECT_EQ(rotr(uint16_t{0x1234}, 0), uint16_t{0x1234});
EXPECT_EQ(rotr(uint32_t{0x12345678UL}, 0), uint32_t{0x12345678UL});
EXPECT_EQ(rotr(uint64_t{0x12345678ABCDEF01ULL}, 0),
uint64_t{0x12345678ABCDEF01ULL});
EXPECT_EQ(rotr(uint8_t{0x12}, 8), uint8_t{0x12});
EXPECT_EQ(rotr(uint16_t{0x1234}, 16), uint16_t{0x1234});
EXPECT_EQ(rotr(uint32_t{0x12345678UL}, 32), uint32_t{0x12345678UL});
EXPECT_EQ(rotr(uint64_t{0x12345678ABCDEF01ULL}, 64),
uint64_t{0x12345678ABCDEF01ULL});
EXPECT_EQ(rotr(uint8_t{0x12}, -8), uint8_t{0x12});
EXPECT_EQ(rotr(uint16_t{0x1234}, -16), uint16_t{0x1234});
EXPECT_EQ(rotr(uint32_t{0x12345678UL}, -32), uint32_t{0x12345678UL});
EXPECT_EQ(rotr(uint64_t{0x12345678ABCDEF01ULL}, -64),
uint64_t{0x12345678ABCDEF01ULL});
EXPECT_EQ(rotr(uint8_t{0x12}, 4), uint8_t{0x21});
EXPECT_EQ(rotr(uint16_t{0x1234}, 4), uint16_t{0x4123});
EXPECT_EQ(rotr(uint32_t{0x12345678UL}, 4), uint32_t{0x81234567UL});
EXPECT_EQ(rotr(uint64_t{0x12345678ABCDEF01ULL}, 4),
uint64_t{0x112345678ABCDEF0ULL});
EXPECT_EQ(rotr(uint8_t{0x12}, -4), uint8_t{0x21});
EXPECT_EQ(rotr(uint16_t{0x1234}, -4), uint16_t{0x2341});
EXPECT_EQ(rotr(uint32_t{0x12345678UL}, -4), uint32_t{0x23456781UL});
EXPECT_EQ(rotr(uint64_t{0x12345678ABCDEF01ULL}, -4),
uint64_t{0x2345678ABCDEF011ULL});
}
TEST(Rotate, Symmetry) {
// rotr(x, s) is equivalent to rotl(x, -s)
absl::BitGen rng;
constexpr int kTrials = 100;
for (int i = 0; i < kTrials; ++i) {
uint8_t value = absl::Uniform(rng, std::numeric_limits<uint8_t>::min(),
std::numeric_limits<uint8_t>::max());
int shift = absl::Uniform(rng, -2 * std::numeric_limits<uint8_t>::digits,
2 * std::numeric_limits<uint8_t>::digits);
EXPECT_EQ(rotl(value, shift), rotr(value, -shift));
}
for (int i = 0; i < kTrials; ++i) {
uint16_t value = absl::Uniform(rng, std::numeric_limits<uint16_t>::min(),
std::numeric_limits<uint16_t>::max());
int shift = absl::Uniform(rng, -2 * std::numeric_limits<uint16_t>::digits,
2 * std::numeric_limits<uint16_t>::digits);
EXPECT_EQ(rotl(value, shift), rotr(value, -shift));
}
for (int i = 0; i < kTrials; ++i) {
uint32_t value = absl::Uniform(rng, std::numeric_limits<uint32_t>::min(),
std::numeric_limits<uint32_t>::max());
int shift = absl::Uniform(rng, -2 * std::numeric_limits<uint32_t>::digits,
2 * std::numeric_limits<uint32_t>::digits);
EXPECT_EQ(rotl(value, shift), rotr(value, -shift));
}
for (int i = 0; i < kTrials; ++i) {
uint64_t value = absl::Uniform(rng, std::numeric_limits<uint64_t>::min(),
std::numeric_limits<uint64_t>::max());
int shift = absl::Uniform(rng, -2 * std::numeric_limits<uint64_t>::digits,
2 * std::numeric_limits<uint64_t>::digits);
EXPECT_EQ(rotl(value, shift), rotr(value, -shift));
}
}
TEST(Counting, LeadingZeroes) {
#if ABSL_INTERNAL_HAS_CONSTEXPR_CLZ
static_assert(countl_zero(uint8_t{}) == 8, "");
static_assert(countl_zero(static_cast<uint8_t>(-1)) == 0, "");
static_assert(countl_zero(uint16_t{}) == 16, "");
static_assert(countl_zero(static_cast<uint16_t>(-1)) == 0, "");
static_assert(countl_zero(uint32_t{}) == 32, "");
static_assert(countl_zero(~uint32_t{}) == 0, "");
static_assert(countl_zero(uint64_t{}) == 64, "");
static_assert(countl_zero(~uint64_t{}) == 0, "");
#endif
EXPECT_EQ(countl_zero(uint8_t{}), 8);
EXPECT_EQ(countl_zero(static_cast<uint8_t>(-1)), 0);
EXPECT_EQ(countl_zero(uint16_t{}), 16);
EXPECT_EQ(countl_zero(static_cast<uint16_t>(-1)), 0);
EXPECT_EQ(countl_zero(uint32_t{}), 32);
EXPECT_EQ(countl_zero(~uint32_t{}), 0);
EXPECT_EQ(countl_zero(uint64_t{}), 64);
EXPECT_EQ(countl_zero(~uint64_t{}), 0);
for (int i = 0; i < 8; i++) {
EXPECT_EQ(countl_zero(static_cast<uint8_t>(1u << i)), 7 - i);
}
for (int i = 0; i < 16; i++) {
EXPECT_EQ(countl_zero(static_cast<uint16_t>(1u << i)), 15 - i);
}
for (int i = 0; i < 32; i++) {
EXPECT_EQ(countl_zero(uint32_t{1} << i), 31 - i);
}
for (int i = 0; i < 64; i++) {
EXPECT_EQ(countl_zero(uint64_t{1} << i), 63 - i);
}
}
TEST(Counting, LeadingOnes) {
#if ABSL_INTERNAL_HAS_CONSTEXPR_CLZ
static_assert(countl_one(uint8_t{}) == 0, "");
static_assert(countl_one(static_cast<uint8_t>(-1)) == 8, "");
static_assert(countl_one(uint16_t{}) == 0, "");
static_assert(countl_one(static_cast<uint16_t>(-1)) == 16, "");
static_assert(countl_one(uint32_t{}) == 0, "");
static_assert(countl_one(~uint32_t{}) == 32, "");
static_assert(countl_one(uint64_t{}) == 0, "");
static_assert(countl_one(~uint64_t{}) == 64, "");
#endif
EXPECT_EQ(countl_one(uint8_t{}), 0);
EXPECT_EQ(countl_one(static_cast<uint8_t>(-1)), 8);
EXPECT_EQ(countl_one(uint16_t{}), 0);
EXPECT_EQ(countl_one(static_cast<uint16_t>(-1)), 16);
EXPECT_EQ(countl_one(uint32_t{}), 0);
EXPECT_EQ(countl_one(~uint32_t{}), 32);
EXPECT_EQ(countl_one(uint64_t{}), 0);
EXPECT_EQ(countl_one(~uint64_t{}), 64);
}
TEST(Counting, TrailingZeroes) {
#if ABSL_INTERNAL_HAS_CONSTEXPR_CTZ
static_assert(countr_zero(uint8_t{}) == 8, "");
static_assert(countr_zero(static_cast<uint8_t>(-1)) == 0, "");
static_assert(countr_zero(uint16_t{}) == 16, "");
static_assert(countr_zero(static_cast<uint16_t>(-1)) == 0, "");
static_assert(countr_zero(uint32_t{}) == 32, "");
static_assert(countr_zero(~uint32_t{}) == 0, "");
static_assert(countr_zero(uint64_t{}) == 64, "");
static_assert(countr_zero(~uint64_t{}) == 0, "");
#endif
EXPECT_EQ(countr_zero(uint8_t{}), 8);
EXPECT_EQ(countr_zero(static_cast<uint8_t>(-1)), 0);
EXPECT_EQ(countr_zero(uint16_t{}), 16);
EXPECT_EQ(countr_zero(static_cast<uint16_t>(-1)), 0);
EXPECT_EQ(countr_zero(uint32_t{}), 32);
EXPECT_EQ(countr_zero(~uint32_t{}), 0);
EXPECT_EQ(countr_zero(uint64_t{}), 64);
EXPECT_EQ(countr_zero(~uint64_t{}), 0);
}
TEST(Counting, TrailingOnes) {
#if ABSL_INTERNAL_HAS_CONSTEXPR_CTZ
static_assert(countr_one(uint8_t{}) == 0, "");
static_assert(countr_one(static_cast<uint8_t>(-1)) == 8, "");
static_assert(countr_one(uint16_t{}) == 0, "");
static_assert(countr_one(static_cast<uint16_t>(-1)) == 16, "");
static_assert(countr_one(uint32_t{}) == 0, "");
static_assert(countr_one(~uint32_t{}) == 32, "");
static_assert(countr_one(uint64_t{}) == 0, "");
static_assert(countr_one(~uint64_t{}) == 64, "");
#endif
EXPECT_EQ(countr_one(uint8_t{}), 0);
EXPECT_EQ(countr_one(static_cast<uint8_t>(-1)), 8);
EXPECT_EQ(countr_one(uint16_t{}), 0);
EXPECT_EQ(countr_one(static_cast<uint16_t>(-1)), 16);
EXPECT_EQ(countr_one(uint32_t{}), 0);
EXPECT_EQ(countr_one(~uint32_t{}), 32);
EXPECT_EQ(countr_one(uint64_t{}), 0);
EXPECT_EQ(countr_one(~uint64_t{}), 64);
}
TEST(Counting, Popcount) {
#if ABSL_INTERNAL_HAS_CONSTEXPR_POPCOUNT
static_assert(popcount(uint8_t{}) == 0, "");
static_assert(popcount(uint8_t{1}) == 1, "");
static_assert(popcount(static_cast<uint8_t>(-1)) == 8, "");
static_assert(popcount(uint16_t{}) == 0, "");
static_assert(popcount(uint16_t{1}) == 1, "");
static_assert(popcount(static_cast<uint16_t>(-1)) == 16, "");
static_assert(popcount(uint32_t{}) == 0, "");
static_assert(popcount(uint32_t{1}) == 1, "");
static_assert(popcount(~uint32_t{}) == 32, "");
static_assert(popcount(uint64_t{}) == 0, "");
static_assert(popcount(uint64_t{1}) == 1, "");
static_assert(popcount(~uint64_t{}) == 64, "");
#endif // ABSL_INTERNAL_HAS_CONSTEXPR_POPCOUNT
EXPECT_EQ(popcount(uint8_t{}), 0);
EXPECT_EQ(popcount(uint8_t{1}), 1);
EXPECT_EQ(popcount(static_cast<uint8_t>(-1)), 8);
EXPECT_EQ(popcount(uint16_t{}), 0);
EXPECT_EQ(popcount(uint16_t{1}), 1);
EXPECT_EQ(popcount(static_cast<uint16_t>(-1)), 16);
EXPECT_EQ(popcount(uint32_t{}), 0);
EXPECT_EQ(popcount(uint32_t{1}), 1);
EXPECT_EQ(popcount(~uint32_t{}), 32);
EXPECT_EQ(popcount(uint64_t{}), 0);
EXPECT_EQ(popcount(uint64_t{1}), 1);
EXPECT_EQ(popcount(~uint64_t{}), 64);
for (int i = 0; i < 8; i++) {
EXPECT_EQ(popcount(static_cast<uint8_t>(uint8_t{1} << i)), 1);
EXPECT_EQ(popcount(static_cast<uint8_t>(static_cast<uint8_t>(-1) ^
(uint8_t{1} << i))),
7);
}
for (int i = 0; i < 16; i++) {
EXPECT_EQ(popcount(static_cast<uint16_t>(uint16_t{1} << i)), 1);
EXPECT_EQ(popcount(static_cast<uint16_t>(static_cast<uint16_t>(-1) ^
(uint16_t{1} << i))),
15);
}
for (int i = 0; i < 32; i++) {
EXPECT_EQ(popcount(uint32_t{1} << i), 1);
EXPECT_EQ(popcount(static_cast<uint32_t>(-1) ^ (uint32_t{1} << i)), 31);
}
for (int i = 0; i < 64; i++) {
EXPECT_EQ(popcount(uint64_t{1} << i), 1);
EXPECT_EQ(popcount(static_cast<uint64_t>(-1) ^ (uint64_t{1} << i)), 63);
}
}
template <typename T>
struct PopcountInput {
T value = 0;
int expected = 0;
};
template <typename T>
PopcountInput<T> GeneratePopcountInput(absl::BitGen& gen) {
PopcountInput<T> ret;
for (int i = 0; i < std::numeric_limits<T>::digits; i++) {
bool coin = absl::Bernoulli(gen, 0.2);
if (coin) {
ret.value |= T{1} << i;
ret.expected++;
}
}
return ret;
}
TEST(Counting, PopcountFuzz) {
absl::BitGen rng;
constexpr int kTrials = 100;
for (int i = 0; i < kTrials; ++i) {
auto input = GeneratePopcountInput<uint8_t>(rng);
EXPECT_EQ(popcount(input.value), input.expected);
}
for (int i = 0; i < kTrials; ++i) {
auto input = GeneratePopcountInput<uint16_t>(rng);
EXPECT_EQ(popcount(input.value), input.expected);
}
for (int i = 0; i < kTrials; ++i) {
auto input = GeneratePopcountInput<uint32_t>(rng);
EXPECT_EQ(popcount(input.value), input.expected);
}
for (int i = 0; i < kTrials; ++i) {
auto input = GeneratePopcountInput<uint64_t>(rng);
EXPECT_EQ(popcount(input.value), input.expected);
}
}
TEST(IntegralPowersOfTwo, SingleBit) {
EXPECT_FALSE(has_single_bit(uint8_t{}));
EXPECT_FALSE(has_single_bit(static_cast<uint8_t>(-1)));
EXPECT_FALSE(has_single_bit(uint16_t{}));
EXPECT_FALSE(has_single_bit(static_cast<uint16_t>(-1)));
EXPECT_FALSE(has_single_bit(uint32_t{}));
EXPECT_FALSE(has_single_bit(~uint32_t{}));
EXPECT_FALSE(has_single_bit(uint64_t{}));
EXPECT_FALSE(has_single_bit(~uint64_t{}));
static_assert(!has_single_bit(0u), "");
static_assert(has_single_bit(1u), "");
static_assert(has_single_bit(2u), "");
static_assert(!has_single_bit(3u), "");
static_assert(has_single_bit(4u), "");
static_assert(!has_single_bit(1337u), "");
static_assert(has_single_bit(65536u), "");
static_assert(has_single_bit(uint32_t{1} << 30), "");
static_assert(has_single_bit(uint64_t{1} << 42), "");
EXPECT_FALSE(has_single_bit(0u));
EXPECT_TRUE(has_single_bit(1u));
EXPECT_TRUE(has_single_bit(2u));
EXPECT_FALSE(has_single_bit(3u));
EXPECT_TRUE(has_single_bit(4u));
EXPECT_FALSE(has_single_bit(1337u));
EXPECT_TRUE(has_single_bit(65536u));
EXPECT_TRUE(has_single_bit(uint32_t{1} << 30));
EXPECT_TRUE(has_single_bit(uint64_t{1} << 42));
EXPECT_TRUE(has_single_bit(
static_cast<uint8_t>(std::numeric_limits<uint8_t>::max() / 2 + 1)));
EXPECT_TRUE(has_single_bit(
static_cast<uint16_t>(std::numeric_limits<uint16_t>::max() / 2 + 1)));
EXPECT_TRUE(has_single_bit(
static_cast<uint32_t>(std::numeric_limits<uint32_t>::max() / 2 + 1)));
EXPECT_TRUE(has_single_bit(
static_cast<uint64_t>(std::numeric_limits<uint64_t>::max() / 2 + 1)));
}
template <typename T, T arg, T = bit_ceil(arg)>
bool IsBitCeilConstantExpression(int) {
return true;
}
template <typename T, T arg>
bool IsBitCeilConstantExpression(char) {
return false;
}
TEST(IntegralPowersOfTwo, Ceiling) {
#if ABSL_INTERNAL_HAS_CONSTEXPR_CLZ
static_assert(bit_ceil(0u) == 1, "");
static_assert(bit_ceil(1u) == 1, "");
static_assert(bit_ceil(2u) == 2, "");
static_assert(bit_ceil(3u) == 4, "");
static_assert(bit_ceil(4u) == 4, "");
static_assert(bit_ceil(1337u) == 2048, "");
static_assert(bit_ceil(65536u) == 65536, "");
static_assert(bit_ceil(65536u - 1337u) == 65536, "");
static_assert(bit_ceil(uint32_t{0x80000000}) == uint32_t{0x80000000}, "");
static_assert(bit_ceil(uint64_t{0x40000000000}) == uint64_t{0x40000000000},
"");
static_assert(
bit_ceil(uint64_t{0x8000000000000000}) == uint64_t{0x8000000000000000},
"");
EXPECT_TRUE((IsBitCeilConstantExpression<uint8_t, uint8_t{0x0}>(0)));
EXPECT_TRUE((IsBitCeilConstantExpression<uint8_t, uint8_t{0x80}>(0)));
EXPECT_FALSE((IsBitCeilConstantExpression<uint8_t, uint8_t{0x81}>(0)));
EXPECT_FALSE((IsBitCeilConstantExpression<uint8_t, uint8_t{0xff}>(0)));
EXPECT_TRUE((IsBitCeilConstantExpression<uint16_t, uint16_t{0x0}>(0)));
EXPECT_TRUE((IsBitCeilConstantExpression<uint16_t, uint16_t{0x8000}>(0)));
EXPECT_FALSE((IsBitCeilConstantExpression<uint16_t, uint16_t{0x8001}>(0)));
EXPECT_FALSE((IsBitCeilConstantExpression<uint16_t, uint16_t{0xffff}>(0)));
EXPECT_TRUE((IsBitCeilConstantExpression<uint32_t, uint32_t{0x0}>(0)));
EXPECT_TRUE((IsBitCeilConstantExpression<uint32_t, uint32_t{0x80000000}>(0)));
EXPECT_FALSE(
(IsBitCeilConstantExpression<uint32_t, uint32_t{0x80000001}>(0)));
EXPECT_FALSE(
(IsBitCeilConstantExpression<uint32_t, uint32_t{0xffffffff}>(0)));
EXPECT_TRUE((IsBitCeilConstantExpression<uint64_t, uint64_t{0x0}>(0)));
EXPECT_TRUE(
(IsBitCeilConstantExpression<uint64_t, uint64_t{0x8000000000000000}>(0)));
EXPECT_FALSE(
(IsBitCeilConstantExpression<uint64_t, uint64_t{0x8000000000000001}>(0)));
EXPECT_FALSE(
(IsBitCeilConstantExpression<uint64_t, uint64_t{0xffffffffffffffff}>(0)));
#endif
EXPECT_EQ(bit_ceil(0u), 1);
EXPECT_EQ(bit_ceil(1u), 1);
EXPECT_EQ(bit_ceil(2u), 2);
EXPECT_EQ(bit_ceil(3u), 4);
EXPECT_EQ(bit_ceil(4u), 4);
EXPECT_EQ(bit_ceil(1337u), 2048);
EXPECT_EQ(bit_ceil(65536u), 65536);
EXPECT_EQ(bit_ceil(65536u - 1337u), 65536);
EXPECT_EQ(bit_ceil(uint64_t{0x40000000000}), uint64_t{0x40000000000});
}
TEST(IntegralPowersOfTwo, Floor) {
#if ABSL_INTERNAL_HAS_CONSTEXPR_CLZ
static_assert(bit_floor(0u) == 0, "");
static_assert(bit_floor(1u) == 1, "");
static_assert(bit_floor(2u) == 2, "");
static_assert(bit_floor(3u) == 2, "");
static_assert(bit_floor(4u) == 4, "");
static_assert(bit_floor(1337u) == 1024, "");
static_assert(bit_floor(65536u) == 65536, "");
static_assert(bit_floor(65536u - 1337u) == 32768, "");
static_assert(bit_floor(uint64_t{0x40000000000}) == uint64_t{0x40000000000},
"");
#endif
EXPECT_EQ(bit_floor(0u), 0);
EXPECT_EQ(bit_floor(1u), 1);
EXPECT_EQ(bit_floor(2u), 2);
EXPECT_EQ(bit_floor(3u), 2);
EXPECT_EQ(bit_floor(4u), 4);
EXPECT_EQ(bit_floor(1337u), 1024);
EXPECT_EQ(bit_floor(65536u), 65536);
EXPECT_EQ(bit_floor(65536u - 1337u), 32768);
EXPECT_EQ(bit_floor(uint64_t{0x40000000000}), uint64_t{0x40000000000});
for (int i = 0; i < 8; i++) {
uint8_t input = uint8_t{1} << i;
EXPECT_EQ(bit_floor(input), input);
if (i > 0) {
EXPECT_EQ(bit_floor(static_cast<uint8_t>(input + 1)), input);
}
}
for (int i = 0; i < 16; i++) {
uint16_t input = uint16_t{1} << i;
EXPECT_EQ(bit_floor(input), input);
if (i > 0) {
EXPECT_EQ(bit_floor(static_cast<uint16_t>(input + 1)), input);
}
}
for (int i = 0; i < 32; i++) {
uint32_t input = uint32_t{1} << i;
EXPECT_EQ(bit_floor(input), input);
if (i > 0) {
EXPECT_EQ(bit_floor(input + 1), input);
}
}
for (int i = 0; i < 64; i++) {
uint64_t input = uint64_t{1} << i;
EXPECT_EQ(bit_floor(input), input);
if (i > 0) {
EXPECT_EQ(bit_floor(input + 1), input);
}
}
}
TEST(IntegralPowersOfTwo, Width) {
#if ABSL_INTERNAL_HAS_CONSTEXPR_CLZ
static_assert(bit_width(uint8_t{}) == 0, "");
static_assert(bit_width(uint8_t{1}) == 1, "");
static_assert(bit_width(uint8_t{3}) == 2, "");
static_assert(bit_width(static_cast<uint8_t>(-1)) == 8, "");
static_assert(bit_width(uint16_t{}) == 0, "");
static_assert(bit_width(uint16_t{1}) == 1, "");
static_assert(bit_width(uint16_t{3}) == 2, "");
static_assert(bit_width(static_cast<uint16_t>(-1)) == 16, "");
static_assert(bit_width(uint32_t{}) == 0, "");
static_assert(bit_width(uint32_t{1}) == 1, "");
static_assert(bit_width(uint32_t{3}) == 2, "");
static_assert(bit_width(~uint32_t{}) == 32, "");
static_assert(bit_width(uint64_t{}) == 0, "");
static_assert(bit_width(uint64_t{1}) == 1, "");
static_assert(bit_width(uint64_t{3}) == 2, "");
static_assert(bit_width(~uint64_t{}) == 64, "");
#endif
EXPECT_EQ(bit_width(uint8_t{}), 0);
EXPECT_EQ(bit_width(uint8_t{1}), 1);
EXPECT_EQ(bit_width(uint8_t{3}), 2);
EXPECT_EQ(bit_width(static_cast<uint8_t>(-1)), 8);
EXPECT_EQ(bit_width(uint16_t{}), 0);
EXPECT_EQ(bit_width(uint16_t{1}), 1);
EXPECT_EQ(bit_width(uint16_t{3}), 2);
EXPECT_EQ(bit_width(static_cast<uint16_t>(-1)), 16);
EXPECT_EQ(bit_width(uint32_t{}), 0);
EXPECT_EQ(bit_width(uint32_t{1}), 1);
EXPECT_EQ(bit_width(uint32_t{3}), 2);
EXPECT_EQ(bit_width(~uint32_t{}), 32);
EXPECT_EQ(bit_width(uint64_t{}), 0);
EXPECT_EQ(bit_width(uint64_t{1}), 1);
EXPECT_EQ(bit_width(uint64_t{3}), 2);
EXPECT_EQ(bit_width(~uint64_t{}), 64);
for (int i = 0; i < 8; i++) {
EXPECT_EQ(bit_width(static_cast<uint8_t>(uint8_t{1} << i)), i + 1);
}
for (int i = 0; i < 16; i++) {
EXPECT_EQ(bit_width(static_cast<uint16_t>(uint16_t{1} << i)), i + 1);
}
for (int i = 0; i < 32; i++) {
EXPECT_EQ(bit_width(uint32_t{1} << i), i + 1);
}
for (int i = 0; i < 64; i++) {
EXPECT_EQ(bit_width(uint64_t{1} << i), i + 1);
}
}
// On GCC and Clang, anticiapte that implementations will be constexpr
#if defined(__GNUC__)
static_assert(ABSL_INTERNAL_HAS_CONSTEXPR_POPCOUNT,
"popcount should be constexpr");
static_assert(ABSL_INTERNAL_HAS_CONSTEXPR_CLZ, "clz should be constexpr");
static_assert(ABSL_INTERNAL_HAS_CONSTEXPR_CTZ, "ctz should be constexpr");
#endif
} // namespace
ABSL_NAMESPACE_END
} // namespace absl

View File

@@ -0,0 +1,383 @@
// 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/numeric/int128.h"
#include <stddef.h>
#include <cassert>
#include <iomanip>
#include <ostream> // NOLINT(readability/streams)
#include <sstream>
#include <string>
#include <type_traits>
#include "absl/base/optimization.h"
#include "absl/numeric/bits.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
ABSL_DLL const uint128 kuint128max = MakeUint128(
std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::max());
namespace {
// Returns the 0-based position of the last set bit (i.e., most significant bit)
// in the given uint128. The argument is not 0.
//
// For example:
// Given: 5 (decimal) == 101 (binary)
// Returns: 2
inline ABSL_ATTRIBUTE_ALWAYS_INLINE int Fls128(uint128 n) {
if (uint64_t hi = Uint128High64(n)) {
ABSL_INTERNAL_ASSUME(hi != 0);
return 127 - countl_zero(hi);
}
const uint64_t low = Uint128Low64(n);
ABSL_INTERNAL_ASSUME(low != 0);
return 63 - countl_zero(low);
}
// Long division/modulo for uint128 implemented using the shift-subtract
// division algorithm adapted from:
// https://stackoverflow.com/questions/5386377/division-without-using
inline void DivModImpl(uint128 dividend, uint128 divisor, uint128* quotient_ret,
uint128* remainder_ret) {
assert(divisor != 0);
if (divisor > dividend) {
*quotient_ret = 0;
*remainder_ret = dividend;
return;
}
if (divisor == dividend) {
*quotient_ret = 1;
*remainder_ret = 0;
return;
}
uint128 denominator = divisor;
uint128 quotient = 0;
// Left aligns the MSB of the denominator and the dividend.
const int shift = Fls128(dividend) - Fls128(denominator);
denominator <<= shift;
// Uses shift-subtract algorithm to divide dividend by denominator. The
// remainder will be left in dividend.
for (int i = 0; i <= shift; ++i) {
quotient <<= 1;
if (dividend >= denominator) {
dividend -= denominator;
quotient |= 1;
}
denominator >>= 1;
}
*quotient_ret = quotient;
*remainder_ret = dividend;
}
template <typename T>
uint128 MakeUint128FromFloat(T v) {
static_assert(std::is_floating_point<T>::value, "");
// Rounding behavior is towards zero, same as for built-in types.
// Undefined behavior if v is NaN or cannot fit into uint128.
assert(std::isfinite(v) && v > -1 &&
(std::numeric_limits<T>::max_exponent <= 128 ||
v < std::ldexp(static_cast<T>(1), 128)));
if (v >= std::ldexp(static_cast<T>(1), 64)) {
uint64_t hi = static_cast<uint64_t>(std::ldexp(v, -64));
uint64_t lo = static_cast<uint64_t>(v - std::ldexp(static_cast<T>(hi), 64));
return MakeUint128(hi, lo);
}
return MakeUint128(0, static_cast<uint64_t>(v));
}
#if defined(__clang__) && !defined(__SSE3__)
// Workaround for clang bug: https://bugs.llvm.org/show_bug.cgi?id=38289
// Casting from long double to uint64_t is miscompiled and drops bits.
// It is more work, so only use when we need the workaround.
uint128 MakeUint128FromFloat(long double v) {
// Go 50 bits at a time, that fits in a double
static_assert(std::numeric_limits<double>::digits >= 50, "");
static_assert(std::numeric_limits<long double>::digits <= 150, "");
// Undefined behavior if v is not finite or cannot fit into uint128.
assert(std::isfinite(v) && v > -1 && v < std::ldexp(1.0L, 128));
v = std::ldexp(v, -100);
uint64_t w0 = static_cast<uint64_t>(static_cast<double>(std::trunc(v)));
v = std::ldexp(v - static_cast<double>(w0), 50);
uint64_t w1 = static_cast<uint64_t>(static_cast<double>(std::trunc(v)));
v = std::ldexp(v - static_cast<double>(w1), 50);
uint64_t w2 = static_cast<uint64_t>(static_cast<double>(std::trunc(v)));
return (static_cast<uint128>(w0) << 100) | (static_cast<uint128>(w1) << 50) |
static_cast<uint128>(w2);
}
#endif // __clang__ && !__SSE3__
} // namespace
uint128::uint128(float v) : uint128(MakeUint128FromFloat(v)) {}
uint128::uint128(double v) : uint128(MakeUint128FromFloat(v)) {}
uint128::uint128(long double v) : uint128(MakeUint128FromFloat(v)) {}
#if !defined(ABSL_HAVE_INTRINSIC_INT128)
uint128 operator/(uint128 lhs, uint128 rhs) {
uint128 quotient = 0;
uint128 remainder = 0;
DivModImpl(lhs, rhs, &quotient, &remainder);
return quotient;
}
uint128 operator%(uint128 lhs, uint128 rhs) {
uint128 quotient = 0;
uint128 remainder = 0;
DivModImpl(lhs, rhs, &quotient, &remainder);
return remainder;
}
#endif // !defined(ABSL_HAVE_INTRINSIC_INT128)
namespace {
std::string Uint128ToFormattedString(uint128 v, std::ios_base::fmtflags flags) {
// Select a divisor which is the largest power of the base < 2^64.
uint128 div;
int div_base_log;
switch (flags & std::ios::basefield) {
case std::ios::hex:
div = 0x1000000000000000; // 16^15
div_base_log = 15;
break;
case std::ios::oct:
div = 01000000000000000000000; // 8^21
div_base_log = 21;
break;
default: // std::ios::dec
div = 10000000000000000000u; // 10^19
div_base_log = 19;
break;
}
// Now piece together the uint128 representation from three chunks of the
// original value, each less than "div" and therefore representable as a
// uint64_t.
std::ostringstream os;
std::ios_base::fmtflags copy_mask =
std::ios::basefield | std::ios::showbase | std::ios::uppercase;
os.setf(flags & copy_mask, copy_mask);
uint128 high = v;
uint128 low;
DivModImpl(high, div, &high, &low);
uint128 mid;
DivModImpl(high, div, &high, &mid);
if (Uint128Low64(high) != 0) {
os << Uint128Low64(high);
os << std::noshowbase << std::setfill('0') << std::setw(div_base_log);
os << Uint128Low64(mid);
os << std::setw(div_base_log);
} else if (Uint128Low64(mid) != 0) {
os << Uint128Low64(mid);
os << std::noshowbase << std::setfill('0') << std::setw(div_base_log);
}
os << Uint128Low64(low);
return os.str();
}
} // namespace
std::ostream& operator<<(std::ostream& os, uint128 v) {
std::ios_base::fmtflags flags = os.flags();
std::string rep = Uint128ToFormattedString(v, flags);
// Add the requisite padding.
std::streamsize width = os.width(0);
if (static_cast<size_t>(width) > rep.size()) {
std::ios::fmtflags adjustfield = flags & std::ios::adjustfield;
if (adjustfield == std::ios::left) {
rep.append(width - rep.size(), os.fill());
} else if (adjustfield == std::ios::internal &&
(flags & std::ios::showbase) &&
(flags & std::ios::basefield) == std::ios::hex && v != 0) {
rep.insert(2, width - rep.size(), os.fill());
} else {
rep.insert(0, width - rep.size(), os.fill());
}
}
return os << rep;
}
namespace {
uint128 UnsignedAbsoluteValue(int128 v) {
// Cast to uint128 before possibly negating because -Int128Min() is undefined.
return Int128High64(v) < 0 ? -uint128(v) : uint128(v);
}
} // namespace
#if !defined(ABSL_HAVE_INTRINSIC_INT128)
namespace {
template <typename T>
int128 MakeInt128FromFloat(T v) {
// Conversion when v is NaN or cannot fit into int128 would be undefined
// behavior if using an intrinsic 128-bit integer.
assert(std::isfinite(v) && (std::numeric_limits<T>::max_exponent <= 127 ||
(v >= -std::ldexp(static_cast<T>(1), 127) &&
v < std::ldexp(static_cast<T>(1), 127))));
// We must convert the absolute value and then negate as needed, because
// floating point types are typically sign-magnitude. Otherwise, the
// difference between the high and low 64 bits when interpreted as two's
// complement overwhelms the precision of the mantissa.
uint128 result = v < 0 ? -MakeUint128FromFloat(-v) : MakeUint128FromFloat(v);
return MakeInt128(int128_internal::BitCastToSigned(Uint128High64(result)),
Uint128Low64(result));
}
} // namespace
int128::int128(float v) : int128(MakeInt128FromFloat(v)) {}
int128::int128(double v) : int128(MakeInt128FromFloat(v)) {}
int128::int128(long double v) : int128(MakeInt128FromFloat(v)) {}
int128 operator/(int128 lhs, int128 rhs) {
assert(lhs != Int128Min() || rhs != -1); // UB on two's complement.
uint128 quotient = 0;
uint128 remainder = 0;
DivModImpl(UnsignedAbsoluteValue(lhs), UnsignedAbsoluteValue(rhs),
&quotient, &remainder);
if ((Int128High64(lhs) < 0) != (Int128High64(rhs) < 0)) quotient = -quotient;
return MakeInt128(int128_internal::BitCastToSigned(Uint128High64(quotient)),
Uint128Low64(quotient));
}
int128 operator%(int128 lhs, int128 rhs) {
assert(lhs != Int128Min() || rhs != -1); // UB on two's complement.
uint128 quotient = 0;
uint128 remainder = 0;
DivModImpl(UnsignedAbsoluteValue(lhs), UnsignedAbsoluteValue(rhs),
&quotient, &remainder);
if (Int128High64(lhs) < 0) remainder = -remainder;
return MakeInt128(int128_internal::BitCastToSigned(Uint128High64(remainder)),
Uint128Low64(remainder));
}
#endif // ABSL_HAVE_INTRINSIC_INT128
std::ostream& operator<<(std::ostream& os, int128 v) {
std::ios_base::fmtflags flags = os.flags();
std::string rep;
// Add the sign if needed.
bool print_as_decimal =
(flags & std::ios::basefield) == std::ios::dec ||
(flags & std::ios::basefield) == std::ios_base::fmtflags();
if (print_as_decimal) {
if (Int128High64(v) < 0) {
rep = "-";
} else if (flags & std::ios::showpos) {
rep = "+";
}
}
rep.append(Uint128ToFormattedString(
print_as_decimal ? UnsignedAbsoluteValue(v) : uint128(v), os.flags()));
// Add the requisite padding.
std::streamsize width = os.width(0);
if (static_cast<size_t>(width) > rep.size()) {
switch (flags & std::ios::adjustfield) {
case std::ios::left:
rep.append(width - rep.size(), os.fill());
break;
case std::ios::internal:
if (print_as_decimal && (rep[0] == '+' || rep[0] == '-')) {
rep.insert(1, width - rep.size(), os.fill());
} else if ((flags & std::ios::basefield) == std::ios::hex &&
(flags & std::ios::showbase) && v != 0) {
rep.insert(2, width - rep.size(), os.fill());
} else {
rep.insert(0, width - rep.size(), os.fill());
}
break;
default: // std::ios::right
rep.insert(0, width - rep.size(), os.fill());
break;
}
}
return os << rep;
}
ABSL_NAMESPACE_END
} // namespace absl
namespace std {
constexpr bool numeric_limits<absl::uint128>::is_specialized;
constexpr bool numeric_limits<absl::uint128>::is_signed;
constexpr bool numeric_limits<absl::uint128>::is_integer;
constexpr bool numeric_limits<absl::uint128>::is_exact;
constexpr bool numeric_limits<absl::uint128>::has_infinity;
constexpr bool numeric_limits<absl::uint128>::has_quiet_NaN;
constexpr bool numeric_limits<absl::uint128>::has_signaling_NaN;
constexpr float_denorm_style numeric_limits<absl::uint128>::has_denorm;
constexpr bool numeric_limits<absl::uint128>::has_denorm_loss;
constexpr float_round_style numeric_limits<absl::uint128>::round_style;
constexpr bool numeric_limits<absl::uint128>::is_iec559;
constexpr bool numeric_limits<absl::uint128>::is_bounded;
constexpr bool numeric_limits<absl::uint128>::is_modulo;
constexpr int numeric_limits<absl::uint128>::digits;
constexpr int numeric_limits<absl::uint128>::digits10;
constexpr int numeric_limits<absl::uint128>::max_digits10;
constexpr int numeric_limits<absl::uint128>::radix;
constexpr int numeric_limits<absl::uint128>::min_exponent;
constexpr int numeric_limits<absl::uint128>::min_exponent10;
constexpr int numeric_limits<absl::uint128>::max_exponent;
constexpr int numeric_limits<absl::uint128>::max_exponent10;
constexpr bool numeric_limits<absl::uint128>::traps;
constexpr bool numeric_limits<absl::uint128>::tinyness_before;
constexpr bool numeric_limits<absl::int128>::is_specialized;
constexpr bool numeric_limits<absl::int128>::is_signed;
constexpr bool numeric_limits<absl::int128>::is_integer;
constexpr bool numeric_limits<absl::int128>::is_exact;
constexpr bool numeric_limits<absl::int128>::has_infinity;
constexpr bool numeric_limits<absl::int128>::has_quiet_NaN;
constexpr bool numeric_limits<absl::int128>::has_signaling_NaN;
constexpr float_denorm_style numeric_limits<absl::int128>::has_denorm;
constexpr bool numeric_limits<absl::int128>::has_denorm_loss;
constexpr float_round_style numeric_limits<absl::int128>::round_style;
constexpr bool numeric_limits<absl::int128>::is_iec559;
constexpr bool numeric_limits<absl::int128>::is_bounded;
constexpr bool numeric_limits<absl::int128>::is_modulo;
constexpr int numeric_limits<absl::int128>::digits;
constexpr int numeric_limits<absl::int128>::digits10;
constexpr int numeric_limits<absl::int128>::max_digits10;
constexpr int numeric_limits<absl::int128>::radix;
constexpr int numeric_limits<absl::int128>::min_exponent;
constexpr int numeric_limits<absl::int128>::min_exponent10;
constexpr int numeric_limits<absl::int128>::max_exponent;
constexpr int numeric_limits<absl::int128>::max_exponent10;
constexpr bool numeric_limits<absl::int128>::traps;
constexpr bool numeric_limits<absl::int128>::tinyness_before;
} // namespace std

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,282 @@
// 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 <algorithm>
#include <cstdint>
#include <limits>
#include <random>
#include <vector>
#include "benchmark/benchmark.h"
#include "absl/base/config.h"
#include "absl/numeric/int128.h"
namespace {
constexpr size_t kSampleSize = 1000000;
std::mt19937 MakeRandomEngine() {
std::random_device r;
std::seed_seq seed({r(), r(), r(), r(), r(), r(), r(), r()});
return std::mt19937(seed);
}
template <typename T,
typename H = typename std::conditional<
std::numeric_limits<T>::is_signed, int64_t, uint64_t>::type>
std::vector<std::pair<T, T>> GetRandomClass128SampleUniformDivisor() {
std::vector<std::pair<T, T>> values;
std::mt19937 random = MakeRandomEngine();
std::uniform_int_distribution<H> uniform_h;
values.reserve(kSampleSize);
for (size_t i = 0; i < kSampleSize; ++i) {
T a{absl::MakeUint128(uniform_h(random), uniform_h(random))};
T b{absl::MakeUint128(uniform_h(random), uniform_h(random))};
values.emplace_back(std::max(a, b), std::max(T(2), std::min(a, b)));
}
return values;
}
template <typename T>
void BM_DivideClass128UniformDivisor(benchmark::State& state) {
auto values = GetRandomClass128SampleUniformDivisor<T>();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first / pair.second);
}
}
}
BENCHMARK_TEMPLATE(BM_DivideClass128UniformDivisor, absl::uint128);
BENCHMARK_TEMPLATE(BM_DivideClass128UniformDivisor, absl::int128);
template <typename T>
void BM_RemainderClass128UniformDivisor(benchmark::State& state) {
auto values = GetRandomClass128SampleUniformDivisor<T>();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first % pair.second);
}
}
}
BENCHMARK_TEMPLATE(BM_RemainderClass128UniformDivisor, absl::uint128);
BENCHMARK_TEMPLATE(BM_RemainderClass128UniformDivisor, absl::int128);
template <typename T,
typename H = typename std::conditional<
std::numeric_limits<T>::is_signed, int64_t, uint64_t>::type>
std::vector<std::pair<T, H>> GetRandomClass128SampleSmallDivisor() {
std::vector<std::pair<T, H>> values;
std::mt19937 random = MakeRandomEngine();
std::uniform_int_distribution<H> uniform_h;
values.reserve(kSampleSize);
for (size_t i = 0; i < kSampleSize; ++i) {
T a{absl::MakeUint128(uniform_h(random), uniform_h(random))};
H b{std::max(H{2}, uniform_h(random))};
values.emplace_back(std::max(a, T(b)), b);
}
return values;
}
template <typename T>
void BM_DivideClass128SmallDivisor(benchmark::State& state) {
auto values = GetRandomClass128SampleSmallDivisor<T>();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first / pair.second);
}
}
}
BENCHMARK_TEMPLATE(BM_DivideClass128SmallDivisor, absl::uint128);
BENCHMARK_TEMPLATE(BM_DivideClass128SmallDivisor, absl::int128);
template <typename T>
void BM_RemainderClass128SmallDivisor(benchmark::State& state) {
auto values = GetRandomClass128SampleSmallDivisor<T>();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first % pair.second);
}
}
}
BENCHMARK_TEMPLATE(BM_RemainderClass128SmallDivisor, absl::uint128);
BENCHMARK_TEMPLATE(BM_RemainderClass128SmallDivisor, absl::int128);
std::vector<std::pair<absl::uint128, absl::uint128>> GetRandomClass128Sample() {
std::vector<std::pair<absl::uint128, absl::uint128>> values;
std::mt19937 random = MakeRandomEngine();
std::uniform_int_distribution<uint64_t> uniform_uint64;
values.reserve(kSampleSize);
for (size_t i = 0; i < kSampleSize; ++i) {
values.emplace_back(
absl::MakeUint128(uniform_uint64(random), uniform_uint64(random)),
absl::MakeUint128(uniform_uint64(random), uniform_uint64(random)));
}
return values;
}
void BM_MultiplyClass128(benchmark::State& state) {
auto values = GetRandomClass128Sample();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first * pair.second);
}
}
}
BENCHMARK(BM_MultiplyClass128);
void BM_AddClass128(benchmark::State& state) {
auto values = GetRandomClass128Sample();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first + pair.second);
}
}
}
BENCHMARK(BM_AddClass128);
#ifdef ABSL_HAVE_INTRINSIC_INT128
// Some implementations of <random> do not support __int128 when it is
// available, so we make our own uniform_int_distribution-like type.
template <typename T,
typename H = typename std::conditional<
std::is_same<T, __int128>::value, int64_t, uint64_t>::type>
class UniformIntDistribution128 {
public:
// NOLINTNEXTLINE: mimicking std::uniform_int_distribution API
T operator()(std::mt19937& generator) {
return (static_cast<T>(dist64_(generator)) << 64) | dist64_(generator);
}
private:
std::uniform_int_distribution<H> dist64_;
};
template <typename T,
typename H = typename std::conditional<
std::is_same<T, __int128>::value, int64_t, uint64_t>::type>
std::vector<std::pair<T, T>> GetRandomIntrinsic128SampleUniformDivisor() {
std::vector<std::pair<T, T>> values;
std::mt19937 random = MakeRandomEngine();
UniformIntDistribution128<T> uniform_128;
values.reserve(kSampleSize);
for (size_t i = 0; i < kSampleSize; ++i) {
T a = uniform_128(random);
T b = uniform_128(random);
values.emplace_back(std::max(a, b),
std::max(static_cast<T>(2), std::min(a, b)));
}
return values;
}
template <typename T>
void BM_DivideIntrinsic128UniformDivisor(benchmark::State& state) {
auto values = GetRandomIntrinsic128SampleUniformDivisor<T>();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first / pair.second);
}
}
}
BENCHMARK_TEMPLATE(BM_DivideIntrinsic128UniformDivisor, unsigned __int128);
BENCHMARK_TEMPLATE(BM_DivideIntrinsic128UniformDivisor, __int128);
template <typename T>
void BM_RemainderIntrinsic128UniformDivisor(benchmark::State& state) {
auto values = GetRandomIntrinsic128SampleUniformDivisor<T>();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first % pair.second);
}
}
}
BENCHMARK_TEMPLATE(BM_RemainderIntrinsic128UniformDivisor, unsigned __int128);
BENCHMARK_TEMPLATE(BM_RemainderIntrinsic128UniformDivisor, __int128);
template <typename T,
typename H = typename std::conditional<
std::is_same<T, __int128>::value, int64_t, uint64_t>::type>
std::vector<std::pair<T, H>> GetRandomIntrinsic128SampleSmallDivisor() {
std::vector<std::pair<T, H>> values;
std::mt19937 random = MakeRandomEngine();
UniformIntDistribution128<T> uniform_int128;
std::uniform_int_distribution<H> uniform_int64;
values.reserve(kSampleSize);
for (size_t i = 0; i < kSampleSize; ++i) {
T a = uniform_int128(random);
H b = std::max(H{2}, uniform_int64(random));
values.emplace_back(std::max(a, static_cast<T>(b)), b);
}
return values;
}
template <typename T>
void BM_DivideIntrinsic128SmallDivisor(benchmark::State& state) {
auto values = GetRandomIntrinsic128SampleSmallDivisor<T>();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first / pair.second);
}
}
}
BENCHMARK_TEMPLATE(BM_DivideIntrinsic128SmallDivisor, unsigned __int128);
BENCHMARK_TEMPLATE(BM_DivideIntrinsic128SmallDivisor, __int128);
template <typename T>
void BM_RemainderIntrinsic128SmallDivisor(benchmark::State& state) {
auto values = GetRandomIntrinsic128SampleSmallDivisor<T>();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first % pair.second);
}
}
}
BENCHMARK_TEMPLATE(BM_RemainderIntrinsic128SmallDivisor, unsigned __int128);
BENCHMARK_TEMPLATE(BM_RemainderIntrinsic128SmallDivisor, __int128);
std::vector<std::pair<unsigned __int128, unsigned __int128>>
GetRandomIntrinsic128Sample() {
std::vector<std::pair<unsigned __int128, unsigned __int128>> values;
std::mt19937 random = MakeRandomEngine();
UniformIntDistribution128<unsigned __int128> uniform_uint128;
values.reserve(kSampleSize);
for (size_t i = 0; i < kSampleSize; ++i) {
values.emplace_back(uniform_uint128(random), uniform_uint128(random));
}
return values;
}
void BM_MultiplyIntrinsic128(benchmark::State& state) {
auto values = GetRandomIntrinsic128Sample();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first * pair.second);
}
}
}
BENCHMARK(BM_MultiplyIntrinsic128);
void BM_AddIntrinsic128(benchmark::State& state) {
auto values = GetRandomIntrinsic128Sample();
while (state.KeepRunningBatch(values.size())) {
for (const auto& pair : values) {
benchmark::DoNotOptimize(pair.first + pair.second);
}
}
}
BENCHMARK(BM_AddIntrinsic128);
#endif // ABSL_HAVE_INTRINSIC_INT128
} // namespace

View File

@@ -0,0 +1,296 @@
//
// 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.
// This file contains :int128 implementation details that depend on internal
// representation when ABSL_HAVE_INTRINSIC_INT128 is defined. This file is
// included by int128.h and relies on ABSL_INTERNAL_WCHAR_T being defined.
namespace int128_internal {
// Casts from unsigned to signed while preserving the underlying binary
// representation.
constexpr __int128 BitCastToSigned(unsigned __int128 v) {
// Casting an unsigned integer to a signed integer of the same
// width is implementation defined behavior if the source value would not fit
// in the destination type. We step around it with a roundtrip bitwise not
// operation to make sure this function remains constexpr. Clang and GCC
// optimize this to a no-op on x86-64.
return v & (static_cast<unsigned __int128>(1) << 127)
? ~static_cast<__int128>(~v)
: static_cast<__int128>(v);
}
} // namespace int128_internal
inline int128& int128::operator=(__int128 v) {
v_ = v;
return *this;
}
constexpr uint64_t Int128Low64(int128 v) {
return static_cast<uint64_t>(v.v_ & ~uint64_t{0});
}
constexpr int64_t Int128High64(int128 v) {
// Initially cast to unsigned to prevent a right shift on a negative value.
return int128_internal::BitCastToSigned(
static_cast<uint64_t>(static_cast<unsigned __int128>(v.v_) >> 64));
}
constexpr int128::int128(int64_t high, uint64_t low)
// Initially cast to unsigned to prevent a left shift that overflows.
: v_(int128_internal::BitCastToSigned(static_cast<unsigned __int128>(high)
<< 64) |
low) {}
constexpr int128::int128(int v) : v_{v} {}
constexpr int128::int128(long v) : v_{v} {} // NOLINT(runtime/int)
constexpr int128::int128(long long v) : v_{v} {} // NOLINT(runtime/int)
constexpr int128::int128(__int128 v) : v_{v} {}
constexpr int128::int128(unsigned int v) : v_{v} {}
constexpr int128::int128(unsigned long v) : v_{v} {} // NOLINT(runtime/int)
// NOLINTNEXTLINE(runtime/int)
constexpr int128::int128(unsigned long long v) : v_{v} {}
constexpr int128::int128(unsigned __int128 v) : v_{static_cast<__int128>(v)} {}
inline int128::int128(float v) {
v_ = static_cast<__int128>(v);
}
inline int128::int128(double v) {
v_ = static_cast<__int128>(v);
}
inline int128::int128(long double v) {
v_ = static_cast<__int128>(v);
}
constexpr int128::int128(uint128 v) : v_{static_cast<__int128>(v)} {}
constexpr int128::operator bool() const { return static_cast<bool>(v_); }
constexpr int128::operator char() const { return static_cast<char>(v_); }
constexpr int128::operator signed char() const {
return static_cast<signed char>(v_);
}
constexpr int128::operator unsigned char() const {
return static_cast<unsigned char>(v_);
}
constexpr int128::operator char16_t() const {
return static_cast<char16_t>(v_);
}
constexpr int128::operator char32_t() const {
return static_cast<char32_t>(v_);
}
constexpr int128::operator ABSL_INTERNAL_WCHAR_T() const {
return static_cast<ABSL_INTERNAL_WCHAR_T>(v_);
}
constexpr int128::operator short() const { // NOLINT(runtime/int)
return static_cast<short>(v_); // NOLINT(runtime/int)
}
constexpr int128::operator unsigned short() const { // NOLINT(runtime/int)
return static_cast<unsigned short>(v_); // NOLINT(runtime/int)
}
constexpr int128::operator int() const {
return static_cast<int>(v_);
}
constexpr int128::operator unsigned int() const {
return static_cast<unsigned int>(v_);
}
constexpr int128::operator long() const { // NOLINT(runtime/int)
return static_cast<long>(v_); // NOLINT(runtime/int)
}
constexpr int128::operator unsigned long() const { // NOLINT(runtime/int)
return static_cast<unsigned long>(v_); // NOLINT(runtime/int)
}
constexpr int128::operator long long() const { // NOLINT(runtime/int)
return static_cast<long long>(v_); // NOLINT(runtime/int)
}
constexpr int128::operator unsigned long long() const { // NOLINT(runtime/int)
return static_cast<unsigned long long>(v_); // NOLINT(runtime/int)
}
constexpr int128::operator __int128() const { return v_; }
constexpr int128::operator unsigned __int128() const {
return static_cast<unsigned __int128>(v_);
}
// Clang on PowerPC sometimes produces incorrect __int128 to floating point
// conversions. In that case, we do the conversion with a similar implementation
// to the conversion operators in int128_no_intrinsic.inc.
#if defined(__clang__) && !defined(__ppc64__)
inline int128::operator float() const { return static_cast<float>(v_); }
inline int128::operator double() const { return static_cast<double>(v_); }
inline int128::operator long double() const {
return static_cast<long double>(v_);
}
#else // Clang on PowerPC
// Forward declaration for conversion operators to floating point types.
constexpr int128 operator-(int128 v);
constexpr bool operator!=(int128 lhs, int128 rhs);
inline int128::operator float() const {
// We must convert the absolute value and then negate as needed, because
// floating point types are typically sign-magnitude. Otherwise, the
// difference between the high and low 64 bits when interpreted as two's
// complement overwhelms the precision of the mantissa.
//
// Also check to make sure we don't negate Int128Min()
return v_ < 0 && *this != Int128Min()
? -static_cast<float>(-*this)
: static_cast<float>(Int128Low64(*this)) +
std::ldexp(static_cast<float>(Int128High64(*this)), 64);
}
inline int128::operator double() const {
// See comment in int128::operator float() above.
return v_ < 0 && *this != Int128Min()
? -static_cast<double>(-*this)
: static_cast<double>(Int128Low64(*this)) +
std::ldexp(static_cast<double>(Int128High64(*this)), 64);
}
inline int128::operator long double() const {
// See comment in int128::operator float() above.
return v_ < 0 && *this != Int128Min()
? -static_cast<long double>(-*this)
: static_cast<long double>(Int128Low64(*this)) +
std::ldexp(static_cast<long double>(Int128High64(*this)),
64);
}
#endif // Clang on PowerPC
// Comparison operators.
constexpr bool operator==(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) == static_cast<__int128>(rhs);
}
constexpr bool operator!=(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) != static_cast<__int128>(rhs);
}
constexpr bool operator<(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) < static_cast<__int128>(rhs);
}
constexpr bool operator>(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) > static_cast<__int128>(rhs);
}
constexpr bool operator<=(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) <= static_cast<__int128>(rhs);
}
constexpr bool operator>=(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) >= static_cast<__int128>(rhs);
}
// Unary operators.
constexpr int128 operator-(int128 v) { return -static_cast<__int128>(v); }
constexpr bool operator!(int128 v) { return !static_cast<__int128>(v); }
constexpr int128 operator~(int128 val) { return ~static_cast<__int128>(val); }
// Arithmetic operators.
constexpr int128 operator+(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) + static_cast<__int128>(rhs);
}
constexpr int128 operator-(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) - static_cast<__int128>(rhs);
}
inline int128 operator*(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) * static_cast<__int128>(rhs);
}
inline int128 operator/(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) / static_cast<__int128>(rhs);
}
inline int128 operator%(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) % static_cast<__int128>(rhs);
}
inline int128 int128::operator++(int) {
int128 tmp(*this);
++v_;
return tmp;
}
inline int128 int128::operator--(int) {
int128 tmp(*this);
--v_;
return tmp;
}
inline int128& int128::operator++() {
++v_;
return *this;
}
inline int128& int128::operator--() {
--v_;
return *this;
}
constexpr int128 operator|(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) | static_cast<__int128>(rhs);
}
constexpr int128 operator&(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) & static_cast<__int128>(rhs);
}
constexpr int128 operator^(int128 lhs, int128 rhs) {
return static_cast<__int128>(lhs) ^ static_cast<__int128>(rhs);
}
constexpr int128 operator<<(int128 lhs, int amount) {
return static_cast<__int128>(lhs) << amount;
}
constexpr int128 operator>>(int128 lhs, int amount) {
return static_cast<__int128>(lhs) >> amount;
}

View File

@@ -0,0 +1,305 @@
//
// 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.
// This file contains :int128 implementation details that depend on internal
// representation when ABSL_HAVE_INTRINSIC_INT128 is *not* defined. This file
// is included by int128.h and relies on ABSL_INTERNAL_WCHAR_T being defined.
constexpr uint64_t Int128Low64(int128 v) { return v.lo_; }
constexpr int64_t Int128High64(int128 v) { return v.hi_; }
#if defined(ABSL_IS_LITTLE_ENDIAN)
constexpr int128::int128(int64_t high, uint64_t low) :
lo_(low), hi_(high) {}
constexpr int128::int128(int v)
: lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
constexpr int128::int128(long v) // NOLINT(runtime/int)
: lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
constexpr int128::int128(long long v) // NOLINT(runtime/int)
: lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
constexpr int128::int128(unsigned int v) : lo_{v}, hi_{0} {}
// NOLINTNEXTLINE(runtime/int)
constexpr int128::int128(unsigned long v) : lo_{v}, hi_{0} {}
// NOLINTNEXTLINE(runtime/int)
constexpr int128::int128(unsigned long long v) : lo_{v}, hi_{0} {}
constexpr int128::int128(uint128 v)
: lo_{Uint128Low64(v)}, hi_{static_cast<int64_t>(Uint128High64(v))} {}
#elif defined(ABSL_IS_BIG_ENDIAN)
constexpr int128::int128(int64_t high, uint64_t low) :
hi_{high}, lo_{low} {}
constexpr int128::int128(int v)
: hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
constexpr int128::int128(long v) // NOLINT(runtime/int)
: hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
constexpr int128::int128(long long v) // NOLINT(runtime/int)
: hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
constexpr int128::int128(unsigned int v) : hi_{0}, lo_{v} {}
// NOLINTNEXTLINE(runtime/int)
constexpr int128::int128(unsigned long v) : hi_{0}, lo_{v} {}
// NOLINTNEXTLINE(runtime/int)
constexpr int128::int128(unsigned long long v) : hi_{0}, lo_{v} {}
constexpr int128::int128(uint128 v)
: hi_{static_cast<int64_t>(Uint128High64(v))}, lo_{Uint128Low64(v)} {}
#else // byte order
#error "Unsupported byte order: must be little-endian or big-endian."
#endif // byte order
constexpr int128::operator bool() const { return lo_ || hi_; }
constexpr int128::operator char() const {
// NOLINTNEXTLINE(runtime/int)
return static_cast<char>(static_cast<long long>(*this));
}
constexpr int128::operator signed char() const {
// NOLINTNEXTLINE(runtime/int)
return static_cast<signed char>(static_cast<long long>(*this));
}
constexpr int128::operator unsigned char() const {
return static_cast<unsigned char>(lo_);
}
constexpr int128::operator char16_t() const {
return static_cast<char16_t>(lo_);
}
constexpr int128::operator char32_t() const {
return static_cast<char32_t>(lo_);
}
constexpr int128::operator ABSL_INTERNAL_WCHAR_T() const {
// NOLINTNEXTLINE(runtime/int)
return static_cast<ABSL_INTERNAL_WCHAR_T>(static_cast<long long>(*this));
}
constexpr int128::operator short() const { // NOLINT(runtime/int)
// NOLINTNEXTLINE(runtime/int)
return static_cast<short>(static_cast<long long>(*this));
}
constexpr int128::operator unsigned short() const { // NOLINT(runtime/int)
return static_cast<unsigned short>(lo_); // NOLINT(runtime/int)
}
constexpr int128::operator int() const {
// NOLINTNEXTLINE(runtime/int)
return static_cast<int>(static_cast<long long>(*this));
}
constexpr int128::operator unsigned int() const {
return static_cast<unsigned int>(lo_);
}
constexpr int128::operator long() const { // NOLINT(runtime/int)
// NOLINTNEXTLINE(runtime/int)
return static_cast<long>(static_cast<long long>(*this));
}
constexpr int128::operator unsigned long() const { // NOLINT(runtime/int)
return static_cast<unsigned long>(lo_); // NOLINT(runtime/int)
}
constexpr int128::operator long long() const { // NOLINT(runtime/int)
// We don't bother checking the value of hi_. If *this < 0, lo_'s high bit
// must be set in order for the value to fit into a long long. Conversely, if
// lo_'s high bit is set, *this must be < 0 for the value to fit.
return int128_internal::BitCastToSigned(lo_);
}
constexpr int128::operator unsigned long long() const { // NOLINT(runtime/int)
return static_cast<unsigned long long>(lo_); // NOLINT(runtime/int)
}
inline int128::operator float() const {
// We must convert the absolute value and then negate as needed, because
// floating point types are typically sign-magnitude. Otherwise, the
// difference between the high and low 64 bits when interpreted as two's
// complement overwhelms the precision of the mantissa.
//
// Also check to make sure we don't negate Int128Min()
return hi_ < 0 && *this != Int128Min()
? -static_cast<float>(-*this)
: static_cast<float>(lo_) +
std::ldexp(static_cast<float>(hi_), 64);
}
inline int128::operator double() const {
// See comment in int128::operator float() above.
return hi_ < 0 && *this != Int128Min()
? -static_cast<double>(-*this)
: static_cast<double>(lo_) +
std::ldexp(static_cast<double>(hi_), 64);
}
inline int128::operator long double() const {
// See comment in int128::operator float() above.
return hi_ < 0 && *this != Int128Min()
? -static_cast<long double>(-*this)
: static_cast<long double>(lo_) +
std::ldexp(static_cast<long double>(hi_), 64);
}
// Comparison operators.
constexpr bool operator==(int128 lhs, int128 rhs) {
return (Int128Low64(lhs) == Int128Low64(rhs) &&
Int128High64(lhs) == Int128High64(rhs));
}
constexpr bool operator!=(int128 lhs, int128 rhs) { return !(lhs == rhs); }
constexpr bool operator<(int128 lhs, int128 rhs) {
return (Int128High64(lhs) == Int128High64(rhs))
? (Int128Low64(lhs) < Int128Low64(rhs))
: (Int128High64(lhs) < Int128High64(rhs));
}
constexpr bool operator>(int128 lhs, int128 rhs) {
return (Int128High64(lhs) == Int128High64(rhs))
? (Int128Low64(lhs) > Int128Low64(rhs))
: (Int128High64(lhs) > Int128High64(rhs));
}
constexpr bool operator<=(int128 lhs, int128 rhs) { return !(lhs > rhs); }
constexpr bool operator>=(int128 lhs, int128 rhs) { return !(lhs < rhs); }
// Unary operators.
constexpr int128 operator-(int128 v) {
return MakeInt128(~Int128High64(v) + (Int128Low64(v) == 0),
~Int128Low64(v) + 1);
}
constexpr bool operator!(int128 v) {
return !Int128Low64(v) && !Int128High64(v);
}
constexpr int128 operator~(int128 val) {
return MakeInt128(~Int128High64(val), ~Int128Low64(val));
}
// Arithmetic operators.
namespace int128_internal {
constexpr int128 SignedAddResult(int128 result, int128 lhs) {
// check for carry
return (Int128Low64(result) < Int128Low64(lhs))
? MakeInt128(Int128High64(result) + 1, Int128Low64(result))
: result;
}
} // namespace int128_internal
constexpr int128 operator+(int128 lhs, int128 rhs) {
return int128_internal::SignedAddResult(
MakeInt128(Int128High64(lhs) + Int128High64(rhs),
Int128Low64(lhs) + Int128Low64(rhs)),
lhs);
}
namespace int128_internal {
constexpr int128 SignedSubstructResult(int128 result, int128 lhs, int128 rhs) {
// check for carry
return (Int128Low64(lhs) < Int128Low64(rhs))
? MakeInt128(Int128High64(result) - 1, Int128Low64(result))
: result;
}
} // namespace int128_internal
constexpr int128 operator-(int128 lhs, int128 rhs) {
return int128_internal::SignedSubstructResult(
MakeInt128(Int128High64(lhs) - Int128High64(rhs),
Int128Low64(lhs) - Int128Low64(rhs)),
lhs, rhs);
}
inline int128 operator*(int128 lhs, int128 rhs) {
return MakeInt128(
int128_internal::BitCastToSigned(Uint128High64(uint128(lhs) * rhs)),
Uint128Low64(uint128(lhs) * rhs));
}
inline int128 int128::operator++(int) {
int128 tmp(*this);
*this += 1;
return tmp;
}
inline int128 int128::operator--(int) {
int128 tmp(*this);
*this -= 1;
return tmp;
}
inline int128& int128::operator++() {
*this += 1;
return *this;
}
inline int128& int128::operator--() {
*this -= 1;
return *this;
}
constexpr int128 operator|(int128 lhs, int128 rhs) {
return MakeInt128(Int128High64(lhs) | Int128High64(rhs),
Int128Low64(lhs) | Int128Low64(rhs));
}
constexpr int128 operator&(int128 lhs, int128 rhs) {
return MakeInt128(Int128High64(lhs) & Int128High64(rhs),
Int128Low64(lhs) & Int128Low64(rhs));
}
constexpr int128 operator^(int128 lhs, int128 rhs) {
return MakeInt128(Int128High64(lhs) ^ Int128High64(rhs),
Int128Low64(lhs) ^ Int128Low64(rhs));
}
constexpr int128 operator<<(int128 lhs, int amount) {
// uint64_t shifts of >= 64 are undefined, so we need some special-casing.
return amount >= 64
? MakeInt128(
static_cast<int64_t>(Int128Low64(lhs) << (amount - 64)), 0)
: amount == 0
? lhs
: MakeInt128(
(Int128High64(lhs) << amount) |
static_cast<int64_t>(Int128Low64(lhs) >> (64 - amount)),
Int128Low64(lhs) << amount);
}
constexpr int128 operator>>(int128 lhs, int amount) {
// uint64_t shifts of >= 64 are undefined, so we need some special-casing.
return amount >= 64
? MakeInt128(
0, static_cast<uint64_t>(Int128High64(lhs) >> (amount - 64)))
: amount == 0
? lhs
: MakeInt128(Int128High64(lhs) >> amount,
(Int128Low64(lhs) >> amount) |
(static_cast<uint64_t>(Int128High64(lhs))
<< (64 - amount)));
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,358 @@
// Copyright 2020 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.
#ifndef ABSL_NUMERIC_INTERNAL_BITS_H_
#define ABSL_NUMERIC_INTERNAL_BITS_H_
#include <cstdint>
#include <limits>
#include <type_traits>
// Clang on Windows has __builtin_clzll; otherwise we need to use the
// windows intrinsic functions.
#if defined(_MSC_VER) && !defined(__clang__)
#include <intrin.h>
#endif
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#if defined(__GNUC__) && !defined(__clang__)
// GCC
#define ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(x) 1
#else
#define ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(x) ABSL_HAVE_BUILTIN(x)
#endif
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountl) && \
ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountll)
#define ABSL_INTERNAL_CONSTEXPR_POPCOUNT constexpr
#define ABSL_INTERNAL_HAS_CONSTEXPR_POPCOUNT 1
#else
#define ABSL_INTERNAL_CONSTEXPR_POPCOUNT
#define ABSL_INTERNAL_HAS_CONSTEXPR_POPCOUNT 0
#endif
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clz) && \
ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clzll)
#define ABSL_INTERNAL_CONSTEXPR_CLZ constexpr
#define ABSL_INTERNAL_HAS_CONSTEXPR_CLZ 1
#else
#define ABSL_INTERNAL_CONSTEXPR_CLZ
#define ABSL_INTERNAL_HAS_CONSTEXPR_CLZ 0
#endif
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctz) && \
ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctzll)
#define ABSL_INTERNAL_CONSTEXPR_CTZ constexpr
#define ABSL_INTERNAL_HAS_CONSTEXPR_CTZ 1
#else
#define ABSL_INTERNAL_CONSTEXPR_CTZ
#define ABSL_INTERNAL_HAS_CONSTEXPR_CTZ 0
#endif
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace numeric_internal {
constexpr bool IsPowerOf2(unsigned int x) noexcept {
return x != 0 && (x & (x - 1)) == 0;
}
template <class T>
ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_ALWAYS_INLINE constexpr T RotateRight(
T x, int s) noexcept {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
"T must have a power-of-2 size");
return static_cast<T>(x >> (s & (std::numeric_limits<T>::digits - 1))) |
static_cast<T>(x << ((-s) & (std::numeric_limits<T>::digits - 1)));
}
template <class T>
ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_ALWAYS_INLINE constexpr T RotateLeft(
T x, int s) noexcept {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
"T must have a power-of-2 size");
return static_cast<T>(x << (s & (std::numeric_limits<T>::digits - 1))) |
static_cast<T>(x >> ((-s) & (std::numeric_limits<T>::digits - 1)));
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
Popcount32(uint32_t x) noexcept {
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcount)
static_assert(sizeof(unsigned int) == sizeof(x),
"__builtin_popcount does not take 32-bit arg");
return __builtin_popcount(x);
#else
x -= ((x >> 1) & 0x55555555);
x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
return static_cast<int>((((x + (x >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24);
#endif
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
Popcount64(uint64_t x) noexcept {
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_popcountll)
static_assert(sizeof(unsigned long long) == sizeof(x), // NOLINT(runtime/int)
"__builtin_popcount does not take 64-bit arg");
return __builtin_popcountll(x);
#else
x -= (x >> 1) & 0x5555555555555555ULL;
x = ((x >> 2) & 0x3333333333333333ULL) + (x & 0x3333333333333333ULL);
return static_cast<int>(
(((x + (x >> 4)) & 0xF0F0F0F0F0F0F0FULL) * 0x101010101010101ULL) >> 56);
#endif
}
template <class T>
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inline int
Popcount(T x) noexcept {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
"T must have a power-of-2 size");
static_assert(sizeof(x) <= sizeof(uint64_t), "T is too large");
return sizeof(x) <= sizeof(uint32_t) ? Popcount32(x) : Popcount64(x);
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
CountLeadingZeroes32(uint32_t x) {
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clz)
// Use __builtin_clz, which uses the following instructions:
// x86: bsr, lzcnt
// ARM64: clz
// PPC: cntlzd
static_assert(sizeof(unsigned int) == sizeof(x),
"__builtin_clz does not take 32-bit arg");
// Handle 0 as a special case because __builtin_clz(0) is undefined.
return x == 0 ? 32 : __builtin_clz(x);
#elif defined(_MSC_VER) && !defined(__clang__)
unsigned long result = 0; // NOLINT(runtime/int)
if (_BitScanReverse(&result, x)) {
return 31 - result;
}
return 32;
#else
int zeroes = 28;
if (x >> 16) {
zeroes -= 16;
x >>= 16;
}
if (x >> 8) {
zeroes -= 8;
x >>= 8;
}
if (x >> 4) {
zeroes -= 4;
x >>= 4;
}
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
#endif
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
CountLeadingZeroes16(uint16_t x) {
#if ABSL_HAVE_BUILTIN(__builtin_clzs)
static_assert(sizeof(unsigned short) == sizeof(x), // NOLINT(runtime/int)
"__builtin_clzs does not take 16-bit arg");
return x == 0 ? 16 : __builtin_clzs(x);
#else
return CountLeadingZeroes32(x) - 16;
#endif
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
CountLeadingZeroes64(uint64_t x) {
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clzll)
// Use __builtin_clzll, which uses the following instructions:
// x86: bsr, lzcnt
// ARM64: clz
// PPC: cntlzd
static_assert(sizeof(unsigned long long) == sizeof(x), // NOLINT(runtime/int)
"__builtin_clzll does not take 64-bit arg");
// Handle 0 as a special case because __builtin_clzll(0) is undefined.
return x == 0 ? 64 : __builtin_clzll(x);
#elif defined(_MSC_VER) && !defined(__clang__) && \
(defined(_M_X64) || defined(_M_ARM64))
// MSVC does not have __buitin_clzll. Use _BitScanReverse64.
unsigned long result = 0; // NOLINT(runtime/int)
if (_BitScanReverse64(&result, x)) {
return 63 - result;
}
return 64;
#elif defined(_MSC_VER) && !defined(__clang__)
// MSVC does not have __buitin_clzll. Compose two calls to _BitScanReverse
unsigned long result = 0; // NOLINT(runtime/int)
if ((x >> 32) &&
_BitScanReverse(&result, static_cast<unsigned long>(x >> 32))) {
return 31 - result;
}
if (_BitScanReverse(&result, static_cast<unsigned long>(x))) {
return 63 - result;
}
return 64;
#else
int zeroes = 60;
if (x >> 32) {
zeroes -= 32;
x >>= 32;
}
if (x >> 16) {
zeroes -= 16;
x >>= 16;
}
if (x >> 8) {
zeroes -= 8;
x >>= 8;
}
if (x >> 4) {
zeroes -= 4;
x >>= 4;
}
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes;
#endif
}
template <typename T>
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
CountLeadingZeroes(T x) {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
"T must have a power-of-2 size");
static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
return sizeof(T) <= sizeof(uint16_t)
? CountLeadingZeroes16(static_cast<uint16_t>(x)) -
(std::numeric_limits<uint16_t>::digits -
std::numeric_limits<T>::digits)
: (sizeof(T) <= sizeof(uint32_t)
? CountLeadingZeroes32(static_cast<uint32_t>(x)) -
(std::numeric_limits<uint32_t>::digits -
std::numeric_limits<T>::digits)
: CountLeadingZeroes64(x));
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
CountTrailingZeroesNonzero32(uint32_t x) {
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctz)
static_assert(sizeof(unsigned int) == sizeof(x),
"__builtin_ctz does not take 32-bit arg");
return __builtin_ctz(x);
#elif defined(_MSC_VER) && !defined(__clang__)
unsigned long result = 0; // NOLINT(runtime/int)
_BitScanForward(&result, x);
return result;
#else
int c = 31;
x &= ~x + 1;
if (x & 0x0000FFFF) c -= 16;
if (x & 0x00FF00FF) c -= 8;
if (x & 0x0F0F0F0F) c -= 4;
if (x & 0x33333333) c -= 2;
if (x & 0x55555555) c -= 1;
return c;
#endif
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
CountTrailingZeroesNonzero64(uint64_t x) {
#if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctzll)
static_assert(sizeof(unsigned long long) == sizeof(x), // NOLINT(runtime/int)
"__builtin_ctzll does not take 64-bit arg");
return __builtin_ctzll(x);
#elif defined(_MSC_VER) && !defined(__clang__) && \
(defined(_M_X64) || defined(_M_ARM64))
unsigned long result = 0; // NOLINT(runtime/int)
_BitScanForward64(&result, x);
return result;
#elif defined(_MSC_VER) && !defined(__clang__)
unsigned long result = 0; // NOLINT(runtime/int)
if (static_cast<uint32_t>(x) == 0) {
_BitScanForward(&result, static_cast<unsigned long>(x >> 32));
return result + 32;
}
_BitScanForward(&result, static_cast<unsigned long>(x));
return result;
#else
int c = 63;
x &= ~x + 1;
if (x & 0x00000000FFFFFFFF) c -= 32;
if (x & 0x0000FFFF0000FFFF) c -= 16;
if (x & 0x00FF00FF00FF00FF) c -= 8;
if (x & 0x0F0F0F0F0F0F0F0F) c -= 4;
if (x & 0x3333333333333333) c -= 2;
if (x & 0x5555555555555555) c -= 1;
return c;
#endif
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
CountTrailingZeroesNonzero16(uint16_t x) {
#if ABSL_HAVE_BUILTIN(__builtin_ctzs)
static_assert(sizeof(unsigned short) == sizeof(x), // NOLINT(runtime/int)
"__builtin_ctzs does not take 16-bit arg");
return __builtin_ctzs(x);
#else
return CountTrailingZeroesNonzero32(x);
#endif
}
template <class T>
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
CountTrailingZeroes(T x) noexcept {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static_assert(IsPowerOf2(std::numeric_limits<T>::digits),
"T must have a power-of-2 size");
static_assert(sizeof(T) <= sizeof(uint64_t), "T too large");
return x == 0 ? std::numeric_limits<T>::digits
: (sizeof(T) <= sizeof(uint16_t)
? CountTrailingZeroesNonzero16(static_cast<uint16_t>(x))
: (sizeof(T) <= sizeof(uint32_t)
? CountTrailingZeroesNonzero32(
static_cast<uint32_t>(x))
: CountTrailingZeroesNonzero64(x)));
}
// If T is narrower than unsigned, T{1} << bit_width will be promoted. We
// want to force it to wraparound so that bit_ceil of an invalid value are not
// core constant expressions.
template <class T>
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline
typename std::enable_if<std::is_unsigned<T>::value, T>::type
BitCeilPromotionHelper(T x, T promotion) {
return (T{1} << (x + promotion)) >> promotion;
}
template <class T>
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline
typename std::enable_if<std::is_unsigned<T>::value, T>::type
BitCeilNonPowerOf2(T x) {
// If T is narrower than unsigned, it undergoes promotion to unsigned when we
// shift. We calculate the number of bits added by the wider type.
return BitCeilPromotionHelper(
static_cast<T>(std::numeric_limits<T>::digits - CountLeadingZeroes(x)),
T{sizeof(T) >= sizeof(unsigned) ? 0
: std::numeric_limits<unsigned>::digits -
std::numeric_limits<T>::digits});
}
} // namespace numeric_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_NUMERIC_INTERNAL_BITS_H_

View File

@@ -0,0 +1,55 @@
// Copyright 2021 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.
#ifndef ABSL_NUMERIC_INTERNAL_REPRESENTATION_H_
#define ABSL_NUMERIC_INTERNAL_REPRESENTATION_H_
#include <limits>
#include "absl/base/config.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace numeric_internal {
// Returns true iff long double is represented as a pair of doubles added
// together.
inline constexpr bool IsDoubleDouble() {
// A double-double value always has exactly twice the precision of a double
// value--one double carries the high digits and one double carries the low
// digits. This property is not shared with any other common floating-point
// representation, so this test won't trigger false positives. For reference,
// this table gives the number of bits of precision of each common
// floating-point representation:
//
// type precision
// IEEE single 24 b
// IEEE double 53
// x86 long double 64
// double-double 106
// IEEE quadruple 113
//
// Note in particular that a quadruple-precision float has greater precision
// than a double-double float despite taking up the same amount of memory; the
// quad has more of its bits allocated to the mantissa than the double-double
// has.
return std::numeric_limits<long double>::digits ==
2 * std::numeric_limits<double>::digits;
}
} // namespace numeric_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_NUMERIC_INTERNAL_REPRESENTATION_H_