|
| 1 | +/* |
| 2 | + * Copyright 2026 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef wasm_support_i65_h |
| 18 | +#define wasm_support_i65_h |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | +#include <iostream> |
| 22 | +#include <limits> |
| 23 | + |
| 24 | +namespace wasm { |
| 25 | + |
| 26 | +// An integer capable of representing numbers in the combined range of 32 and |
| 27 | +// 64-bit integers, both signed and unsigned. That is, in the range |
| 28 | +// |
| 29 | +// std::numeric_limits<int64_t>::min() .. std::numeric_limits<uint64_t>::max() |
| 30 | +// |
| 31 | +// This is basically an i64 combined with a u64 in terms of range, hence "IU64". |
| 32 | +struct IU64 { |
| 33 | + // A 64-bit payload with an extra 65th sign bit. |
| 34 | + uint64_t value = 0; |
| 35 | + bool negative = false; |
| 36 | + |
| 37 | + constexpr IU64() = default; |
| 38 | + |
| 39 | + // Unsigned values are simple. |
| 40 | + constexpr IU64(uint32_t x) : value(x) {} |
| 41 | + constexpr IU64(uint64_t x) : value(x) {} |
| 42 | + |
| 43 | + // Signed values need to be checked for being negative. |
| 44 | + constexpr IU64(int32_t x) { |
| 45 | + if (x >= 0) { |
| 46 | + value = x; |
| 47 | + } else { |
| 48 | + negative = true; |
| 49 | + value = -int64_t(x); |
| 50 | + } |
| 51 | + } |
| 52 | + constexpr IU64(int64_t x) { |
| 53 | + if (x >= 0) { |
| 54 | + value = x; |
| 55 | + } else { |
| 56 | + negative = true; |
| 57 | + |
| 58 | + // One does not simply negate MIN_INT64. |
| 59 | + if (x == std::numeric_limits<int64_t>::min()) { |
| 60 | + value = uint64_t(1) << 63; |
| 61 | + } else { |
| 62 | + value = -int64_t(x); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + constexpr bool operator==(const IU64& other) const { |
| 68 | + return value == other.value && negative == other.negative; |
| 69 | + } |
| 70 | + constexpr bool operator!=(const IU64& other) const { |
| 71 | + return !(*this == other); |
| 72 | + } |
| 73 | + |
| 74 | + constexpr bool operator<(const IU64& other) const { |
| 75 | + if (negative) { |
| 76 | + if (other.negative) { |
| 77 | + // Both negative; we are smaller if absolute value is larger. |
| 78 | + return value > other.value; |
| 79 | + } else { |
| 80 | + // Only we are negative, so we are smaller. |
| 81 | + return true; |
| 82 | + } |
| 83 | + } else { |
| 84 | + if (other.negative) { |
| 85 | + // Only the other is negative, so we are larger. |
| 86 | + return false; |
| 87 | + } else { |
| 88 | + // Both positive; we are smaller if absolute value is smaller. |
| 89 | + return value < other.value; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + constexpr bool operator<=(const IU64& other) const { |
| 94 | + return *this < other || *this == other; |
| 95 | + } |
| 96 | + constexpr bool operator>(const IU64& other) const { |
| 97 | + return !(*this <= other); |
| 98 | + } |
| 99 | + constexpr bool operator>=(const IU64& other) const { |
| 100 | + return !(*this < other); |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +inline std::ostream& operator<<(std::ostream& os, const IU64& x) { |
| 105 | + if (x.negative) { |
| 106 | + os << '-'; |
| 107 | + } |
| 108 | + return os << x.value; |
| 109 | +} |
| 110 | + |
| 111 | +} // namespace wasm |
| 112 | + |
| 113 | +namespace std { |
| 114 | + |
| 115 | +template<> class numeric_limits<wasm::IU64> { |
| 116 | +public: |
| 117 | + static constexpr bool is_specialized = true; |
| 118 | + static constexpr bool is_signed = true; |
| 119 | + static constexpr bool is_integer = true; |
| 120 | + static constexpr bool is_exact = true; |
| 121 | + static constexpr bool has_infinity = false; |
| 122 | + static constexpr bool has_quiet_NaN = false; |
| 123 | + static constexpr bool has_signaling_NaN = false; |
| 124 | + static constexpr float_denorm_style has_denorm = denorm_absent; |
| 125 | + static constexpr bool has_denorm_loss = false; |
| 126 | + static constexpr float_round_style round_style = round_toward_zero; |
| 127 | + static constexpr bool is_iec559 = false; |
| 128 | + static constexpr bool is_bounded = true; |
| 129 | + static constexpr bool is_modulo = false; |
| 130 | + static constexpr int digits = 65; |
| 131 | + static constexpr int digits10 = 19; |
| 132 | + static constexpr int max_digits10 = 0; |
| 133 | + static constexpr int radix = 2; |
| 134 | + static constexpr int min_exponent = 0; |
| 135 | + static constexpr int min_exponent10 = 0; |
| 136 | + static constexpr int max_exponent = 0; |
| 137 | + static constexpr int max_exponent10 = 0; |
| 138 | + static constexpr bool traps = false; |
| 139 | + static constexpr bool tinyness_before = false; |
| 140 | + |
| 141 | + static constexpr wasm::IU64 min() noexcept { |
| 142 | + return wasm::IU64(std::numeric_limits<int64_t>::min()); |
| 143 | + } |
| 144 | + static constexpr wasm::IU64 lowest() noexcept { return min(); } |
| 145 | + static constexpr wasm::IU64 max() noexcept { |
| 146 | + return wasm::IU64(std::numeric_limits<uint64_t>::max()); |
| 147 | + } |
| 148 | +}; |
| 149 | + |
| 150 | +} // namespace std |
| 151 | + |
| 152 | +#endif // wasm_support_i65_h |
0 commit comments