adding pods method of package managing
This commit is contained in:
Generated
+404
@@ -0,0 +1,404 @@
|
||||
// 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>
|
||||
|
||||
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 uint64_t. The argument may not be 0.
|
||||
//
|
||||
// For example:
|
||||
// Given: 5 (decimal) == 101 (binary)
|
||||
// Returns: 2
|
||||
#define STEP(T, n, pos, sh) \
|
||||
do { \
|
||||
if ((n) >= (static_cast<T>(1) << (sh))) { \
|
||||
(n) = (n) >> (sh); \
|
||||
(pos) |= (sh); \
|
||||
} \
|
||||
} while (0)
|
||||
static inline int Fls64(uint64_t n) {
|
||||
assert(n != 0);
|
||||
int pos = 0;
|
||||
STEP(uint64_t, n, pos, 0x20);
|
||||
uint32_t n32 = static_cast<uint32_t>(n);
|
||||
STEP(uint32_t, n32, pos, 0x10);
|
||||
STEP(uint32_t, n32, pos, 0x08);
|
||||
STEP(uint32_t, n32, pos, 0x04);
|
||||
return pos + ((uint64_t{0x3333333322221100} >> (n32 << 2)) & 0x3);
|
||||
}
|
||||
#undef STEP
|
||||
|
||||
// Like Fls64() above, but returns the 0-based position of the last set bit
|
||||
// (i.e., most significant bit) in the given uint128. The argument may not be 0.
|
||||
static inline int Fls128(uint128 n) {
|
||||
if (uint64_t hi = Uint128High64(n)) {
|
||||
return Fls64(hi) + 64;
|
||||
}
|
||||
return Fls64(Uint128Low64(n));
|
||||
}
|
||||
|
||||
// Long division/modulo for uint128 implemented using the shift-subtract
|
||||
// division algorithm adapted from:
|
||||
// https://stackoverflow.com/questions/5386377/division-without-using
|
||||
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)) {}
|
||||
|
||||
uint128 operator/(uint128 lhs, uint128 rhs) {
|
||||
#if defined(ABSL_HAVE_INTRINSIC_INT128)
|
||||
return static_cast<unsigned __int128>(lhs) /
|
||||
static_cast<unsigned __int128>(rhs);
|
||||
#else // ABSL_HAVE_INTRINSIC_INT128
|
||||
uint128 quotient = 0;
|
||||
uint128 remainder = 0;
|
||||
DivModImpl(lhs, rhs, "ient, &remainder);
|
||||
return quotient;
|
||||
#endif // ABSL_HAVE_INTRINSIC_INT128
|
||||
}
|
||||
uint128 operator%(uint128 lhs, uint128 rhs) {
|
||||
#if defined(ABSL_HAVE_INTRINSIC_INT128)
|
||||
return static_cast<unsigned __int128>(lhs) %
|
||||
static_cast<unsigned __int128>(rhs);
|
||||
#else // ABSL_HAVE_INTRINSIC_INT128
|
||||
uint128 quotient = 0;
|
||||
uint128 remainder = 0;
|
||||
DivModImpl(lhs, rhs, "ient, &remainder);
|
||||
return remainder;
|
||||
#endif // 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),
|
||||
"ient, &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),
|
||||
"ient, &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
|
||||
Generated
+1091
File diff suppressed because it is too large
Load Diff
+302
@@ -0,0 +1,302 @@
|
||||
//
|
||||
// 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.
|
||||
int128 operator-(int128 v);
|
||||
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.
|
||||
|
||||
inline bool operator==(int128 lhs, int128 rhs) {
|
||||
return static_cast<__int128>(lhs) == static_cast<__int128>(rhs);
|
||||
}
|
||||
|
||||
inline bool operator!=(int128 lhs, int128 rhs) {
|
||||
return static_cast<__int128>(lhs) != static_cast<__int128>(rhs);
|
||||
}
|
||||
|
||||
inline bool operator<(int128 lhs, int128 rhs) {
|
||||
return static_cast<__int128>(lhs) < static_cast<__int128>(rhs);
|
||||
}
|
||||
|
||||
inline bool operator>(int128 lhs, int128 rhs) {
|
||||
return static_cast<__int128>(lhs) > static_cast<__int128>(rhs);
|
||||
}
|
||||
|
||||
inline bool operator<=(int128 lhs, int128 rhs) {
|
||||
return static_cast<__int128>(lhs) <= static_cast<__int128>(rhs);
|
||||
}
|
||||
|
||||
inline bool operator>=(int128 lhs, int128 rhs) {
|
||||
return static_cast<__int128>(lhs) >= static_cast<__int128>(rhs);
|
||||
}
|
||||
|
||||
// Unary operators.
|
||||
|
||||
inline int128 operator-(int128 v) {
|
||||
return -static_cast<__int128>(v);
|
||||
}
|
||||
|
||||
inline bool operator!(int128 v) {
|
||||
return !static_cast<__int128>(v);
|
||||
}
|
||||
|
||||
inline int128 operator~(int128 val) {
|
||||
return ~static_cast<__int128>(val);
|
||||
}
|
||||
|
||||
// Arithmetic operators.
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
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 operator<<(int128 lhs, int amount) {
|
||||
return static_cast<__int128>(lhs) << amount;
|
||||
}
|
||||
|
||||
inline int128 operator>>(int128 lhs, int amount) {
|
||||
return static_cast<__int128>(lhs) >> amount;
|
||||
}
|
||||
+308
@@ -0,0 +1,308 @@
|
||||
//
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Forward declaration for conversion operators to floating point types.
|
||||
int128 operator-(int128 v);
|
||||
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 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.
|
||||
|
||||
inline bool operator==(int128 lhs, int128 rhs) {
|
||||
return (Int128Low64(lhs) == Int128Low64(rhs) &&
|
||||
Int128High64(lhs) == Int128High64(rhs));
|
||||
}
|
||||
|
||||
inline bool operator!=(int128 lhs, int128 rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
inline bool operator<(int128 lhs, int128 rhs) {
|
||||
return (Int128High64(lhs) == Int128High64(rhs))
|
||||
? (Int128Low64(lhs) < Int128Low64(rhs))
|
||||
: (Int128High64(lhs) < Int128High64(rhs));
|
||||
}
|
||||
|
||||
inline bool operator>(int128 lhs, int128 rhs) {
|
||||
return (Int128High64(lhs) == Int128High64(rhs))
|
||||
? (Int128Low64(lhs) > Int128Low64(rhs))
|
||||
: (Int128High64(lhs) > Int128High64(rhs));
|
||||
}
|
||||
|
||||
inline bool operator<=(int128 lhs, int128 rhs) {
|
||||
return !(lhs > rhs);
|
||||
}
|
||||
|
||||
inline bool operator>=(int128 lhs, int128 rhs) {
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
// Unary operators.
|
||||
|
||||
inline int128 operator-(int128 v) {
|
||||
int64_t hi = ~Int128High64(v);
|
||||
uint64_t lo = ~Int128Low64(v) + 1;
|
||||
if (lo == 0) ++hi; // carry
|
||||
return MakeInt128(hi, lo);
|
||||
}
|
||||
|
||||
inline bool operator!(int128 v) {
|
||||
return !Int128Low64(v) && !Int128High64(v);
|
||||
}
|
||||
|
||||
inline int128 operator~(int128 val) {
|
||||
return MakeInt128(~Int128High64(val), ~Int128Low64(val));
|
||||
}
|
||||
|
||||
// Arithmetic operators.
|
||||
|
||||
inline int128 operator+(int128 lhs, int128 rhs) {
|
||||
int128 result = MakeInt128(Int128High64(lhs) + Int128High64(rhs),
|
||||
Int128Low64(lhs) + Int128Low64(rhs));
|
||||
if (Int128Low64(result) < Int128Low64(lhs)) { // check for carry
|
||||
return MakeInt128(Int128High64(result) + 1, Int128Low64(result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline int128 operator-(int128 lhs, int128 rhs) {
|
||||
int128 result = MakeInt128(Int128High64(lhs) - Int128High64(rhs),
|
||||
Int128Low64(lhs) - Int128Low64(rhs));
|
||||
if (Int128Low64(lhs) < Int128Low64(rhs)) { // check for carry
|
||||
return MakeInt128(Int128High64(result) - 1, Int128Low64(result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline int128 operator*(int128 lhs, int128 rhs) {
|
||||
uint128 result = uint128(lhs) * rhs;
|
||||
return MakeInt128(int128_internal::BitCastToSigned(Uint128High64(result)),
|
||||
Uint128Low64(result));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
inline int128 operator|(int128 lhs, int128 rhs) {
|
||||
return MakeInt128(Int128High64(lhs) | Int128High64(rhs),
|
||||
Int128Low64(lhs) | Int128Low64(rhs));
|
||||
}
|
||||
|
||||
inline int128 operator&(int128 lhs, int128 rhs) {
|
||||
return MakeInt128(Int128High64(lhs) & Int128High64(rhs),
|
||||
Int128Low64(lhs) & Int128Low64(rhs));
|
||||
}
|
||||
|
||||
inline int128 operator^(int128 lhs, int128 rhs) {
|
||||
return MakeInt128(Int128High64(lhs) ^ Int128High64(rhs),
|
||||
Int128Low64(lhs) ^ Int128Low64(rhs));
|
||||
}
|
||||
|
||||
inline int128 operator<<(int128 lhs, int amount) {
|
||||
// uint64_t shifts of >= 64 are undefined, so we need some special-casing.
|
||||
if (amount < 64) {
|
||||
if (amount != 0) {
|
||||
return MakeInt128(
|
||||
(Int128High64(lhs) << amount) |
|
||||
static_cast<int64_t>(Int128Low64(lhs) >> (64 - amount)),
|
||||
Int128Low64(lhs) << amount);
|
||||
}
|
||||
return lhs;
|
||||
}
|
||||
return MakeInt128(static_cast<int64_t>(Int128Low64(lhs) << (amount - 64)), 0);
|
||||
}
|
||||
|
||||
inline int128 operator>>(int128 lhs, int amount) {
|
||||
// uint64_t shifts of >= 64 are undefined, so we need some special-casing.
|
||||
if (amount < 64) {
|
||||
if (amount != 0) {
|
||||
return MakeInt128(
|
||||
Int128High64(lhs) >> amount,
|
||||
(Int128Low64(lhs) >> amount) |
|
||||
(static_cast<uint64_t>(Int128High64(lhs)) << (64 - amount)));
|
||||
}
|
||||
return lhs;
|
||||
}
|
||||
return MakeInt128(0,
|
||||
static_cast<uint64_t>(Int128High64(lhs) >> (amount - 64)));
|
||||
}
|
||||
Reference in New Issue
Block a user