| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#ifdef JS_HAS_INTL_API |
| 6 |
|
| 7 |
# include "mozilla/Compiler.h" |
| 8 |
# include "mozilla/TextUtils.h" |
| 9 |
|
| 10 |
# include <array> |
| 11 |
# include <climits> |
| 12 |
# include <cstdlib> |
| 13 |
# include <limits> |
| 14 |
# include <optional> |
| 15 |
# include <stdint.h> |
| 16 |
# include <utility> |
| 17 |
|
| 18 |
# include "builtin/temporal/Int128.h" |
| 19 |
# include "jsapi-tests/tests.h" |
| 20 |
|
| 21 |
// Use static_assert in compilers which support CWG2518. In all other cases |
| 22 |
// fall back to std::abort(). |
| 23 |
// |
| 24 |
// https://cplusplus.github.io/CWG/issues/2518.html |
| 25 |
# if defined(__clang__) |
| 26 |
# if (__clang_major__ >= 17) |
| 27 |
# define UINT128_PARSE_ERROR(...) static_assert(false, __VA_ARGS__) |
| 28 |
# endif |
| 29 |
# elif MOZ_IS_GCC |
| 30 |
# if MOZ_GCC_VERSION_AT_LEAST(13, 1, 0) |
| 31 |
# define UINT128_PARSE_ERROR(...) static_assert(false, __VA_ARGS__) |
| 32 |
# endif |
| 33 |
# endif |
| 34 |
# ifndef UINT128_PARSE_ERROR |
| 35 |
# define UINT128_PARSE_ERROR(...) std::abort() |
| 36 |
# endif |
| 37 |
|
| 38 |
using Int128 = js::temporal::Int128; |
| 39 |
using Uint128 = js::temporal::Uint128; |
| 40 |
|
| 41 |
// Simple Uint128 parser. |
| 42 |
template <char... DIGITS> |
| 43 |
constexpr Uint128 operator""_u128() { |
| 44 |
static_assert(sizeof...(DIGITS) > 0); |
| 45 |
|
| 46 |
constexpr auto digits = std::array{DIGITS...}; |
| 47 |
|
| 48 |
// Add [[maybe_unused]] everywhere to please GCC <10. |
| 49 |
|
| 50 |
[[maybe_unused]] constexpr auto isBinaryDigit = [](auto c) { |
| 51 |
return (c >= '0' && c <= '1') || c == '\''; |
| 52 |
}; |
| 53 |
|
| 54 |
[[maybe_unused]] constexpr auto isOctalDigit = [](auto c) { |
| 55 |
return (c >= '0' && c <= '7') || c == '\''; |
| 56 |
}; |
| 57 |
|
| 58 |
[[maybe_unused]] constexpr auto isDigit = [](auto c) { |
| 59 |
return mozilla::IsAsciiDigit(c) || c == '\''; |
| 60 |
}; |
| 61 |
|
| 62 |
[[maybe_unused]] constexpr auto isHexDigit = [](auto c) { |
| 63 |
return mozilla::IsAsciiHexDigit(c) || c == '\''; |
| 64 |
}; |
| 65 |
|
| 66 |
[[maybe_unused]] constexpr auto isBinary = |
| 67 |
[isBinaryDigit](auto zero, auto prefix, auto... rest) { |
| 68 |
return zero == '0' && (prefix == 'b' || prefix == 'B') && |
| 69 |
(isBinaryDigit(rest) && ...); |
| 70 |
}; |
| 71 |
|
| 72 |
[[maybe_unused]] constexpr auto isHex = [isHexDigit](auto zero, auto prefix, |
| 73 |
auto... rest) { |
| 74 |
return zero == '0' && (prefix == 'x' || prefix == 'X') && |
| 75 |
(isHexDigit(rest) && ...); |
| 76 |
}; |
| 77 |
|
| 78 |
[[maybe_unused]] constexpr auto binary = |
| 79 |
[digits]() -> std::optional<Uint128> { |
| 80 |
auto value = Uint128{}; |
| 81 |
for (size_t i = 2; i < digits.size(); ++i) { |
| 82 |
auto digit = digits[i]; |
| 83 |
if (digit == '\'') { |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
// Detect overflow. |
| 88 |
if (((value << 1) >> 1) != value) { |
| 89 |
return std::nullopt; |
| 90 |
} |
| 91 |
value = (value << 1) | Uint128{uint64_t(digit - '0')}; |
| 92 |
} |
| 93 |
return value; |
| 94 |
}; |
| 95 |
|
| 96 |
[[maybe_unused]] constexpr auto octal = [digits]() -> std::optional<Uint128> { |
| 97 |
auto value = Uint128{}; |
| 98 |
for (size_t i = 1; i < digits.size(); ++i) { |
| 99 |
auto digit = digits[i]; |
| 100 |
if (digit == '\'') { |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
// Detect overflow. |
| 105 |
if (((value << 3) >> 3) != value) { |
| 106 |
return std::nullopt; |
| 107 |
} |
| 108 |
value = (value << 3) | Uint128{uint64_t(digit - '0')}; |
| 109 |
} |
| 110 |
return value; |
| 111 |
}; |
| 112 |
|
| 113 |
[[maybe_unused]] constexpr auto decimal = |
| 114 |
[digits]() -> std::optional<Uint128> { |
| 115 |
auto value = Uint128{}; |
| 116 |
for (size_t i = 0; i < digits.size(); ++i) { |
| 117 |
auto digit = digits[i]; |
| 118 |
if (digit == '\'') { |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
// NB: Overflow check not implemented. |
| 123 |
value = (value * Uint128{10}) + Uint128{uint64_t(digit - '0')}; |
| 124 |
} |
| 125 |
return value; |
| 126 |
}; |
| 127 |
|
| 128 |
[[maybe_unused]] constexpr auto hexadecimal = |
| 129 |
[digits]() -> std::optional<Uint128> { |
| 130 |
auto value = Uint128{}; |
| 131 |
for (size_t i = 2; i < digits.size(); ++i) { |
| 132 |
auto digit = digits[i]; |
| 133 |
if (digit == '\'') { |
| 134 |
continue; |
| 135 |
} |
| 136 |
|
| 137 |
// Detect overflow. |
| 138 |
if (((value << 4) >> 4) != value) { |
| 139 |
return std::nullopt; |
| 140 |
} |
| 141 |
value = |
| 142 |
(value << 4) | Uint128{uint64_t(digit >= 'a' ? (digit - 'a') + 10 |
| 143 |
: digit >= 'A' ? (digit - 'A') + 10 |
| 144 |
: digit - '0')}; |
| 145 |
} |
| 146 |
return value; |
| 147 |
}; |
| 148 |
|
| 149 |
if constexpr (digits.size() > 2 && digits[0] == '0' && |
| 150 |
!mozilla::IsAsciiDigit(digits[1])) { |
| 151 |
if constexpr (isBinary(DIGITS...)) { |
| 152 |
if constexpr (constexpr auto value = binary()) { |
| 153 |
return *value; |
| 154 |
} else { |
| 155 |
UINT128_PARSE_ERROR("binary literal too large"); |
| 156 |
} |
| 157 |
} else if constexpr (isHex(DIGITS...)) { |
| 158 |
if constexpr (constexpr auto value = hexadecimal()) { |
| 159 |
return *value; |
| 160 |
} else { |
| 161 |
UINT128_PARSE_ERROR("hexadecimal literal too large"); |
| 162 |
} |
| 163 |
} else { |
| 164 |
UINT128_PARSE_ERROR("invalid prefix literal"); |
| 165 |
} |
| 166 |
} else if constexpr (digits.size() > 1 && digits[0] == '0') { |
| 167 |
if constexpr ((isOctalDigit(DIGITS) && ...)) { |
| 168 |
if constexpr (constexpr auto value = octal()) { |
| 169 |
return *value; |
| 170 |
} else { |
| 171 |
UINT128_PARSE_ERROR("octal literal too large"); |
| 172 |
} |
| 173 |
} else { |
| 174 |
UINT128_PARSE_ERROR("invalid octal literal"); |
| 175 |
} |
| 176 |
} else if constexpr ((isDigit(DIGITS) && ...)) { |
| 177 |
if constexpr (constexpr auto value = decimal()) { |
| 178 |
return *value; |
| 179 |
} else { |
| 180 |
UINT128_PARSE_ERROR("decimal literal too large"); |
| 181 |
} |
| 182 |
} else { |
| 183 |
UINT128_PARSE_ERROR("invalid literal"); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
template <char... DIGITS> |
| 188 |
constexpr Int128 operator""_i128() { |
| 189 |
return Int128{operator""_u128 < DIGITS...>()}; |
| 190 |
} |
| 191 |
|
| 192 |
class ConversionFixture : public JSAPIRuntimeTest { |
| 193 |
public: |
| 194 |
virtual ~ConversionFixture() = default; |
| 195 |
|
| 196 |
template <typename T, typename U, size_t N> |
| 197 |
bool testConversion(const std::array<U, N>& values); |
| 198 |
}; |
| 199 |
|
| 200 |
template <typename T, typename U, size_t N> |
| 201 |
bool ConversionFixture::testConversion(const std::array<U, N>& values) { |
| 202 |
for (auto v : values) { |
| 203 |
// Conversion to signed int. |
| 204 |
CHECK_EQUAL(int64_t(T{v}), int64_t(v)); |
| 205 |
CHECK_EQUAL(int32_t(T{v}), int32_t(v)); |
| 206 |
CHECK_EQUAL(int16_t(T{v}), int16_t(v)); |
| 207 |
CHECK_EQUAL(int8_t(T{v}), int8_t(v)); |
| 208 |
|
| 209 |
// Conversion to unsigned int. |
| 210 |
CHECK_EQUAL(uint64_t(T{v}), uint64_t(v)); |
| 211 |
CHECK_EQUAL(uint32_t(T{v}), uint32_t(v)); |
| 212 |
CHECK_EQUAL(uint16_t(T{v}), uint16_t(v)); |
| 213 |
CHECK_EQUAL(uint8_t(T{v}), uint8_t(v)); |
| 214 |
|
| 215 |
// Conversion to double. |
| 216 |
CHECK_EQUAL(double(T{v}), double(v)); |
| 217 |
|
| 218 |
// Conversion to bool. |
| 219 |
CHECK_EQUAL(bool(T{v}), bool(v)); |
| 220 |
} |
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
BEGIN_FIXTURE_TEST(ConversionFixture, testInt128_conversion) { |
| 225 |
auto values = std::array{ |
| 226 |
INT64_MIN, |
| 227 |
INT64_MIN + 1, |
| 228 |
int64_t(INT32_MIN) - 1, |
| 229 |
int64_t(INT32_MIN), |
| 230 |
int64_t(INT32_MIN) + 1, |
| 231 |
int64_t(-1), |
| 232 |
int64_t(0), |
| 233 |
int64_t(1), |
| 234 |
int64_t(INT32_MAX) - 1, |
| 235 |
int64_t(INT32_MAX), |
| 236 |
int64_t(INT32_MAX) + 1, |
| 237 |
INT64_MAX - 1, |
| 238 |
INT64_MAX, |
| 239 |
}; |
| 240 |
|
| 241 |
CHECK(testConversion<Int128>(values)); |
| 242 |
|
| 243 |
return true; |
| 244 |
} |
| 245 |
END_FIXTURE_TEST(ConversionFixture, testInt128_conversion) |
| 246 |
|
| 247 |
BEGIN_FIXTURE_TEST(ConversionFixture, testUint128_conversion) { |
| 248 |
auto values = std::array{ |
| 249 |
uint64_t(0), |
| 250 |
uint64_t(1), |
| 251 |
uint64_t(UINT32_MAX) - 1, |
| 252 |
uint64_t(UINT32_MAX), |
| 253 |
uint64_t(UINT32_MAX) + 1, |
| 254 |
UINT64_MAX - 1, |
| 255 |
UINT64_MAX, |
| 256 |
}; |
| 257 |
|
| 258 |
CHECK(testConversion<Uint128>(values)); |
| 259 |
|
| 260 |
return true; |
| 261 |
} |
| 262 |
END_FIXTURE_TEST(ConversionFixture, testUint128_conversion) |
| 263 |
|
| 264 |
class OperatorFixture : public JSAPIRuntimeTest { |
| 265 |
public: |
| 266 |
virtual ~OperatorFixture() = default; |
| 267 |
|
| 268 |
template <typename T, typename U, size_t N> |
| 269 |
bool testOperator(const std::array<U, N>& values); |
| 270 |
}; |
| 271 |
|
| 272 |
template <typename T, typename U, size_t N> |
| 273 |
bool OperatorFixture::testOperator(const std::array<U, N>& values) { |
| 274 |
// Unary operators. |
| 275 |
for (auto x : values) { |
| 276 |
// Sign operators. |
| 277 |
CHECK_EQUAL(U(+T{x}), +x); |
| 278 |
CHECK_EQUAL(U(-T{x}), -x); |
| 279 |
|
| 280 |
// Bitwise operators. |
| 281 |
CHECK_EQUAL(U(~T{x}), ~x); |
| 282 |
|
| 283 |
// Increment/Decrement operators. |
| 284 |
auto y = T{x}; |
| 285 |
CHECK_EQUAL(U(++y), x + 1); |
| 286 |
CHECK_EQUAL(U(y), x + 1); |
| 287 |
|
| 288 |
y = T{x}; |
| 289 |
CHECK_EQUAL(U(y++), x); |
| 290 |
CHECK_EQUAL(U(y), x + 1); |
| 291 |
|
| 292 |
y = T{x}; |
| 293 |
CHECK_EQUAL(U(--y), x - 1); |
| 294 |
CHECK_EQUAL(U(y), x - 1); |
| 295 |
|
| 296 |
y = T{x}; |
| 297 |
CHECK_EQUAL(U(y--), x); |
| 298 |
CHECK_EQUAL(U(y), x - 1); |
| 299 |
} |
| 300 |
|
| 301 |
// Binary operators. |
| 302 |
for (auto x : values) { |
| 303 |
for (auto y : values) { |
| 304 |
// Comparison operators. |
| 305 |
CHECK_EQUAL((T{x} == T{y}), (x == y)); |
| 306 |
CHECK_EQUAL((T{x} != T{y}), (x != y)); |
| 307 |
CHECK_EQUAL((T{x} < T{y}), (x < y)); |
| 308 |
CHECK_EQUAL((T{x} <= T{y}), (x <= y)); |
| 309 |
CHECK_EQUAL((T{x} > T{y}), (x > y)); |
| 310 |
CHECK_EQUAL((T{x} >= T{y}), (x >= y)); |
| 311 |
|
| 312 |
// Add/Sub/Mul operators. |
| 313 |
CHECK_EQUAL(U(T{x} + T{y}), (x + y)); |
| 314 |
CHECK_EQUAL(U(T{x} - T{y}), (x - y)); |
| 315 |
CHECK_EQUAL(U(T{x} * T{y}), (x * y)); |
| 316 |
|
| 317 |
// Division operators. |
| 318 |
if (y != 0) { |
| 319 |
CHECK_EQUAL(U(T{x} / T{y}), (x / y)); |
| 320 |
CHECK_EQUAL(U(T{x} % T{y}), (x % y)); |
| 321 |
} |
| 322 |
|
| 323 |
// Shift operators. |
| 324 |
if (y >= 0) { |
| 325 |
CHECK_EQUAL(U(T{x} << y), (x << y)); |
| 326 |
CHECK_EQUAL(U(T{x} >> y), (x >> y)); |
| 327 |
} |
| 328 |
|
| 329 |
// Bitwise operators. |
| 330 |
CHECK_EQUAL(U(T{x} & T{y}), (x & y)); |
| 331 |
CHECK_EQUAL(U(T{x} | T{y}), (x | y)); |
| 332 |
CHECK_EQUAL(U(T{x} ^ T{y}), (x ^ y)); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
// Compound assignment operators. |
| 337 |
for (auto x : values) { |
| 338 |
for (auto y : values) { |
| 339 |
auto z = T{x}; |
| 340 |
z += T{y}; |
| 341 |
CHECK_EQUAL(U(z), x + y); |
| 342 |
|
| 343 |
z = T{x}; |
| 344 |
z -= T{y}; |
| 345 |
CHECK_EQUAL(U(z), x - y); |
| 346 |
|
| 347 |
z = T{x}; |
| 348 |
z *= T{y}; |
| 349 |
CHECK_EQUAL(U(z), x * y); |
| 350 |
|
| 351 |
if (y != 0) { |
| 352 |
z = T{x}; |
| 353 |
z /= T{y}; |
| 354 |
CHECK_EQUAL(U(z), x / y); |
| 355 |
|
| 356 |
z = T{x}; |
| 357 |
z %= T{y}; |
| 358 |
CHECK_EQUAL(U(z), x % y); |
| 359 |
} |
| 360 |
|
| 361 |
if (y >= 0) { |
| 362 |
z = T{x}; |
| 363 |
z <<= y; |
| 364 |
CHECK_EQUAL(U(z), x << y); |
| 365 |
|
| 366 |
z = T{x}; |
| 367 |
z >>= y; |
| 368 |
CHECK_EQUAL(U(z), x >> y); |
| 369 |
} |
| 370 |
|
| 371 |
z = T{x}; |
| 372 |
z &= T{y}; |
| 373 |
CHECK_EQUAL(U(z), x & y); |
| 374 |
|
| 375 |
z = T{x}; |
| 376 |
z |= T{y}; |
| 377 |
CHECK_EQUAL(U(z), x | y); |
| 378 |
|
| 379 |
z = T{x}; |
| 380 |
z ^= T{y}; |
| 381 |
CHECK_EQUAL(U(z), x ^ y); |
| 382 |
} |
| 383 |
} |
| 384 |
return true; |
| 385 |
} |
| 386 |
|
| 387 |
BEGIN_FIXTURE_TEST(OperatorFixture, testInt128_operator) { |
| 388 |
auto values = std::array{ |
| 389 |
int64_t(-3), int64_t(-2), int64_t(-1), int64_t(0), |
| 390 |
int64_t(1), int64_t(2), int64_t(3), int64_t(63), |
| 391 |
}; |
| 392 |
|
| 393 |
CHECK(testOperator<Int128>(values)); |
| 394 |
|
| 395 |
// Values larger than INT64_MAX. |
| 396 |
CHECK((Int128{INT64_MAX} * Int128{2}) == |
| 397 |
(Int128{INT64_MAX} + Int128{INT64_MAX})); |
| 398 |
CHECK((Int128{INT64_MAX} * Int128{3}) == |
| 399 |
(Int128{INT64_MAX} * Int128{4} - Int128{INT64_MAX})); |
| 400 |
CHECK((Int128{INT64_MAX} * Int128{2}) == (Int128{INT64_MAX} << 1)); |
| 401 |
CHECK((Int128{INT64_MAX} * Int128{8}) == (Int128{INT64_MAX} << 3)); |
| 402 |
CHECK((Int128{INT64_MAX} * Int128{8} / Int128{2}) == |
| 403 |
(Int128{INT64_MAX} << 2)); |
| 404 |
CHECK((Int128{INT64_MAX} * Int128{23} % Int128{13}) == (Int128{5})); |
| 405 |
|
| 406 |
// Values smaller than INT64_MIN. |
| 407 |
CHECK((Int128{INT64_MIN} * Int128{2}) == |
| 408 |
(Int128{INT64_MIN} + Int128{INT64_MIN})); |
| 409 |
CHECK((Int128{INT64_MIN} * Int128{3}) == |
| 410 |
(Int128{INT64_MIN} * Int128{4} - Int128{INT64_MIN})); |
| 411 |
CHECK((Int128{INT64_MIN} * Int128{2}) == (Int128{INT64_MIN} << 1)); |
| 412 |
CHECK((Int128{INT64_MIN} * Int128{8}) == (Int128{INT64_MIN} << 3)); |
| 413 |
CHECK((Int128{INT64_MIN} * Int128{8} / Int128{2}) == |
| 414 |
(Int128{INT64_MIN} << 2)); |
| 415 |
CHECK((Int128{INT64_MIN} * Int128{23} % Int128{13}) == (Int128{-2})); |
| 416 |
|
| 417 |
return true; |
| 418 |
} |
| 419 |
END_FIXTURE_TEST(OperatorFixture, testInt128_operator) |
| 420 |
|
| 421 |
BEGIN_FIXTURE_TEST(OperatorFixture, testUint128_operator) { |
| 422 |
auto values = std::array{ |
| 423 |
uint64_t(0), uint64_t(1), uint64_t(2), |
| 424 |
uint64_t(3), uint64_t(5), uint64_t(63), |
| 425 |
}; |
| 426 |
|
| 427 |
CHECK(testOperator<Uint128>(values)); |
| 428 |
|
| 429 |
// Values larger than UINT64_MAX. |
| 430 |
CHECK((Uint128{UINT64_MAX} * Uint128{2}) == |
| 431 |
(Uint128{UINT64_MAX} + Uint128{UINT64_MAX})); |
| 432 |
CHECK((Uint128{UINT64_MAX} * Uint128{3}) == |
| 433 |
(Uint128{UINT64_MAX} * Uint128{4} - Uint128{UINT64_MAX})); |
| 434 |
CHECK((Uint128{UINT64_MAX} * Uint128{2}) == (Uint128{UINT64_MAX} << 1)); |
| 435 |
CHECK((Uint128{UINT64_MAX} * Uint128{8}) == (Uint128{UINT64_MAX} << 3)); |
| 436 |
CHECK((Uint128{UINT64_MAX} * Uint128{8} / Uint128{2}) == |
| 437 |
(Uint128{UINT64_MAX} << 2)); |
| 438 |
CHECK((Uint128{UINT64_MAX} * Uint128{23} % Uint128{13}) == (Uint128{7})); |
| 439 |
|
| 440 |
return true; |
| 441 |
} |
| 442 |
END_FIXTURE_TEST(OperatorFixture, testUint128_operator) |
| 443 |
|
| 444 |
BEGIN_TEST(testInt128_literal) { |
| 445 |
CHECK_EQUAL(int64_t(0x7fff'ffff'ffff'ffff_i128), INT64_MAX); |
| 446 |
CHECK_EQUAL(int64_t(-0x8000'0000'0000'0000_i128), INT64_MIN); |
| 447 |
|
| 448 |
CHECK(std::numeric_limits<Int128>::max() == |
| 449 |
0x7fff'ffff'ffff'ffff'ffff'ffff'ffff'ffff_i128); |
| 450 |
CHECK(std::numeric_limits<Int128>::min() == |
| 451 |
-0x8000'0000'0000'0000'0000'0000'0000'0000_i128); |
| 452 |
|
| 453 |
auto x = (Int128{INT64_MAX} + Int128{1}) * Int128{3}; |
| 454 |
CHECK(x == 27670116110564327424_i128); |
| 455 |
CHECK(x == 0x1'8000'0000'0000'0000_i128); |
| 456 |
|
| 457 |
auto y = Int128{0} - (Int128{5} * Int128{INT64_MAX}); |
| 458 |
CHECK(y == -46116860184273879035_i128); |
| 459 |
CHECK(y == -0x2'7fff'ffff'ffff'fffb_i128); |
| 460 |
|
| 461 |
// NB: This shift expression overflows. |
| 462 |
auto z = Int128{0x1122'3344} << 100; |
| 463 |
CHECK(z == 0x1223'3440'0000'0000'0000'0000'0000'0000_i128); |
| 464 |
CHECK(z == 0221063210000000000000000000000000000000000_i128); |
| 465 |
CHECK(z == 24108894070078995479046745700448600064_i128); |
| 466 |
CHECK( |
| 467 |
z == |
| 468 |
0b10010001000110011010001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_i128); |
| 469 |
|
| 470 |
z >>= 80; |
| 471 |
CHECK(z == 0X1223'3440'0000_i128); |
| 472 |
CHECK(z == 0442146420000000_i128); |
| 473 |
CHECK(z == 19942409764864_i128); |
| 474 |
CHECK(z == 0B100100010001100110100010000000000000000000000_i128); |
| 475 |
|
| 476 |
auto v = Int128{INT64_MAX} * Int128{INT64_MAX}; |
| 477 |
CHECK(v == 0x3fff'ffff'ffff'ffff'0000'0000'0000'0001_i128); |
| 478 |
CHECK((v + v) == 0x7fff'ffff'ffff'fffe'0000'0000'0000'0002_i128); |
| 479 |
CHECK((v * v) == 0x7fff'ffff'ffff'fffe'0000'0000'0000'0001_i128); |
| 480 |
CHECK((v * -v) == -0x7fff'ffff'ffff'fffe'0000'0000'0000'0001_i128); |
| 481 |
CHECK((-v * v) == -0x7fff'ffff'ffff'fffe'0000'0000'0000'0001_i128); |
| 482 |
CHECK((-v * -v) == 0x7fff'ffff'ffff'fffe'0000'0000'0000'0001_i128); |
| 483 |
|
| 484 |
auto w = Int128{INT64_MIN} * Int128{INT64_MIN}; |
| 485 |
CHECK(w == 0x4000'0000'0000'0000'0000'0000'0000'0000_i128); |
| 486 |
CHECK((w + w) == -0x8000'0000'0000'0000'0000'0000'0000'0000_i128); |
| 487 |
CHECK((w * w) == 0_i128); |
| 488 |
|
| 489 |
CHECK((Int128{1} << 120) == 0x100'0000'0000'0000'0000'0000'0000'0000_i128); |
| 490 |
|
| 491 |
return true; |
| 492 |
} |
| 493 |
END_TEST(testInt128_literal) |
| 494 |
|
| 495 |
BEGIN_TEST(testUint128_literal) { |
| 496 |
CHECK_EQUAL(uint64_t(0xffff'ffff'ffff'ffff_u128), UINT64_MAX); |
| 497 |
|
| 498 |
CHECK(std::numeric_limits<Uint128>::max() == |
| 499 |
0xffff'ffff'ffff'ffff'ffff'ffff'ffff'ffff_u128); |
| 500 |
|
| 501 |
auto x = (Uint128{UINT64_MAX} + Uint128{3}) * Uint128{3}; |
| 502 |
CHECK(x == 55340232221128654854_u128); |
| 503 |
CHECK(x == 0x3'0000'0000'0000'0006_u128); |
| 504 |
|
| 505 |
// NB: This shift expression overflows. |
| 506 |
auto z = Uint128{0x1122'3344} << 100; |
| 507 |
CHECK(z == 0x1223'3440'0000'0000'0000'0000'0000'0000_u128); |
| 508 |
CHECK(z == 0221063210000000000000000000000000000000000_u128); |
| 509 |
CHECK(z == 24108894070078995479046745700448600064_u128); |
| 510 |
CHECK( |
| 511 |
z == |
| 512 |
0b10010001000110011010001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128); |
| 513 |
|
| 514 |
z >>= 80; |
| 515 |
CHECK(z == 0X1223'3440'0000_u128); |
| 516 |
CHECK(z == 0442146420000000_u128); |
| 517 |
CHECK(z == 19942409764864_u128); |
| 518 |
CHECK(z == 0B100100010001100110100010000000000000000000000_u128); |
| 519 |
|
| 520 |
auto v = Uint128{UINT64_MAX} * Uint128{UINT64_MAX}; |
| 521 |
CHECK(v == 0xffff'ffff'ffff'fffe'0000'0000'0000'0001_u128); |
| 522 |
CHECK((v + v) == 0xffff'ffff'ffff'fffc'0000'0000'0000'0002_u128); |
| 523 |
CHECK((v * v) == 0xffff'ffff'ffff'fffc'0000'0000'0000'0001_u128); |
| 524 |
CHECK((v * -v) == 0x3'ffff'ffff'ffff'ffff_u128); |
| 525 |
CHECK((-v * v) == 0x3'ffff'ffff'ffff'ffff_u128); |
| 526 |
CHECK((-v * -v) == 0xffff'ffff'ffff'fffc'0000'0000'0000'0001_u128); |
| 527 |
|
| 528 |
CHECK((Uint128{1} << 120) == 0x100'0000'0000'0000'0000'0000'0000'0000_u128); |
| 529 |
|
| 530 |
return true; |
| 531 |
} |
| 532 |
END_TEST(testUint128_literal) |
| 533 |
|
| 534 |
BEGIN_TEST(testInt128_division) { |
| 535 |
auto x = Int128{INT64_MAX} * Int128{4}; |
| 536 |
CHECK((x / Int128{2}) == 0xffff'ffff'ffff'fffe_i128); |
| 537 |
CHECK((x / Int128{2}) == (x >> 1)); |
| 538 |
|
| 539 |
auto y = Int128{INT64_MAX} * Int128{16}; |
| 540 |
CHECK((y / Int128{2}) == 0x3'ffff'ffff'ffff'fff8_i128); |
| 541 |
CHECK((y / Int128{2}) == (y >> 1)); |
| 542 |
|
| 543 |
CHECK((0x1122'3344'5566'7788'aabb'ccdd'ff12'3456_i128 / 7_i128) == |
| 544 |
0x272'999c'0c33'35a5'cf3f'6668'db4b'be55_i128); |
| 545 |
CHECK((0x1122'3344'5566'7788'aabb'ccdd'ff12'3456_i128 / |
| 546 |
0x1'2345'6789'abcd'ef11'abcd'ef11_i128) == 0xf0f0f0f_i128); |
| 547 |
CHECK((7_i128 / 0x1122'3344'5566'7788'aabb'ccdd'ff12'3456_i128) == 0_i128); |
| 548 |
|
| 549 |
CHECK((0x1122'3344'5566'7788'aabb'ccdd'ff12'3456_i128 % 7_i128) == 3_i128); |
| 550 |
CHECK((0x1122'3344'5566'7788'aabb'ccdd'ff12'3456_i128 % |
| 551 |
0x1'2345'6789'abcd'ef11'abcd'ef11_i128) == |
| 552 |
0x1122'3353'7d8e'9fb0'dc00'3357_i128); |
| 553 |
CHECK((7_i128 % 0x1122'3344'5566'7788'aabb'ccdd'ff12'3456_i128) == 7_i128); |
| 554 |
|
| 555 |
return true; |
| 556 |
} |
| 557 |
END_TEST(testInt128_division) |
| 558 |
|
| 559 |
BEGIN_TEST(testInt128_abs) { |
| 560 |
CHECK((0_i128).abs() == 0_u128); |
| 561 |
|
| 562 |
CHECK((0x1122'3344_i128).abs() == 0x1122'3344_u128); |
| 563 |
CHECK((-0x1122'3344_i128).abs() == 0x1122'3344_u128); |
| 564 |
|
| 565 |
CHECK((0x1111'2222'3333'4444'5555'6666'7777'8888_i128).abs() == |
| 566 |
0x1111'2222'3333'4444'5555'6666'7777'8888_u128); |
| 567 |
CHECK((-0x1111'2222'3333'4444'5555'6666'7777'8888_i128).abs() == |
| 568 |
0x1111'2222'3333'4444'5555'6666'7777'8888_u128); |
| 569 |
|
| 570 |
CHECK(std::numeric_limits<Int128>::min().abs() == |
| 571 |
0x8000'0000'0000'0000'0000'0000'0000'0000_u128); |
| 572 |
CHECK(std::numeric_limits<Int128>::max().abs() == |
| 573 |
0x7fff'ffff'ffff'ffff'ffff'ffff'ffff'ffff_u128); |
| 574 |
|
| 575 |
return true; |
| 576 |
} |
| 577 |
END_TEST(testInt128_abs) |
| 578 |
|
| 579 |
#endif |