Commit 2103fd9a by Abseil Team Committed by Gennadiy Civil

Export of internal Abseil changes

--
d447fdcb801036cf08197eece193a5a706661120 by Gennadiy Rozental <rogeeff@google.com>:

Eliminate the need for static function holding help message. This decreases the cost of ABSL_FLAG abstraction by 120 bytes under clang.

PiperOrigin-RevId: 281107806

--
0aa6b91189f0e8b2381438c33465673a7ae02487 by Derek Mauro <dmauro@google.com>:

Disable the weak symbol CCTZ extension in the time test_util
on MinGW, which does not support it.

PiperOrigin-RevId: 280719769

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

Tune PeriodicSampler implementation (for internal-use only)

PiperOrigin-RevId: 280708943

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

Clean up public headers not to have warnings for "-Wcomma"

PiperOrigin-RevId: 280695373

--
981acd1ef3b13a83a84f04f11c8931f4ed4451c9 by Matthew Brown <matthewbr@google.com>:

Release absl::int128.

PiperOrigin-RevId: 280690817

--
d30fae9d2ec30b81322d2eb5afe7e13e45b4b422 by Derek Mauro <dmauro@google.com>:

Fix -Wundef warnings in random platform detection

PiperOrigin-RevId: 280669598
GitOrigin-RevId: d447fdcb801036cf08197eece193a5a706661120
Change-Id: Ie5e10e567c54b7de211833607689f233d4ddf734
parent 3df7b52a
...@@ -50,10 +50,22 @@ namespace base_internal { ...@@ -50,10 +50,22 @@ namespace base_internal {
ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64Slow(uint64_t n) { ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64Slow(uint64_t n) {
int zeroes = 60; int zeroes = 60;
if (n >> 32) zeroes -= 32, n >>= 32; if (n >> 32) {
if (n >> 16) zeroes -= 16, n >>= 16; zeroes -= 32;
if (n >> 8) zeroes -= 8, n >>= 8; n >>= 32;
if (n >> 4) zeroes -= 4, n >>= 4; }
if (n >> 16) {
zeroes -= 16;
n >>= 16;
}
if (n >> 8) {
zeroes -= 8;
n >>= 8;
}
if (n >> 4) {
zeroes -= 4;
n >>= 4;
}
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes; return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
} }
...@@ -95,9 +107,18 @@ ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64(uint64_t n) { ...@@ -95,9 +107,18 @@ ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64(uint64_t n) {
ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros32Slow(uint64_t n) { ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros32Slow(uint64_t n) {
int zeroes = 28; int zeroes = 28;
if (n >> 16) zeroes -= 16, n >>= 16; if (n >> 16) {
if (n >> 8) zeroes -= 8, n >>= 8; zeroes -= 16;
if (n >> 4) zeroes -= 4, n >>= 4; n >>= 16;
}
if (n >> 8) {
zeroes -= 8;
n >>= 8;
}
if (n >> 4) {
zeroes -= 4;
n >>= 4;
}
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes; return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
} }
......
...@@ -36,13 +36,13 @@ bool PeriodicSamplerBase::SubtleConfirmSample() noexcept { ...@@ -36,13 +36,13 @@ bool PeriodicSamplerBase::SubtleConfirmSample() noexcept {
// Check if this is the first call to Sample() // Check if this is the first call to Sample()
if (ABSL_PREDICT_FALSE(stride_ == 1)) { if (ABSL_PREDICT_FALSE(stride_ == 1)) {
stride_ = -1 - GetExponentialBiased(current_period); stride_ = static_cast<uint64_t>(-1 - GetExponentialBiased(current_period));
if (stride_ < -1) { if (static_cast<int64_t>(stride_) < -1) {
++stride_; ++stride_;
return false; return false;
} }
} }
stride_ = -1 - GetExponentialBiased(current_period); stride_ = static_cast<uint64_t>(-1 - GetExponentialBiased(current_period));
return true; return true;
} }
......
...@@ -111,33 +111,49 @@ class PeriodicSamplerBase { ...@@ -111,33 +111,49 @@ class PeriodicSamplerBase {
// Returns the current period of this sampler. Thread-safe. // Returns the current period of this sampler. Thread-safe.
virtual int period() const noexcept = 0; virtual int period() const noexcept = 0;
int64_t stride_ = 0; // Keep and decrement stride_ as an unsigned integer, but compare the value
// to zero casted as a signed int. clang and msvc do not create optimum code
// if we use signed for the combined decrement and sign comparison.
//
// Below 3 alternative options, all compiles generate the best code
// using the unsigned increment <---> signed int comparison option.
//
// Option 1:
// int64_t stride_;
// if (ABSL_PREDICT_TRUE(++stride_ < 0)) { ... }
//
// GCC x64 (OK) : https://gcc.godbolt.org/z/R5MzzA
// GCC ppc (OK) : https://gcc.godbolt.org/z/z7NZAt
// Clang x64 (BAD): https://gcc.godbolt.org/z/t4gPsd
// ICC x64 (OK) : https://gcc.godbolt.org/z/rE6s8W
// MSVC x64 (OK) : https://gcc.godbolt.org/z/ARMXqS
//
// Option 2:
// int64_t stride_ = 0;
// if (ABSL_PREDICT_TRUE(--stride_ >= 0)) { ... }
//
// GCC x64 (OK) : https://gcc.godbolt.org/z/jSQxYK
// GCC ppc (OK) : https://gcc.godbolt.org/z/VJdYaA
// Clang x64 (BAD): https://gcc.godbolt.org/z/Xm4NjX
// ICC x64 (OK) : https://gcc.godbolt.org/z/4snaFd
// MSVC x64 (BAD): https://gcc.godbolt.org/z/BgnEKE
//
// Option 3:
// uint64_t stride_;
// if (ABSL_PREDICT_TRUE(static_cast<int64_t>(++stride_) < 0)) { ... }
//
// GCC x64 (OK) : https://gcc.godbolt.org/z/bFbfPy
// GCC ppc (OK) : https://gcc.godbolt.org/z/S9KkUE
// Clang x64 (OK) : https://gcc.godbolt.org/z/UYzRb4
// ICC x64 (OK) : https://gcc.godbolt.org/z/ptTNfD
// MSVC x64 (OK) : https://gcc.godbolt.org/z/76j4-5
uint64_t stride_ = 0;
ExponentialBiased rng_; ExponentialBiased rng_;
}; };
inline bool PeriodicSamplerBase::SubtleMaybeSample() noexcept { inline bool PeriodicSamplerBase::SubtleMaybeSample() noexcept {
// We explicitly count up and not down, as the compiler does not generate // See comments on `stride_` for the unsigned increment / signed compare.
// ideal code for counting down. See also https://gcc.godbolt.org/z/FTPDSM if (ABSL_PREDICT_TRUE(static_cast<int64_t>(++stride_) < 0)) {
//
// With `if (ABSL_PREDICT_FALSE(++stride_ < 0))`
// add QWORD PTR fs:sampler@tpoff+8, 1
// jns .L15
// ret
//
// With `if (ABSL_PREDICT_FALSE(--stride_ > 0))`
// mov rax, QWORD PTR fs:sampler@tpoff+8
// sub rax, 1
// mov QWORD PTR fs:sampler@tpoff+8, rax
// test rax, rax
// jle .L15
// ret
// add QWORD PTR fs:sampler@tpoff+8, 1
// jns .L15
// ret
//
// --stride >= 0 does work, but makes our logic slightly harder as in that
// case we have less convenient zero-init and over-run values.
if (ABSL_PREDICT_FALSE(++stride_ < 0)) {
return false; return false;
} }
return true; return true;
......
...@@ -96,8 +96,7 @@ class Flag { ...@@ -96,8 +96,7 @@ class Flag {
constexpr Flag(const char* name, const flags_internal::HelpGenFunc help_gen, constexpr Flag(const char* name, const flags_internal::HelpGenFunc help_gen,
const char* filename, const char* filename,
const flags_internal::FlagMarshallingOpFn marshalling_op, const flags_internal::FlagMarshallingOpFn marshalling_op,
const flags_internal::InitialValGenFunc initial_value_gen, const flags_internal::InitialValGenFunc initial_value_gen)
bool, void*)
: name_(name), : name_(name),
help_gen_(help_gen), help_gen_(help_gen),
filename_(filename), filename_(filename),
...@@ -115,8 +114,11 @@ class Flag { ...@@ -115,8 +114,11 @@ class Flag {
return impl_; return impl_;
} }
impl_ = new flags_internal::Flag<T>(name_, help_gen_, filename_, impl_ = new flags_internal::Flag<T>(
marshalling_op_, initial_value_gen_); name_,
{flags_internal::FlagHelpSrc(help_gen_),
flags_internal::FlagHelpSrcKind::kGenFunc},
filename_, marshalling_op_, initial_value_gen_);
inited_.store(true, std::memory_order_release); inited_.store(true, std::memory_order_release);
} }
...@@ -307,9 +309,19 @@ void SetFlag(absl::Flag<T>* flag, const V& v) { ...@@ -307,9 +309,19 @@ void SetFlag(absl::Flag<T>* flag, const V& v) {
#define ABSL_FLAG_IMPL_FLAGHELP(txt) txt #define ABSL_FLAG_IMPL_FLAGHELP(txt) txt
#endif #endif
#define ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, txt) \ // AbslFlagHelpGenFor##name is used to encapsulate both immediate (method Const)
static std::string AbslFlagsWrapHelp##name() { \ // and lazy (method NonConst) evaluation of help message expression. We choose
return ABSL_FLAG_IMPL_FLAGHELP(txt); \ // between the two via the call to HelpArg in absl::Flag instantiation below.
// If help message expression is constexpr evaluable compiler will optimize
// away this whole struct.
#define ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, txt) \
struct AbslFlagHelpGenFor##name { \
template <typename T = void> \
static constexpr const char* Const() { \
return absl::flags_internal::HelpConstexprWrap( \
ABSL_FLAG_IMPL_FLAGHELP(txt)); \
} \
static std::string NonConst() { return ABSL_FLAG_IMPL_FLAGHELP(txt); } \
} }
#define ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ #define ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
...@@ -326,29 +338,28 @@ void SetFlag(absl::Flag<T>* flag, const V& v) { ...@@ -326,29 +338,28 @@ void SetFlag(absl::Flag<T>* flag, const V& v) {
#define ABSL_FLAG_IMPL(Type, name, default_value, help) \ #define ABSL_FLAG_IMPL(Type, name, default_value, help) \
namespace absl /* block flags in namespaces */ {} \ namespace absl /* block flags in namespaces */ {} \
ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help) \ ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \
ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \ ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
ABSL_FLAG_IMPL_FLAGNAME(#name), &AbslFlagsWrapHelp##name, \ ABSL_FLAG_IMPL_FLAGNAME(#name), \
absl::flags_internal::HelpArg<AbslFlagHelpGenFor##name>(0), \
ABSL_FLAG_IMPL_FILENAME(), \ ABSL_FLAG_IMPL_FILENAME(), \
&absl::flags_internal::FlagMarshallingOps<Type>, \ &absl::flags_internal::FlagMarshallingOps<Type>, \
&AbslFlagsInitFlag##name}; \ &AbslFlagsInitFlag##name}; \
extern bool FLAGS_no##name; \ extern bool FLAGS_no##name; \
bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name) bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
#else #else
// MSVC version uses aggregate initialization. // MSVC version uses aggregate initialization. We also do not try to
#define ABSL_FLAG_IMPL(Type, name, default_value, help) \ // optimize away help wrapper.
namespace absl /* block flags in namespaces */ {} \ #define ABSL_FLAG_IMPL(Type, name, default_value, help) \
ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ namespace absl /* block flags in namespaces */ {} \
ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help) \ ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \ ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \
ABSL_FLAG_IMPL_FLAGNAME(#name), \ ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
&AbslFlagsWrapHelp##name, \ ABSL_FLAG_IMPL_FLAGNAME(#name), &AbslFlagHelpGenFor##name::NonConst, \
ABSL_FLAG_IMPL_FILENAME(), \ ABSL_FLAG_IMPL_FILENAME(), \
&absl::flags_internal::FlagMarshallingOps<Type>, \ &absl::flags_internal::FlagMarshallingOps<Type>, \
&AbslFlagsInitFlag##name, \ &AbslFlagsInitFlag##name}; \
false, \ extern bool FLAGS_no##name; \
nullptr}; \
extern bool FLAGS_no##name; \
bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name) bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
#endif #endif
......
...@@ -32,7 +32,7 @@ namespace { ...@@ -32,7 +32,7 @@ namespace {
namespace flags = absl::flags_internal; namespace flags = absl::flags_internal;
std::string TestHelpMsg() { return "help"; } std::string TestHelpMsg() { return "dynamic help"; }
template <typename T> template <typename T>
void* TestMakeDflt() { void* TestMakeDflt() {
return new T{}; return new T{};
...@@ -41,19 +41,22 @@ void TestCallback() {} ...@@ -41,19 +41,22 @@ void TestCallback() {}
template <typename T> template <typename T>
bool TestConstructionFor() { bool TestConstructionFor() {
constexpr flags::Flag<T> f1("f1", &TestHelpMsg, "file", constexpr flags::HelpInitArg help_arg{flags::FlagHelpSrc("literal help"),
flags::FlagHelpSrcKind::kLiteral};
constexpr flags::Flag<T> f1("f1", help_arg, "file",
&flags::FlagMarshallingOps<T>, &TestMakeDflt<T>); &flags::FlagMarshallingOps<T>, &TestMakeDflt<T>);
EXPECT_EQ(f1.Name(), "f1"); EXPECT_EQ(f1.Name(), "f1");
EXPECT_EQ(f1.Help(), "help"); EXPECT_EQ(f1.Help(), "literal help");
EXPECT_EQ(f1.Filename(), "file"); EXPECT_EQ(f1.Filename(), "file");
ABSL_CONST_INIT static flags::Flag<T> f2("f2", &TestHelpMsg, "file", ABSL_CONST_INIT static flags::Flag<T> f2(
&flags::FlagMarshallingOps<T>, "f2",
&TestMakeDflt<T>); {flags::FlagHelpSrc(&TestHelpMsg), flags::FlagHelpSrcKind::kGenFunc},
"file", &flags::FlagMarshallingOps<T>, &TestMakeDflt<T>);
flags::FlagRegistrar<T, false>(&f2).OnUpdate(TestCallback); flags::FlagRegistrar<T, false>(&f2).OnUpdate(TestCallback);
EXPECT_EQ(f2.Name(), "f2"); EXPECT_EQ(f2.Name(), "f2");
EXPECT_EQ(f2.Help(), "help"); EXPECT_EQ(f2.Help(), "dynamic help");
EXPECT_EQ(f2.Filename(), "file"); EXPECT_EQ(f2.Filename(), "file");
return true; return true;
......
...@@ -56,12 +56,5 @@ std::string CommandLineFlag::Filename() const { ...@@ -56,12 +56,5 @@ std::string CommandLineFlag::Filename() const {
return flags_internal::GetUsageConfig().normalize_filename(filename_); return flags_internal::GetUsageConfig().normalize_filename(filename_);
} }
std::string HelpText::GetHelpText() const {
if (help_function_) return help_function_();
if (help_message_) return help_message_;
return {};
}
} // namespace flags_internal } // namespace flags_internal
} // namespace absl } // namespace absl
...@@ -60,14 +60,6 @@ enum ValueSource { ...@@ -60,14 +60,6 @@ enum ValueSource {
kProgrammaticChange, kProgrammaticChange,
}; };
// Signature for the help generation function used as an argument for the
// absl::Flag constructor.
using HelpGenFunc = std::string (*)();
// Signature for the function generating the initial flag value based (usually
// based on default value supplied in flag's definition)
using InitialValGenFunc = void* (*)();
extern const char kStrippedFlagHelp[]; extern const char kStrippedFlagHelp[];
// The per-type function // The per-type function
...@@ -149,34 +141,6 @@ inline size_t Sizeof(FlagOpFn op) { ...@@ -149,34 +141,6 @@ inline size_t Sizeof(FlagOpFn op) {
op(flags_internal::kSizeof, nullptr, nullptr))); op(flags_internal::kSizeof, nullptr, nullptr)));
} }
// Holds either a pointer to help text or a function which produces it. This is
// needed for supporting both static initialization of Flags while supporting
// the legacy registration framework. We can't use absl::variant<const char*,
// const char*(*)()> since anybody passing 0 or nullptr in to a CommandLineFlag
// would find an ambiguity.
class HelpText {
public:
static constexpr HelpText FromFunctionPointer(const HelpGenFunc fn) {
return HelpText(fn, nullptr);
}
static constexpr HelpText FromStaticCString(const char* msg) {
return HelpText(nullptr, msg);
}
std::string GetHelpText() const;
HelpText() = delete;
HelpText(const HelpText&) = default;
HelpText(HelpText&&) = default;
private:
explicit constexpr HelpText(const HelpGenFunc fn, const char* msg)
: help_function_(fn), help_message_(msg) {}
HelpGenFunc help_function_;
const char* help_message_;
};
// Handle to FlagState objects. Specific flag state objects will restore state // Handle to FlagState objects. Specific flag state objects will restore state
// of a flag produced this flag state from method CommandLineFlag::SaveState(). // of a flag produced this flag state from method CommandLineFlag::SaveState().
class FlagStateInterface { class FlagStateInterface {
...@@ -190,9 +154,8 @@ class FlagStateInterface { ...@@ -190,9 +154,8 @@ class FlagStateInterface {
// Holds all information for a flag. // Holds all information for a flag.
class CommandLineFlag { class CommandLineFlag {
public: public:
constexpr CommandLineFlag(const char* name, HelpText help_text, constexpr CommandLineFlag(const char* name, const char* filename)
const char* filename) : name_(name), filename_(filename) {}
: name_(name), help_(help_text), filename_(filename) {}
// Virtual destructor // Virtual destructor
virtual void Destroy() const = 0; virtual void Destroy() const = 0;
...@@ -203,7 +166,6 @@ class CommandLineFlag { ...@@ -203,7 +166,6 @@ class CommandLineFlag {
// Non-polymorphic access methods. // Non-polymorphic access methods.
absl::string_view Name() const { return name_; } absl::string_view Name() const { return name_; }
std::string Help() const { return help_.GetHelpText(); }
absl::string_view Typename() const; absl::string_view Typename() const;
std::string Filename() const; std::string Filename() const;
...@@ -250,6 +212,8 @@ class CommandLineFlag { ...@@ -250,6 +212,8 @@ class CommandLineFlag {
// Polymorphic access methods // Polymorphic access methods
// Returns help message associated with this flag
virtual std::string Help() const = 0;
// Returns true iff this object corresponds to retired flag // Returns true iff this object corresponds to retired flag
virtual bool IsRetired() const { return false; } virtual bool IsRetired() const { return false; }
// Returns true iff this is a handle to an Abseil Flag. // Returns true iff this is a handle to an Abseil Flag.
...@@ -285,12 +249,11 @@ class CommandLineFlag { ...@@ -285,12 +249,11 @@ class CommandLineFlag {
// flag's value type. // flag's value type.
virtual void CheckDefaultValueParsingRoundtrip() const = 0; virtual void CheckDefaultValueParsingRoundtrip() const = 0;
// Constant configuration for a particular flag.
protected: protected:
~CommandLineFlag() = default; ~CommandLineFlag() = default;
// Constant configuration for a particular flag.
const char* const name_; // Flags name passed to ABSL_FLAG as second arg. const char* const name_; // Flags name passed to ABSL_FLAG as second arg.
const HelpText help_; // The function generating help message.
const char* const filename_; // The file name where ABSL_FLAG resides. const char* const filename_; // The file name where ABSL_FLAG resides.
private: private:
......
...@@ -85,6 +85,11 @@ void FlagImpl::Destroy() const { ...@@ -85,6 +85,11 @@ void FlagImpl::Destroy() const {
delete locks_; delete locks_;
} }
std::string FlagImpl::Help() const {
return help_source_kind_ == FlagHelpSrcKind::kLiteral ? help_.literal
: help_.gen_func();
}
bool FlagImpl::IsModified() const { bool FlagImpl::IsModified() const {
absl::MutexLock l(DataGuard()); absl::MutexLock l(DataGuard());
return modified_; return modified_;
......
...@@ -61,6 +61,68 @@ class FlagState : public flags_internal::FlagStateInterface { ...@@ -61,6 +61,68 @@ class FlagState : public flags_internal::FlagStateInterface {
int64_t counter_; int64_t counter_;
}; };
// This is help argument for absl::Flag encapsulating the string literal pointer
// or pointer to function generating it as well as enum descriminating two
// cases.
using HelpGenFunc = std::string (*)();
union FlagHelpSrc {
constexpr explicit FlagHelpSrc(const char* help_msg) : literal(help_msg) {}
constexpr explicit FlagHelpSrc(HelpGenFunc help_gen) : gen_func(help_gen) {}
const char* literal;
HelpGenFunc gen_func;
};
enum class FlagHelpSrcKind : int8_t { kLiteral, kGenFunc };
struct HelpInitArg {
FlagHelpSrc source;
FlagHelpSrcKind kind;
};
// HelpConstexprWrap is used by struct AbslFlagHelpGenFor##name generated by
// ABSL_FLAG macro. It is only used to silence the compiler in the case where
// help message expression is not constexpr and does not have type const char*.
// If help message expression is indeed constexpr const char* HelpConstexprWrap
// is just a trivial identity function.
template <typename T>
const char* HelpConstexprWrap(const T&) {
return nullptr;
}
constexpr const char* HelpConstexprWrap(const char* p) { return p; }
constexpr const char* HelpConstexprWrap(char* p) { return p; }
// These two HelpArg overloads allows us to select at compile time one of two
// way to pass Help argument to absl::Flag. We'll be passing
// AbslFlagHelpGenFor##name as T and integer 0 as a single argument to prefer
// first overload if possible. If T::Const is evaluatable on constexpr
// context (see non template int parameter below) we'll choose first overload.
// In this case the help message expression is immediately evaluated and is used
// to construct the absl::Flag. No additionl code is generated by ABSL_FLAG.
// Otherwise SFINAE kicks in and first overload is dropped from the
// consideration, in which case the second overload will be used. The second
// overload does not attempt to evaluate the help message expression
// immediately and instead delays the evaluation by returing the function
// pointer (&T::NonConst) genering the help message when necessary. This is
// evaluatable in constexpr context, but the cost is an extra function being
// generated in the ABSL_FLAG code.
template <typename T, int = (T::Const(), 1)>
constexpr flags_internal::HelpInitArg HelpArg(int) {
return {flags_internal::FlagHelpSrc(T::Const()),
flags_internal::FlagHelpSrcKind::kLiteral};
}
template <typename T>
constexpr flags_internal::HelpInitArg HelpArg(char) {
return {flags_internal::FlagHelpSrc(&T::NonConst),
flags_internal::FlagHelpSrcKind::kGenFunc};
}
// Signature for the function generating the initial flag value based (usually
// based on default value supplied in flag's definition)
using InitialValGenFunc = void* (*)();
// Signature for the mutation callback used by watched Flags // Signature for the mutation callback used by watched Flags
// The callback is noexcept. // The callback is noexcept.
// TODO(rogeeff): add noexcept after C++17 support is added. // TODO(rogeeff): add noexcept after C++17 support is added.
...@@ -74,15 +136,19 @@ class FlagImpl { ...@@ -74,15 +136,19 @@ class FlagImpl {
public: public:
constexpr FlagImpl(const flags_internal::FlagOpFn op, constexpr FlagImpl(const flags_internal::FlagOpFn op,
const flags_internal::FlagMarshallingOpFn marshalling_op, const flags_internal::FlagMarshallingOpFn marshalling_op,
const flags_internal::InitialValGenFunc initial_value_gen) const flags_internal::InitialValGenFunc initial_value_gen,
const HelpInitArg help)
: op_(op), : op_(op),
marshalling_op_(marshalling_op), marshalling_op_(marshalling_op),
initial_value_gen_(initial_value_gen) {} initial_value_gen_(initial_value_gen),
help_(help.source),
help_source_kind_(help.kind) {}
// Forces destruction of the Flag's data. // Forces destruction of the Flag's data.
void Destroy() const; void Destroy() const;
// Constant access methods // Constant access methods
std::string Help() const;
bool IsModified() const ABSL_LOCKS_EXCLUDED(locks_->primary_mu); bool IsModified() const ABSL_LOCKS_EXCLUDED(locks_->primary_mu);
bool IsSpecifiedOnCommandLine() const ABSL_LOCKS_EXCLUDED(locks_->primary_mu); bool IsSpecifiedOnCommandLine() const ABSL_LOCKS_EXCLUDED(locks_->primary_mu);
std::string DefaultValue() const ABSL_LOCKS_EXCLUDED(locks_->primary_mu); std::string DefaultValue() const ABSL_LOCKS_EXCLUDED(locks_->primary_mu);
...@@ -149,9 +215,12 @@ class FlagImpl { ...@@ -149,9 +215,12 @@ class FlagImpl {
absl::Mutex* DataGuard() const ABSL_LOCK_RETURNED(locks_->primary_mu); absl::Mutex* DataGuard() const ABSL_LOCK_RETURNED(locks_->primary_mu);
// Immutable Flag's data. // Immutable Flag's data.
const FlagOpFn op_; // Type-specific handler const FlagOpFn op_; // Type-specific handler.
const FlagMarshallingOpFn marshalling_op_; // Marshalling ops handler const FlagMarshallingOpFn marshalling_op_; // Marshalling ops handler.
const InitialValGenFunc initial_value_gen_; // Makes flag's initial value const InitialValGenFunc initial_value_gen_; // Makes flag's initial value.
const FlagHelpSrc help_; // Help message literal or function to generate it.
// Indicates if help message was supplied as literal or generator func.
const FlagHelpSrcKind help_source_kind_;
// Mutable Flag's data. (guarded by locks_->primary_mu). // Mutable Flag's data. (guarded by locks_->primary_mu).
// Indicates that locks_, cur_ and def_ fields have been lazily initialized. // Indicates that locks_, cur_ and def_ fields have been lazily initialized.
...@@ -191,14 +260,13 @@ class FlagImpl { ...@@ -191,14 +260,13 @@ class FlagImpl {
template <typename T> template <typename T>
class Flag final : public flags_internal::CommandLineFlag { class Flag final : public flags_internal::CommandLineFlag {
public: public:
constexpr Flag(const char* name, const flags_internal::HelpGenFunc help_gen, constexpr Flag(const char* name, const flags_internal::HelpInitArg help,
const char* filename, const char* filename,
const flags_internal::FlagMarshallingOpFn marshalling_op, const flags_internal::FlagMarshallingOpFn marshalling_op,
const flags_internal::InitialValGenFunc initial_value_gen) const flags_internal::InitialValGenFunc initial_value_gen)
: flags_internal::CommandLineFlag( : flags_internal::CommandLineFlag(name, filename),
name, flags_internal::HelpText::FromFunctionPointer(help_gen), impl_(&flags_internal::FlagOps<T>, marshalling_op, initial_value_gen,
filename), help) {}
impl_(&flags_internal::FlagOps<T>, marshalling_op, initial_value_gen) {}
T Get() const { T Get() const {
// See implementation notes in CommandLineFlag::Get(). // See implementation notes in CommandLineFlag::Get().
...@@ -222,6 +290,7 @@ class Flag final : public flags_internal::CommandLineFlag { ...@@ -222,6 +290,7 @@ class Flag final : public flags_internal::CommandLineFlag {
} }
// CommandLineFlag interface // CommandLineFlag interface
std::string Help() const override { return impl_.Help(); }
bool IsModified() const override { return impl_.IsModified(); } bool IsModified() const override { return impl_.IsModified(); }
bool IsSpecifiedOnCommandLine() const override { bool IsSpecifiedOnCommandLine() const override {
return impl_.IsSpecifiedOnCommandLine(); return impl_.IsSpecifiedOnCommandLine();
......
...@@ -276,9 +276,8 @@ namespace { ...@@ -276,9 +276,8 @@ namespace {
class RetiredFlagObj final : public flags_internal::CommandLineFlag { class RetiredFlagObj final : public flags_internal::CommandLineFlag {
public: public:
constexpr RetiredFlagObj(const char* name, FlagOpFn ops) constexpr RetiredFlagObj(const char* name, FlagOpFn ops)
: flags_internal::CommandLineFlag( : flags_internal::CommandLineFlag(name,
name, flags_internal::HelpText::FromStaticCString(nullptr), /*filename=*/"RETIRED"),
/*filename=*/"RETIRED"),
op_(ops) {} op_(ops) {}
private: private:
...@@ -288,6 +287,7 @@ class RetiredFlagObj final : public flags_internal::CommandLineFlag { ...@@ -288,6 +287,7 @@ class RetiredFlagObj final : public flags_internal::CommandLineFlag {
} }
flags_internal::FlagOpFn TypeId() const override { return op_; } flags_internal::FlagOpFn TypeId() const override { return op_; }
std::string Help() const override { return ""; }
bool IsRetired() const override { return true; } bool IsRetired() const override { return true; }
bool IsModified() const override { return false; } bool IsModified() const override { return false; }
bool IsSpecifiedOnCommandLine() const override { return false; } bool IsSpecifiedOnCommandLine() const override { return false; }
......
...@@ -244,6 +244,111 @@ std::ostream& operator<<(std::ostream& os, uint128 v) { ...@@ -244,6 +244,111 @@ std::ostream& operator<<(std::ostream& os, uint128 v) {
return os << rep; 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;
}
} // namespace absl } // namespace absl
namespace std { namespace std {
...@@ -270,4 +375,28 @@ constexpr int numeric_limits<absl::uint128>::max_exponent; ...@@ -270,4 +375,28 @@ constexpr int numeric_limits<absl::uint128>::max_exponent;
constexpr int numeric_limits<absl::uint128>::max_exponent10; constexpr int numeric_limits<absl::uint128>::max_exponent10;
constexpr bool numeric_limits<absl::uint128>::traps; constexpr bool numeric_limits<absl::uint128>::traps;
constexpr bool numeric_limits<absl::uint128>::tinyness_before; 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 } // namespace std
...@@ -16,3 +16,287 @@ ...@@ -16,3 +16,287 @@
// This file contains :int128 implementation details that depend on internal // This file contains :int128 implementation details that depend on internal
// representation when ABSL_HAVE_INTRINSIC_INT128 is defined. This file is // representation when ABSL_HAVE_INTRINSIC_INT128 is defined. This file is
// included by int128.h and relies on ABSL_INTERNAL_WCHAR_T being defined. // 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;
}
...@@ -16,3 +16,293 @@ ...@@ -16,3 +16,293 @@
// This file contains :int128 implementation details that depend on internal // This file contains :int128 implementation details that depend on internal
// representation when ABSL_HAVE_INTRINSIC_INT128 is *not* defined. This file // 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. // 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)));
}
...@@ -162,7 +162,8 @@ ...@@ -162,7 +162,8 @@
// iOS does not support dispatch, even on x86, since applications // iOS does not support dispatch, even on x86, since applications
// should be bundled as fat binaries, with a different build tailored for // should be bundled as fat binaries, with a different build tailored for
// each specific supported platform/architecture. // each specific supported platform/architecture.
#if defined(__APPLE__) && (TARGET_OS_IPHONE || TARGET_OS_IPHONE_SIMULATOR) #if (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) || \
(defined(TARGET_OS_IPHONE_SIMULATOR) && TARGET_OS_IPHONE_SIMULATOR)
#undef ABSL_RANDOM_INTERNAL_AES_DISPATCH #undef ABSL_RANDOM_INTERNAL_AES_DISPATCH
#define ABSL_RANDOM_INTERNAL_AES_DISPATCH 0 #define ABSL_RANDOM_INTERNAL_AES_DISPATCH 0
#endif #endif
......
...@@ -32,6 +32,10 @@ void ReducePadding(size_t n, size_t *capacity) { ...@@ -32,6 +32,10 @@ void ReducePadding(size_t n, size_t *capacity) {
template <typename T> template <typename T>
struct MakeUnsigned : std::make_unsigned<T> {}; struct MakeUnsigned : std::make_unsigned<T> {};
template <> template <>
struct MakeUnsigned<absl::int128> {
using type = absl::uint128;
};
template <>
struct MakeUnsigned<absl::uint128> { struct MakeUnsigned<absl::uint128> {
using type = absl::uint128; using type = absl::uint128;
}; };
...@@ -39,6 +43,8 @@ struct MakeUnsigned<absl::uint128> { ...@@ -39,6 +43,8 @@ struct MakeUnsigned<absl::uint128> {
template <typename T> template <typename T>
struct IsSigned : std::is_signed<T> {}; struct IsSigned : std::is_signed<T> {};
template <> template <>
struct IsSigned<absl::int128> : std::true_type {};
template <>
struct IsSigned<absl::uint128> : std::false_type {}; struct IsSigned<absl::uint128> : std::false_type {};
class ConvertedIntInfo { class ConvertedIntInfo {
...@@ -363,6 +369,11 @@ IntegralConvertResult FormatConvertImpl(unsigned long long v, // NOLINT ...@@ -363,6 +369,11 @@ IntegralConvertResult FormatConvertImpl(unsigned long long v, // NOLINT
FormatSinkImpl *sink) { FormatSinkImpl *sink) {
return {ConvertIntArg(v, conv, sink)}; return {ConvertIntArg(v, conv, sink)};
} }
IntegralConvertResult FormatConvertImpl(absl::int128 v,
const ConversionSpec conv,
FormatSinkImpl *sink) {
return {ConvertIntArg(v, conv, sink)};
}
IntegralConvertResult FormatConvertImpl(absl::uint128 v, IntegralConvertResult FormatConvertImpl(absl::uint128 v,
const ConversionSpec conv, const ConversionSpec conv,
FormatSinkImpl *sink) { FormatSinkImpl *sink) {
...@@ -372,6 +383,7 @@ IntegralConvertResult FormatConvertImpl(absl::uint128 v, ...@@ -372,6 +383,7 @@ IntegralConvertResult FormatConvertImpl(absl::uint128 v,
ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(); ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_();
} // namespace str_format_internal } // namespace str_format_internal
} // namespace absl } // namespace absl
...@@ -144,6 +144,8 @@ IntegralConvertResult FormatConvertImpl(long long v, // NOLINT ...@@ -144,6 +144,8 @@ IntegralConvertResult FormatConvertImpl(long long v, // NOLINT
IntegralConvertResult FormatConvertImpl(unsigned long long v, // NOLINT IntegralConvertResult FormatConvertImpl(unsigned long long v, // NOLINT
ConversionSpec conv, ConversionSpec conv,
FormatSinkImpl* sink); FormatSinkImpl* sink);
IntegralConvertResult FormatConvertImpl(int128 v, ConversionSpec conv,
FormatSinkImpl* sink);
IntegralConvertResult FormatConvertImpl(uint128 v, ConversionSpec conv, IntegralConvertResult FormatConvertImpl(uint128 v, ConversionSpec conv,
FormatSinkImpl* sink); FormatSinkImpl* sink);
template <typename T, enable_if_t<std::is_same<T, bool>::value, int> = 0> template <typename T, enable_if_t<std::is_same<T, bool>::value, int> = 0>
...@@ -408,6 +410,7 @@ class FormatArgImpl { ...@@ -408,6 +410,7 @@ class FormatArgImpl {
__VA_ARGS__); \ __VA_ARGS__); \
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned long long, /* NOLINT */ \ ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned long long, /* NOLINT */ \
__VA_ARGS__); \ __VA_ARGS__); \
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(int128, __VA_ARGS__); \
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(uint128, __VA_ARGS__); \ ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(uint128, __VA_ARGS__); \
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(float, __VA_ARGS__); \ ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(float, __VA_ARGS__); \
ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(double, __VA_ARGS__); \ ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(double, __VA_ARGS__); \
...@@ -418,6 +421,7 @@ class FormatArgImpl { ...@@ -418,6 +421,7 @@ class FormatArgImpl {
ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(extern); ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(extern);
} // namespace str_format_internal } // namespace str_format_internal
} // namespace absl } // namespace absl
......
...@@ -390,7 +390,6 @@ typedef ::testing::Types< ...@@ -390,7 +390,6 @@ typedef ::testing::Types<
AllIntTypes; AllIntTypes;
INSTANTIATE_TYPED_TEST_CASE_P(TypedFormatConvertTestWithAllIntTypes, INSTANTIATE_TYPED_TEST_CASE_P(TypedFormatConvertTestWithAllIntTypes,
TypedFormatConvertTest, AllIntTypes); TypedFormatConvertTest, AllIntTypes);
TEST_F(FormatConvertTest, VectorBool) { TEST_F(FormatConvertTest, VectorBool) {
// Make sure vector<bool>'s values behave as bools. // Make sure vector<bool>'s values behave as bools.
std::vector<bool> v = {true, false}; std::vector<bool> v = {true, false};
...@@ -402,6 +401,42 @@ TEST_F(FormatConvertTest, VectorBool) { ...@@ -402,6 +401,42 @@ TEST_F(FormatConvertTest, VectorBool) {
FormatArgImpl(cv[0]), FormatArgImpl(cv[1])}))); FormatArgImpl(cv[0]), FormatArgImpl(cv[1])})));
} }
TEST_F(FormatConvertTest, Int128) {
absl::int128 positive = static_cast<absl::int128>(0x1234567890abcdef) * 1979;
absl::int128 negative = -positive;
absl::int128 max = absl::Int128Max(), min = absl::Int128Min();
const FormatArgImpl args[] = {FormatArgImpl(positive),
FormatArgImpl(negative), FormatArgImpl(max),
FormatArgImpl(min)};
struct Case {
const char* format;
const char* expected;
} cases[] = {
{"%1$d", "2595989796776606496405"},
{"%1$30d", " 2595989796776606496405"},
{"%1$-30d", "2595989796776606496405 "},
{"%1$u", "2595989796776606496405"},
{"%1$x", "8cba9876066020f695"},
{"%2$d", "-2595989796776606496405"},
{"%2$30d", " -2595989796776606496405"},
{"%2$-30d", "-2595989796776606496405 "},
{"%2$u", "340282366920938460867384810655161715051"},
{"%2$x", "ffffffffffffff73456789f99fdf096b"},
{"%3$d", "170141183460469231731687303715884105727"},
{"%3$u", "170141183460469231731687303715884105727"},
{"%3$x", "7fffffffffffffffffffffffffffffff"},
{"%4$d", "-170141183460469231731687303715884105728"},
{"%4$x", "80000000000000000000000000000000"},
};
for (auto c : cases) {
UntypedFormatSpecImpl format(c.format);
EXPECT_EQ(c.expected, FormatPack(format, absl::MakeSpan(args)));
}
}
TEST_F(FormatConvertTest, Uint128) { TEST_F(FormatConvertTest, Uint128) {
absl::uint128 v = static_cast<absl::uint128>(0x1234567890abcdef) * 1979; absl::uint128 v = static_cast<absl::uint128>(0x1234567890abcdef) * 1979;
absl::uint128 max = absl::Uint128Max(); absl::uint128 max = absl::Uint128Max();
......
...@@ -290,7 +290,8 @@ class string_view { ...@@ -290,7 +290,8 @@ class string_view {
constexpr const_reference at(size_type i) const { constexpr const_reference at(size_type i) const {
return ABSL_PREDICT_TRUE(i < size()) return ABSL_PREDICT_TRUE(i < size())
? ptr_[i] ? ptr_[i]
: (base_internal::ThrowStdOutOfRange("absl::string_view::at"), : ((void)base_internal::ThrowStdOutOfRange(
"absl::string_view::at"),
ptr_[i]); ptr_[i]);
} }
...@@ -511,7 +512,7 @@ class string_view { ...@@ -511,7 +512,7 @@ class string_view {
(std::numeric_limits<difference_type>::max)(); (std::numeric_limits<difference_type>::max)();
static constexpr size_type CheckLengthInternal(size_type len) { static constexpr size_type CheckLengthInternal(size_type len) {
return ABSL_ASSERT(len <= kMaxSize), len; return (void)ABSL_ASSERT(len <= kMaxSize), len;
} }
static constexpr size_type StrlenInternal(const char* str) { static constexpr size_type StrlenInternal(const char* str) {
......
...@@ -116,7 +116,10 @@ std::unique_ptr<cctz::ZoneInfoSource> TestFactory( ...@@ -116,7 +116,10 @@ std::unique_ptr<cctz::ZoneInfoSource> TestFactory(
} // namespace } // namespace
#if !defined(__MINGW32__)
// MinGW does not support the weak symbol extension mechanism.
ZoneInfoSourceFactory zone_info_source_factory = TestFactory; ZoneInfoSourceFactory zone_info_source_factory = TestFactory;
#endif
} // namespace cctz_extension } // namespace cctz_extension
} // namespace time_internal } // namespace time_internal
......
...@@ -1412,8 +1412,7 @@ constexpr Duration FromInt64(int64_t v, std::ratio<3600>) { ...@@ -1412,8 +1412,7 @@ constexpr Duration FromInt64(int64_t v, std::ratio<3600>) {
// IsValidRep64<T>(0) is true if the expression `int64_t{std::declval<T>()}` is // IsValidRep64<T>(0) is true if the expression `int64_t{std::declval<T>()}` is
// valid. That is, if a T can be assigned to an int64_t without narrowing. // valid. That is, if a T can be assigned to an int64_t without narrowing.
template <typename T> template <typename T>
constexpr auto IsValidRep64(int) constexpr auto IsValidRep64(int) -> decltype(int64_t{std::declval<T>()} == 0) {
-> decltype(int64_t{std::declval<T>()}, bool()) {
return true; return true;
} }
template <typename T> template <typename T>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment