Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/absl/numeric/int128_have_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 defined. This file is
0018 // included by int128.h and relies on ABSL_INTERNAL_WCHAR_T being defined.
0019 
0020 namespace int128_internal {
0021 
0022 // Casts from unsigned to signed while preserving the underlying binary
0023 // representation.
0024 constexpr __int128 BitCastToSigned(unsigned __int128 v) {
0025   // Casting an unsigned integer to a signed integer of the same
0026   // width is implementation defined behavior if the source value would not fit
0027   // in the destination type. We step around it with a roundtrip bitwise not
0028   // operation to make sure this function remains constexpr. Clang and GCC
0029   // optimize this to a no-op on x86-64.
0030   return v & (static_cast<unsigned __int128>(1) << 127)
0031              ? ~static_cast<__int128>(~v)
0032              : static_cast<__int128>(v);
0033 }
0034 
0035 }  // namespace int128_internal
0036 
0037 inline int128& int128::operator=(__int128 v) {
0038   v_ = v;
0039   return *this;
0040 }
0041 
0042 constexpr uint64_t Int128Low64(int128 v) {
0043   return static_cast<uint64_t>(v.v_ & ~uint64_t{0});
0044 }
0045 
0046 constexpr int64_t Int128High64(int128 v) {
0047   // Initially cast to unsigned to prevent a right shift on a negative value.
0048   return int128_internal::BitCastToSigned(
0049       static_cast<uint64_t>(static_cast<unsigned __int128>(v.v_) >> 64));
0050 }
0051 
0052 constexpr int128::int128(int64_t high, uint64_t low)
0053     // Initially cast to unsigned to prevent a left shift that overflows.
0054     : v_(int128_internal::BitCastToSigned(static_cast<unsigned __int128>(high)
0055                                            << 64) |
0056          low) {}
0057 
0058 
0059 constexpr int128::int128(int v) : v_{v} {}
0060 
0061 constexpr int128::int128(long v) : v_{v} {}       // NOLINT(runtime/int)
0062 
0063 constexpr int128::int128(long long v) : v_{v} {}  // NOLINT(runtime/int)
0064 
0065 constexpr int128::int128(__int128 v) : v_{v} {}
0066 
0067 constexpr int128::int128(unsigned int v) : v_{v} {}
0068 
0069 constexpr int128::int128(unsigned long v) : v_{v} {}  // NOLINT(runtime/int)
0070 
0071 // NOLINTNEXTLINE(runtime/int)
0072 constexpr int128::int128(unsigned long long v) : v_{v} {}
0073 
0074 constexpr int128::int128(unsigned __int128 v) : v_{static_cast<__int128>(v)} {}
0075 
0076 inline int128::int128(float v) {
0077   v_ = static_cast<__int128>(v);
0078 }
0079 
0080 inline int128::int128(double v) {
0081   v_ = static_cast<__int128>(v);
0082 }
0083 
0084 inline int128::int128(long double v) {
0085   v_ = static_cast<__int128>(v);
0086 }
0087 
0088 constexpr int128::int128(uint128 v) : v_{static_cast<__int128>(v)} {}
0089 
0090 constexpr int128::operator bool() const { return static_cast<bool>(v_); }
0091 
0092 constexpr int128::operator char() const { return static_cast<char>(v_); }
0093 
0094 constexpr int128::operator signed char() const {
0095   return static_cast<signed char>(v_);
0096 }
0097 
0098 constexpr int128::operator unsigned char() const {
0099   return static_cast<unsigned char>(v_);
0100 }
0101 
0102 constexpr int128::operator char16_t() const {
0103   return static_cast<char16_t>(v_);
0104 }
0105 
0106 constexpr int128::operator char32_t() const {
0107   return static_cast<char32_t>(v_);
0108 }
0109 
0110 constexpr int128::operator ABSL_INTERNAL_WCHAR_T() const {
0111   return static_cast<ABSL_INTERNAL_WCHAR_T>(v_);
0112 }
0113 
0114 constexpr int128::operator short() const {  // NOLINT(runtime/int)
0115   return static_cast<short>(v_);            // NOLINT(runtime/int)
0116 }
0117 
0118 constexpr int128::operator unsigned short() const {  // NOLINT(runtime/int)
0119   return static_cast<unsigned short>(v_);            // NOLINT(runtime/int)
0120 }
0121 
0122 constexpr int128::operator int() const {
0123   return static_cast<int>(v_);
0124 }
0125 
0126 constexpr int128::operator unsigned int() const {
0127   return static_cast<unsigned int>(v_);
0128 }
0129 
0130 constexpr int128::operator long() const {  // NOLINT(runtime/int)
0131   return static_cast<long>(v_);            // NOLINT(runtime/int)
0132 }
0133 
0134 constexpr int128::operator unsigned long() const {  // NOLINT(runtime/int)
0135   return static_cast<unsigned long>(v_);            // NOLINT(runtime/int)
0136 }
0137 
0138 constexpr int128::operator long long() const {  // NOLINT(runtime/int)
0139   return static_cast<long long>(v_);            // NOLINT(runtime/int)
0140 }
0141 
0142 constexpr int128::operator unsigned long long() const {  // NOLINT(runtime/int)
0143   return static_cast<unsigned long long>(v_);            // NOLINT(runtime/int)
0144 }
0145 
0146 constexpr int128::operator __int128() const { return v_; }
0147 
0148 constexpr int128::operator unsigned __int128() const {
0149   return static_cast<unsigned __int128>(v_);
0150 }
0151 
0152 // Clang on PowerPC sometimes produces incorrect __int128 to floating point
0153 // conversions. In that case, we do the conversion with a similar implementation
0154 // to the conversion operators in int128_no_intrinsic.inc.
0155 #if defined(__clang__) && !defined(__ppc64__)
0156 inline int128::operator float() const { return static_cast<float>(v_); }
0157 
0158 inline int128::operator double() const { return static_cast<double>(v_); }
0159 
0160 inline int128::operator long double() const {
0161   return static_cast<long double>(v_);
0162 }
0163 
0164 #else  // Clang on PowerPC
0165 
0166 inline int128::operator float() const {
0167   // We must convert the absolute value and then negate as needed, because
0168   // floating point types are typically sign-magnitude. Otherwise, the
0169   // difference between the high and low 64 bits when interpreted as two's
0170   // complement overwhelms the precision of the mantissa.
0171   //
0172   // Also check to make sure we don't negate Int128Min()
0173   return v_ < 0 && *this != Int128Min()
0174              ? -static_cast<float>(-*this)
0175              : static_cast<float>(Int128Low64(*this)) +
0176                    std::ldexp(static_cast<float>(Int128High64(*this)), 64);
0177 }
0178 
0179 inline int128::operator double() const {
0180   // See comment in int128::operator float() above.
0181   return v_ < 0 && *this != Int128Min()
0182              ? -static_cast<double>(-*this)
0183              : static_cast<double>(Int128Low64(*this)) +
0184                    std::ldexp(static_cast<double>(Int128High64(*this)), 64);
0185 }
0186 
0187 inline int128::operator long double() const {
0188   // See comment in int128::operator float() above.
0189   return v_ < 0 && *this != Int128Min()
0190              ? -static_cast<long double>(-*this)
0191              : static_cast<long double>(Int128Low64(*this)) +
0192                    std::ldexp(static_cast<long double>(Int128High64(*this)),
0193                               64);
0194 }
0195 #endif  // Clang on PowerPC
0196 
0197 // Comparison operators.
0198 
0199 constexpr bool operator==(int128 lhs, int128 rhs) {
0200   return static_cast<__int128>(lhs) == static_cast<__int128>(rhs);
0201 }
0202 
0203 constexpr bool operator!=(int128 lhs, int128 rhs) {
0204   return static_cast<__int128>(lhs) != static_cast<__int128>(rhs);
0205 }
0206 
0207 constexpr bool operator<(int128 lhs, int128 rhs) {
0208   return static_cast<__int128>(lhs) < static_cast<__int128>(rhs);
0209 }
0210 
0211 constexpr bool operator>(int128 lhs, int128 rhs) {
0212   return static_cast<__int128>(lhs) > static_cast<__int128>(rhs);
0213 }
0214 
0215 constexpr bool operator<=(int128 lhs, int128 rhs) {
0216   return static_cast<__int128>(lhs) <= static_cast<__int128>(rhs);
0217 }
0218 
0219 constexpr bool operator>=(int128 lhs, int128 rhs) {
0220   return static_cast<__int128>(lhs) >= static_cast<__int128>(rhs);
0221 }
0222 
0223 #ifdef __cpp_impl_three_way_comparison
0224 constexpr absl::strong_ordering operator<=>(int128 lhs, int128 rhs) {
0225   if (auto lhs_128 = static_cast<__int128>(lhs),
0226       rhs_128 = static_cast<__int128>(rhs);
0227       lhs_128 < rhs_128) {
0228     return absl::strong_ordering::less;
0229   } else if (lhs_128 > rhs_128) {
0230     return absl::strong_ordering::greater;
0231   } else {
0232     return absl::strong_ordering::equal;
0233   }
0234 }
0235 #endif
0236 
0237 // Unary operators.
0238 
0239 constexpr int128 operator-(int128 v) { return -static_cast<__int128>(v); }
0240 
0241 constexpr bool operator!(int128 v) { return !static_cast<__int128>(v); }
0242 
0243 constexpr int128 operator~(int128 val) { return ~static_cast<__int128>(val); }
0244 
0245 // Arithmetic operators.
0246 
0247 constexpr int128 operator+(int128 lhs, int128 rhs) {
0248   return static_cast<__int128>(lhs) + static_cast<__int128>(rhs);
0249 }
0250 
0251 constexpr int128 operator-(int128 lhs, int128 rhs) {
0252   return static_cast<__int128>(lhs) - static_cast<__int128>(rhs);
0253 }
0254 
0255 inline int128 operator*(int128 lhs, int128 rhs) {
0256   return static_cast<__int128>(lhs) * static_cast<__int128>(rhs);
0257 }
0258 
0259 inline int128 operator/(int128 lhs, int128 rhs) {
0260   return static_cast<__int128>(lhs) / static_cast<__int128>(rhs);
0261 }
0262 
0263 inline int128 operator%(int128 lhs, int128 rhs) {
0264   return static_cast<__int128>(lhs) % static_cast<__int128>(rhs);
0265 }
0266 
0267 inline int128 int128::operator++(int) {
0268   int128 tmp(*this);
0269   ++v_;
0270   return tmp;
0271 }
0272 
0273 inline int128 int128::operator--(int) {
0274   int128 tmp(*this);
0275   --v_;
0276   return tmp;
0277 }
0278 
0279 inline int128& int128::operator++() {
0280   ++v_;
0281   return *this;
0282 }
0283 
0284 inline int128& int128::operator--() {
0285   --v_;
0286   return *this;
0287 }
0288 
0289 constexpr int128 operator|(int128 lhs, int128 rhs) {
0290   return static_cast<__int128>(lhs) | static_cast<__int128>(rhs);
0291 }
0292 
0293 constexpr int128 operator&(int128 lhs, int128 rhs) {
0294   return static_cast<__int128>(lhs) & static_cast<__int128>(rhs);
0295 }
0296 
0297 constexpr int128 operator^(int128 lhs, int128 rhs) {
0298   return static_cast<__int128>(lhs) ^ static_cast<__int128>(rhs);
0299 }
0300 
0301 constexpr int128 operator<<(int128 lhs, int amount) {
0302   return static_cast<__int128>(lhs) << amount;
0303 }
0304 
0305 constexpr int128 operator>>(int128 lhs, int amount) {
0306   return static_cast<__int128>(lhs) >> amount;
0307 }