Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2017 The Abseil Authors. |
| 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 | // https://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 | // This file contains :int128 implementation details that depend on internal |
| 17 | // representation when ABSL_HAVE_INTRINSIC_INT128 is defined. This file is |
| 18 | // included by int128.h and relies on ABSL_INTERNAL_WCHAR_T being defined. |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 19 | |
| 20 | namespace int128_internal { |
| 21 | |
| 22 | // Casts from unsigned to signed while preserving the underlying binary |
| 23 | // representation. |
| 24 | constexpr __int128 BitCastToSigned(unsigned __int128 v) { |
| 25 | // Casting an unsigned integer to a signed integer of the same |
| 26 | // width is implementation defined behavior if the source value would not fit |
| 27 | // in the destination type. We step around it with a roundtrip bitwise not |
| 28 | // operation to make sure this function remains constexpr. Clang and GCC |
| 29 | // optimize this to a no-op on x86-64. |
| 30 | return v & (static_cast<unsigned __int128>(1) << 127) |
| 31 | ? ~static_cast<__int128>(~v) |
| 32 | : static_cast<__int128>(v); |
| 33 | } |
| 34 | |
| 35 | } // namespace int128_internal |
| 36 | |
| 37 | inline int128& int128::operator=(__int128 v) { |
| 38 | v_ = v; |
| 39 | return *this; |
| 40 | } |
| 41 | |
| 42 | constexpr uint64_t Int128Low64(int128 v) { |
| 43 | return static_cast<uint64_t>(v.v_ & ~uint64_t{0}); |
| 44 | } |
| 45 | |
| 46 | constexpr int64_t Int128High64(int128 v) { |
| 47 | // Initially cast to unsigned to prevent a right shift on a negative value. |
| 48 | return int128_internal::BitCastToSigned( |
| 49 | static_cast<uint64_t>(static_cast<unsigned __int128>(v.v_) >> 64)); |
| 50 | } |
| 51 | |
| 52 | constexpr int128::int128(int64_t high, uint64_t low) |
| 53 | // Initially cast to unsigned to prevent a left shift that overflows. |
| 54 | : v_(int128_internal::BitCastToSigned(static_cast<unsigned __int128>(high) |
| 55 | << 64) | |
| 56 | low) {} |
| 57 | |
| 58 | |
| 59 | constexpr int128::int128(int v) : v_{v} {} |
| 60 | |
| 61 | constexpr int128::int128(long v) : v_{v} {} // NOLINT(runtime/int) |
| 62 | |
| 63 | constexpr int128::int128(long long v) : v_{v} {} // NOLINT(runtime/int) |
| 64 | |
| 65 | constexpr int128::int128(__int128 v) : v_{v} {} |
| 66 | |
| 67 | constexpr int128::int128(unsigned int v) : v_{v} {} |
| 68 | |
| 69 | constexpr int128::int128(unsigned long v) : v_{v} {} // NOLINT(runtime/int) |
| 70 | |
| 71 | // NOLINTNEXTLINE(runtime/int) |
| 72 | constexpr int128::int128(unsigned long long v) : v_{v} {} |
| 73 | |
| 74 | constexpr int128::int128(unsigned __int128 v) : v_{static_cast<__int128>(v)} {} |
| 75 | |
| 76 | inline int128::int128(float v) { |
| 77 | v_ = static_cast<__int128>(v); |
| 78 | } |
| 79 | |
| 80 | inline int128::int128(double v) { |
| 81 | v_ = static_cast<__int128>(v); |
| 82 | } |
| 83 | |
| 84 | inline int128::int128(long double v) { |
| 85 | v_ = static_cast<__int128>(v); |
| 86 | } |
| 87 | |
| 88 | constexpr int128::int128(uint128 v) : v_{static_cast<__int128>(v)} {} |
| 89 | |
| 90 | constexpr int128::operator bool() const { return static_cast<bool>(v_); } |
| 91 | |
| 92 | constexpr int128::operator char() const { return static_cast<char>(v_); } |
| 93 | |
| 94 | constexpr int128::operator signed char() const { |
| 95 | return static_cast<signed char>(v_); |
| 96 | } |
| 97 | |
| 98 | constexpr int128::operator unsigned char() const { |
| 99 | return static_cast<unsigned char>(v_); |
| 100 | } |
| 101 | |
| 102 | constexpr int128::operator char16_t() const { |
| 103 | return static_cast<char16_t>(v_); |
| 104 | } |
| 105 | |
| 106 | constexpr int128::operator char32_t() const { |
| 107 | return static_cast<char32_t>(v_); |
| 108 | } |
| 109 | |
| 110 | constexpr int128::operator ABSL_INTERNAL_WCHAR_T() const { |
| 111 | return static_cast<ABSL_INTERNAL_WCHAR_T>(v_); |
| 112 | } |
| 113 | |
| 114 | constexpr int128::operator short() const { // NOLINT(runtime/int) |
| 115 | return static_cast<short>(v_); // NOLINT(runtime/int) |
| 116 | } |
| 117 | |
| 118 | constexpr int128::operator unsigned short() const { // NOLINT(runtime/int) |
| 119 | return static_cast<unsigned short>(v_); // NOLINT(runtime/int) |
| 120 | } |
| 121 | |
| 122 | constexpr int128::operator int() const { |
| 123 | return static_cast<int>(v_); |
| 124 | } |
| 125 | |
| 126 | constexpr int128::operator unsigned int() const { |
| 127 | return static_cast<unsigned int>(v_); |
| 128 | } |
| 129 | |
| 130 | constexpr int128::operator long() const { // NOLINT(runtime/int) |
| 131 | return static_cast<long>(v_); // NOLINT(runtime/int) |
| 132 | } |
| 133 | |
| 134 | constexpr int128::operator unsigned long() const { // NOLINT(runtime/int) |
| 135 | return static_cast<unsigned long>(v_); // NOLINT(runtime/int) |
| 136 | } |
| 137 | |
| 138 | constexpr int128::operator long long() const { // NOLINT(runtime/int) |
| 139 | return static_cast<long long>(v_); // NOLINT(runtime/int) |
| 140 | } |
| 141 | |
| 142 | constexpr int128::operator unsigned long long() const { // NOLINT(runtime/int) |
| 143 | return static_cast<unsigned long long>(v_); // NOLINT(runtime/int) |
| 144 | } |
| 145 | |
| 146 | constexpr int128::operator __int128() const { return v_; } |
| 147 | |
| 148 | constexpr int128::operator unsigned __int128() const { |
| 149 | return static_cast<unsigned __int128>(v_); |
| 150 | } |
| 151 | |
| 152 | // Clang on PowerPC sometimes produces incorrect __int128 to floating point |
| 153 | // conversions. In that case, we do the conversion with a similar implementation |
| 154 | // to the conversion operators in int128_no_intrinsic.inc. |
| 155 | #if defined(__clang__) && !defined(__ppc64__) |
| 156 | inline int128::operator float() const { return static_cast<float>(v_); } |
| 157 | |
| 158 | inline int128::operator double () const { return static_cast<double>(v_); } |
| 159 | |
| 160 | inline int128::operator long double() const { |
| 161 | return static_cast<long double>(v_); |
| 162 | } |
| 163 | |
| 164 | #else // Clang on PowerPC |
| 165 | // Forward declaration for conversion operators to floating point types. |
| 166 | int128 operator-(int128 v); |
| 167 | bool operator!=(int128 lhs, int128 rhs); |
| 168 | |
| 169 | inline int128::operator float() const { |
| 170 | // We must convert the absolute value and then negate as needed, because |
| 171 | // floating point types are typically sign-magnitude. Otherwise, the |
| 172 | // difference between the high and low 64 bits when interpreted as two's |
| 173 | // complement overwhelms the precision of the mantissa. |
| 174 | // |
| 175 | // Also check to make sure we don't negate Int128Min() |
| 176 | return v_ < 0 && *this != Int128Min() |
| 177 | ? -static_cast<float>(-*this) |
| 178 | : static_cast<float>(Int128Low64(*this)) + |
| 179 | std::ldexp(static_cast<float>(Int128High64(*this)), 64); |
| 180 | } |
| 181 | |
| 182 | inline int128::operator double() const { |
| 183 | // See comment in int128::operator float() above. |
| 184 | return v_ < 0 && *this != Int128Min() |
| 185 | ? -static_cast<double>(-*this) |
| 186 | : static_cast<double>(Int128Low64(*this)) + |
| 187 | std::ldexp(static_cast<double>(Int128High64(*this)), 64); |
| 188 | } |
| 189 | |
| 190 | inline int128::operator long double() const { |
| 191 | // See comment in int128::operator float() above. |
| 192 | return v_ < 0 && *this != Int128Min() |
| 193 | ? -static_cast<long double>(-*this) |
| 194 | : static_cast<long double>(Int128Low64(*this)) + |
| 195 | std::ldexp(static_cast<long double>(Int128High64(*this)), |
| 196 | 64); |
| 197 | } |
| 198 | #endif // Clang on PowerPC |
| 199 | |
| 200 | // Comparison operators. |
| 201 | |
| 202 | inline bool operator==(int128 lhs, int128 rhs) { |
| 203 | return static_cast<__int128>(lhs) == static_cast<__int128>(rhs); |
| 204 | } |
| 205 | |
| 206 | inline bool operator!=(int128 lhs, int128 rhs) { |
| 207 | return static_cast<__int128>(lhs) != static_cast<__int128>(rhs); |
| 208 | } |
| 209 | |
| 210 | inline bool operator<(int128 lhs, int128 rhs) { |
| 211 | return static_cast<__int128>(lhs) < static_cast<__int128>(rhs); |
| 212 | } |
| 213 | |
| 214 | inline bool operator>(int128 lhs, int128 rhs) { |
| 215 | return static_cast<__int128>(lhs) > static_cast<__int128>(rhs); |
| 216 | } |
| 217 | |
| 218 | inline bool operator<=(int128 lhs, int128 rhs) { |
| 219 | return static_cast<__int128>(lhs) <= static_cast<__int128>(rhs); |
| 220 | } |
| 221 | |
| 222 | inline bool operator>=(int128 lhs, int128 rhs) { |
| 223 | return static_cast<__int128>(lhs) >= static_cast<__int128>(rhs); |
| 224 | } |
| 225 | |
| 226 | // Unary operators. |
| 227 | |
| 228 | inline int128 operator-(int128 v) { |
| 229 | return -static_cast<__int128>(v); |
| 230 | } |
| 231 | |
| 232 | inline bool operator!(int128 v) { |
| 233 | return !static_cast<__int128>(v); |
| 234 | } |
| 235 | |
| 236 | inline int128 operator~(int128 val) { |
| 237 | return ~static_cast<__int128>(val); |
| 238 | } |
| 239 | |
| 240 | // Arithmetic operators. |
| 241 | |
| 242 | inline int128 operator+(int128 lhs, int128 rhs) { |
| 243 | return static_cast<__int128>(lhs) + static_cast<__int128>(rhs); |
| 244 | } |
| 245 | |
| 246 | inline int128 operator-(int128 lhs, int128 rhs) { |
| 247 | return static_cast<__int128>(lhs) - static_cast<__int128>(rhs); |
| 248 | } |
| 249 | |
| 250 | inline int128 operator*(int128 lhs, int128 rhs) { |
| 251 | return static_cast<__int128>(lhs) * static_cast<__int128>(rhs); |
| 252 | } |
| 253 | |
| 254 | inline int128 operator/(int128 lhs, int128 rhs) { |
| 255 | return static_cast<__int128>(lhs) / static_cast<__int128>(rhs); |
| 256 | } |
| 257 | |
| 258 | inline int128 operator%(int128 lhs, int128 rhs) { |
| 259 | return static_cast<__int128>(lhs) % static_cast<__int128>(rhs); |
| 260 | } |
| 261 | |
| 262 | inline int128 int128::operator++(int) { |
| 263 | int128 tmp(*this); |
| 264 | ++v_; |
| 265 | return tmp; |
| 266 | } |
| 267 | |
| 268 | inline int128 int128::operator--(int) { |
| 269 | int128 tmp(*this); |
| 270 | --v_; |
| 271 | return tmp; |
| 272 | } |
| 273 | |
| 274 | inline int128& int128::operator++() { |
| 275 | ++v_; |
| 276 | return *this; |
| 277 | } |
| 278 | |
| 279 | inline int128& int128::operator--() { |
| 280 | --v_; |
| 281 | return *this; |
| 282 | } |
| 283 | |
| 284 | inline int128 operator|(int128 lhs, int128 rhs) { |
| 285 | return static_cast<__int128>(lhs) | static_cast<__int128>(rhs); |
| 286 | } |
| 287 | |
| 288 | inline int128 operator&(int128 lhs, int128 rhs) { |
| 289 | return static_cast<__int128>(lhs) & static_cast<__int128>(rhs); |
| 290 | } |
| 291 | |
| 292 | inline int128 operator^(int128 lhs, int128 rhs) { |
| 293 | return static_cast<__int128>(lhs) ^ static_cast<__int128>(rhs); |
| 294 | } |
| 295 | |
| 296 | inline int128 operator<<(int128 lhs, int amount) { |
| 297 | return static_cast<__int128>(lhs) << amount; |
| 298 | } |
| 299 | |
| 300 | inline int128 operator>>(int128 lhs, int amount) { |
| 301 | return static_cast<__int128>(lhs) >> amount; |
| 302 | } |