Changes imported from Abseil "staging" branch:
- 8dea174f3f4178dd8b428e5cf73c37c4eefeb2ea Minor style changes and reorganization. Add missing copyr... by Alex Strelnikov <strel@google.com> GitOrigin-RevId: 8dea174f3f4178dd8b428e5cf73c37c4eefeb2ea Change-Id: I239f9dd6882172790b241f1af7a2acb23d54fb61
This commit is contained in:
parent
4132ce2595
commit
25274b35a0
4 changed files with 64 additions and 28 deletions
|
@ -139,9 +139,9 @@ uint128& uint128::operator%=(uint128 other) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& o, uint128 b) {
|
namespace {
|
||||||
std::ios_base::fmtflags flags = o.flags();
|
|
||||||
|
|
||||||
|
std::string Uint128ToFormattedString(uint128 v, std::ios_base::fmtflags flags) {
|
||||||
// Select a divisor which is the largest power of the base < 2^64.
|
// Select a divisor which is the largest power of the base < 2^64.
|
||||||
uint128 div;
|
uint128 div;
|
||||||
int div_base_log;
|
int div_base_log;
|
||||||
|
@ -160,14 +160,14 @@ std::ostream& operator<<(std::ostream& o, uint128 b) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now piece together the uint128 representation from three chunks of
|
// Now piece together the uint128 representation from three chunks of the
|
||||||
// the original value, each less than "div" and therefore representable
|
// original value, each less than "div" and therefore representable as a
|
||||||
// as a uint64_t.
|
// uint64_t.
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
std::ios_base::fmtflags copy_mask =
|
std::ios_base::fmtflags copy_mask =
|
||||||
std::ios::basefield | std::ios::showbase | std::ios::uppercase;
|
std::ios::basefield | std::ios::showbase | std::ios::uppercase;
|
||||||
os.setf(flags & copy_mask, copy_mask);
|
os.setf(flags & copy_mask, copy_mask);
|
||||||
uint128 high = b;
|
uint128 high = v;
|
||||||
uint128 low;
|
uint128 low;
|
||||||
DivModImpl(high, div, &high, &low);
|
DivModImpl(high, div, &high, &low);
|
||||||
uint128 mid;
|
uint128 mid;
|
||||||
|
@ -182,25 +182,31 @@ std::ostream& operator<<(std::ostream& o, uint128 b) {
|
||||||
os << std::noshowbase << std::setfill('0') << std::setw(div_base_log);
|
os << std::noshowbase << std::setfill('0') << std::setw(div_base_log);
|
||||||
}
|
}
|
||||||
os << Uint128Low64(low);
|
os << Uint128Low64(low);
|
||||||
std::string rep = os.str();
|
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.
|
// Add the requisite padding.
|
||||||
std::streamsize width = o.width(0);
|
std::streamsize width = os.width(0);
|
||||||
if (static_cast<size_t>(width) > rep.size()) {
|
if (static_cast<size_t>(width) > rep.size()) {
|
||||||
std::ios::fmtflags adjustfield = flags & std::ios::adjustfield;
|
std::ios::fmtflags adjustfield = flags & std::ios::adjustfield;
|
||||||
if (adjustfield == std::ios::left) {
|
if (adjustfield == std::ios::left) {
|
||||||
rep.append(width - rep.size(), o.fill());
|
rep.append(width - rep.size(), os.fill());
|
||||||
} else if (adjustfield == std::ios::internal &&
|
} else if (adjustfield == std::ios::internal &&
|
||||||
(flags & std::ios::showbase) &&
|
(flags & std::ios::showbase) &&
|
||||||
(flags & std::ios::basefield) == std::ios::hex && b != 0) {
|
(flags & std::ios::basefield) == std::ios::hex && v != 0) {
|
||||||
rep.insert(2, width - rep.size(), o.fill());
|
rep.insert(2, width - rep.size(), os.fill());
|
||||||
} else {
|
} else {
|
||||||
rep.insert(0, width - rep.size(), o.fill());
|
rep.insert(0, width - rep.size(), os.fill());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stream the final representation in a single "<<" call.
|
return os << rep;
|
||||||
return o << rep;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace absl
|
} // namespace absl
|
||||||
|
|
|
@ -62,7 +62,7 @@ namespace absl {
|
||||||
// However, a `uint128` differs from intrinsic integral types in the following
|
// However, a `uint128` differs from intrinsic integral types in the following
|
||||||
// ways:
|
// ways:
|
||||||
//
|
//
|
||||||
// * Errors on implicit conversions that does not preserve value (such as
|
// * Errors on implicit conversions that do not preserve value (such as
|
||||||
// loss of precision when converting to float values).
|
// loss of precision when converting to float values).
|
||||||
// * Requires explicit construction from and conversion to floating point
|
// * Requires explicit construction from and conversion to floating point
|
||||||
// types.
|
// types.
|
||||||
|
@ -175,10 +175,10 @@ class alignas(16) uint128 {
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// absl::uint128 big = absl::MakeUint128(1, 0);
|
// absl::uint128 big = absl::MakeUint128(1, 0);
|
||||||
friend constexpr uint128 MakeUint128(uint64_t top, uint64_t bottom);
|
friend constexpr uint128 MakeUint128(uint64_t high, uint64_t low);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
constexpr uint128(uint64_t top, uint64_t bottom);
|
constexpr uint128(uint64_t high, uint64_t low);
|
||||||
|
|
||||||
// TODO(strel) Update implementation to use __int128 once all users of
|
// TODO(strel) Update implementation to use __int128 once all users of
|
||||||
// uint128 are fixed to not depend on alignof(uint128) == 8. Also add
|
// uint128 are fixed to not depend on alignof(uint128) == 8. Also add
|
||||||
|
@ -198,7 +198,7 @@ class alignas(16) uint128 {
|
||||||
extern const uint128 kuint128max;
|
extern const uint128 kuint128max;
|
||||||
|
|
||||||
// allow uint128 to be logged
|
// allow uint128 to be logged
|
||||||
extern std::ostream& operator<<(std::ostream& o, uint128 b);
|
extern std::ostream& operator<<(std::ostream& os, uint128 v);
|
||||||
|
|
||||||
// TODO(strel) add operator>>(std::istream&, uint128)
|
// TODO(strel) add operator>>(std::istream&, uint128)
|
||||||
|
|
||||||
|
@ -208,8 +208,8 @@ extern std::ostream& operator<<(std::ostream& o, uint128 b);
|
||||||
// Implementation details follow
|
// Implementation details follow
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
constexpr uint128 MakeUint128(uint64_t top, uint64_t bottom) {
|
constexpr uint128 MakeUint128(uint64_t high, uint64_t low) {
|
||||||
return uint128(top, bottom);
|
return uint128(high, low);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assignment from integer types.
|
// Assignment from integer types.
|
||||||
|
@ -287,8 +287,8 @@ constexpr uint64_t Uint128High64(uint128 v) { return v.hi_; }
|
||||||
|
|
||||||
#if defined(ABSL_IS_LITTLE_ENDIAN)
|
#if defined(ABSL_IS_LITTLE_ENDIAN)
|
||||||
|
|
||||||
constexpr uint128::uint128(uint64_t top, uint64_t bottom)
|
constexpr uint128::uint128(uint64_t high, uint64_t low)
|
||||||
: lo_(bottom), hi_(top) {}
|
: lo_(low), hi_(high) {}
|
||||||
|
|
||||||
constexpr uint128::uint128(int v)
|
constexpr uint128::uint128(int v)
|
||||||
: lo_(v), hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0) {}
|
: lo_(v), hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0) {}
|
||||||
|
@ -314,8 +314,8 @@ constexpr uint128::uint128(unsigned __int128 v)
|
||||||
|
|
||||||
#elif defined(ABSL_IS_BIG_ENDIAN)
|
#elif defined(ABSL_IS_BIG_ENDIAN)
|
||||||
|
|
||||||
constexpr uint128::uint128(uint64_t top, uint64_t bottom)
|
constexpr uint128::uint128(uint64_t high, uint64_t low)
|
||||||
: hi_(top), lo_(bottom) {}
|
: hi_(high), lo_(low) {}
|
||||||
|
|
||||||
constexpr uint128::uint128(int v)
|
constexpr uint128::uint128(int v)
|
||||||
: hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0), lo_(v) {}
|
: hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0), lo_(v) {}
|
||||||
|
|
|
@ -1,3 +1,18 @@
|
||||||
// This file will contain :int128 implementation details that depend on internal
|
//
|
||||||
// representation when ABSL_HAVE_INTRINSIC_INT128 is defined. This file will be
|
// 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
|
||||||
|
//
|
||||||
|
// http://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.
|
// included by int128.h.
|
||||||
|
|
|
@ -1,3 +1,18 @@
|
||||||
// This file will contain :int128 implementation details that depend on internal
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
// http://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
|
// representation when ABSL_HAVE_INTRINSIC_INT128 is *not* defined. This file
|
||||||
// will be included by int128.h.
|
// is included by int128.h.
|
||||||
|
|
Loading…
Reference in a new issue