Warning, /include/absl/numeric/int128_no_intrinsic.inc is written in an unsupported language. File is not indexed.
0001 //
0002 // Copyright 2017 The Abseil Authors.
0003 //
0004 // Licensed under the Apache License, Version 2.0 (the "License");
0005 // you may not use this file except in compliance with the License.
0006 // You may obtain a copy of the License at
0007 //
0008 // https://www.apache.org/licenses/LICENSE-2.0
0009 //
0010 // Unless required by applicable law or agreed to in writing, software
0011 // distributed under the License is distributed on an "AS IS" BASIS,
0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013 // See the License for the specific language governing permissions and
0014 // limitations under the License.
0015
0016 // This file contains :int128 implementation details that depend on internal
0017 // representation when ABSL_HAVE_INTRINSIC_INT128 is *not* defined. This file
0018 // is included by int128.h and relies on ABSL_INTERNAL_WCHAR_T being defined.
0019
0020 constexpr uint64_t Int128Low64(int128 v) { return v.lo_; }
0021
0022 constexpr int64_t Int128High64(int128 v) { return v.hi_; }
0023
0024 #if defined(ABSL_IS_LITTLE_ENDIAN)
0025
0026 constexpr int128::int128(int64_t high, uint64_t low) : lo_(low), hi_(high) {}
0027
0028 constexpr int128::int128(int v)
0029 : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
0030 constexpr int128::int128(long v) // NOLINT(runtime/int)
0031 : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
0032 constexpr int128::int128(long long v) // NOLINT(runtime/int)
0033 : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
0034
0035 constexpr int128::int128(unsigned int v) : lo_{v}, hi_{0} {}
0036 // NOLINTNEXTLINE(runtime/int)
0037 constexpr int128::int128(unsigned long v) : lo_{v}, hi_{0} {}
0038 // NOLINTNEXTLINE(runtime/int)
0039 constexpr int128::int128(unsigned long long v) : lo_{v}, hi_{0} {}
0040
0041 constexpr int128::int128(uint128 v)
0042 : lo_{Uint128Low64(v)}, hi_{static_cast<int64_t>(Uint128High64(v))} {}
0043
0044 #elif defined(ABSL_IS_BIG_ENDIAN)
0045
0046 constexpr int128::int128(int64_t high, uint64_t low) : hi_{high}, lo_{low} {}
0047
0048 constexpr int128::int128(int v)
0049 : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
0050 constexpr int128::int128(long v) // NOLINT(runtime/int)
0051 : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
0052 constexpr int128::int128(long long v) // NOLINT(runtime/int)
0053 : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
0054
0055 constexpr int128::int128(unsigned int v) : hi_{0}, lo_{v} {}
0056 // NOLINTNEXTLINE(runtime/int)
0057 constexpr int128::int128(unsigned long v) : hi_{0}, lo_{v} {}
0058 // NOLINTNEXTLINE(runtime/int)
0059 constexpr int128::int128(unsigned long long v) : hi_{0}, lo_{v} {}
0060
0061 constexpr int128::int128(uint128 v)
0062 : hi_{static_cast<int64_t>(Uint128High64(v))}, lo_{Uint128Low64(v)} {}
0063
0064 #else // byte order
0065 #error "Unsupported byte order: must be little-endian or big-endian."
0066 #endif // byte order
0067
0068 constexpr int128::operator bool() const { return lo_ || hi_; }
0069
0070 constexpr int128::operator char() const {
0071 // NOLINTNEXTLINE(runtime/int)
0072 return static_cast<char>(static_cast<long long>(*this));
0073 }
0074
0075 constexpr int128::operator signed char() const {
0076 // NOLINTNEXTLINE(runtime/int)
0077 return static_cast<signed char>(static_cast<long long>(*this));
0078 }
0079
0080 constexpr int128::operator unsigned char() const {
0081 return static_cast<unsigned char>(lo_);
0082 }
0083
0084 constexpr int128::operator char16_t() const {
0085 return static_cast<char16_t>(lo_);
0086 }
0087
0088 constexpr int128::operator char32_t() const {
0089 return static_cast<char32_t>(lo_);
0090 }
0091
0092 constexpr int128::operator ABSL_INTERNAL_WCHAR_T() const {
0093 // NOLINTNEXTLINE(runtime/int)
0094 return static_cast<ABSL_INTERNAL_WCHAR_T>(static_cast<long long>(*this));
0095 }
0096
0097 constexpr int128::operator short() const { // NOLINT(runtime/int)
0098 // NOLINTNEXTLINE(runtime/int)
0099 return static_cast<short>(static_cast<long long>(*this));
0100 }
0101
0102 constexpr int128::operator unsigned short() const { // NOLINT(runtime/int)
0103 return static_cast<unsigned short>(lo_); // NOLINT(runtime/int)
0104 }
0105
0106 constexpr int128::operator int() const {
0107 // NOLINTNEXTLINE(runtime/int)
0108 return static_cast<int>(static_cast<long long>(*this));
0109 }
0110
0111 constexpr int128::operator unsigned int() const {
0112 return static_cast<unsigned int>(lo_);
0113 }
0114
0115 constexpr int128::operator long() const { // NOLINT(runtime/int)
0116 // NOLINTNEXTLINE(runtime/int)
0117 return static_cast<long>(static_cast<long long>(*this));
0118 }
0119
0120 constexpr int128::operator unsigned long() const { // NOLINT(runtime/int)
0121 return static_cast<unsigned long>(lo_); // NOLINT(runtime/int)
0122 }
0123
0124 constexpr int128::operator long long() const { // NOLINT(runtime/int)
0125 // We don't bother checking the value of hi_. If *this < 0, lo_'s high bit
0126 // must be set in order for the value to fit into a long long. Conversely, if
0127 // lo_'s high bit is set, *this must be < 0 for the value to fit.
0128 return int128_internal::BitCastToSigned(lo_);
0129 }
0130
0131 constexpr int128::operator unsigned long long() const { // NOLINT(runtime/int)
0132 return static_cast<unsigned long long>(lo_); // NOLINT(runtime/int)
0133 }
0134
0135 inline int128::operator float() const {
0136 // We must convert the absolute value and then negate as needed, because
0137 // floating point types are typically sign-magnitude. Otherwise, the
0138 // difference between the high and low 64 bits when interpreted as two's
0139 // complement overwhelms the precision of the mantissa.
0140 //
0141 // Also check to make sure we don't negate Int128Min()
0142 return hi_ < 0 && *this != Int128Min()
0143 ? -static_cast<float>(-*this)
0144 : static_cast<float>(lo_) +
0145 std::ldexp(static_cast<float>(hi_), 64);
0146 }
0147
0148 inline int128::operator double() const {
0149 // See comment in int128::operator float() above.
0150 return hi_ < 0 && *this != Int128Min()
0151 ? -static_cast<double>(-*this)
0152 : static_cast<double>(lo_) +
0153 std::ldexp(static_cast<double>(hi_), 64);
0154 }
0155
0156 inline int128::operator long double() const {
0157 // See comment in int128::operator float() above.
0158 return hi_ < 0 && *this != Int128Min()
0159 ? -static_cast<long double>(-*this)
0160 : static_cast<long double>(lo_) +
0161 std::ldexp(static_cast<long double>(hi_), 64);
0162 }
0163
0164 // Comparison operators.
0165
0166 constexpr bool operator==(int128 lhs, int128 rhs) {
0167 return (Int128Low64(lhs) == Int128Low64(rhs) &&
0168 Int128High64(lhs) == Int128High64(rhs));
0169 }
0170
0171 constexpr bool operator!=(int128 lhs, int128 rhs) { return !(lhs == rhs); }
0172
0173 constexpr bool operator<(int128 lhs, int128 rhs) {
0174 return (Int128High64(lhs) == Int128High64(rhs))
0175 ? (Int128Low64(lhs) < Int128Low64(rhs))
0176 : (Int128High64(lhs) < Int128High64(rhs));
0177 }
0178
0179 constexpr bool operator>(int128 lhs, int128 rhs) {
0180 return (Int128High64(lhs) == Int128High64(rhs))
0181 ? (Int128Low64(lhs) > Int128Low64(rhs))
0182 : (Int128High64(lhs) > Int128High64(rhs));
0183 }
0184
0185 constexpr bool operator<=(int128 lhs, int128 rhs) { return !(lhs > rhs); }
0186
0187 constexpr bool operator>=(int128 lhs, int128 rhs) { return !(lhs < rhs); }
0188
0189 #ifdef __cpp_impl_three_way_comparison
0190 constexpr absl::strong_ordering operator<=>(int128 lhs, int128 rhs) {
0191 if (int64_t lhs_high = Int128High64(lhs), rhs_high = Int128High64(rhs);
0192 lhs_high < rhs_high) {
0193 return absl::strong_ordering::less;
0194 } else if (lhs_high > rhs_high) {
0195 return absl::strong_ordering::greater;
0196 } else if (uint64_t lhs_low = Uint128Low64(lhs), rhs_low = Uint128Low64(rhs);
0197 lhs_low < rhs_low) {
0198 return absl::strong_ordering::less;
0199 } else if (lhs_low > rhs_low) {
0200 return absl::strong_ordering::greater;
0201 } else {
0202 return absl::strong_ordering::equal;
0203 }
0204 }
0205 #endif
0206
0207 // Unary operators.
0208
0209 constexpr int128 operator-(int128 v) {
0210 return MakeInt128(~Int128High64(v) + (Int128Low64(v) == 0),
0211 ~Int128Low64(v) + 1);
0212 }
0213
0214 constexpr bool operator!(int128 v) {
0215 return !Int128Low64(v) && !Int128High64(v);
0216 }
0217
0218 constexpr int128 operator~(int128 val) {
0219 return MakeInt128(~Int128High64(val), ~Int128Low64(val));
0220 }
0221
0222 // Arithmetic operators.
0223
0224 namespace int128_internal {
0225 constexpr int128 SignedAddResult(int128 result, int128 lhs) {
0226 // check for carry
0227 return (Int128Low64(result) < Int128Low64(lhs))
0228 ? MakeInt128(Int128High64(result) + 1, Int128Low64(result))
0229 : result;
0230 }
0231 } // namespace int128_internal
0232 constexpr int128 operator+(int128 lhs, int128 rhs) {
0233 return int128_internal::SignedAddResult(
0234 MakeInt128(Int128High64(lhs) + Int128High64(rhs),
0235 Int128Low64(lhs) + Int128Low64(rhs)),
0236 lhs);
0237 }
0238
0239 namespace int128_internal {
0240 constexpr int128 SignedSubstructResult(int128 result, int128 lhs, int128 rhs) {
0241 // check for carry
0242 return (Int128Low64(lhs) < Int128Low64(rhs))
0243 ? MakeInt128(Int128High64(result) - 1, Int128Low64(result))
0244 : result;
0245 }
0246 } // namespace int128_internal
0247 constexpr int128 operator-(int128 lhs, int128 rhs) {
0248 return int128_internal::SignedSubstructResult(
0249 MakeInt128(Int128High64(lhs) - Int128High64(rhs),
0250 Int128Low64(lhs) - Int128Low64(rhs)),
0251 lhs, rhs);
0252 }
0253
0254 inline int128 operator*(int128 lhs, int128 rhs) {
0255 return MakeInt128(
0256 int128_internal::BitCastToSigned(Uint128High64(uint128(lhs) * rhs)),
0257 Uint128Low64(uint128(lhs) * rhs));
0258 }
0259
0260 inline int128 int128::operator++(int) {
0261 int128 tmp(*this);
0262 *this += 1;
0263 return tmp;
0264 }
0265
0266 inline int128 int128::operator--(int) {
0267 int128 tmp(*this);
0268 *this -= 1;
0269 return tmp;
0270 }
0271
0272 inline int128& int128::operator++() {
0273 *this += 1;
0274 return *this;
0275 }
0276
0277 inline int128& int128::operator--() {
0278 *this -= 1;
0279 return *this;
0280 }
0281
0282 constexpr int128 operator|(int128 lhs, int128 rhs) {
0283 return MakeInt128(Int128High64(lhs) | Int128High64(rhs),
0284 Int128Low64(lhs) | Int128Low64(rhs));
0285 }
0286
0287 constexpr int128 operator&(int128 lhs, int128 rhs) {
0288 return MakeInt128(Int128High64(lhs) & Int128High64(rhs),
0289 Int128Low64(lhs) & Int128Low64(rhs));
0290 }
0291
0292 constexpr int128 operator^(int128 lhs, int128 rhs) {
0293 return MakeInt128(Int128High64(lhs) ^ Int128High64(rhs),
0294 Int128Low64(lhs) ^ Int128Low64(rhs));
0295 }
0296
0297 constexpr int128 operator<<(int128 lhs, int amount) {
0298 // int64_t shifts of >= 63 are undefined, so we need some special-casing.
0299 assert(amount >= 0 && amount < 127);
0300 if (amount <= 0) {
0301 return lhs;
0302 } else if (amount < 63) {
0303 return MakeInt128(
0304 (Int128High64(lhs) << amount) |
0305 static_cast<int64_t>(Int128Low64(lhs) >> (64 - amount)),
0306 Int128Low64(lhs) << amount);
0307 } else if (amount == 63) {
0308 return MakeInt128(((Int128High64(lhs) << 32) << 31) |
0309 static_cast<int64_t>(Int128Low64(lhs) >> 1),
0310 (Int128Low64(lhs) << 32) << 31);
0311 } else if (amount == 127) {
0312 return MakeInt128(static_cast<int64_t>(Int128Low64(lhs) << 63), 0);
0313 } else if (amount > 127) {
0314 return MakeInt128(0, 0);
0315 } else {
0316 // amount >= 64 && amount < 127
0317 return MakeInt128(static_cast<int64_t>(Int128Low64(lhs) << (amount - 64)),
0318 0);
0319 }
0320 }
0321
0322 constexpr int128 operator>>(int128 lhs, int amount) {
0323 // int64_t shifts of >= 63 are undefined, so we need some special-casing.
0324 assert(amount >= 0 && amount < 127);
0325 if (amount <= 0) {
0326 return lhs;
0327 } else if (amount < 63) {
0328 return MakeInt128(
0329 Int128High64(lhs) >> amount,
0330 Int128Low64(lhs) >> amount | static_cast<uint64_t>(Int128High64(lhs))
0331 << (64 - amount));
0332 } else if (amount == 63) {
0333 return MakeInt128((Int128High64(lhs) >> 32) >> 31,
0334 static_cast<uint64_t>(Int128High64(lhs) << 1) |
0335 (Int128Low64(lhs) >> 32) >> 31);
0336
0337 } else if (amount >= 127) {
0338 return MakeInt128((Int128High64(lhs) >> 32) >> 31,
0339 static_cast<uint64_t>((Int128High64(lhs) >> 32) >> 31));
0340 } else {
0341 // amount >= 64 && amount < 127
0342 return MakeInt128(
0343 (Int128High64(lhs) >> 32) >> 31,
0344 static_cast<uint64_t>(Int128High64(lhs) >> (amount - 64)));
0345 }
0346 }