1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
// 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 "config.h"
#include <wtf/Int128.h>
#include <stddef.h>
#include <cassert>
#include <iomanip>
#include <ostream> // NOLINT(readability/streams)
#include <sstream>
#include <string>
#include <type_traits>
#include <wtf/MathExtras.h>
#include <wtf/PrintStream.h>
#include <wtf/Vector.h>
#include <wtf/text/IntegerToStringConversion.h>
namespace WTF {
namespace {
// Returns the 0-based position of the last set bit (i.e., most significant bit)
// in the given UInt128Impl. The argument is not 0.
//
// For example:
// Given: 5 (decimal) == 101 (binary)
// Returns: 2
static ALWAYS_INLINE int Fls128(UInt128Impl n) {
if (uint64_t hi = UInt128High64(n)) {
ASSERT(hi != 0);
return 127 - clz(hi);
}
const uint64_t low = UInt128Low64(n);
ASSERT(low != 0);
return 63 - clz(low);
}
// Long division/modulo for UInt128Impl implemented using the shift-subtract
// division algorithm adapted from:
// https://stackoverflow.com/questions/5386377/division-without-using
static inline void DivModImpl(UInt128Impl dividend, UInt128Impl divisor, UInt128Impl* quotient_ret, UInt128Impl* 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;
}
UInt128Impl denominator = divisor;
UInt128Impl 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>
static UInt128Impl 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 UInt128Impl.
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));
}
} // namespace
UInt128Impl::UInt128Impl(float v) : UInt128Impl(MakeUInt128FromFloat(v)) {}
UInt128Impl::UInt128Impl(double v) : UInt128Impl(MakeUInt128FromFloat(v)) {}
UInt128Impl::UInt128Impl(long double v) : UInt128Impl(MakeUInt128FromFloat(v)) {}
UInt128Impl operator/(UInt128Impl lhs, UInt128Impl rhs) {
UInt128Impl quotient = 0;
UInt128Impl remainder = 0;
DivModImpl(lhs, rhs, "ient, &remainder);
return quotient;
}
UInt128Impl operator%(UInt128Impl lhs, UInt128Impl rhs) {
UInt128Impl quotient = 0;
UInt128Impl remainder = 0;
DivModImpl(lhs, rhs, "ient, &remainder);
return remainder;
}
namespace {
static std::string UInt128ToFormattedString(UInt128Impl v, std::ios_base::fmtflags flags) {
// Select a divisor which is the largest power of the base < 2^64.
UInt128Impl 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 UInt128Impl 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);
UInt128Impl high = v;
UInt128Impl low;
DivModImpl(high, div, &high, &low);
UInt128Impl 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, UInt128Impl 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 {
static UInt128Impl UnsignedAbsoluteValue(Int128Impl v) {
// Cast to UInt128Impl before possibly negating because -Int128Min() is undefined.
return Int128High64(v) < 0 ? -UInt128Impl(v) : UInt128Impl(v);
}
} // namespace
namespace {
template <typename T>
static Int128Impl MakeInt128FromFloat(T v) {
// Conversion when v is NaN or cannot fit into Int128Impl 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.
UInt128Impl result = v < 0 ? -MakeUInt128FromFloat(-v) : MakeUInt128FromFloat(v);
return MakeInt128(int128_internal::BitCastToSigned(UInt128High64(result)),
UInt128Low64(result));
}
} // namespace
Int128Impl::Int128Impl(float v) : Int128Impl(MakeInt128FromFloat(v)) {}
Int128Impl::Int128Impl(double v) : Int128Impl(MakeInt128FromFloat(v)) {}
Int128Impl::Int128Impl(long double v) : Int128Impl(MakeInt128FromFloat(v)) {}
Int128Impl operator/(Int128Impl lhs, Int128Impl rhs) {
assert(lhs != Int128Min() || rhs != -1); // UB on two's complement.
UInt128Impl quotient = 0;
UInt128Impl 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));
}
Int128Impl operator%(Int128Impl lhs, Int128Impl rhs) {
assert(lhs != Int128Min() || rhs != -1); // UB on two's complement.
UInt128Impl quotient = 0;
UInt128Impl remainder = 0;
DivModImpl(UnsignedAbsoluteValue(lhs), UnsignedAbsoluteValue(rhs),
"ient, &remainder);
if (Int128High64(lhs) < 0) remainder = -remainder;
return MakeInt128(int128_internal::BitCastToSigned(UInt128High64(remainder)),
UInt128Low64(remainder));
}
std::ostream& operator<<(std::ostream& os, Int128Impl 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.append("-");
} else if (flags & std::ios::showpos) {
rep.append("+");
}
}
rep.append(UInt128ToFormattedString(
print_as_decimal ? UnsignedAbsoluteValue(v) : UInt128Impl(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;
}
void printInternal(PrintStream& out, UInt128 value)
{
auto vector = numberToStringUnsigned<Vector<LChar, 50>>(value);
vector.append('\0');
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
out.printf("%s", std::bit_cast<const char*>(vector.data()));
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
}
void printInternal(PrintStream& out, Int128 value)
{
if (value >= 0) {
printInternal(out, static_cast<UInt128>(value));
return;
}
UInt128 positive;
if (value == std::numeric_limits<Int128>::min())
positive = static_cast<UInt128>(0x8000'0000'0000'0000ULL) << 64;
else
positive = -value;
auto vector = numberToStringUnsigned<Vector<LChar, 50>>(positive);
vector.append('\0');
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
out.printf("-%s", std::bit_cast<const char*>(vector.data()));
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
}
} // namespace WTF
namespace std {
constexpr bool numeric_limits<WTF::UInt128Impl>::is_specialized;
constexpr bool numeric_limits<WTF::UInt128Impl>::is_signed;
constexpr bool numeric_limits<WTF::UInt128Impl>::is_integer;
constexpr bool numeric_limits<WTF::UInt128Impl>::is_exact;
constexpr bool numeric_limits<WTF::UInt128Impl>::has_infinity;
constexpr bool numeric_limits<WTF::UInt128Impl>::has_quiet_NaN;
constexpr bool numeric_limits<WTF::UInt128Impl>::has_signaling_NaN;
constexpr bool numeric_limits<WTF::UInt128Impl>::has_denorm_loss;
constexpr float_round_style numeric_limits<WTF::UInt128Impl>::round_style;
constexpr bool numeric_limits<WTF::UInt128Impl>::is_iec559;
constexpr bool numeric_limits<WTF::UInt128Impl>::is_bounded;
constexpr bool numeric_limits<WTF::UInt128Impl>::is_modulo;
constexpr int numeric_limits<WTF::UInt128Impl>::digits;
constexpr int numeric_limits<WTF::UInt128Impl>::digits10;
constexpr int numeric_limits<WTF::UInt128Impl>::max_digits10;
constexpr int numeric_limits<WTF::UInt128Impl>::radix;
constexpr int numeric_limits<WTF::UInt128Impl>::min_exponent;
constexpr int numeric_limits<WTF::UInt128Impl>::min_exponent10;
constexpr int numeric_limits<WTF::UInt128Impl>::max_exponent;
constexpr int numeric_limits<WTF::UInt128Impl>::max_exponent10;
constexpr bool numeric_limits<WTF::UInt128Impl>::traps;
constexpr bool numeric_limits<WTF::UInt128Impl>::tinyness_before;
constexpr bool numeric_limits<WTF::Int128Impl>::is_specialized;
constexpr bool numeric_limits<WTF::Int128Impl>::is_signed;
constexpr bool numeric_limits<WTF::Int128Impl>::is_integer;
constexpr bool numeric_limits<WTF::Int128Impl>::is_exact;
constexpr bool numeric_limits<WTF::Int128Impl>::has_infinity;
constexpr bool numeric_limits<WTF::Int128Impl>::has_quiet_NaN;
constexpr bool numeric_limits<WTF::Int128Impl>::has_signaling_NaN;
constexpr bool numeric_limits<WTF::Int128Impl>::has_denorm_loss;
constexpr float_round_style numeric_limits<WTF::Int128Impl>::round_style;
constexpr bool numeric_limits<WTF::Int128Impl>::is_iec559;
constexpr bool numeric_limits<WTF::Int128Impl>::is_bounded;
constexpr bool numeric_limits<WTF::Int128Impl>::is_modulo;
constexpr int numeric_limits<WTF::Int128Impl>::digits;
constexpr int numeric_limits<WTF::Int128Impl>::digits10;
constexpr int numeric_limits<WTF::Int128Impl>::max_digits10;
constexpr int numeric_limits<WTF::Int128Impl>::radix;
constexpr int numeric_limits<WTF::Int128Impl>::min_exponent;
constexpr int numeric_limits<WTF::Int128Impl>::min_exponent10;
constexpr int numeric_limits<WTF::Int128Impl>::max_exponent;
constexpr int numeric_limits<WTF::Int128Impl>::max_exponent10;
constexpr bool numeric_limits<WTF::Int128Impl>::traps;
constexpr bool numeric_limits<WTF::Int128Impl>::tinyness_before;
} // namespace std
|