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 *not* defined. This file 18// is included by int128.h and relies on ABSL_INTERNAL_WCHAR_T being defined. 19 20constexpr uint64_t Int128Low64(int128 v) { return v.lo_; } 21 22constexpr int64_t Int128High64(int128 v) { return v.hi_; } 23 24#if defined(ABSL_IS_LITTLE_ENDIAN) 25 26constexpr int128::int128(int64_t high, uint64_t low) : 27 lo_(low), hi_(high) {} 28 29constexpr int128::int128(int v) 30 : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {} 31constexpr int128::int128(long v) // NOLINT(runtime/int) 32 : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {} 33constexpr int128::int128(long long v) // NOLINT(runtime/int) 34 : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {} 35 36constexpr int128::int128(unsigned int v) : lo_{v}, hi_{0} {} 37// NOLINTNEXTLINE(runtime/int) 38constexpr int128::int128(unsigned long v) : lo_{v}, hi_{0} {} 39// NOLINTNEXTLINE(runtime/int) 40constexpr int128::int128(unsigned long long v) : lo_{v}, hi_{0} {} 41 42constexpr int128::int128(uint128 v) 43 : lo_{Uint128Low64(v)}, hi_{static_cast<int64_t>(Uint128High64(v))} {} 44 45#elif defined(ABSL_IS_BIG_ENDIAN) 46 47constexpr int128::int128(int64_t high, uint64_t low) : 48 hi_{high}, lo_{low} {} 49 50constexpr int128::int128(int v) 51 : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {} 52constexpr int128::int128(long v) // NOLINT(runtime/int) 53 : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {} 54constexpr int128::int128(long long v) // NOLINT(runtime/int) 55 : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {} 56 57constexpr int128::int128(unsigned int v) : hi_{0}, lo_{v} {} 58// NOLINTNEXTLINE(runtime/int) 59constexpr int128::int128(unsigned long v) : hi_{0}, lo_{v} {} 60// NOLINTNEXTLINE(runtime/int) 61constexpr int128::int128(unsigned long long v) : hi_{0}, lo_{v} {} 62 63constexpr int128::int128(uint128 v) 64 : hi_{static_cast<int64_t>(Uint128High64(v))}, lo_{Uint128Low64(v)} {} 65 66#else // byte order 67#error "Unsupported byte order: must be little-endian or big-endian." 68#endif // byte order 69 70constexpr int128::operator bool() const { return lo_ || hi_; } 71 72constexpr int128::operator char() const { 73 // NOLINTNEXTLINE(runtime/int) 74 return static_cast<char>(static_cast<long long>(*this)); 75} 76 77constexpr int128::operator signed char() const { 78 // NOLINTNEXTLINE(runtime/int) 79 return static_cast<signed char>(static_cast<long long>(*this)); 80} 81 82constexpr int128::operator unsigned char() const { 83 return static_cast<unsigned char>(lo_); 84} 85 86constexpr int128::operator char16_t() const { 87 return static_cast<char16_t>(lo_); 88} 89 90constexpr int128::operator char32_t() const { 91 return static_cast<char32_t>(lo_); 92} 93 94constexpr int128::operator ABSL_INTERNAL_WCHAR_T() const { 95 // NOLINTNEXTLINE(runtime/int) 96 return static_cast<ABSL_INTERNAL_WCHAR_T>(static_cast<long long>(*this)); 97} 98 99constexpr int128::operator short() const { // NOLINT(runtime/int) 100 // NOLINTNEXTLINE(runtime/int) 101 return static_cast<short>(static_cast<long long>(*this)); 102} 103 104constexpr int128::operator unsigned short() const { // NOLINT(runtime/int) 105 return static_cast<unsigned short>(lo_); // NOLINT(runtime/int) 106} 107 108constexpr int128::operator int() const { 109 // NOLINTNEXTLINE(runtime/int) 110 return static_cast<int>(static_cast<long long>(*this)); 111} 112 113constexpr int128::operator unsigned int() const { 114 return static_cast<unsigned int>(lo_); 115} 116 117constexpr int128::operator long() const { // NOLINT(runtime/int) 118 // NOLINTNEXTLINE(runtime/int) 119 return static_cast<long>(static_cast<long long>(*this)); 120} 121 122constexpr int128::operator unsigned long() const { // NOLINT(runtime/int) 123 return static_cast<unsigned long>(lo_); // NOLINT(runtime/int) 124} 125 126constexpr int128::operator long long() const { // NOLINT(runtime/int) 127 // We don't bother checking the value of hi_. If *this < 0, lo_'s high bit 128 // must be set in order for the value to fit into a long long. Conversely, if 129 // lo_'s high bit is set, *this must be < 0 for the value to fit. 130 return int128_internal::BitCastToSigned(lo_); 131} 132 133constexpr int128::operator unsigned long long() const { // NOLINT(runtime/int) 134 return static_cast<unsigned long long>(lo_); // NOLINT(runtime/int) 135} 136 137inline int128::operator float() const { 138 // We must convert the absolute value and then negate as needed, because 139 // floating point types are typically sign-magnitude. Otherwise, the 140 // difference between the high and low 64 bits when interpreted as two's 141 // complement overwhelms the precision of the mantissa. 142 // 143 // Also check to make sure we don't negate Int128Min() 144 return hi_ < 0 && *this != Int128Min() 145 ? -static_cast<float>(-*this) 146 : static_cast<float>(lo_) + 147 std::ldexp(static_cast<float>(hi_), 64); 148} 149 150inline int128::operator double() const { 151 // See comment in int128::operator float() above. 152 return hi_ < 0 && *this != Int128Min() 153 ? -static_cast<double>(-*this) 154 : static_cast<double>(lo_) + 155 std::ldexp(static_cast<double>(hi_), 64); 156} 157 158inline int128::operator long double() const { 159 // See comment in int128::operator float() above. 160 return hi_ < 0 && *this != Int128Min() 161 ? -static_cast<long double>(-*this) 162 : static_cast<long double>(lo_) + 163 std::ldexp(static_cast<long double>(hi_), 64); 164} 165 166// Comparison operators. 167 168constexpr bool operator==(int128 lhs, int128 rhs) { 169 return (Int128Low64(lhs) == Int128Low64(rhs) && 170 Int128High64(lhs) == Int128High64(rhs)); 171} 172 173constexpr bool operator!=(int128 lhs, int128 rhs) { return !(lhs == rhs); } 174 175constexpr bool operator<(int128 lhs, int128 rhs) { 176 return (Int128High64(lhs) == Int128High64(rhs)) 177 ? (Int128Low64(lhs) < Int128Low64(rhs)) 178 : (Int128High64(lhs) < Int128High64(rhs)); 179} 180 181constexpr bool operator>(int128 lhs, int128 rhs) { 182 return (Int128High64(lhs) == Int128High64(rhs)) 183 ? (Int128Low64(lhs) > Int128Low64(rhs)) 184 : (Int128High64(lhs) > Int128High64(rhs)); 185} 186 187constexpr bool operator<=(int128 lhs, int128 rhs) { return !(lhs > rhs); } 188 189constexpr bool operator>=(int128 lhs, int128 rhs) { return !(lhs < rhs); } 190 191// Unary operators. 192 193constexpr int128 operator-(int128 v) { 194 return MakeInt128(~Int128High64(v) + (Int128Low64(v) == 0), 195 ~Int128Low64(v) + 1); 196} 197 198constexpr bool operator!(int128 v) { 199 return !Int128Low64(v) && !Int128High64(v); 200} 201 202constexpr int128 operator~(int128 val) { 203 return MakeInt128(~Int128High64(val), ~Int128Low64(val)); 204} 205 206// Arithmetic operators. 207 208namespace int128_internal { 209constexpr int128 SignedAddResult(int128 result, int128 lhs) { 210 // check for carry 211 return (Int128Low64(result) < Int128Low64(lhs)) 212 ? MakeInt128(Int128High64(result) + 1, Int128Low64(result)) 213 : result; 214} 215} // namespace int128_internal 216constexpr int128 operator+(int128 lhs, int128 rhs) { 217 return int128_internal::SignedAddResult( 218 MakeInt128(Int128High64(lhs) + Int128High64(rhs), 219 Int128Low64(lhs) + Int128Low64(rhs)), 220 lhs); 221} 222 223namespace int128_internal { 224constexpr int128 SignedSubstructResult(int128 result, int128 lhs, int128 rhs) { 225 // check for carry 226 return (Int128Low64(lhs) < Int128Low64(rhs)) 227 ? MakeInt128(Int128High64(result) - 1, Int128Low64(result)) 228 : result; 229} 230} // namespace int128_internal 231constexpr int128 operator-(int128 lhs, int128 rhs) { 232 return int128_internal::SignedSubstructResult( 233 MakeInt128(Int128High64(lhs) - Int128High64(rhs), 234 Int128Low64(lhs) - Int128Low64(rhs)), 235 lhs, rhs); 236} 237 238inline int128 operator*(int128 lhs, int128 rhs) { 239 return MakeInt128( 240 int128_internal::BitCastToSigned(Uint128High64(uint128(lhs) * rhs)), 241 Uint128Low64(uint128(lhs) * rhs)); 242} 243 244inline int128 int128::operator++(int) { 245 int128 tmp(*this); 246 *this += 1; 247 return tmp; 248} 249 250inline int128 int128::operator--(int) { 251 int128 tmp(*this); 252 *this -= 1; 253 return tmp; 254} 255 256inline int128& int128::operator++() { 257 *this += 1; 258 return *this; 259} 260 261inline int128& int128::operator--() { 262 *this -= 1; 263 return *this; 264} 265 266constexpr int128 operator|(int128 lhs, int128 rhs) { 267 return MakeInt128(Int128High64(lhs) | Int128High64(rhs), 268 Int128Low64(lhs) | Int128Low64(rhs)); 269} 270 271constexpr int128 operator&(int128 lhs, int128 rhs) { 272 return MakeInt128(Int128High64(lhs) & Int128High64(rhs), 273 Int128Low64(lhs) & Int128Low64(rhs)); 274} 275 276constexpr int128 operator^(int128 lhs, int128 rhs) { 277 return MakeInt128(Int128High64(lhs) ^ Int128High64(rhs), 278 Int128Low64(lhs) ^ Int128Low64(rhs)); 279} 280 281constexpr int128 operator<<(int128 lhs, int amount) { 282 // int64_t shifts of >= 64 are undefined, so we need some special-casing. 283 return amount >= 64 284 ? MakeInt128( 285 static_cast<int64_t>(Int128Low64(lhs) << (amount - 64)), 0) 286 : amount == 0 287 ? lhs 288 : MakeInt128( 289 (Int128High64(lhs) << amount) | 290 static_cast<int64_t>(Int128Low64(lhs) >> (64 - amount)), 291 Int128Low64(lhs) << amount); 292} 293 294constexpr int128 operator>>(int128 lhs, int amount) { 295 // int64_t shifts of >= 64 are undefined, so we need some special-casing. 296 // The (Int128High64(lhs) >> 32) >> 32 "trick" causes the the most significant 297 // int64 to be inititialized with all zeros or all ones correctly. It takes 298 // into account whether the number is negative or positive, and whether the 299 // current architecture does arithmetic or logical right shifts for negative 300 // numbers. 301 return amount >= 64 302 ? MakeInt128( 303 (Int128High64(lhs) >> 32) >> 32, 304 static_cast<uint64_t>(Int128High64(lhs) >> (amount - 64))) 305 : amount == 0 306 ? lhs 307 : MakeInt128(Int128High64(lhs) >> amount, 308 (Int128Low64(lhs) >> amount) | 309 (static_cast<uint64_t>(Int128High64(lhs)) 310 << (64 - amount))); 311} 312