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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
|
From: Shen-Ta Hsieh <beststeve@secondstate.io>
Date: Mon, 30 Sep 2024 10:59:39 +0800
Subject: Remove patch for fmt, add support fmt version 9, fix #3782
Signed-off-by: Shen-Ta Hsieh <beststeve@secondstate.io>
---
...arithmetic-operations-in-uint128_fallback.patch | 201 -------
cmake/Helper.cmake | 2 -
include/common/int128.h | 601 ++++++++++++++-------
.../executor/engine/unary_numeric_vector_msvc.ipp | 2 +-
include/runtime/instance/global.h | 2 +-
include/runtime/instance/memory.h | 2 +-
lib/api/wasmedge.cpp | 3 +-
lib/driver/runtimeTool.cpp | 2 +-
lib/executor/engine/refInstr.cpp | 4 +-
lib/executor/helper.cpp | 8 +-
lib/loader/ast/instruction.cpp | 7 +-
test/common/int128Test.cpp | 91 ++--
12 files changed, 461 insertions(+), 464 deletions(-)
delete mode 100644 cmake/0001-support-arithmetic-operations-in-uint128_fallback.patch
diff --git a/cmake/0001-support-arithmetic-operations-in-uint128_fallback.patch b/cmake/0001-support-arithmetic-operations-in-uint128_fallback.patch
deleted file mode 100644
index 9f60c96..0000000
--- a/cmake/0001-support-arithmetic-operations-in-uint128_fallback.patch
+++ /dev/null
@@ -1,201 +0,0 @@
-From 707dc7d2fe66629aeea8ac1afac7e26372245193 Mon Sep 17 00:00:00 2001
-From: Shen-Ta Hsieh <ibmibmibm.tw@gmail.com>
-Date: Thu, 29 Aug 2024 17:08:27 +0800
-Subject: [PATCH] support arithmetic operations in uint128_fallback
-
----
- include/fmt/format.h | 154 ++++++++++++++++++++++++++++++++++++++++++-
- 1 file changed, 151 insertions(+), 3 deletions(-)
-
-diff --git a/include/fmt/format.h b/include/fmt/format.h
-index 67f0ab7..629c9a4 100644
---- a/include/fmt/format.h
-+++ b/include/fmt/format.h
-@@ -317,6 +317,65 @@ inline auto is_big_endian() -> bool {
- #endif
- }
-
-+FMT_CONSTEXPR20 inline auto countl_zero(uint32_t n) -> int;
-+FMT_CONSTEXPR20 inline auto udiv128by64to64(uint64_t u1, uint64_t u0, uint64_t v, uint64_t &r) {
-+ if (!is_constant_evaluated()) {
-+#if FMT_MSC_VERSION && !FMT_CLANG_VERSION && defined(_M_X64)
-+ return _udiv128(u1, u0, v, &r);
-+#elif defined(__x86_64__)
-+ uint64_t result = 0;
-+ __asm__("divq %[v]" : "=a"(result), "=d"(r) : [v]"r"(v), "a"(u0), "d"(u1));
-+ return result;
-+#endif
-+ }
-+ const uint32_t v0 = static_cast<uint32_t>(v);
-+ const uint32_t v1 = static_cast<uint32_t>(v >> 32);
-+ if (v1 == 0) {
-+ auto rem = (u1 << 32) | (u0 >> 32);
-+ auto result = rem / v0;
-+ rem = ((rem % v0) << 32) | static_cast<uint32_t>(u0);
-+ result = (result << 32) | (rem / v0);
-+ r = rem % v0;
-+ return result;
-+ }
-+ uint64_t un64 = 0, un10 = 0;
-+ const auto s = countl_zero(v1);
-+ if (s > 0) {
-+ v <<= s;
-+ un64 = (u1 << s) | (u0 >> (64 - s));
-+ un10 = u0 << s;
-+ } else {
-+ un64 = u1;
-+ un10 = u0;
-+ }
-+ uint64_t vn1 = static_cast<uint32_t>(v >> 32);
-+ uint64_t vn0 = static_cast<uint32_t>(v);
-+ uint64_t un1 = static_cast<uint32_t>(un10 >> 32);
-+ uint64_t un0 = static_cast<uint32_t>(un10);
-+ uint64_t q1 = un64 / vn1;
-+ uint64_t rhat = un64 - q1 * vn1;
-+ while ((q1 >> 32) >= 1 || q1 * vn0 > (rhat << 32) + un1) {
-+ --q1;
-+ rhat += vn1;
-+ if ((rhat >> 32) >= 1) {
-+ break;
-+ }
-+ }
-+
-+ uint64_t un21 = (un64 << 32) + un1 - q1 * v;
-+ uint64_t q0 = un21 / vn1;
-+ rhat = un21 - q0 * vn1;
-+ while ((q0 >> 32) >= 1 || q0 * vn0 > (rhat << 32) + un0) {
-+ --q0;
-+ rhat += vn1;
-+ if ((rhat >> 32) >= 1) {
-+ break;
-+ }
-+ }
-+ r = ((un21 << 32) + un0 - q0 * v) >> s;
-+ return (q1 << 32) + q0;
-+}
-+
- class uint128_fallback {
- private:
- uint64_t lo_, hi_;
-@@ -345,6 +404,18 @@ class uint128_fallback {
- const uint128_fallback& rhs) -> bool {
- return lhs.hi_ != rhs.hi_ ? lhs.hi_ > rhs.hi_ : lhs.lo_ > rhs.lo_;
- }
-+ friend constexpr auto operator<(const uint128_fallback& lhs,
-+ int rhs) -> bool {
-+ return rhs >= 0 && lhs.hi_ == 0 && lhs.lo_ < static_cast<unsigned int>(rhs);
-+ }
-+ friend constexpr auto operator<(const uint128_fallback& lhs,
-+ const uint128_fallback& rhs) -> bool {
-+ return lhs.hi_ < rhs.hi_ || (lhs.hi_ == rhs.hi_ && lhs.lo_ < rhs.lo_);
-+ }
-+ friend constexpr auto operator>=(const uint128_fallback& lhs,
-+ int rhs) -> bool {
-+ return !(lhs < rhs);
-+ }
- friend constexpr auto operator|(const uint128_fallback& lhs,
- const uint128_fallback& rhs)
- -> uint128_fallback {
-@@ -426,6 +497,41 @@ class uint128_fallback {
- #endif
- return *this;
- }
-+ constexpr auto operator%=(unsigned int n) -> uint128_fallback& {
-+ if (hi_ == 0 && lo_ < n) {
-+ return *this;
-+ }
-+ uint64_t rem = 0;
-+ udiv128by64to64(hi_ % n, lo_, n, rem);
-+ hi_ = 0;
-+ lo_ = rem;
-+ return *this;
-+ }
-+ friend constexpr auto operator%(const uint128_fallback& lhs,
-+ unsigned int rhs) -> uint128_fallback {
-+ auto result = uint128_fallback(lhs);
-+ result %= rhs;
-+ return result;
-+ }
-+ constexpr auto operator/=(unsigned int rhs) -> uint128_fallback& {
-+ if (hi_ == 0 && lo_ < rhs) {
-+ lo_ = 0;
-+ return *this;
-+ }
-+ if (hi_ < rhs) {
-+ uint64_t rem = 0;
-+ uint64_t q_lo = udiv128by64to64(hi_, lo_, rhs, rem);
-+ lo_ = q_lo;
-+ hi_ = 0;
-+ return *this;
-+ }
-+ uint64_t q_hi = hi_ / rhs;
-+ uint64_t rem = 0;
-+ uint64_t q_lo = udiv128by64to64(hi_ % rhs, lo_, rhs, rem);
-+ lo_ = q_lo;
-+ hi_ = q_hi;
-+ return *this;
-+ }
- };
-
- using uint128_t = conditional_t<FMT_USE_INT128, uint128_opt, uint128_fallback>;
-@@ -1144,12 +1250,54 @@ template <typename T> FMT_CONSTEXPR auto count_digits_fallback(T n) -> int {
- count += 4;
- }
- }
--#if FMT_USE_INT128
--FMT_CONSTEXPR inline auto count_digits(uint128_opt n) -> int {
-- return count_digits_fallback(n);
-+
-+#ifdef FMT_BUILTIN_CLZLL
-+inline auto do_count_digits(uint64_t n) -> int;
-+inline auto do_count_digits(uint128_t n) -> int {
-+ if (static_cast<uint64_t>(n >> 64) == 0) {
-+ return do_count_digits(static_cast<uint64_t>(n));
-+ }
-+ // Maps bsr(n) to ceil(log10(pow(2, bsr(n) + 1) - 1)).
-+ static constexpr uint8_t bsr2log10[] = {
-+ 20, 20, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25,
-+ 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29,
-+ 30, 30, 30, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 34, 34, 34,
-+ 35, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 38, 39, 39};
-+ auto c = FMT_BUILTIN_CLZLL(static_cast<uint64_t>(n >> 64));
-+ auto t = bsr2log10[c ^ 63];
-+ static constexpr const uint128_t powers_of_10[] = {
-+ 0,
-+ (uint128_t(5ULL) << 64) | uint128_t(7766279631452241920ULL),
-+ (uint128_t(54ULL) << 64) | uint128_t(3875820019684212736ULL),
-+ (uint128_t(542ULL) << 64) | uint128_t(1864712049423024128ULL),
-+ (uint128_t(5421ULL) << 64) | uint128_t(200376420520689664ULL),
-+ (uint128_t(54210ULL) << 64) | uint128_t(2003764205206896640ULL),
-+ (uint128_t(542101ULL) << 64) | uint128_t(1590897978359414784ULL),
-+ (uint128_t(5421010ULL) << 64) | uint128_t(15908979783594147840ULL),
-+ (uint128_t(54210108ULL) << 64) | uint128_t(11515845246265065472ULL),
-+ (uint128_t(542101086ULL) << 64) | uint128_t(4477988020393345024ULL),
-+ (uint128_t(5421010862ULL) << 64) | uint128_t(7886392056514347008ULL),
-+ (uint128_t(54210108624ULL) << 64) | uint128_t(5076944270305263616ULL),
-+ (uint128_t(542101086242ULL) << 64) | uint128_t(13875954555633532928ULL),
-+ (uint128_t(5421010862427ULL) << 64) | uint128_t(9632337040368467968ULL),
-+ (uint128_t(54210108624275ULL) << 64) | uint128_t(4089650035136921600ULL),
-+ (uint128_t(542101086242752ULL) << 64) | uint128_t(4003012203950112768ULL),
-+ (uint128_t(5421010862427522ULL) << 64) | uint128_t(3136633892082024448ULL),
-+ (uint128_t(54210108624275221ULL) << 64) | uint128_t(12919594847110692864ULL),
-+ (uint128_t(542101086242752217ULL) << 64) | uint128_t(68739955140067328ULL),
-+ (uint128_t(5421010862427522170ULL) << 64) | uint128_t(687399551400673280ULL),
-+ };
-+ return t - (n < powers_of_10[t - 20]);
- }
- #endif
-
-+FMT_CONSTEXPR20 inline auto count_digits(uint128_t n) -> int {
-+#ifdef FMT_BUILTIN_CLZLL
-+ if (!is_constant_evaluated()) return do_count_digits(n);
-+#endif
-+ return count_digits_fallback(n);
-+}
-+
- #ifdef FMT_BUILTIN_CLZLL
- // It is a separate function rather than a part of count_digits to workaround
- // the lack of static constexpr in constexpr functions.
---
-2.46.0.windows.1
-
diff --git a/cmake/Helper.cmake b/cmake/Helper.cmake
index c7b58ea..34a00ee 100644
--- a/cmake/Helper.cmake
+++ b/cmake/Helper.cmake
@@ -349,8 +349,6 @@ function(wasmedge_setup_spdlog)
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 11.0.2
GIT_SHALLOW TRUE
- PATCH_COMMAND "${GIT_CMD}" checkout 11.0.2 .
- COMMAND "${GIT_CMD}" "apply" "--whitespace=fix" "${CMAKE_SOURCE_DIR}/cmake/0001-support-arithmetic-operations-in-uint128_fallback.patch"
)
set(FMT_INSTALL OFF CACHE BOOL "Generate the install target." FORCE)
FetchContent_MakeAvailable(fmt)
diff --git a/include/common/int128.h b/include/common/int128.h
index 41a32fc..6756e29 100644
--- a/include/common/int128.h
+++ b/include/common/int128.h
@@ -13,17 +13,10 @@
//===----------------------------------------------------------------------===//
#pragma once
-// If there is a built-in type __int128, then use it directly
-#if defined(__x86_64__) || defined(__aarch64__) || \
- (defined(__riscv) && __riscv_xlen == 64)
-namespace WasmEdge {
-using int128_t = __int128;
-using uint128_t = unsigned __int128;
-} // namespace WasmEdge
-#else
-
#if defined(_MSC_VER) && !defined(__clang__)
+#pragma intrinsic(_BitScanReverse)
#pragma intrinsic(_BitScanReverse64)
+#include <immintrin.h>
#endif
// We have to detect for those environments who don't support __int128 type
// natively.
@@ -40,28 +33,113 @@ using uint128_t = unsigned __int128;
namespace WasmEdge {
-class int128_t;
-class uint128_t;
+inline constexpr int clz(uint32_t V) noexcept {
+#if defined(_MSC_VER) && !defined(__clang__)
+ if (V) {
+ unsigned long LeadingZero = 0;
+ _BitScanReverse(&LeadingZero, V);
+ return 31 ^ static_cast<int>(LeadingZero);
+ }
+ return 32;
+#else
+ return __builtin_clz(V);
+#endif
+}
+
+inline constexpr int clz(uint64_t V) noexcept {
+#if defined(_MSC_VER) && !defined(__clang__)
+ if (V) {
+ unsigned long LeadingZero = 0;
+ _BitScanReverse64(&LeadingZero, V);
+ return 63 ^ static_cast<int>(LeadingZero);
+ }
+ return 64;
+#else
+ return __builtin_clzll(V);
+#endif
+}
+
+inline auto udiv128by64to64(uint64_t U1, uint64_t U0, uint64_t V,
+ uint64_t &R) noexcept {
+ {
+#if defined(_M_X64) && defined(_MSC_VER) && !defined(__clang__)
+ return _udiv128(U1, U0, V, &R);
+#elif defined(__x86_64__)
+ uint64_t Result = 0;
+ __asm__("divq %[v]" : "=a"(Result), "=d"(R) : [v] "r"(V), "a"(U0), "d"(U1));
+ return Result;
+#endif
+ }
+ const uint32_t V0 = static_cast<uint32_t>(V);
+ const uint32_t V1 = static_cast<uint32_t>(V >> 32);
+ if (V1 == 0) {
+ auto Rem = (U1 << 32) | (U0 >> 32);
+ auto Result = Rem / V0;
+ Rem = ((Rem % V0) << 32) | static_cast<uint32_t>(U0);
+ Result = (Result << 32) | (Rem / V0);
+ R = Rem % V0;
+ return Result;
+ }
+ uint64_t Un64 = 0, Un10 = 0;
+ const auto s = clz(V1);
+ if (s > 0) {
+ V <<= s;
+ Un64 = (U1 << s) | (U0 >> (64 - s));
+ Un10 = U0 << s;
+ } else {
+ Un64 = U1;
+ Un10 = U0;
+ }
+ uint64_t Vn1 = static_cast<uint32_t>(V >> 32);
+ uint64_t Vn0 = static_cast<uint32_t>(V);
+ uint64_t Un1 = static_cast<uint32_t>(Un10 >> 32);
+ uint64_t Un0 = static_cast<uint32_t>(Un10);
+ uint64_t Q1 = Un64 / Vn1;
+ uint64_t Rhat = Un64 - Q1 * Vn1;
+ while ((Q1 >> 32) >= 1 || Q1 * Vn0 > (Rhat << 32) + Un1) {
+ --Q1;
+ Rhat += Vn1;
+ if ((Rhat >> 32) >= 1) {
+ break;
+ }
+ }
+
+ uint64_t Un21 = (Un64 << 32) + Un1 - Q1 * V;
+ uint64_t Q0 = Un21 / Vn1;
+ Rhat = Un21 - Q0 * Vn1;
+ while ((Q0 >> 32) >= 1 || Q0 * Vn0 > (Rhat << 32) + Un0) {
+ --Q0;
+ Rhat += Vn1;
+ if ((Rhat >> 32) >= 1) {
+ break;
+ }
+ }
+ R = ((Un21 << 32) + Un0 - Q0 * V) >> s;
+ return (Q1 << 32) + Q0;
+}
+
+class int128;
+class uint128;
-class uint128_t {
+class uint128 {
public:
- uint128_t() noexcept = default;
- constexpr uint128_t(const uint128_t &) noexcept = default;
- constexpr uint128_t(uint128_t &&) noexcept = default;
- constexpr uint128_t &operator=(const uint128_t &V) noexcept = default;
- constexpr uint128_t &operator=(uint128_t &&V) noexcept = default;
-
- constexpr uint128_t(int V) noexcept
- : Low(V), High(V < 0 ? std::numeric_limits<uint64_t>::max() : 0) {}
- constexpr uint128_t(long V) noexcept
- : Low(V), High(V < 0 ? std::numeric_limits<uint64_t>::max() : 0) {}
- constexpr uint128_t(long long V) noexcept
- : Low(V), High(V < 0 ? std::numeric_limits<uint64_t>::max() : 0) {}
- constexpr uint128_t(unsigned int V) noexcept : Low(V), High(0) {}
- constexpr uint128_t(unsigned long V) noexcept : Low(V), High(0) {}
- constexpr uint128_t(unsigned long long V) noexcept : Low(V), High(0) {}
- constexpr uint128_t(int128_t V) noexcept;
- constexpr uint128_t(uint64_t H, uint64_t L) noexcept : Low(L), High(H) {}
+ uint128() noexcept = default;
+ constexpr uint128(const uint128 &) noexcept = default;
+ constexpr uint128(uint128 &&) noexcept = default;
+ constexpr uint128 &operator=(const uint128 &V) noexcept = default;
+ constexpr uint128 &operator=(uint128 &&V) noexcept = default;
+
+ constexpr uint128(unsigned int V) noexcept : Low(V), High(0) {}
+ constexpr uint128(unsigned long V) noexcept : Low(V), High(0) {}
+ constexpr uint128(unsigned long long V) noexcept : Low(V), High(0) {}
+ constexpr uint128(int128 V) noexcept;
+ constexpr uint128(uint64_t H, uint64_t L) noexcept : Low(L), High(H) {}
+
+#if defined(__x86_64__) || defined(__aarch64__) || \
+ (defined(__riscv) && __riscv_xlen == 64)
+ constexpr uint128(unsigned __int128 V) noexcept
+ : Low(static_cast<uint64_t>(V)), High(static_cast<uint64_t>(V >> 64)) {}
+#endif
constexpr operator bool() const noexcept {
return static_cast<bool>(Low) || static_cast<bool>(High);
@@ -79,114 +157,127 @@ public:
return static_cast<uint64_t>(Low);
}
- constexpr uint128_t &operator=(int V) noexcept {
- return *this = uint128_t(V);
- }
- constexpr uint128_t &operator=(long V) noexcept {
- return *this = uint128_t(V);
- }
- constexpr uint128_t &operator=(long long V) noexcept {
- return *this = uint128_t(V);
+ constexpr uint128 &operator=(unsigned int V) noexcept {
+ return *this = uint128(V);
}
- constexpr uint128_t &operator=(unsigned int V) noexcept {
- return *this = uint128_t(V);
+ constexpr uint128 &operator=(unsigned long V) noexcept {
+ return *this = uint128(V);
}
- constexpr uint128_t &operator=(unsigned long V) noexcept {
- return *this = uint128_t(V);
+ constexpr uint128 &operator=(unsigned long long V) noexcept {
+ return *this = uint128(V);
}
- constexpr uint128_t &operator=(unsigned long long V) noexcept {
- return *this = uint128_t(V);
- }
- constexpr uint128_t &operator+=(uint128_t Other) noexcept {
+ constexpr uint128 &operator+=(uint128 Other) noexcept {
return *this = *this + Other;
}
- constexpr uint128_t &operator-=(uint128_t Other) noexcept {
+ constexpr uint128 &operator-=(uint128 Other) noexcept {
return *this = *this - Other;
}
- constexpr uint128_t &operator*=(uint128_t Other) noexcept {
+ constexpr uint128 &operator*=(uint128 Other) noexcept {
return *this = *this * Other;
}
- constexpr uint128_t &operator/=(uint128_t Other) noexcept {
+ constexpr uint128 &operator/=(uint128 Other) noexcept {
return *this = *this / Other;
}
- constexpr uint128_t &operator%=(uint128_t Other) noexcept {
+ constexpr uint128 &operator%=(uint128 Other) noexcept {
return *this = *this % Other;
}
- constexpr uint128_t &operator&=(uint128_t Other) noexcept {
+ constexpr uint128 &operator&=(uint128 Other) noexcept {
return *this = *this & Other;
}
- constexpr uint128_t &operator|=(uint128_t Other) noexcept {
+ constexpr uint128 &operator|=(uint128 Other) noexcept {
return *this = *this | Other;
}
- constexpr uint128_t &operator^=(uint128_t Other) noexcept {
+ constexpr uint128 &operator^=(uint128 Other) noexcept {
return *this = *this ^ Other;
}
- constexpr uint128_t &operator<<=(unsigned int Other) noexcept {
+ constexpr uint128 &operator<<=(unsigned int Other) noexcept {
return *this = *this << Other;
}
- constexpr uint128_t &operator>>=(unsigned int Other) noexcept {
+ constexpr uint128 &operator>>=(unsigned int Other) noexcept {
return *this = *this >> Other;
}
- constexpr uint128_t &operator<<=(int Other) noexcept {
+ constexpr uint128 &operator<<=(int Other) noexcept {
return *this = *this << Other;
}
- constexpr uint128_t &operator>>=(int Other) noexcept {
+ constexpr uint128 &operator>>=(int Other) noexcept {
return *this = *this >> Other;
}
- constexpr uint128_t &operator=(int128_t V) noexcept;
+ constexpr uint128 &operator=(int128 V) noexcept;
- friend constexpr bool operator==(uint128_t LHS, uint128_t RHS) noexcept {
+ friend constexpr bool operator==(uint128 LHS, uint128 RHS) noexcept {
return LHS.Low == RHS.Low && LHS.High == RHS.High;
}
- friend constexpr bool operator<(uint128_t LHS, uint128_t RHS) noexcept {
+ friend constexpr bool operator<(uint128 LHS, uint128 RHS) noexcept {
return LHS.High == RHS.High ? LHS.Low < RHS.Low : LHS.High < RHS.High;
}
- friend constexpr bool operator>(uint128_t LHS, uint128_t RHS) noexcept {
+ friend constexpr bool operator>(uint128 LHS, uint128 RHS) noexcept {
return RHS < LHS;
}
- friend constexpr bool operator!=(uint128_t LHS, uint128_t RHS) noexcept {
+ friend constexpr bool operator!=(uint128 LHS, uint128 RHS) noexcept {
return !(LHS == RHS);
}
- friend constexpr bool operator<=(uint128_t LHS, uint128_t RHS) noexcept {
+ friend constexpr bool operator<=(uint128 LHS, uint128 RHS) noexcept {
return !(LHS > RHS);
}
- friend constexpr bool operator>=(uint128_t LHS, uint128_t RHS) noexcept {
+ friend constexpr bool operator>=(uint128 LHS, uint128 RHS) noexcept {
return !(LHS < RHS);
}
- friend constexpr uint128_t operator+(uint128_t LHS, uint128_t RHS) noexcept {
+ friend constexpr uint128 operator+(uint128 LHS, uint128 RHS) noexcept {
uint64_t Carry =
(std::numeric_limits<uint64_t>::max() - LHS.Low) < RHS.Low ? 1 : 0;
- return uint128_t(LHS.High + RHS.High + Carry, LHS.Low + RHS.Low);
+ return uint128(LHS.High + RHS.High + Carry, LHS.Low + RHS.Low);
}
- friend constexpr uint128_t operator-(uint128_t LHS, uint128_t RHS) noexcept {
+ friend constexpr uint128 operator-(uint128 LHS, uint128 RHS) noexcept {
uint64_t Carry = LHS.Low < RHS.Low ? 1 : 0;
- return uint128_t(LHS.High - RHS.High - Carry, LHS.Low - RHS.Low);
+ return uint128(LHS.High - RHS.High - Carry, LHS.Low - RHS.Low);
}
- friend constexpr uint128_t operator*(uint128_t LHS, uint128_t RHS) noexcept {
+ friend constexpr uint128 operator*(uint128 LHS, uint128 RHS) noexcept {
uint64_t A32 = LHS.Low >> 32;
uint64_t A00 = LHS.Low & UINT64_C(0xffffffff);
uint64_t B32 = RHS.Low >> 32;
uint64_t B00 = RHS.Low & UINT64_C(0xffffffff);
- uint128_t Result = uint128_t(
- LHS.High * RHS.Low + LHS.Low * RHS.High + A32 * B32, A00 * B00);
- Result += uint128_t(A32 * B00) << 32U;
- Result += uint128_t(A00 * B32) << 32U;
+ uint128 Result =
+ uint128(LHS.High * RHS.Low + LHS.Low * RHS.High + A32 * B32, A00 * B00);
+ Result += uint128(A32 * B00) << 32U;
+ Result += uint128(A00 * B32) << 32U;
return Result;
}
- friend constexpr uint128_t operator/(uint128_t LHS, uint128_t RHS) noexcept {
+ friend uint128 operator/(uint128 LHS, uint64_t RHS) noexcept {
+ if (LHS.High == 0 && LHS.Low < RHS) {
+ LHS.Low = 0;
+ return LHS;
+ }
+ if (LHS.High < RHS) {
+ uint64_t Rem = 0;
+ uint64_t QLow = udiv128by64to64(LHS.High, LHS.Low, RHS, Rem);
+ LHS.Low = QLow;
+ LHS.High = 0;
+ return LHS;
+ }
+ uint64_t QHigh = LHS.High / RHS;
+ uint64_t Rem = 0;
+ uint64_t QLow = udiv128by64to64(LHS.High % RHS, LHS.Low, RHS, Rem);
+ LHS.Low = QLow;
+ LHS.High = QHigh;
+ return LHS;
+ }
+ friend constexpr uint128 operator/(uint128 LHS, uint128 RHS) noexcept {
if (RHS > LHS) {
- return 0;
+ return 0U;
}
if (RHS == LHS) {
- return 1;
+ return 1U;
+ }
+ if (RHS.High == 0) {
+ return LHS / RHS.Low;
}
- uint128_t Denominator = RHS;
- uint128_t Quotient = 0;
+ uint128 Denominator = RHS;
+ uint128 Quotient = 0U;
const unsigned int Shift = RHS.clz() - LHS.clz();
Denominator <<= Shift;
- for (unsigned int I = 0; I <= Shift; ++I) {
+ for (unsigned int I = 0U; I <= Shift; ++I) {
Quotient <<= 1U;
if (LHS >= Denominator) {
LHS -= Denominator;
@@ -196,14 +287,14 @@ public:
}
return Quotient;
}
- friend constexpr uint128_t operator%(uint128_t LHS, uint128_t RHS) noexcept {
+ friend constexpr uint128 operator%(uint128 LHS, uint128 RHS) noexcept {
if (RHS > LHS) {
return LHS;
}
if (RHS == LHS) {
- return 0;
+ return 0U;
}
- uint128_t Denominator = RHS;
+ uint128 Denominator = RHS;
const unsigned int Shift = RHS.clz() - LHS.clz();
Denominator <<= Shift;
for (unsigned int I = 0; I <= Shift; ++I) {
@@ -214,87 +305,74 @@ public:
}
return LHS;
}
- friend constexpr uint128_t operator&(uint128_t LHS, uint128_t RHS) noexcept {
- return uint128_t(LHS.High & RHS.High, LHS.Low & RHS.Low);
+ friend constexpr uint128 operator&(uint128 LHS, uint128 RHS) noexcept {
+ return uint128(LHS.High & RHS.High, LHS.Low & RHS.Low);
}
- friend constexpr uint128_t operator|(uint128_t LHS, uint128_t RHS) noexcept {
- return uint128_t(LHS.High | RHS.High, LHS.Low | RHS.Low);
+ friend constexpr uint128 operator|(uint128 LHS, uint128 RHS) noexcept {
+ return uint128(LHS.High | RHS.High, LHS.Low | RHS.Low);
}
- friend constexpr uint128_t operator^(uint128_t LHS, uint128_t RHS) noexcept {
- return uint128_t(LHS.High ^ RHS.High, LHS.Low ^ RHS.Low);
+ friend constexpr uint128 operator^(uint128 LHS, uint128 RHS) noexcept {
+ return uint128(LHS.High ^ RHS.High, LHS.Low ^ RHS.Low);
}
- friend constexpr uint128_t operator~(uint128_t Value) noexcept {
- return uint128_t(~Value.High, ~Value.Low);
+ friend constexpr uint128 operator~(uint128 Value) noexcept {
+ return uint128(~Value.High, ~Value.Low);
}
- friend constexpr uint128_t operator<<(uint128_t Value,
- unsigned int Shift) noexcept {
+ friend constexpr uint128 operator<<(uint128 Value,
+ unsigned int Shift) noexcept {
if (Shift < 64) {
if (Shift != 0) {
- return uint128_t((Value.High << Shift) | (Value.Low >> (64 - Shift)),
- Value.Low << Shift);
+ return uint128((Value.High << Shift) | (Value.Low >> (64 - Shift)),
+ Value.Low << Shift);
}
return Value;
}
- return uint128_t(Value.Low << (Shift - 64), 0);
+ return uint128(Value.Low << (Shift - 64), 0);
}
- friend constexpr uint128_t operator>>(uint128_t Value,
- unsigned int Shift) noexcept {
+ friend constexpr uint128 operator>>(uint128 Value,
+ unsigned int Shift) noexcept {
if (Shift < 64) {
if (Shift != 0) {
- return uint128_t((Value.High >> Shift),
- Value.Low >> Shift | (Value.High << (64 - Shift)));
+ return uint128((Value.High >> Shift),
+ Value.Low >> Shift | (Value.High << (64 - Shift)));
}
return Value;
}
- return uint128_t(0, Value.High >> (Shift - 64));
+ return uint128(0, Value.High >> (Shift - 64));
}
- friend constexpr uint128_t operator<<(uint128_t Value, int Shift) noexcept {
+ friend constexpr uint128 operator<<(uint128 Value, int Shift) noexcept {
return Value << static_cast<unsigned int>(Shift);
}
- friend constexpr uint128_t operator>>(uint128_t Value, int Shift) noexcept {
+ friend constexpr uint128 operator>>(uint128 Value, int Shift) noexcept {
return Value >> static_cast<unsigned int>(Shift);
}
- friend constexpr uint128_t operator<<(uint128_t Value,
- unsigned long long Shift) noexcept {
+ friend constexpr uint128 operator<<(uint128 Value,
+ unsigned long long Shift) noexcept {
return Value << static_cast<unsigned int>(Shift);
}
- friend constexpr uint128_t operator>>(uint128_t Value,
- unsigned long long Shift) noexcept {
+ friend constexpr uint128 operator>>(uint128 Value,
+ unsigned long long Shift) noexcept {
return Value >> static_cast<unsigned int>(Shift);
}
- static constexpr uint128_t numericMin() noexcept {
- return uint128_t(std::numeric_limits<uint64_t>::min(),
- std::numeric_limits<uint64_t>::min());
+ static constexpr uint128 numericMin() noexcept {
+ return uint128(std::numeric_limits<uint64_t>::min(),
+ std::numeric_limits<uint64_t>::min());
}
- static constexpr uint128_t numericMax() noexcept {
- return uint128_t(std::numeric_limits<uint64_t>::max(),
- std::numeric_limits<uint64_t>::max());
+ static constexpr uint128 numericMax() noexcept {
+ return uint128(std::numeric_limits<uint64_t>::max(),
+ std::numeric_limits<uint64_t>::max());
}
constexpr uint64_t low() const noexcept { return Low; }
constexpr uint64_t high() const noexcept { return High; }
constexpr unsigned int clz() const noexcept {
-#if defined(_MSC_VER) && !defined(__clang__)
- unsigned long LeadingZero = 0;
- if (High) {
- _BitScanReverse64(&LeadingZero, High);
- return (63 - LeadingZero);
- }
- if (Low) {
- _BitScanReverse64(&LeadingZero, Low);
- return (63 - LeadingZero) + 64;
- }
- return 128;
-#else
if (High) {
- return __builtin_clzll(High);
+ return static_cast<unsigned int>(WasmEdge::clz(High));
}
if (Low) {
- return __builtin_clzll(Low) + 64;
+ return static_cast<unsigned int>(WasmEdge::clz(Low)) + 64U;
}
- return 128;
-#endif
+ return 128U;
}
private:
@@ -302,50 +380,54 @@ private:
uint64_t High;
};
-class int128_t {
+class int128 {
public:
- int128_t() noexcept = default;
- constexpr int128_t(const int128_t &) noexcept = default;
- constexpr int128_t(int128_t &&) noexcept = default;
- constexpr int128_t &operator=(const int128_t &V) noexcept = default;
- constexpr int128_t &operator=(int128_t &&V) noexcept = default;
+ int128() noexcept = default;
+ constexpr int128(const int128 &) noexcept = default;
+ constexpr int128(int128 &&) noexcept = default;
+ constexpr int128 &operator=(const int128 &V) noexcept = default;
+ constexpr int128 &operator=(int128 &&V) noexcept = default;
- constexpr int128_t(int V) noexcept
+ constexpr int128(int V) noexcept
: Low(static_cast<uint64_t>(V)), High(V < 0 ? INT64_C(-1) : INT64_C(0)) {}
- constexpr int128_t(long V) noexcept
+ constexpr int128(long V) noexcept
: Low(static_cast<uint64_t>(V)), High(V < 0 ? INT64_C(-1) : INT64_C(0)) {}
- constexpr int128_t(long long V) noexcept
+ constexpr int128(long long V) noexcept
: Low(static_cast<uint64_t>(V)), High(V < 0 ? INT64_C(-1) : INT64_C(0)) {}
- constexpr int128_t(unsigned int V) noexcept : Low(V), High(INT64_C(0)) {}
- constexpr int128_t(unsigned long V) noexcept : Low(V), High(INT64_C(0)) {}
- constexpr int128_t(unsigned long long V) noexcept
- : Low(V), High(INT64_C(0)) {}
- constexpr int128_t(uint128_t V) noexcept;
- constexpr int128_t(int64_t H, uint64_t L) noexcept : Low(L), High(H) {}
+ constexpr int128(unsigned int V) noexcept : Low(V), High(INT64_C(0)) {}
+ constexpr int128(unsigned long V) noexcept : Low(V), High(INT64_C(0)) {}
+ constexpr int128(unsigned long long V) noexcept : Low(V), High(INT64_C(0)) {}
+ constexpr int128(uint128 V) noexcept;
+ constexpr int128(int64_t H, uint64_t L) noexcept : Low(L), High(H) {}
+#if defined(__x86_64__) || defined(__aarch64__) || \
+ (defined(__riscv) && __riscv_xlen == 64)
+ constexpr int128(__int128 V) noexcept
+ : Low(static_cast<uint64_t>(V)), High(V >> 64) {}
+#endif
- constexpr int128_t &operator=(int V) noexcept { return *this = int128_t(V); }
- constexpr int128_t &operator=(long V) noexcept { return *this = int128_t(V); }
- constexpr int128_t &operator=(long long V) noexcept {
- return *this = int128_t(V);
+ constexpr int128 &operator=(int V) noexcept { return *this = int128(V); }
+ constexpr int128 &operator=(long V) noexcept { return *this = int128(V); }
+ constexpr int128 &operator=(long long V) noexcept {
+ return *this = int128(V);
}
- constexpr int128_t &operator=(unsigned int V) noexcept {
- return *this = int128_t(V);
+ constexpr int128 &operator=(unsigned int V) noexcept {
+ return *this = int128(V);
}
- constexpr int128_t &operator=(unsigned long V) noexcept {
- return *this = int128_t(V);
+ constexpr int128 &operator=(unsigned long V) noexcept {
+ return *this = int128(V);
}
- constexpr int128_t &operator=(unsigned long long V) noexcept {
- return *this = int128_t(V);
+ constexpr int128 &operator=(unsigned long long V) noexcept {
+ return *this = int128(V);
}
- constexpr int128_t &operator=(uint128_t V) noexcept;
+ constexpr int128 &operator=(uint128 V) noexcept;
- static constexpr int128_t numericMin() noexcept {
- return int128_t(std::numeric_limits<int64_t>::min(), 0);
+ static constexpr int128 numericMin() noexcept {
+ return int128(std::numeric_limits<int64_t>::min(), 0);
}
- static constexpr int128_t numericMax() noexcept {
- return int128_t(std::numeric_limits<int64_t>::max(),
- std::numeric_limits<uint64_t>::max());
+ static constexpr int128 numericMax() noexcept {
+ return int128(std::numeric_limits<int64_t>::max(),
+ std::numeric_limits<uint64_t>::max());
}
constexpr uint64_t low() const noexcept { return Low; }
@@ -356,24 +438,24 @@ private:
int64_t High;
};
-inline constexpr uint128_t::uint128_t(int128_t V) noexcept
+inline constexpr uint128::uint128(int128 V) noexcept
: Low(V.low()), High(static_cast<uint64_t>(V.high())) {}
-inline constexpr uint128_t &uint128_t::operator=(int128_t V) noexcept {
- return *this = uint128_t(V);
+inline constexpr uint128 &uint128::operator=(int128 V) noexcept {
+ return *this = uint128(V);
}
-inline constexpr int128_t::int128_t(uint128_t V) noexcept
+inline constexpr int128::int128(uint128 V) noexcept
: Low(V.low()), High(static_cast<int64_t>(V.high())) {}
-inline constexpr int128_t &int128_t::operator=(uint128_t V) noexcept {
- return *this = int128_t(V);
+inline constexpr int128 &int128::operator=(uint128 V) noexcept {
+ return *this = int128(V);
}
} // namespace WasmEdge
namespace std {
-template <> class numeric_limits<WasmEdge::uint128_t> {
+template <> class numeric_limits<WasmEdge::uint128> {
public:
static constexpr bool is_specialized = true;
static constexpr bool is_signed = false;
@@ -399,23 +481,23 @@ public:
static constexpr bool traps = numeric_limits<uint64_t>::traps;
static constexpr bool tinyness_before = false;
- static constexpr WasmEdge::uint128_t min() {
- return WasmEdge::uint128_t::numericMin();
+ static constexpr WasmEdge::uint128 min() {
+ return WasmEdge::uint128::numericMin();
}
- static constexpr WasmEdge::uint128_t lowest() {
- return WasmEdge::uint128_t::numericMin();
+ static constexpr WasmEdge::uint128 lowest() {
+ return WasmEdge::uint128::numericMin();
}
- static constexpr WasmEdge::uint128_t max() {
- return WasmEdge::uint128_t::numericMax();
+ static constexpr WasmEdge::uint128 max() {
+ return WasmEdge::uint128::numericMax();
}
- static constexpr WasmEdge::uint128_t epsilon() { return 0; }
- static constexpr WasmEdge::uint128_t round_error() { return 0; }
- static constexpr WasmEdge::uint128_t infinity() { return 0; }
- static constexpr WasmEdge::uint128_t quiet_NaN() { return 0; }
- static constexpr WasmEdge::uint128_t signaling_NaN() { return 0; }
- static constexpr WasmEdge::uint128_t denorm_min() { return 0; }
+ static constexpr WasmEdge::uint128 epsilon() { return 0U; }
+ static constexpr WasmEdge::uint128 round_error() { return 0U; }
+ static constexpr WasmEdge::uint128 infinity() { return 0U; }
+ static constexpr WasmEdge::uint128 quiet_NaN() { return 0U; }
+ static constexpr WasmEdge::uint128 signaling_NaN() { return 0U; }
+ static constexpr WasmEdge::uint128 denorm_min() { return 0U; }
};
-template <> class numeric_limits<WasmEdge::int128_t> {
+template <> class numeric_limits<WasmEdge::int128> {
public:
static constexpr bool is_specialized = true;
static constexpr bool is_signed = true;
@@ -441,48 +523,164 @@ public:
static constexpr bool traps = numeric_limits<uint64_t>::traps;
static constexpr bool tinyness_before = false;
- static constexpr WasmEdge::int128_t min() {
- return WasmEdge::int128_t::numericMin();
+ static constexpr WasmEdge::int128 min() {
+ return WasmEdge::int128::numericMin();
}
- static constexpr WasmEdge::int128_t lowest() {
- return WasmEdge::int128_t::numericMin();
+ static constexpr WasmEdge::int128 lowest() {
+ return WasmEdge::int128::numericMin();
}
- static constexpr WasmEdge::int128_t max() {
- return WasmEdge::int128_t::numericMax();
+ static constexpr WasmEdge::int128 max() {
+ return WasmEdge::int128::numericMax();
}
- static constexpr WasmEdge::int128_t epsilon() { return 0; }
- static constexpr WasmEdge::int128_t round_error() { return 0; }
- static constexpr WasmEdge::int128_t infinity() { return 0; }
- static constexpr WasmEdge::int128_t quiet_NaN() { return 0; }
- static constexpr WasmEdge::int128_t signaling_NaN() { return 0; }
- static constexpr WasmEdge::int128_t denorm_min() { return 0; }
+ static constexpr WasmEdge::int128 epsilon() { return 0; }
+ static constexpr WasmEdge::int128 round_error() { return 0; }
+ static constexpr WasmEdge::int128 infinity() { return 0; }
+ static constexpr WasmEdge::int128 quiet_NaN() { return 0; }
+ static constexpr WasmEdge::int128 signaling_NaN() { return 0; }
+ static constexpr WasmEdge::int128 denorm_min() { return 0; }
};
} // namespace std
-#endif
-
#include <type_traits>
namespace std {
-template <> struct is_class<WasmEdge::uint128_t> : std::true_type {};
+template <> struct is_class<WasmEdge::uint128> : std::true_type {};
} // namespace std
-#include <fmt/format.h>
+namespace WasmEdge {
+// If there is a built-in type __int128, then use it directly
+#if defined(__x86_64__) || defined(__aarch64__) || \
+ (defined(__riscv) && __riscv_xlen == 64)
+using int128_t = __int128;
+using uint128_t = unsigned __int128;
+#else
+using int128_t = int128;
+using uint128_t = uint128;
+#endif
+} // namespace WasmEdge
-#if !FMT_USE_INT128
+#include <fmt/format.h>
FMT_BEGIN_NAMESPACE
-template <typename Char> struct formatter<WasmEdge::uint128_t, Char> {
+namespace detail {
+inline constexpr bool operator>=(detail::uint128_fallback LHS,
+ unsigned int RHS) {
+ return LHS.high() != 0 || LHS.low() >= static_cast<uint64_t>(RHS);
+}
+
+inline constexpr bool operator<(detail::uint128_fallback LHS,
+ unsigned int RHS) {
+ return LHS.high() == 0 && LHS.low() < static_cast<uint64_t>(RHS);
+}
+
+inline constexpr bool operator<(detail::uint128_fallback LHS,
+ detail::uint128_fallback RHS) {
+ return LHS.high() < RHS.high() ||
+ (LHS.high() == RHS.high() && LHS.low() < RHS.low());
+}
+
+inline constexpr detail::uint128_fallback &
+operator/=(detail::uint128_fallback &LHS, unsigned int RHSi) {
+ const uint64_t RHS = static_cast<uint64_t>(RHSi);
+ if (LHS.high() == 0 && LHS.low() < RHS) {
+ LHS = 0;
+ return LHS;
+ }
+ if (LHS.high() < RHS) {
+ uint64_t Rem = 0;
+ uint64_t QLo = WasmEdge::udiv128by64to64(LHS.high(), LHS.low(), RHS, Rem);
+ LHS = QLo;
+ return LHS;
+ }
+ uint64_t QHi = LHS.high() / RHS;
+ uint64_t Rem = 0;
+ uint64_t QLo =
+ WasmEdge::udiv128by64to64(LHS.high() % RHS, LHS.low(), RHS, Rem);
+ LHS = (detail::uint128_t{QHi} << 64u) | QLo;
+ return LHS;
+}
+
+inline constexpr detail::uint128_fallback
+operator%(detail::uint128_fallback LHS, unsigned int RHSi) {
+ const uint64_t RHS = static_cast<uint64_t>(RHSi);
+ if (LHS.high() == 0 && LHS.low() < RHS) {
+ return LHS;
+ }
+ uint64_t Rem = 0;
+ WasmEdge::udiv128by64to64(LHS.high() % RHS, LHS.low(), RHS, Rem);
+ return Rem;
+}
+
+inline int do_count_digits(detail::uint128_fallback N) {
+ const uint64_t Low = static_cast<uint64_t>(N);
+ const uint64_t High = static_cast<uint64_t>(N >> 64);
+ if (High == 0) {
+ return detail::count_digits(Low);
+ }
+ // Maps bsr(n) to ceil(log10(pow(2, bsr(n) + 1) - 1)).
+ static constexpr uint8_t Bsr2Log10[] = {
+ 20, 20, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25,
+ 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29,
+ 30, 30, 30, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 34, 34, 34,
+ 35, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 38, 39, 39};
+ auto C = WasmEdge::clz(High);
+ auto T = Bsr2Log10[C ^ 63];
+ static constexpr const uint128_t PowersOf10[] = {
+ 0,
+ (uint128_t(5ULL) << 64) | uint128_t(7766279631452241920ULL),
+ (uint128_t(54ULL) << 64) | uint128_t(3875820019684212736ULL),
+ (uint128_t(542ULL) << 64) | uint128_t(1864712049423024128ULL),
+ (uint128_t(5421ULL) << 64) | uint128_t(200376420520689664ULL),
+ (uint128_t(54210ULL) << 64) | uint128_t(2003764205206896640ULL),
+ (uint128_t(542101ULL) << 64) | uint128_t(1590897978359414784ULL),
+ (uint128_t(5421010ULL) << 64) | uint128_t(15908979783594147840ULL),
+ (uint128_t(54210108ULL) << 64) | uint128_t(11515845246265065472ULL),
+ (uint128_t(542101086ULL) << 64) | uint128_t(4477988020393345024ULL),
+ (uint128_t(5421010862ULL) << 64) | uint128_t(7886392056514347008ULL),
+ (uint128_t(54210108624ULL) << 64) | uint128_t(5076944270305263616ULL),
+ (uint128_t(542101086242ULL) << 64) | uint128_t(13875954555633532928ULL),
+ (uint128_t(5421010862427ULL) << 64) | uint128_t(9632337040368467968ULL),
+ (uint128_t(54210108624275ULL) << 64) | uint128_t(4089650035136921600ULL),
+ (uint128_t(542101086242752ULL) << 64) | uint128_t(4003012203950112768ULL),
+ (uint128_t(5421010862427522ULL) << 64) |
+ uint128_t(3136633892082024448ULL),
+ (uint128_t(54210108624275221ULL) << 64) |
+ uint128_t(12919594847110692864ULL),
+ (uint128_t(542101086242752217ULL) << 64) |
+ uint128_t(68739955140067328ULL),
+ (uint128_t(5421010862427522170ULL) << 64) |
+ uint128_t(687399551400673280ULL),
+ };
+ return T - (N < PowersOf10[T - 20]);
+}
+
+FMT_CONSTEXPR20 inline int count_digits(detail::uint128_fallback N) {
+ if (!is_constant_evaluated()) {
+ return do_count_digits(N);
+ }
+ return count_digits_fallback(N);
+}
+
+} // namespace detail
+
+template <typename Char> struct formatter<WasmEdge::uint128, Char> {
private:
- detail::dynamic_format_specs<> Specs;
+ detail::dynamic_format_specs<Char> Specs;
public:
template <typename ParseContext> constexpr auto parse(ParseContext &Ctx) {
+#if FMT_VERSION >= 100000
return parse_format_specs(Ctx.begin(), Ctx.end(), Specs, Ctx,
detail::type::uint_type);
+#else
+ using HandlerType = detail::dynamic_specs_handler<ParseContext>;
+ detail::specs_checker<HandlerType> Handler(HandlerType(Specs, Ctx),
+ detail::type::uint_type);
+ return parse_format_specs(Ctx.begin(), Ctx.end(), Handler);
+#endif
}
template <typename FormatContext>
- auto format(WasmEdge::uint128_t V, FormatContext &Ctx) const {
+ auto format(WasmEdge::uint128 V, FormatContext &Ctx) const {
auto S = Specs;
detail::handle_dynamic_spec<detail::width_checker>(S.width, S.width_ref,
Ctx);
@@ -490,8 +688,9 @@ public:
S.precision, S.precision_ref, Ctx);
constexpr const unsigned Prefixes[4] = {0, 0, 0x1000000u | '+',
0x1000000u | ' '};
- const detail::uint128_t U{static_cast<uint64_t>(V >> 64),
- static_cast<uint64_t>(V)};
+ const detail::uint128_t U =
+ (detail::uint128_t{static_cast<uint64_t>(V >> 64)} << 64) |
+ detail::uint128_t{static_cast<uint64_t>(V)};
return detail::write_int<Char>(
Ctx.out(),
detail::write_int_arg<detail::uint128_t>{U, Prefixes[S.sign]}, S,
@@ -499,5 +698,3 @@ public:
}
};
FMT_END_NAMESPACE
-
-#endif
diff --git a/include/executor/engine/unary_numeric_vector_msvc.ipp b/include/executor/engine/unary_numeric_vector_msvc.ipp
index 6000d5d..a103f63 100644
--- a/include/executor/engine/unary_numeric_vector_msvc.ipp
+++ b/include/executor/engine/unary_numeric_vector_msvc.ipp
@@ -196,7 +196,7 @@ inline Expect<void> Executor::runVectorPromoteOp(ValVariant &Val) const {
inline Expect<void> Executor::runVectorAnyTrueOp(ValVariant &Val) const {
auto &Vector = Val.get<uint128_t>();
- const uint128_t Zero = 0;
+ const uint128_t Zero = 0U;
const uint32_t Result = (Vector != Zero);
Val.emplace<uint32_t>(Result);
diff --git a/include/runtime/instance/global.h b/include/runtime/instance/global.h
index d5f0008..a44751d 100644
--- a/include/runtime/instance/global.h
+++ b/include/runtime/instance/global.h
@@ -23,7 +23,7 @@ class GlobalInstance {
public:
GlobalInstance() = delete;
GlobalInstance(const AST::GlobalType >ype,
- ValVariant Val = uint128_t(0)) noexcept
+ ValVariant Val = uint128_t(0U)) noexcept
: GlobType(GType), Value(Val) {
assuming(GType.getValType().isNumType() ||
GType.getValType().isNullableRefType() ||
diff --git a/include/runtime/instance/memory.h b/include/runtime/instance/memory.h
index 6f72441..9ea4f30 100644
--- a/include/runtime/instance/memory.h
+++ b/include/runtime/instance/memory.h
@@ -285,7 +285,7 @@ public:
} else {
if constexpr (sizeof(T) > 8) {
assuming(sizeof(T) == 16);
- Value = 0;
+ Value = 0U;
std::memcpy(&Value, &DataPtr[Offset], Length);
} else {
uint64_t LoadVal = 0;
diff --git a/lib/api/wasmedge.cpp b/lib/api/wasmedge.cpp
index 61dcea4..b9ae954 100644
--- a/lib/api/wasmedge.cpp
+++ b/lib/api/wasmedge.cpp
@@ -2628,7 +2628,8 @@ WasmEdge_GlobalInstanceGetValue(const WasmEdge_GlobalInstanceContext *Cxt) {
fromGlobCxt(Cxt)->getGlobalType().getValType());
}
return genWasmEdge_Value(
- WasmEdge::ValVariant(static_cast<WasmEdge::uint128_t>(0)), TypeCode::I32);
+ WasmEdge::ValVariant(static_cast<WasmEdge::uint128_t>(0U)),
+ TypeCode::I32);
}
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_GlobalInstanceSetValue(
diff --git a/lib/driver/runtimeTool.cpp b/lib/driver/runtimeTool.cpp
index 069b2aa..1322de3 100644
--- a/lib/driver/runtimeTool.cpp
+++ b/lib/driver/runtimeTool.cpp
@@ -314,7 +314,7 @@ int Tool(struct DriverToolOptions &Opt) noexcept {
fmt::print("{}\n"sv, (*Result)[I].first.get<double>());
break;
case TypeCode::V128:
- fmt::print("{}\n"sv, (*Result)[I].first.get<uint128_t>());
+ fmt::print("{}\n"sv, uint128((*Result)[I].first.get<uint128_t>()));
break;
/// TODO: FuncRef and ExternRef
default:
diff --git a/lib/executor/engine/refInstr.cpp b/lib/executor/engine/refInstr.cpp
index d2374e5..a8cf59d 100644
--- a/lib/executor/engine/refInstr.cpp
+++ b/lib/executor/engine/refInstr.cpp
@@ -111,7 +111,7 @@ Expect<void> Executor::runStructNewOp(Runtime::StackManager &StackMgr,
const auto &VType = CompType.getFieldTypes()[I].getStorageType();
Vals[I] = VType.isRefType()
? ValVariant(RefVariant(toBottomType(StackMgr, VType)))
- : ValVariant(static_cast<uint128_t>(0));
+ : ValVariant(static_cast<uint128_t>(0U));
}
} else {
Vals = StackMgr.pop(N);
@@ -173,7 +173,7 @@ Expect<void> Executor::runArrayNewOp(Runtime::StackManager &StackMgr,
if (InitCnt == 0) {
auto InitVal = VType.isRefType()
? ValVariant(RefVariant(toBottomType(StackMgr, VType)))
- : ValVariant(static_cast<uint128_t>(0));
+ : ValVariant(static_cast<uint128_t>(0U));
auto *Inst =
const_cast<Runtime::Instance::ModuleInstance *>(StackMgr.getModule())
->newArray(DefIndex, ValCnt, InitVal);
diff --git a/lib/executor/helper.cpp b/lib/executor/helper.cpp
index ba9ac30..5c829f1 100644
--- a/lib/executor/helper.cpp
+++ b/lib/executor/helper.cpp
@@ -378,25 +378,25 @@ void Executor::cleanNumericVal(ValVariant &Val,
switch (Type.getCode()) {
case TypeCode::I32: {
uint32_t V = Val.get<uint32_t>();
- Val.emplace<uint128_t>(static_cast<uint128_t>(0));
+ Val.emplace<uint128_t>(static_cast<uint128_t>(0U));
Val.emplace<uint32_t>(V);
break;
}
case TypeCode::F32: {
float V = Val.get<float>();
- Val.emplace<uint128_t>(static_cast<uint128_t>(0));
+ Val.emplace<uint128_t>(static_cast<uint128_t>(0U));
Val.emplace<float>(V);
break;
}
case TypeCode::I64: {
uint64_t V = Val.get<uint64_t>();
- Val.emplace<uint128_t>(static_cast<uint128_t>(0));
+ Val.emplace<uint128_t>(static_cast<uint128_t>(0U));
Val.emplace<uint64_t>(V);
break;
}
case TypeCode::F64: {
double V = Val.get<double>();
- Val.emplace<uint128_t>(static_cast<uint128_t>(0));
+ Val.emplace<uint128_t>(static_cast<uint128_t>(0U));
Val.emplace<double>(V);
break;
}
diff --git a/lib/loader/ast/instruction.cpp b/lib/loader/ast/instruction.cpp
index 2ae84e6..fc02b20 100644
--- a/lib/loader/ast/instruction.cpp
+++ b/lib/loader/ast/instruction.cpp
@@ -931,13 +931,14 @@ Expect<void> Loader::loadInstruction(AST::Instruction &Instr) {
// SIMD Shuffle Instruction.
case OpCode::I8x16__shuffle: {
// Read value.
- uint128_t Value = 0;
- for (uint32_t I = 0; I < 16; ++I) {
+ uint128_t Value = 0U;
+ for (uint32_t I = 0U; I < 16U; ++I) {
if (auto Res = FMgr.readByte(); unlikely(!Res)) {
return logLoadError(Res.error(), FMgr.getLastOffset(),
ASTNodeAttr::Instruction);
} else {
- Value |= static_cast<uint128_t>(*Res) << (I * 8);
+ Value |= static_cast<uint128_t>(static_cast<uint32_t>(*Res))
+ << (I * 8U);
}
}
Instr.setNum(Value);
diff --git a/test/common/int128Test.cpp b/test/common/int128Test.cpp
index f3c785a..83bee4e 100644
--- a/test/common/int128Test.cpp
+++ b/test/common/int128Test.cpp
@@ -12,95 +12,96 @@ using namespace std::literals;
TEST(Int128Test, Int128OutputTest) {
{
- const WasmEdge::uint128_t Value = 0;
- EXPECT_EQ(fmt::format("{}"sv, Value), "0");
- EXPECT_EQ(fmt::format("{:x}"sv, Value), "0");
- EXPECT_EQ(fmt::format("{:#x}"sv, Value), "0x0");
- EXPECT_EQ(fmt::format("{:b}"sv, Value), "0");
- EXPECT_EQ(fmt::format("{:#b}"sv, Value), "0b0");
- EXPECT_EQ(fmt::format("{:o}"sv, Value), "0");
+ const WasmEdge::uint128_t Value = 0U;
+ EXPECT_EQ(fmt::format("{}"sv, WasmEdge::uint128(Value)), "0");
+ EXPECT_EQ(fmt::format("{:x}"sv, WasmEdge::uint128(Value)), "0");
+ EXPECT_EQ(fmt::format("{:#x}"sv, WasmEdge::uint128(Value)), "0x0");
+ EXPECT_EQ(fmt::format("{:b}"sv, WasmEdge::uint128(Value)), "0");
+ EXPECT_EQ(fmt::format("{:#b}"sv, WasmEdge::uint128(Value)), "0b0");
+ EXPECT_EQ(fmt::format("{:o}"sv, WasmEdge::uint128(Value)), "0");
}
{
- const WasmEdge::uint128_t Value = WasmEdge::uint128_t(1) << 69;
- EXPECT_EQ(fmt::format("{}"sv, Value), "590295810358705651712");
- EXPECT_EQ(fmt::format("{:x}"sv, Value), "200000000000000000");
+ const WasmEdge::uint128_t Value = WasmEdge::uint128_t(1U) << 69U;
+ EXPECT_EQ(fmt::format("{}"sv, WasmEdge::uint128(Value)),
+ "590295810358705651712");
+ EXPECT_EQ(fmt::format("{:x}"sv, WasmEdge::uint128(Value)),
+ "200000000000000000");
}
{
- const WasmEdge::uint128_t Value = WasmEdge::uint128_t(1) << 127;
- EXPECT_EQ(fmt::format("{}"sv, Value),
+ const WasmEdge::uint128_t Value = WasmEdge::uint128_t(1U) << 127U;
+ EXPECT_EQ(fmt::format("{}"sv, WasmEdge::uint128(Value)),
"170141183460469231731687303715884105728");
- EXPECT_EQ(fmt::format("{:x}"sv, Value), "80000000000000000000000000000000");
+ EXPECT_EQ(fmt::format("{:x}"sv, WasmEdge::uint128(Value)),
+ "80000000000000000000000000000000");
}
{
- const WasmEdge::uint128_t Value = ~WasmEdge::uint128_t(0);
- EXPECT_EQ(fmt::format("{}"sv, Value),
+ const WasmEdge::uint128_t Value = ~WasmEdge::uint128_t(0U);
+ EXPECT_EQ(fmt::format("{}"sv, WasmEdge::uint128(Value)),
"340282366920938463463374607431768211455");
- EXPECT_EQ(fmt::format("{:x}"sv, Value), "ffffffffffffffffffffffffffffffff");
- EXPECT_EQ(fmt::format("{:#X}"sv, Value),
+ EXPECT_EQ(fmt::format("{:x}"sv, WasmEdge::uint128(Value)),
+ "ffffffffffffffffffffffffffffffff");
+ EXPECT_EQ(fmt::format("{:#X}"sv, WasmEdge::uint128(Value)),
"0XFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
}
{
- const WasmEdge::uint128_t P10(10000000000000);
+ const WasmEdge::uint128_t P10(10000000000000U);
const WasmEdge::uint128_t Value =
- WasmEdge::uint128_t(1234567890123) * P10 * P10 +
- WasmEdge::uint128_t(1234567890123);
- EXPECT_EQ(fmt::format("{}"sv, Value),
+ WasmEdge::uint128_t(1234567890123U) * P10 * P10 +
+ WasmEdge::uint128_t(1234567890123U);
+ EXPECT_EQ(fmt::format("{}"sv, WasmEdge::uint128(Value)),
"123456789012300000000000001234567890123");
- EXPECT_EQ(fmt::format("{:x}"sv, Value), "5ce0e9a55ff035e3783f03ea3dfb04cb");
+ EXPECT_EQ(fmt::format("{:x}"sv, WasmEdge::uint128(Value)),
+ "5ce0e9a55ff035e3783f03ea3dfb04cb");
EXPECT_EQ(
- fmt::format("{:b}"sv, Value),
+ fmt::format("{:b}"sv, WasmEdge::uint128(Value)),
"1011100111000001110100110100101010111111111000000110101111000110111100"
"000111111000000111110101000111101111110110000010011001011");
}
{
- const WasmEdge::uint128_t P10(10000000000000);
+ const WasmEdge::uint128_t P10(10000000000000U);
const WasmEdge::uint128_t Value =
- WasmEdge::uint128_t(1234567890123) * P10 * P10;
- EXPECT_EQ(fmt::format("{}"sv, Value),
+ WasmEdge::uint128_t(1234567890123U) * P10 * P10;
+ EXPECT_EQ(fmt::format("{}"sv, WasmEdge::uint128(Value)),
"123456789012300000000000000000000000000");
- EXPECT_EQ(fmt::format("{:o}"sv, Value),
+ EXPECT_EQ(fmt::format("{:o}"sv, WasmEdge::uint128(Value)),
"1347016464527770065706740770054531400000000");
}
{
- const WasmEdge::uint128_t P10(10000000000000);
+ const WasmEdge::uint128_t P10(10000000000000U);
const WasmEdge::uint128_t Value =
- WasmEdge::uint128_t(1234567890123) * P10 * P10 +
- WasmEdge::uint128_t(1234567890123) * P10;
- EXPECT_EQ(fmt::format("{}"sv, Value),
+ WasmEdge::uint128_t(1234567890123U) * P10 * P10 +
+ WasmEdge::uint128_t(1234567890123U) * P10;
+ EXPECT_EQ(fmt::format("{}"sv, WasmEdge::uint128(Value)),
"123456789012312345678901230000000000000");
}
{
auto Convert = [](WasmEdge::uint128_t V) -> fmt::detail::uint128_t {
-#if !FMT_USE_INT128
- return fmt::detail::uint128_t{static_cast<uint64_t>(V >> 64),
- static_cast<uint64_t>(V)};
-#else
- return V;
-#endif
+ return (fmt::detail::uint128_t(static_cast<uint64_t>(V >> 64U)) << 64U) |
+ fmt::detail::uint128_t(static_cast<uint64_t>(V));
};
std::string S0 = "1"s;
std::string S9;
- WasmEdge::uint128_t X(1);
- for (unsigned int I = 1; I <= 38; ++I) {
- X *= 10;
- const auto Y = X - WasmEdge::uint128_t(1);
- EXPECT_EQ(fmt::detail::count_digits(Convert(Y - WasmEdge::uint128_t(1))),
+ WasmEdge::uint128_t X(1U);
+ for (unsigned int I = 1U; I <= 38U; ++I) {
+ X *= 10U;
+ const auto Y = X - WasmEdge::uint128_t(1U);
+ EXPECT_EQ(fmt::detail::count_digits(Convert(Y - WasmEdge::uint128_t(1U))),
I);
EXPECT_EQ(fmt::detail::count_digits(Convert(Y)), I);
EXPECT_EQ(fmt::detail::count_digits(Convert(X)), I + 1);
- EXPECT_EQ(fmt::detail::count_digits(Convert(X + WasmEdge::uint128_t(1))),
+ EXPECT_EQ(fmt::detail::count_digits(Convert(X + WasmEdge::uint128_t(1U))),
I + 1);
S0 += '0';
S9 += '9';
- EXPECT_EQ(fmt::format("{}"sv, X), S0);
- EXPECT_EQ(fmt::format("{}"sv, Y), S9);
+ EXPECT_EQ(fmt::format("{}"sv, WasmEdge::uint128(X)), S0);
+ EXPECT_EQ(fmt::format("{}"sv, WasmEdge::uint128(Y)), S9);
}
}
}
|