15 #include "absl/numeric/int128.h"
24 #include <type_traits>
26 #include "absl/base/optimization.h"
27 #include "absl/numeric/bits.h"
56 inline void DivModImpl(uint128 dividend, uint128 divisor, uint128* quotient_ret,
57 uint128* remainder_ret) {
60 if (divisor > dividend) {
62 *remainder_ret = dividend;
66 if (divisor == dividend) {
72 uint128 denominator = divisor;
76 const int shift =
Fls128(dividend) -
Fls128(denominator);
77 denominator <<= shift;
81 for (
int i = 0;
i <= shift; ++
i) {
83 if (dividend >= denominator) {
84 dividend -= denominator;
90 *quotient_ret = quotient;
91 *remainder_ret = dividend;
95 uint128 MakeUint128FromFloat(
T v) {
102 (std::numeric_limits<T>::max_exponent <= 128 ||
103 v < std::ldexp(
static_cast<T>(1), 128)));
105 if (
v >= std::ldexp(
static_cast<T>(1), 64)) {
114 #if defined(__clang__) && !defined(__SSE3__)
118 uint128 MakeUint128FromFloat(
long double v) {
120 static_assert(std::numeric_limits<double>::digits >= 50,
"");
121 static_assert(std::numeric_limits<long double>::digits <= 150,
"");
125 v = std::ldexp(
v, -100);
127 v = std::ldexp(
v -
static_cast<double>(w0), 50);
129 v = std::ldexp(
v -
static_cast<double>(w1), 50);
131 return (
static_cast<uint128
>(w0) << 100) | (
static_cast<uint128
>(w1) << 50) |
132 static_cast<uint128
>(w2);
134 #endif // __clang__ && !__SSE3__
141 #if !defined(ABSL_HAVE_INTRINSIC_INT128)
145 DivModImpl(lhs, rhs, "ient, &remainder);
152 DivModImpl(lhs, rhs, "ient, &remainder);
155 #endif // !defined(ABSL_HAVE_INTRINSIC_INT128)
163 switch (
flags & std::ios::basefield) {
165 div = 0x1000000000000000;
169 div = 01000000000000000000000;
173 div = 10000000000000000000
u;
181 std::ostringstream os;
182 std::ios_base::fmtflags copy_mask =
183 std::ios::basefield | std::ios::showbase | std::ios::uppercase;
184 os.setf(
flags & copy_mask, copy_mask);
187 DivModImpl(high, div, &high, &low);
189 DivModImpl(high, div, &high, &mid);
192 os << std::noshowbase << std::setfill(
'0') << std::setw(div_base_log);
194 os << std::setw(div_base_log);
197 os << std::noshowbase << std::setfill(
'0') << std::setw(div_base_log);
206 std::ios_base::fmtflags
flags = os.flags();
210 std::streamsize
width = os.width(0);
211 if (
static_cast<size_t>(
width) >
rep.size()) {
212 std::ios::fmtflags adjustfield =
flags & std::ios::adjustfield;
213 if (adjustfield == std::ios::left) {
215 }
else if (adjustfield == std::ios::internal &&
216 (
flags & std::ios::showbase) &&
217 (
flags & std::ios::basefield) == std::ios::hex &&
v != 0) {
229 uint128 UnsignedAbsoluteValue(int128
v) {
231 return Int128High64(
v) < 0 ? -uint128(
v) : uint128(
v);
236 #if !defined(ABSL_HAVE_INTRINSIC_INT128)
239 template <
typename T>
240 int128 MakeInt128FromFloat(
T v) {
243 assert(
std::isfinite(
v) && (std::numeric_limits<T>::max_exponent <= 127 ||
244 (
v >= -std::ldexp(
static_cast<T>(1), 127) &&
245 v < std::ldexp(
static_cast<T>(1), 127))));
251 uint128
result =
v < 0 ? -MakeUint128FromFloat(-
v) : MakeUint128FromFloat(
v);
267 DivModImpl(UnsignedAbsoluteValue(lhs), UnsignedAbsoluteValue(rhs),
268 "ient, &remainder);
269 if ((Int128High64(lhs) < 0) != (Int128High64(rhs) < 0)) quotient = -quotient;
279 DivModImpl(UnsignedAbsoluteValue(lhs), UnsignedAbsoluteValue(rhs),
280 "ient, &remainder);
281 if (Int128High64(lhs) < 0) remainder = -remainder;
285 #endif // ABSL_HAVE_INTRINSIC_INT128
288 std::ios_base::fmtflags
flags = os.flags();
292 bool print_as_decimal =
293 (
flags & std::ios::basefield) == std::ios::dec ||
294 (
flags & std::ios::basefield) == std::ios_base::fmtflags();
295 if (print_as_decimal) {
296 if (Int128High64(
v) < 0) {
298 }
else if (
flags & std::ios::showpos) {
303 rep.append(Uint128ToFormattedString(
304 print_as_decimal ? UnsignedAbsoluteValue(
v) :
uint128(
v), os.flags()));
307 std::streamsize
width = os.width(0);
308 if (
static_cast<size_t>(
width) >
rep.size()) {
309 switch (
flags & std::ios::adjustfield) {
313 case std::ios::internal:
314 if (print_as_decimal && (
rep[0] ==
'+' ||
rep[0] ==
'-')) {
316 }
else if ((
flags & std::ios::basefield) == std::ios::hex &&
317 (
flags & std::ios::showbase) &&
v != 0) {
335 #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL