| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef builtin_temporal_Int128_h |
| 8 |
#define builtin_temporal_Int128_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" |
| 11 |
#include "mozilla/EndianUtils.h" |
| 12 |
#include "mozilla/MathAlgorithms.h" |
| 13 |
|
| 14 |
#include <climits> |
| 15 |
#include <limits> |
| 16 |
#include <stdint.h> |
| 17 |
#include <utility> |
| 18 |
|
| 19 |
namespace js::temporal { |
| 20 |
|
| 21 |
class Int128; |
| 22 |
class Uint128; |
| 23 |
|
| 24 |
/** |
| 25 |
* Unsigned 128-bit integer, implemented as a pair of unsigned 64-bit integers. |
| 26 |
* |
| 27 |
* Supports all basic arithmetic operators. |
| 28 |
*/ |
| 29 |
class alignas(16) Uint128 final { |
| 30 |
#if MOZ_LITTLE_ENDIAN() |
| 31 |
uint64_t low = 0; |
| 32 |
uint64_t high = 0; |
| 33 |
#else |
| 34 |
uint64_t high = 0; |
| 35 |
uint64_t low = 0; |
| 36 |
#endif |
| 37 |
|
| 38 |
friend class Int128; |
| 39 |
|
| 40 |
constexpr Uint128(uint64_t low, uint64_t high) : low(low), high(high) {} |
| 41 |
|
| 42 |
/** |
| 43 |
* Return the high double-word of the multiplication of `u * v`. |
| 44 |
* |
| 45 |
* Based on "Multiply high unsigned" from Hacker's Delight, 2nd edition, |
| 46 |
* figure 8-2. |
| 47 |
*/ |
| 48 |
static constexpr uint64_t mulhu(uint64_t u, uint64_t v) { |
| 49 |
uint64_t u0 = u & 0xffff'ffff; |
| 50 |
uint64_t u1 = u >> 32; |
| 51 |
|
| 52 |
uint64_t v0 = v & 0xffff'ffff; |
| 53 |
uint64_t v1 = v >> 32; |
| 54 |
|
| 55 |
uint64_t w0 = u0 * v0; |
| 56 |
uint64_t t = u1 * v0 + (w0 >> 32); |
| 57 |
uint64_t w1 = t & 0xffff'ffff; |
| 58 |
uint64_t w2 = t >> 32; |
| 59 |
w1 = u0 * v1 + w1; |
| 60 |
return u1 * v1 + w2 + (w1 >> 32); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Based on "Unsigned doubleword division from long division" from |
| 65 |
* Hacker's Delight, 2nd edition, figure 9-5. |
| 66 |
*/ |
| 67 |
static std::pair<Uint128, Uint128> udivdi(const Uint128& u, |
| 68 |
const Uint128& v) { |
| 69 |
MOZ_ASSERT(v != Uint128{}); |
| 70 |
|
| 71 |
// If v < 2**64 |
| 72 |
if (v.high == 0) { |
| 73 |
// If u < 2**64 |
| 74 |
if (u.high == 0) { |
| 75 |
// Prefer built-in division if possible. |
| 76 |
return {Uint128{u.low / v.low, 0}, Uint128{u.low % v.low, 0}}; |
| 77 |
} |
| 78 |
|
| 79 |
// If u/v cannot overflow, just do one division. |
| 80 |
if (Uint128{u.high, 0} < v) { |
| 81 |
auto [q, r] = divlu(u.high, u.low, v.low); |
| 82 |
return {Uint128{q, 0}, Uint128{r, 0}}; |
| 83 |
} |
| 84 |
|
| 85 |
// If u/v would overflow: Break u up into two halves. |
| 86 |
|
| 87 |
// First quotient digit and first remainder, < v. |
| 88 |
auto [q1, r1] = divlu(0, u.high, v.low); |
| 89 |
|
| 90 |
// Second quotient digit. |
| 91 |
auto [q0, r0] = divlu(r1, u.low, v.low); |
| 92 |
|
| 93 |
// Return quotient and remainder. |
| 94 |
return {Uint128{q0, q1}, Uint128{r0, 0}}; |
| 95 |
} |
| 96 |
|
| 97 |
// Here v >= 2**64. |
| 98 |
|
| 99 |
// 0 <= n <= 63 |
| 100 |
auto n = mozilla::CountLeadingZeroes64(v.high); |
| 101 |
|
| 102 |
// Normalize the divisor so its MSB is 1. |
| 103 |
auto v1 = (v << n).high; |
| 104 |
|
| 105 |
// To ensure no overflow. |
| 106 |
auto u1 = u >> 1; |
| 107 |
|
| 108 |
// Get quotient from divide unsigned instruction. |
| 109 |
auto [q1, r1] = divlu(u1.high, u1.low, v1); |
| 110 |
|
| 111 |
// Undo normalization and division of u by 2. |
| 112 |
auto q0 = (Uint128{q1, 0} << n) >> 63; |
| 113 |
|
| 114 |
// Make q0 correct or too small by 1. |
| 115 |
if (q0 != Uint128{0}) { |
| 116 |
q0 -= Uint128{1}; |
| 117 |
} |
| 118 |
|
| 119 |
// Now q0 is correct. |
| 120 |
auto r0 = u - q0 * v; |
| 121 |
if (r0 >= v) { |
| 122 |
q0 += Uint128{1}; |
| 123 |
r0 -= v; |
| 124 |
} |
| 125 |
|
| 126 |
// Return quotient and remainder. |
| 127 |
return {q0, r0}; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Based on "Divide long unsigned, using fullword division instructions" from |
| 132 |
* Hacker's Delight, 2nd edition, figure 9-3. |
| 133 |
*/ |
| 134 |
static std::pair<uint64_t, uint64_t> divlu(uint64_t u1, uint64_t u0, |
| 135 |
uint64_t v) { |
| 136 |
// Number base (32 bits). |
| 137 |
constexpr uint64_t base = 4294967296; |
| 138 |
|
| 139 |
// If overflow, set the remainder to an impossible value and return the |
| 140 |
// largest possible quotient. |
| 141 |
if (u1 >= v) { |
| 142 |
return {UINT64_MAX, UINT64_MAX}; |
| 143 |
} |
| 144 |
|
| 145 |
// Shift amount for normalization. (0 <= s <= 63) |
| 146 |
int64_t s = mozilla::CountLeadingZeroes64(v); |
| 147 |
|
| 148 |
// Normalize the divisor. |
| 149 |
v = v << s; |
| 150 |
|
| 151 |
// Normalized divisor digits. |
| 152 |
// |
| 153 |
// Break divisor up into two 32-bit digits. |
| 154 |
uint64_t vn1 = v >> 32; |
| 155 |
uint64_t vn0 = uint32_t(v); |
| 156 |
|
| 157 |
// Dividend digit pairs. |
| 158 |
// |
| 159 |
// Shift dividend left. |
| 160 |
uint64_t un32 = (u1 << s) | ((u0 >> ((64 - s) & 63)) & (-s >> 63)); |
| 161 |
uint64_t un10 = u0 << s; |
| 162 |
|
| 163 |
// Normalized dividend least significant digits. |
| 164 |
// |
| 165 |
// Break right half of dividend into two digits. |
| 166 |
uint64_t un1 = un10 >> 32; |
| 167 |
uint64_t un0 = uint32_t(un10); |
| 168 |
|
| 169 |
// Compute the first quotient digit and its remainder. |
| 170 |
uint64_t q1 = un32 / vn1; |
| 171 |
uint64_t rhat = un32 - q1 * vn1; |
| 172 |
while (q1 >= base || q1 * vn0 > base * rhat + un1) { |
| 173 |
q1 -= 1; |
| 174 |
rhat += vn1; |
| 175 |
if (rhat >= base) { |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
// Multiply and subtract. |
| 181 |
uint64_t un21 = un32 * base + un1 - q1 * v; |
| 182 |
|
| 183 |
// Compute the second quotient digit and its remainder. |
| 184 |
uint64_t q0 = un21 / vn1; |
| 185 |
rhat = un21 - q0 * vn1; |
| 186 |
while (q0 >= base || q0 * vn0 > base * rhat + un0) { |
| 187 |
q0 -= 1; |
| 188 |
rhat += vn1; |
| 189 |
if (rhat >= base) { |
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// Return the quotient and remainder. |
| 195 |
uint64_t q = q1 * base + q0; |
| 196 |
uint64_t r = (un21 * base + un0 - q0 * v) >> s; |
| 197 |
return {q, r}; |
| 198 |
} |
| 199 |
|
| 200 |
static double toDouble(const Uint128& x, bool negative); |
| 201 |
|
| 202 |
public: |
| 203 |
constexpr Uint128() = default; |
| 204 |
constexpr Uint128(const Uint128&) = default; |
| 205 |
|
| 206 |
explicit constexpr Uint128(uint64_t value) |
| 207 |
: Uint128(uint64_t(value), uint64_t(0)) {} |
| 208 |
|
| 209 |
constexpr bool operator==(const Uint128& other) const { |
| 210 |
return low == other.low && high == other.high; |
| 211 |
} |
| 212 |
|
| 213 |
constexpr bool operator<(const Uint128& other) const { |
| 214 |
if (high == other.high) { |
| 215 |
return low < other.low; |
| 216 |
} |
| 217 |
return high < other.high; |
| 218 |
} |
| 219 |
|
| 220 |
// Other operators are implemented in terms of operator== and operator<. |
| 221 |
constexpr bool operator!=(const Uint128& other) const { |
| 222 |
return !(*this == other); |
| 223 |
} |
| 224 |
constexpr bool operator>(const Uint128& other) const { return other < *this; } |
| 225 |
constexpr bool operator<=(const Uint128& other) const { |
| 226 |
return !(other < *this); |
| 227 |
} |
| 228 |
constexpr bool operator>=(const Uint128& other) const { |
| 229 |
return !(*this < other); |
| 230 |
} |
| 231 |
|
| 232 |
explicit constexpr operator bool() const { return !(*this == Uint128{}); } |
| 233 |
|
| 234 |
explicit constexpr operator int8_t() const { return int8_t(low); } |
| 235 |
explicit constexpr operator int16_t() const { return int16_t(low); } |
| 236 |
explicit constexpr operator int32_t() const { return int32_t(low); } |
| 237 |
explicit constexpr operator int64_t() const { return int64_t(low); } |
| 238 |
|
| 239 |
explicit constexpr operator uint8_t() const { return uint8_t(low); } |
| 240 |
explicit constexpr operator uint16_t() const { return uint16_t(low); } |
| 241 |
explicit constexpr operator uint32_t() const { return uint32_t(low); } |
| 242 |
explicit constexpr operator uint64_t() const { return uint64_t(low); } |
| 243 |
|
| 244 |
explicit constexpr operator Int128() const; |
| 245 |
|
| 246 |
explicit operator double() const { return toDouble(*this, false); } |
| 247 |
|
| 248 |
constexpr Uint128 operator+(const Uint128& other) const { |
| 249 |
// "§2-16 Double-Length Add/Subtract" from Hacker's Delight, 2nd edition. |
| 250 |
Uint128 result; |
| 251 |
result.low = low + other.low; |
| 252 |
result.high = high + other.high + static_cast<uint64_t>(result.low < low); |
| 253 |
return result; |
| 254 |
} |
| 255 |
|
| 256 |
constexpr Uint128 operator-(const Uint128& other) const { |
| 257 |
// "§2-16 Double-Length Add/Subtract" from Hacker's Delight, 2nd edition. |
| 258 |
Uint128 result; |
| 259 |
result.low = low - other.low; |
| 260 |
result.high = high - other.high - static_cast<uint64_t>(low < other.low); |
| 261 |
return result; |
| 262 |
} |
| 263 |
|
| 264 |
constexpr Uint128 operator*(const Uint128& other) const { |
| 265 |
uint64_t w01 = low * other.high; |
| 266 |
uint64_t w10 = high * other.low; |
| 267 |
uint64_t w00 = mulhu(low, other.low); |
| 268 |
|
| 269 |
uint64_t w1 = w00 + w10 + w01; |
| 270 |
uint64_t w0 = low * other.low; |
| 271 |
|
| 272 |
return Uint128{w0, w1}; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Return the quotient and remainder of the division. |
| 277 |
*/ |
| 278 |
std::pair<Uint128, Uint128> divrem(const Uint128& divisor) const { |
| 279 |
return udivdi(*this, divisor); |
| 280 |
} |
| 281 |
|
| 282 |
Uint128 operator/(const Uint128& other) const { |
| 283 |
auto [quot, rem] = divrem(other); |
| 284 |
return quot; |
| 285 |
} |
| 286 |
|
| 287 |
Uint128 operator%(const Uint128& other) const { |
| 288 |
auto [quot, rem] = divrem(other); |
| 289 |
return rem; |
| 290 |
} |
| 291 |
|
| 292 |
constexpr Uint128 operator<<(int shift) const { |
| 293 |
MOZ_ASSERT(0 <= shift && shift <= 127, "undefined shift amount"); |
| 294 |
|
| 295 |
// Ensure the shift amount is in range. |
| 296 |
shift &= 127; |
| 297 |
|
| 298 |
// "§2-17 Double-Length Shifts" from Hacker's Delight, 2nd edition. |
| 299 |
if (shift >= 64) { |
| 300 |
uint64_t y0 = 0; |
| 301 |
uint64_t y1 = low << (shift - 64); |
| 302 |
return Uint128{y0, y1}; |
| 303 |
} |
| 304 |
if (shift > 0) { |
| 305 |
uint64_t y0 = low << shift; |
| 306 |
uint64_t y1 = high << shift | low >> (64 - shift); |
| 307 |
return Uint128{y0, y1}; |
| 308 |
} |
| 309 |
return *this; |
| 310 |
} |
| 311 |
|
| 312 |
constexpr Uint128 operator>>(int shift) const { |
| 313 |
MOZ_ASSERT(0 <= shift && shift <= 127, "undefined shift amount"); |
| 314 |
|
| 315 |
// Ensure the shift amount is in range. |
| 316 |
shift &= 127; |
| 317 |
|
| 318 |
// "§2-17 Double-Length Shifts" from Hacker's Delight, 2nd edition. |
| 319 |
if (shift >= 64) { |
| 320 |
uint64_t y0 = high >> (shift - 64); |
| 321 |
uint64_t y1 = 0; |
| 322 |
return Uint128{y0, y1}; |
| 323 |
} |
| 324 |
if (shift > 0) { |
| 325 |
uint64_t y0 = low >> shift | high << (64 - shift); |
| 326 |
uint64_t y1 = high >> shift; |
| 327 |
return Uint128{y0, y1}; |
| 328 |
} |
| 329 |
return *this; |
| 330 |
} |
| 331 |
|
| 332 |
constexpr Uint128 operator&(const Uint128& other) const { |
| 333 |
return Uint128{low & other.low, high & other.high}; |
| 334 |
} |
| 335 |
|
| 336 |
constexpr Uint128 operator|(const Uint128& other) const { |
| 337 |
return Uint128{low | other.low, high | other.high}; |
| 338 |
} |
| 339 |
|
| 340 |
constexpr Uint128 operator^(const Uint128& other) const { |
| 341 |
return Uint128{low ^ other.low, high ^ other.high}; |
| 342 |
} |
| 343 |
|
| 344 |
constexpr Uint128 operator~() const { return Uint128{~low, ~high}; } |
| 345 |
|
| 346 |
constexpr Uint128 operator-() const { return Uint128{} - *this; } |
| 347 |
|
| 348 |
constexpr Uint128 operator+() const { return *this; } |
| 349 |
|
| 350 |
constexpr Uint128& operator++() { |
| 351 |
*this = *this + Uint128{1, 0}; |
| 352 |
return *this; |
| 353 |
} |
| 354 |
|
| 355 |
constexpr Uint128 operator++(int) { |
| 356 |
auto result = *this; |
| 357 |
++*this; |
| 358 |
return result; |
| 359 |
} |
| 360 |
|
| 361 |
constexpr Uint128& operator--() { |
| 362 |
*this = *this - Uint128{1, 0}; |
| 363 |
return *this; |
| 364 |
} |
| 365 |
|
| 366 |
constexpr Uint128 operator--(int) { |
| 367 |
auto result = *this; |
| 368 |
--*this; |
| 369 |
return result; |
| 370 |
} |
| 371 |
|
| 372 |
constexpr Uint128 operator+=(const Uint128& other) { |
| 373 |
*this = *this + other; |
| 374 |
return *this; |
| 375 |
} |
| 376 |
|
| 377 |
constexpr Uint128 operator-=(const Uint128& other) { |
| 378 |
*this = *this - other; |
| 379 |
return *this; |
| 380 |
} |
| 381 |
|
| 382 |
constexpr Uint128 operator*=(const Uint128& other) { |
| 383 |
*this = *this * other; |
| 384 |
return *this; |
| 385 |
} |
| 386 |
|
| 387 |
Uint128 operator/=(const Uint128& other) { |
| 388 |
*this = *this / other; |
| 389 |
return *this; |
| 390 |
} |
| 391 |
|
| 392 |
Uint128 operator%=(const Uint128& other) { |
| 393 |
*this = *this % other; |
| 394 |
return *this; |
| 395 |
} |
| 396 |
|
| 397 |
constexpr Uint128 operator&=(const Uint128& other) { |
| 398 |
*this = *this & other; |
| 399 |
return *this; |
| 400 |
} |
| 401 |
|
| 402 |
constexpr Uint128 operator|=(const Uint128& other) { |
| 403 |
*this = *this | other; |
| 404 |
return *this; |
| 405 |
} |
| 406 |
|
| 407 |
constexpr Uint128 operator^=(const Uint128& other) { |
| 408 |
*this = *this ^ other; |
| 409 |
return *this; |
| 410 |
} |
| 411 |
|
| 412 |
constexpr Uint128 operator<<=(int shift) { |
| 413 |
*this = *this << shift; |
| 414 |
return *this; |
| 415 |
} |
| 416 |
|
| 417 |
constexpr Uint128 operator>>=(int shift) { |
| 418 |
*this = *this >> shift; |
| 419 |
return *this; |
| 420 |
} |
| 421 |
}; |
| 422 |
|
| 423 |
/** |
| 424 |
* Signed 128-bit integer, implemented as a pair of unsigned 64-bit integers. |
| 425 |
* |
| 426 |
* Supports all basic arithmetic operators. |
| 427 |
*/ |
| 428 |
class alignas(16) Int128 final { |
| 429 |
#if MOZ_LITTLE_ENDIAN() |
| 430 |
uint64_t low = 0; |
| 431 |
uint64_t high = 0; |
| 432 |
#else |
| 433 |
uint64_t high = 0; |
| 434 |
uint64_t low = 0; |
| 435 |
#endif |
| 436 |
|
| 437 |
friend class Uint128; |
| 438 |
|
| 439 |
constexpr Int128(uint64_t low, uint64_t high) : low(low), high(high) {} |
| 440 |
|
| 441 |
/** |
| 442 |
* Based on "Signed doubleword division from unsigned doubleword division" |
| 443 |
* from Hacker's Delight, 2nd edition, figure 9-6. |
| 444 |
*/ |
| 445 |
static std::pair<Int128, Int128> divdi(const Int128& u, const Int128& v) { |
| 446 |
auto [q, r] = Uint128::udivdi(u.abs(), v.abs()); |
| 447 |
|
| 448 |
// If u and v have different signs, negate q. |
| 449 |
// If is negative, negate r. |
| 450 |
auto t = static_cast<Uint128>((u ^ v) >> 127); |
| 451 |
auto s = static_cast<Uint128>(u >> 127); |
| 452 |
return {static_cast<Int128>((q ^ t) - t), static_cast<Int128>((r ^ s) - s)}; |
| 453 |
} |
| 454 |
|
| 455 |
public: |
| 456 |
constexpr Int128() = default; |
| 457 |
constexpr Int128(const Int128&) = default; |
| 458 |
|
| 459 |
explicit constexpr Int128(int64_t value) |
| 460 |
: Int128(uint64_t(value), uint64_t(value >> 63)) {} |
| 461 |
|
| 462 |
/** |
| 463 |
* Return the quotient and remainder of the division. |
| 464 |
*/ |
| 465 |
std::pair<Int128, Int128> divrem(const Int128& divisor) const { |
| 466 |
return divdi(*this, divisor); |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* Return the absolute value of this integer. |
| 471 |
*/ |
| 472 |
constexpr Uint128 abs() const { |
| 473 |
if (*this >= Int128{}) { |
| 474 |
return Uint128{low, high}; |
| 475 |
} |
| 476 |
auto neg = -*this; |
| 477 |
return Uint128{neg.low, neg.high}; |
| 478 |
} |
| 479 |
|
| 480 |
constexpr bool operator==(const Int128& other) const { |
| 481 |
return low == other.low && high == other.high; |
| 482 |
} |
| 483 |
|
| 484 |
constexpr bool operator<(const Int128& other) const { |
| 485 |
if (high == other.high) { |
| 486 |
return low < other.low; |
| 487 |
} |
| 488 |
return int64_t(high) < int64_t(other.high); |
| 489 |
} |
| 490 |
|
| 491 |
// Other operators are implemented in terms of operator== and operator<. |
| 492 |
constexpr bool operator!=(const Int128& other) const { |
| 493 |
return !(*this == other); |
| 494 |
} |
| 495 |
constexpr bool operator>(const Int128& other) const { return other < *this; } |
| 496 |
constexpr bool operator<=(const Int128& other) const { |
| 497 |
return !(other < *this); |
| 498 |
} |
| 499 |
constexpr bool operator>=(const Int128& other) const { |
| 500 |
return !(*this < other); |
| 501 |
} |
| 502 |
|
| 503 |
explicit constexpr operator bool() const { return !(*this == Int128{}); } |
| 504 |
|
| 505 |
explicit constexpr operator int8_t() const { return int8_t(low); } |
| 506 |
explicit constexpr operator int16_t() const { return int16_t(low); } |
| 507 |
explicit constexpr operator int32_t() const { return int32_t(low); } |
| 508 |
explicit constexpr operator int64_t() const { return int64_t(low); } |
| 509 |
|
| 510 |
explicit constexpr operator uint8_t() const { return uint8_t(low); } |
| 511 |
explicit constexpr operator uint16_t() const { return uint16_t(low); } |
| 512 |
explicit constexpr operator uint32_t() const { return uint32_t(low); } |
| 513 |
explicit constexpr operator uint64_t() const { return uint64_t(low); } |
| 514 |
|
| 515 |
explicit constexpr operator Uint128() const { return Uint128{low, high}; } |
| 516 |
|
| 517 |
explicit operator double() const { |
| 518 |
return Uint128::toDouble(abs(), *this < Int128{0}); |
| 519 |
} |
| 520 |
|
| 521 |
constexpr Int128 operator+(const Int128& other) const { |
| 522 |
return Int128{Uint128{*this} + Uint128{other}}; |
| 523 |
} |
| 524 |
|
| 525 |
constexpr Int128 operator-(const Int128& other) const { |
| 526 |
return Int128{Uint128{*this} - Uint128{other}}; |
| 527 |
} |
| 528 |
|
| 529 |
constexpr Int128 operator*(const Int128& other) const { |
| 530 |
return Int128{Uint128{*this} * Uint128{other}}; |
| 531 |
} |
| 532 |
|
| 533 |
Int128 operator/(const Int128& other) const { |
| 534 |
auto [quot, rem] = divrem(other); |
| 535 |
return quot; |
| 536 |
} |
| 537 |
|
| 538 |
Int128 operator%(const Int128& other) const { |
| 539 |
auto [quot, rem] = divrem(other); |
| 540 |
return rem; |
| 541 |
} |
| 542 |
|
| 543 |
constexpr Int128 operator<<(int shift) const { |
| 544 |
return Int128{Uint128{*this} << shift}; |
| 545 |
} |
| 546 |
|
| 547 |
constexpr Int128 operator>>(int shift) const { |
| 548 |
MOZ_ASSERT(0 <= shift && shift <= 127, "undefined shift amount"); |
| 549 |
|
| 550 |
// Ensure the shift amount is in range. |
| 551 |
shift &= 127; |
| 552 |
|
| 553 |
// "§2-17 Double-Length Shifts" from Hacker's Delight, 2nd edition. |
| 554 |
if (shift >= 64) { |
| 555 |
uint64_t y0 = uint64_t(int64_t(high) >> (shift - 64)); |
| 556 |
uint64_t y1 = uint64_t(int64_t(high) >> 63); |
| 557 |
return Int128{y0, y1}; |
| 558 |
} |
| 559 |
if (shift > 0) { |
| 560 |
uint64_t y0 = low >> shift | high << (64 - shift); |
| 561 |
uint64_t y1 = uint64_t(int64_t(high) >> shift); |
| 562 |
return Int128{y0, y1}; |
| 563 |
} |
| 564 |
return *this; |
| 565 |
} |
| 566 |
|
| 567 |
constexpr Int128 operator&(const Int128& other) const { |
| 568 |
return Int128{low & other.low, high & other.high}; |
| 569 |
} |
| 570 |
|
| 571 |
constexpr Int128 operator|(const Int128& other) const { |
| 572 |
return Int128{low | other.low, high | other.high}; |
| 573 |
} |
| 574 |
|
| 575 |
constexpr Int128 operator^(const Int128& other) const { |
| 576 |
return Int128{low ^ other.low, high ^ other.high}; |
| 577 |
} |
| 578 |
|
| 579 |
constexpr Int128 operator~() const { return Int128{~low, ~high}; } |
| 580 |
|
| 581 |
constexpr Int128 operator-() const { return Int128{} - *this; } |
| 582 |
|
| 583 |
constexpr Int128 operator+() const { return *this; } |
| 584 |
|
| 585 |
constexpr Int128& operator++() { |
| 586 |
*this = *this + Int128{1, 0}; |
| 587 |
return *this; |
| 588 |
} |
| 589 |
|
| 590 |
constexpr Int128 operator++(int) { |
| 591 |
auto result = *this; |
| 592 |
++*this; |
| 593 |
return result; |
| 594 |
} |
| 595 |
|
| 596 |
constexpr Int128& operator--() { |
| 597 |
*this = *this - Int128{1, 0}; |
| 598 |
return *this; |
| 599 |
} |
| 600 |
|
| 601 |
constexpr Int128 operator--(int) { |
| 602 |
auto result = *this; |
| 603 |
--*this; |
| 604 |
return result; |
| 605 |
} |
| 606 |
|
| 607 |
constexpr Int128 operator+=(const Int128& other) { |
| 608 |
*this = *this + other; |
| 609 |
return *this; |
| 610 |
} |
| 611 |
|
| 612 |
constexpr Int128 operator-=(const Int128& other) { |
| 613 |
*this = *this - other; |
| 614 |
return *this; |
| 615 |
} |
| 616 |
|
| 617 |
constexpr Int128 operator*=(const Int128& other) { |
| 618 |
*this = *this * other; |
| 619 |
return *this; |
| 620 |
} |
| 621 |
|
| 622 |
Int128 operator/=(const Int128& other) { |
| 623 |
*this = *this / other; |
| 624 |
return *this; |
| 625 |
} |
| 626 |
|
| 627 |
Int128 operator%=(const Int128& other) { |
| 628 |
*this = *this % other; |
| 629 |
return *this; |
| 630 |
} |
| 631 |
|
| 632 |
constexpr Int128 operator&=(const Int128& other) { |
| 633 |
*this = *this & other; |
| 634 |
return *this; |
| 635 |
} |
| 636 |
|
| 637 |
constexpr Int128 operator|=(const Int128& other) { |
| 638 |
*this = *this | other; |
| 639 |
return *this; |
| 640 |
} |
| 641 |
|
| 642 |
constexpr Int128 operator^=(const Int128& other) { |
| 643 |
*this = *this ^ other; |
| 644 |
return *this; |
| 645 |
} |
| 646 |
|
| 647 |
constexpr Int128 operator<<=(int shift) { |
| 648 |
*this = *this << shift; |
| 649 |
return *this; |
| 650 |
} |
| 651 |
|
| 652 |
constexpr Int128 operator>>=(int shift) { |
| 653 |
*this = *this >> shift; |
| 654 |
return *this; |
| 655 |
} |
| 656 |
}; |
| 657 |
|
| 658 |
constexpr Uint128::operator Int128() const { return Int128{low, high}; } |
| 659 |
|
| 660 |
} /* namespace js::temporal */ |
| 661 |
|
| 662 |
template <> |
| 663 |
class std::numeric_limits<js::temporal::Int128> { |
| 664 |
public: |
| 665 |
static constexpr bool is_specialized = true; |
| 666 |
static constexpr bool is_signed = true; |
| 667 |
static constexpr bool is_integer = true; |
| 668 |
static constexpr bool is_exact = true; |
| 669 |
static constexpr bool has_infinity = false; |
| 670 |
static constexpr bool has_quiet_NaN = false; |
| 671 |
static constexpr bool has_signaling_NaN = false; |
| 672 |
static constexpr std::float_denorm_style has_denorm = std::denorm_absent; |
| 673 |
static constexpr bool has_denorm_loss = false; |
| 674 |
static constexpr std::float_round_style round_style = std::round_toward_zero; |
| 675 |
static constexpr bool is_iec559 = false; |
| 676 |
static constexpr bool is_bounded = true; |
| 677 |
static constexpr bool is_modulo = true; |
| 678 |
static constexpr int digits = CHAR_BIT * sizeof(js::temporal::Int128) - 1; |
| 679 |
static constexpr int digits10 = int(digits * /* std::log10(2) */ 0.30102999); |
| 680 |
static constexpr int max_digits10 = 0; |
| 681 |
static constexpr int radix = 2; |
| 682 |
static constexpr int min_exponent = 0; |
| 683 |
static constexpr int min_exponent10 = 0; |
| 684 |
static constexpr int max_exponent = 0; |
| 685 |
static constexpr int max_exponent10 = 0; |
| 686 |
static constexpr bool traps = true; |
| 687 |
static constexpr bool tinyness_before = false; |
| 688 |
|
| 689 |
static constexpr auto min() noexcept { |
| 690 |
return js::temporal::Int128{1} << 127; |
| 691 |
} |
| 692 |
static constexpr auto lowest() noexcept { return min(); } |
| 693 |
static constexpr auto max() noexcept { return ~min(); } |
| 694 |
static constexpr auto epsilon() noexcept { return js::temporal::Int128{}; } |
| 695 |
static constexpr auto round_error() noexcept { |
| 696 |
return js::temporal::Int128{}; |
| 697 |
} |
| 698 |
static constexpr auto infinity() noexcept { return js::temporal::Int128{}; } |
| 699 |
static constexpr auto quiet_NaN() noexcept { return js::temporal::Int128{}; } |
| 700 |
static constexpr auto signaling_NaN() noexcept { |
| 701 |
return js::temporal::Int128{}; |
| 702 |
} |
| 703 |
static constexpr auto denorm_min() noexcept { return js::temporal::Int128{}; } |
| 704 |
}; |
| 705 |
|
| 706 |
template <> |
| 707 |
class std::numeric_limits<js::temporal::Uint128> { |
| 708 |
public: |
| 709 |
static constexpr bool is_specialized = true; |
| 710 |
static constexpr bool is_signed = false; |
| 711 |
static constexpr bool is_integer = true; |
| 712 |
static constexpr bool is_exact = true; |
| 713 |
static constexpr bool has_infinity = false; |
| 714 |
static constexpr bool has_quiet_NaN = false; |
| 715 |
static constexpr bool has_signaling_NaN = false; |
| 716 |
static constexpr std::float_denorm_style has_denorm = std::denorm_absent; |
| 717 |
static constexpr bool has_denorm_loss = false; |
| 718 |
static constexpr std::float_round_style round_style = std::round_toward_zero; |
| 719 |
static constexpr bool is_iec559 = false; |
| 720 |
static constexpr bool is_bounded = true; |
| 721 |
static constexpr bool is_modulo = true; |
| 722 |
static constexpr int digits = CHAR_BIT * sizeof(js::temporal::Uint128); |
| 723 |
static constexpr int digits10 = int(digits * /* std::log10(2) */ 0.30102999); |
| 724 |
static constexpr int max_digits10 = 0; |
| 725 |
static constexpr int radix = 2; |
| 726 |
static constexpr int min_exponent = 0; |
| 727 |
static constexpr int min_exponent10 = 0; |
| 728 |
static constexpr int max_exponent = 0; |
| 729 |
static constexpr int max_exponent10 = 0; |
| 730 |
static constexpr bool traps = true; |
| 731 |
static constexpr bool tinyness_before = false; |
| 732 |
|
| 733 |
static constexpr auto min() noexcept { return js::temporal::Uint128{}; } |
| 734 |
static constexpr auto lowest() noexcept { return min(); } |
| 735 |
static constexpr auto max() noexcept { return ~js::temporal::Uint128{}; } |
| 736 |
static constexpr auto epsilon() noexcept { return js::temporal::Uint128{}; } |
| 737 |
static constexpr auto round_error() noexcept { |
| 738 |
return js::temporal::Uint128{}; |
| 739 |
} |
| 740 |
static constexpr auto infinity() noexcept { return js::temporal::Uint128{}; } |
| 741 |
static constexpr auto quiet_NaN() noexcept { return js::temporal::Uint128{}; } |
| 742 |
static constexpr auto signaling_NaN() noexcept { |
| 743 |
return js::temporal::Uint128{}; |
| 744 |
} |
| 745 |
static constexpr auto denorm_min() noexcept { |
| 746 |
return js::temporal::Uint128{}; |
| 747 |
} |
| 748 |
}; |
| 749 |
|
| 750 |
#endif /* builtin_temporal_Int128_h */ |