misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [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 | // |
nik7273 | 38b7043 | 2019-03-08 10:27:53 -0500 | [diff] [blame] | 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 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 | // File: int128.h |
| 18 | // ----------------------------------------------------------------------------- |
| 19 | // |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 20 | // This header file defines 128-bit integer types, `uint128` and `int128`. |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 21 | // |
| 22 | // TODO(absl-team): This module is inconsistent as many inline `uint128` methods |
| 23 | // are defined in this file, while many inline `int128` methods are defined in |
| 24 | // the `int128_*_intrinsic.inc` files. |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 25 | |
| 26 | #ifndef ABSL_NUMERIC_INT128_H_ |
| 27 | #define ABSL_NUMERIC_INT128_H_ |
| 28 | |
| 29 | #include <cassert> |
| 30 | #include <cmath> |
| 31 | #include <cstdint> |
| 32 | #include <cstring> |
| 33 | #include <iosfwd> |
| 34 | #include <limits> |
Tsige Solomon | 34eb767 | 2023-06-21 08:52:53 -0700 | [diff] [blame] | 35 | #include <string> |
Abseil Team | 2c5af55 | 2018-07-18 11:29:01 -0700 | [diff] [blame] | 36 | #include <utility> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 37 | |
| 38 | #include "absl/base/config.h" |
| 39 | #include "absl/base/macros.h" |
| 40 | #include "absl/base/port.h" |
Benjamin Barenblat | 192e959 | 2024-04-18 10:36:51 -0700 | [diff] [blame] | 41 | #include "absl/types/compare.h" |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 42 | |
Abseil Team | 389ec3f | 2018-12-13 10:30:03 -0800 | [diff] [blame] | 43 | #if defined(_MSC_VER) |
| 44 | // In very old versions of MSVC and when the /Zc:wchar_t flag is off, wchar_t is |
| 45 | // a typedef for unsigned short. Otherwise wchar_t is mapped to the __wchar_t |
| 46 | // builtin type. We need to make sure not to define operator wchar_t() |
| 47 | // alongside operator unsigned short() in these instances. |
| 48 | #define ABSL_INTERNAL_WCHAR_T __wchar_t |
Ben Niu | 4c015db | 2022-03-18 13:10:10 -0700 | [diff] [blame] | 49 | #if defined(_M_X64) && !defined(_M_ARM64EC) |
Abseil Team | 7b46e1d | 2018-11-13 13:22:00 -0800 | [diff] [blame] | 50 | #include <intrin.h> |
| 51 | #pragma intrinsic(_umul128) |
Tom Tan | aca0473 | 2019-01-02 17:55:33 -0800 | [diff] [blame] | 52 | #endif // defined(_M_X64) |
Abseil Team | 389ec3f | 2018-12-13 10:30:03 -0800 | [diff] [blame] | 53 | #else // defined(_MSC_VER) |
| 54 | #define ABSL_INTERNAL_WCHAR_T wchar_t |
| 55 | #endif // defined(_MSC_VER) |
Abseil Team | 7b46e1d | 2018-11-13 13:22:00 -0800 | [diff] [blame] | 56 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 57 | namespace absl { |
Abseil Team | 12bc53e | 2019-12-12 10:36:03 -0800 | [diff] [blame] | 58 | ABSL_NAMESPACE_BEGIN |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 59 | |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 60 | class int128; |
| 61 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 62 | // uint128 |
| 63 | // |
| 64 | // An unsigned 128-bit integer type. The API is meant to mimic an intrinsic type |
| 65 | // as closely as is practical, including exhibiting undefined behavior in |
| 66 | // analogous cases (e.g. division by zero). This type is intended to be a |
| 67 | // drop-in replacement once C++ supports an intrinsic `uint128_t` type; when |
Abseil Team | 0d40cb7 | 2018-02-22 08:36:39 -0800 | [diff] [blame] | 68 | // that occurs, existing well-behaved uses of `uint128` will continue to work |
| 69 | // using that new type. |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 70 | // |
Abseil Team | 8d8dcb0 | 2017-09-29 08:44:28 -0700 | [diff] [blame] | 71 | // Note: code written with this type will continue to compile once `uint128_t` |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 72 | // is introduced, provided the replacement helper functions |
| 73 | // `Uint128(Low|High)64()` and `MakeUint128()` are made. |
| 74 | // |
| 75 | // A `uint128` supports the following: |
| 76 | // |
| 77 | // * Implicit construction from integral types |
| 78 | // * Explicit conversion to integral types |
| 79 | // |
| 80 | // Additionally, if your compiler supports `__int128`, `uint128` is |
| 81 | // interoperable with that type. (Abseil checks for this compatibility through |
| 82 | // the `ABSL_HAVE_INTRINSIC_INT128` macro.) |
| 83 | // |
| 84 | // However, a `uint128` differs from intrinsic integral types in the following |
| 85 | // ways: |
| 86 | // |
Abseil Team | 25274b3 | 2018-01-09 08:46:23 -0800 | [diff] [blame] | 87 | // * Errors on implicit conversions that do not preserve value (such as |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 88 | // loss of precision when converting to float values). |
| 89 | // * Requires explicit construction from and conversion to floating point |
| 90 | // types. |
| 91 | // * Conversion to integral types requires an explicit static_cast() to |
| 92 | // mimic use of the `-Wnarrowing` compiler flag. |
Abseil Team | 0d40cb7 | 2018-02-22 08:36:39 -0800 | [diff] [blame] | 93 | // * The alignment requirement of `uint128` may differ from that of an |
| 94 | // intrinsic 128-bit integer type depending on platform and build |
| 95 | // configuration. |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 96 | // |
| 97 | // Example: |
| 98 | // |
Abseil Team | be40fdf | 2018-01-12 07:45:05 -0800 | [diff] [blame] | 99 | // float y = absl::Uint128Max(); // Error. uint128 cannot be implicitly |
| 100 | // // converted to float. |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 101 | // |
Abseil Team | 6a88b40 | 2017-11-20 13:53:50 -0800 | [diff] [blame] | 102 | // absl::uint128 v; |
Abseil Team | 475d64f | 2018-04-09 10:01:14 -0700 | [diff] [blame] | 103 | // uint64_t i = v; // Error |
| 104 | // uint64_t i = static_cast<uint64_t>(v); // OK |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 105 | // |
Abseil Team | 0d40cb7 | 2018-02-22 08:36:39 -0800 | [diff] [blame] | 106 | class |
| 107 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 108 | alignas(unsigned __int128) |
| 109 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 110 | uint128 { |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 111 | public: |
| 112 | uint128() = default; |
| 113 | |
| 114 | // Constructors from arithmetic types |
| 115 | constexpr uint128(int v); // NOLINT(runtime/explicit) |
| 116 | constexpr uint128(unsigned int v); // NOLINT(runtime/explicit) |
| 117 | constexpr uint128(long v); // NOLINT(runtime/int) |
| 118 | constexpr uint128(unsigned long v); // NOLINT(runtime/int) |
| 119 | constexpr uint128(long long v); // NOLINT(runtime/int) |
| 120 | constexpr uint128(unsigned long long v); // NOLINT(runtime/int) |
| 121 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 122 | constexpr uint128(__int128 v); // NOLINT(runtime/explicit) |
| 123 | constexpr uint128(unsigned __int128 v); // NOLINT(runtime/explicit) |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 124 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 125 | constexpr uint128(int128 v); // NOLINT(runtime/explicit) |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 126 | explicit uint128(float v); |
| 127 | explicit uint128(double v); |
| 128 | explicit uint128(long double v); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 129 | |
| 130 | // Assignment operators from arithmetic types |
| 131 | uint128& operator=(int v); |
| 132 | uint128& operator=(unsigned int v); |
| 133 | uint128& operator=(long v); // NOLINT(runtime/int) |
| 134 | uint128& operator=(unsigned long v); // NOLINT(runtime/int) |
| 135 | uint128& operator=(long long v); // NOLINT(runtime/int) |
| 136 | uint128& operator=(unsigned long long v); // NOLINT(runtime/int) |
| 137 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 138 | uint128& operator=(__int128 v); |
| 139 | uint128& operator=(unsigned __int128 v); |
| 140 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 141 | uint128& operator=(int128 v); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 142 | |
| 143 | // Conversion operators to other arithmetic types |
| 144 | constexpr explicit operator bool() const; |
| 145 | constexpr explicit operator char() const; |
| 146 | constexpr explicit operator signed char() const; |
| 147 | constexpr explicit operator unsigned char() const; |
| 148 | constexpr explicit operator char16_t() const; |
| 149 | constexpr explicit operator char32_t() const; |
Abseil Team | 389ec3f | 2018-12-13 10:30:03 -0800 | [diff] [blame] | 150 | constexpr explicit operator ABSL_INTERNAL_WCHAR_T() const; |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 151 | constexpr explicit operator short() const; // NOLINT(runtime/int) |
| 152 | // NOLINTNEXTLINE(runtime/int) |
| 153 | constexpr explicit operator unsigned short() const; |
| 154 | constexpr explicit operator int() const; |
| 155 | constexpr explicit operator unsigned int() const; |
| 156 | constexpr explicit operator long() const; // NOLINT(runtime/int) |
| 157 | // NOLINTNEXTLINE(runtime/int) |
| 158 | constexpr explicit operator unsigned long() const; |
| 159 | // NOLINTNEXTLINE(runtime/int) |
| 160 | constexpr explicit operator long long() const; |
| 161 | // NOLINTNEXTLINE(runtime/int) |
| 162 | constexpr explicit operator unsigned long long() const; |
| 163 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 164 | constexpr explicit operator __int128() const; |
| 165 | constexpr explicit operator unsigned __int128() const; |
| 166 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 167 | explicit operator float() const; |
| 168 | explicit operator double() const; |
| 169 | explicit operator long double() const; |
| 170 | |
| 171 | // Trivial copy constructor, assignment operator and destructor. |
| 172 | |
| 173 | // Arithmetic operators. |
Abseil Team | 1b8dacc | 2017-12-05 10:58:48 -0800 | [diff] [blame] | 174 | uint128& operator+=(uint128 other); |
| 175 | uint128& operator-=(uint128 other); |
| 176 | uint128& operator*=(uint128 other); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 177 | // Long division/modulo for uint128. |
Abseil Team | 1b8dacc | 2017-12-05 10:58:48 -0800 | [diff] [blame] | 178 | uint128& operator/=(uint128 other); |
| 179 | uint128& operator%=(uint128 other); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 180 | uint128 operator++(int); |
| 181 | uint128 operator--(int); |
| 182 | uint128& operator<<=(int); |
| 183 | uint128& operator>>=(int); |
Abseil Team | 1b8dacc | 2017-12-05 10:58:48 -0800 | [diff] [blame] | 184 | uint128& operator&=(uint128 other); |
| 185 | uint128& operator|=(uint128 other); |
| 186 | uint128& operator^=(uint128 other); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 187 | uint128& operator++(); |
| 188 | uint128& operator--(); |
| 189 | |
| 190 | // Uint128Low64() |
| 191 | // |
| 192 | // Returns the lower 64-bit value of a `uint128` value. |
Abseil Team | 1b8dacc | 2017-12-05 10:58:48 -0800 | [diff] [blame] | 193 | friend constexpr uint64_t Uint128Low64(uint128 v); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 194 | |
| 195 | // Uint128High64() |
| 196 | // |
| 197 | // Returns the higher 64-bit value of a `uint128` value. |
Abseil Team | 1b8dacc | 2017-12-05 10:58:48 -0800 | [diff] [blame] | 198 | friend constexpr uint64_t Uint128High64(uint128 v); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 199 | |
| 200 | // MakeUInt128() |
| 201 | // |
| 202 | // Constructs a `uint128` numeric value from two 64-bit unsigned integers. |
| 203 | // Note that this factory function is the only way to construct a `uint128` |
| 204 | // from integer values greater than 2^64. |
| 205 | // |
| 206 | // Example: |
| 207 | // |
| 208 | // absl::uint128 big = absl::MakeUint128(1, 0); |
Abseil Team | 25274b3 | 2018-01-09 08:46:23 -0800 | [diff] [blame] | 209 | friend constexpr uint128 MakeUint128(uint64_t high, uint64_t low); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 210 | |
Abseil Team | be40fdf | 2018-01-12 07:45:05 -0800 | [diff] [blame] | 211 | // Uint128Max() |
| 212 | // |
| 213 | // Returns the highest value for a 128-bit unsigned integer. |
| 214 | friend constexpr uint128 Uint128Max(); |
| 215 | |
Abseil Team | f21d187 | 2018-10-02 12:09:18 -0700 | [diff] [blame] | 216 | // Support for absl::Hash. |
| 217 | template <typename H> |
| 218 | friend H AbslHashValue(H h, uint128 v) { |
| 219 | return H::combine(std::move(h), Uint128High64(v), Uint128Low64(v)); |
| 220 | } |
| 221 | |
Tsige Solomon | 34eb767 | 2023-06-21 08:52:53 -0700 | [diff] [blame] | 222 | // Support for absl::StrCat() etc. |
| 223 | template <typename Sink> |
| 224 | friend void AbslStringify(Sink& sink, uint128 v) { |
| 225 | sink.Append(v.ToString()); |
| 226 | } |
| 227 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 228 | private: |
Abseil Team | 25274b3 | 2018-01-09 08:46:23 -0800 | [diff] [blame] | 229 | constexpr uint128(uint64_t high, uint64_t low); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 230 | |
Tsige Solomon | 34eb767 | 2023-06-21 08:52:53 -0700 | [diff] [blame] | 231 | std::string ToString() const; |
| 232 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 233 | // TODO(strel) Update implementation to use __int128 once all users of |
| 234 | // uint128 are fixed to not depend on alignof(uint128) == 8. Also add |
| 235 | // alignas(16) to class definition to keep alignment consistent across |
| 236 | // platforms. |
| 237 | #if defined(ABSL_IS_LITTLE_ENDIAN) |
| 238 | uint64_t lo_; |
| 239 | uint64_t hi_; |
| 240 | #elif defined(ABSL_IS_BIG_ENDIAN) |
| 241 | uint64_t hi_; |
| 242 | uint64_t lo_; |
| 243 | #else // byte order |
| 244 | #error "Unsupported byte order: must be little-endian or big-endian." |
| 245 | #endif // byte order |
| 246 | }; |
| 247 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 248 | // allow uint128 to be logged |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 249 | std::ostream& operator<<(std::ostream& os, uint128 v); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 250 | |
Abseil Team | 1b8dacc | 2017-12-05 10:58:48 -0800 | [diff] [blame] | 251 | // TODO(strel) add operator>>(std::istream&, uint128) |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 252 | |
Abseil Team | 87a4c07 | 2018-06-25 09:18:19 -0700 | [diff] [blame] | 253 | constexpr uint128 Uint128Max() { |
Abseil Team | a4c3fff | 2018-10-29 15:53:34 -0700 | [diff] [blame] | 254 | return uint128((std::numeric_limits<uint64_t>::max)(), |
| 255 | (std::numeric_limits<uint64_t>::max)()); |
Abseil Team | 87a4c07 | 2018-06-25 09:18:19 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Abseil Team | 12bc53e | 2019-12-12 10:36:03 -0800 | [diff] [blame] | 258 | ABSL_NAMESPACE_END |
Abseil Team | 87a4c07 | 2018-06-25 09:18:19 -0700 | [diff] [blame] | 259 | } // namespace absl |
| 260 | |
| 261 | // Specialized numeric_limits for uint128. |
| 262 | namespace std { |
| 263 | template <> |
| 264 | class numeric_limits<absl::uint128> { |
| 265 | public: |
| 266 | static constexpr bool is_specialized = true; |
| 267 | static constexpr bool is_signed = false; |
| 268 | static constexpr bool is_integer = true; |
| 269 | static constexpr bool is_exact = true; |
| 270 | static constexpr bool has_infinity = false; |
| 271 | static constexpr bool has_quiet_NaN = false; |
| 272 | static constexpr bool has_signaling_NaN = false; |
Pavel P | 0908376 | 2024-04-18 13:00:54 -0700 | [diff] [blame] | 273 | ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING |
Abseil Team | 87a4c07 | 2018-06-25 09:18:19 -0700 | [diff] [blame] | 274 | static constexpr float_denorm_style has_denorm = denorm_absent; |
Pavel P | 0908376 | 2024-04-18 13:00:54 -0700 | [diff] [blame] | 275 | ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING |
Abseil Team | 87a4c07 | 2018-06-25 09:18:19 -0700 | [diff] [blame] | 276 | static constexpr bool has_denorm_loss = false; |
| 277 | static constexpr float_round_style round_style = round_toward_zero; |
| 278 | static constexpr bool is_iec559 = false; |
| 279 | static constexpr bool is_bounded = true; |
| 280 | static constexpr bool is_modulo = true; |
| 281 | static constexpr int digits = 128; |
| 282 | static constexpr int digits10 = 38; |
| 283 | static constexpr int max_digits10 = 0; |
| 284 | static constexpr int radix = 2; |
| 285 | static constexpr int min_exponent = 0; |
| 286 | static constexpr int min_exponent10 = 0; |
| 287 | static constexpr int max_exponent = 0; |
| 288 | static constexpr int max_exponent10 = 0; |
| 289 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 290 | static constexpr bool traps = numeric_limits<unsigned __int128>::traps; |
| 291 | #else // ABSL_HAVE_INTRINSIC_INT128 |
| 292 | static constexpr bool traps = numeric_limits<uint64_t>::traps; |
| 293 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 294 | static constexpr bool tinyness_before = false; |
| 295 | |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 296 | static constexpr absl::uint128(min)() { return 0; } |
Abseil Team | 87a4c07 | 2018-06-25 09:18:19 -0700 | [diff] [blame] | 297 | static constexpr absl::uint128 lowest() { return 0; } |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 298 | static constexpr absl::uint128(max)() { return absl::Uint128Max(); } |
Abseil Team | 87a4c07 | 2018-06-25 09:18:19 -0700 | [diff] [blame] | 299 | static constexpr absl::uint128 epsilon() { return 0; } |
| 300 | static constexpr absl::uint128 round_error() { return 0; } |
| 301 | static constexpr absl::uint128 infinity() { return 0; } |
| 302 | static constexpr absl::uint128 quiet_NaN() { return 0; } |
| 303 | static constexpr absl::uint128 signaling_NaN() { return 0; } |
| 304 | static constexpr absl::uint128 denorm_min() { return 0; } |
| 305 | }; |
| 306 | } // namespace std |
| 307 | |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 308 | namespace absl { |
Abseil Team | 12bc53e | 2019-12-12 10:36:03 -0800 | [diff] [blame] | 309 | ABSL_NAMESPACE_BEGIN |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 310 | |
| 311 | // int128 |
| 312 | // |
| 313 | // A signed 128-bit integer type. The API is meant to mimic an intrinsic |
| 314 | // integral type as closely as is practical, including exhibiting undefined |
| 315 | // behavior in analogous cases (e.g. division by zero). |
| 316 | // |
| 317 | // An `int128` supports the following: |
| 318 | // |
| 319 | // * Implicit construction from integral types |
| 320 | // * Explicit conversion to integral types |
| 321 | // |
| 322 | // However, an `int128` differs from intrinsic integral types in the following |
| 323 | // ways: |
| 324 | // |
| 325 | // * It is not implicitly convertible to other integral types. |
| 326 | // * Requires explicit construction from and conversion to floating point |
| 327 | // types. |
| 328 | |
| 329 | // Additionally, if your compiler supports `__int128`, `int128` is |
| 330 | // interoperable with that type. (Abseil checks for this compatibility through |
| 331 | // the `ABSL_HAVE_INTRINSIC_INT128` macro.) |
| 332 | // |
| 333 | // The design goal for `int128` is that it will be compatible with a future |
| 334 | // `int128_t`, if that type becomes a part of the standard. |
| 335 | // |
| 336 | // Example: |
| 337 | // |
| 338 | // float y = absl::int128(17); // Error. int128 cannot be implicitly |
| 339 | // // converted to float. |
| 340 | // |
| 341 | // absl::int128 v; |
| 342 | // int64_t i = v; // Error |
| 343 | // int64_t i = static_cast<int64_t>(v); // OK |
| 344 | // |
| 345 | class int128 { |
| 346 | public: |
| 347 | int128() = default; |
| 348 | |
| 349 | // Constructors from arithmetic types |
| 350 | constexpr int128(int v); // NOLINT(runtime/explicit) |
| 351 | constexpr int128(unsigned int v); // NOLINT(runtime/explicit) |
| 352 | constexpr int128(long v); // NOLINT(runtime/int) |
| 353 | constexpr int128(unsigned long v); // NOLINT(runtime/int) |
| 354 | constexpr int128(long long v); // NOLINT(runtime/int) |
| 355 | constexpr int128(unsigned long long v); // NOLINT(runtime/int) |
| 356 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 357 | constexpr int128(__int128 v); // NOLINT(runtime/explicit) |
| 358 | constexpr explicit int128(unsigned __int128 v); |
| 359 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 360 | constexpr explicit int128(uint128 v); |
| 361 | explicit int128(float v); |
| 362 | explicit int128(double v); |
| 363 | explicit int128(long double v); |
| 364 | |
| 365 | // Assignment operators from arithmetic types |
| 366 | int128& operator=(int v); |
| 367 | int128& operator=(unsigned int v); |
| 368 | int128& operator=(long v); // NOLINT(runtime/int) |
| 369 | int128& operator=(unsigned long v); // NOLINT(runtime/int) |
| 370 | int128& operator=(long long v); // NOLINT(runtime/int) |
| 371 | int128& operator=(unsigned long long v); // NOLINT(runtime/int) |
| 372 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 373 | int128& operator=(__int128 v); |
| 374 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 375 | |
| 376 | // Conversion operators to other arithmetic types |
| 377 | constexpr explicit operator bool() const; |
| 378 | constexpr explicit operator char() const; |
| 379 | constexpr explicit operator signed char() const; |
| 380 | constexpr explicit operator unsigned char() const; |
| 381 | constexpr explicit operator char16_t() const; |
| 382 | constexpr explicit operator char32_t() const; |
| 383 | constexpr explicit operator ABSL_INTERNAL_WCHAR_T() const; |
| 384 | constexpr explicit operator short() const; // NOLINT(runtime/int) |
| 385 | // NOLINTNEXTLINE(runtime/int) |
| 386 | constexpr explicit operator unsigned short() const; |
| 387 | constexpr explicit operator int() const; |
| 388 | constexpr explicit operator unsigned int() const; |
| 389 | constexpr explicit operator long() const; // NOLINT(runtime/int) |
| 390 | // NOLINTNEXTLINE(runtime/int) |
| 391 | constexpr explicit operator unsigned long() const; |
| 392 | // NOLINTNEXTLINE(runtime/int) |
| 393 | constexpr explicit operator long long() const; |
| 394 | // NOLINTNEXTLINE(runtime/int) |
| 395 | constexpr explicit operator unsigned long long() const; |
| 396 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 397 | constexpr explicit operator __int128() const; |
| 398 | constexpr explicit operator unsigned __int128() const; |
| 399 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 400 | explicit operator float() const; |
| 401 | explicit operator double() const; |
| 402 | explicit operator long double() const; |
| 403 | |
| 404 | // Trivial copy constructor, assignment operator and destructor. |
| 405 | |
| 406 | // Arithmetic operators |
| 407 | int128& operator+=(int128 other); |
| 408 | int128& operator-=(int128 other); |
| 409 | int128& operator*=(int128 other); |
| 410 | int128& operator/=(int128 other); |
| 411 | int128& operator%=(int128 other); |
| 412 | int128 operator++(int); // postfix increment: i++ |
| 413 | int128 operator--(int); // postfix decrement: i-- |
| 414 | int128& operator++(); // prefix increment: ++i |
| 415 | int128& operator--(); // prefix decrement: --i |
| 416 | int128& operator&=(int128 other); |
| 417 | int128& operator|=(int128 other); |
| 418 | int128& operator^=(int128 other); |
| 419 | int128& operator<<=(int amount); |
| 420 | int128& operator>>=(int amount); |
| 421 | |
| 422 | // Int128Low64() |
| 423 | // |
| 424 | // Returns the lower 64-bit value of a `int128` value. |
| 425 | friend constexpr uint64_t Int128Low64(int128 v); |
| 426 | |
| 427 | // Int128High64() |
| 428 | // |
| 429 | // Returns the higher 64-bit value of a `int128` value. |
| 430 | friend constexpr int64_t Int128High64(int128 v); |
| 431 | |
| 432 | // MakeInt128() |
| 433 | // |
| 434 | // Constructs a `int128` numeric value from two 64-bit integers. Note that |
| 435 | // signedness is conveyed in the upper `high` value. |
| 436 | // |
| 437 | // (absl::int128(1) << 64) * high + low |
| 438 | // |
| 439 | // Note that this factory function is the only way to construct a `int128` |
| 440 | // from integer values greater than 2^64 or less than -2^64. |
| 441 | // |
| 442 | // Example: |
| 443 | // |
| 444 | // absl::int128 big = absl::MakeInt128(1, 0); |
| 445 | // absl::int128 big_n = absl::MakeInt128(-1, 0); |
| 446 | friend constexpr int128 MakeInt128(int64_t high, uint64_t low); |
| 447 | |
| 448 | // Int128Max() |
| 449 | // |
| 450 | // Returns the maximum value for a 128-bit signed integer. |
| 451 | friend constexpr int128 Int128Max(); |
| 452 | |
| 453 | // Int128Min() |
| 454 | // |
| 455 | // Returns the minimum value for a 128-bit signed integer. |
| 456 | friend constexpr int128 Int128Min(); |
| 457 | |
| 458 | // Support for absl::Hash. |
| 459 | template <typename H> |
| 460 | friend H AbslHashValue(H h, int128 v) { |
| 461 | return H::combine(std::move(h), Int128High64(v), Int128Low64(v)); |
| 462 | } |
| 463 | |
Tsige Solomon | 34eb767 | 2023-06-21 08:52:53 -0700 | [diff] [blame] | 464 | // Support for absl::StrCat() etc. |
| 465 | template <typename Sink> |
| 466 | friend void AbslStringify(Sink& sink, int128 v) { |
| 467 | sink.Append(v.ToString()); |
| 468 | } |
| 469 | |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 470 | private: |
| 471 | constexpr int128(int64_t high, uint64_t low); |
| 472 | |
Tsige Solomon | 34eb767 | 2023-06-21 08:52:53 -0700 | [diff] [blame] | 473 | std::string ToString() const; |
| 474 | |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 475 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 476 | __int128 v_; |
| 477 | #else // ABSL_HAVE_INTRINSIC_INT128 |
| 478 | #if defined(ABSL_IS_LITTLE_ENDIAN) |
| 479 | uint64_t lo_; |
| 480 | int64_t hi_; |
| 481 | #elif defined(ABSL_IS_BIG_ENDIAN) |
| 482 | int64_t hi_; |
| 483 | uint64_t lo_; |
| 484 | #else // byte order |
| 485 | #error "Unsupported byte order: must be little-endian or big-endian." |
| 486 | #endif // byte order |
| 487 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 488 | }; |
| 489 | |
| 490 | std::ostream& operator<<(std::ostream& os, int128 v); |
| 491 | |
| 492 | // TODO(absl-team) add operator>>(std::istream&, int128) |
| 493 | |
| 494 | constexpr int128 Int128Max() { |
| 495 | return int128((std::numeric_limits<int64_t>::max)(), |
| 496 | (std::numeric_limits<uint64_t>::max)()); |
| 497 | } |
| 498 | |
| 499 | constexpr int128 Int128Min() { |
| 500 | return int128((std::numeric_limits<int64_t>::min)(), 0); |
| 501 | } |
| 502 | |
Abseil Team | 12bc53e | 2019-12-12 10:36:03 -0800 | [diff] [blame] | 503 | ABSL_NAMESPACE_END |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 504 | } // namespace absl |
| 505 | |
| 506 | // Specialized numeric_limits for int128. |
| 507 | namespace std { |
| 508 | template <> |
| 509 | class numeric_limits<absl::int128> { |
| 510 | public: |
| 511 | static constexpr bool is_specialized = true; |
| 512 | static constexpr bool is_signed = true; |
| 513 | static constexpr bool is_integer = true; |
| 514 | static constexpr bool is_exact = true; |
| 515 | static constexpr bool has_infinity = false; |
| 516 | static constexpr bool has_quiet_NaN = false; |
| 517 | static constexpr bool has_signaling_NaN = false; |
Pavel P | 0908376 | 2024-04-18 13:00:54 -0700 | [diff] [blame] | 518 | ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 519 | static constexpr float_denorm_style has_denorm = denorm_absent; |
Pavel P | 0908376 | 2024-04-18 13:00:54 -0700 | [diff] [blame] | 520 | ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 521 | static constexpr bool has_denorm_loss = false; |
| 522 | static constexpr float_round_style round_style = round_toward_zero; |
| 523 | static constexpr bool is_iec559 = false; |
| 524 | static constexpr bool is_bounded = true; |
| 525 | static constexpr bool is_modulo = false; |
| 526 | static constexpr int digits = 127; |
| 527 | static constexpr int digits10 = 38; |
| 528 | static constexpr int max_digits10 = 0; |
| 529 | static constexpr int radix = 2; |
| 530 | static constexpr int min_exponent = 0; |
| 531 | static constexpr int min_exponent10 = 0; |
| 532 | static constexpr int max_exponent = 0; |
| 533 | static constexpr int max_exponent10 = 0; |
| 534 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 535 | static constexpr bool traps = numeric_limits<__int128>::traps; |
| 536 | #else // ABSL_HAVE_INTRINSIC_INT128 |
| 537 | static constexpr bool traps = numeric_limits<uint64_t>::traps; |
| 538 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 539 | static constexpr bool tinyness_before = false; |
| 540 | |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 541 | static constexpr absl::int128(min)() { return absl::Int128Min(); } |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 542 | static constexpr absl::int128 lowest() { return absl::Int128Min(); } |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 543 | static constexpr absl::int128(max)() { return absl::Int128Max(); } |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 544 | static constexpr absl::int128 epsilon() { return 0; } |
| 545 | static constexpr absl::int128 round_error() { return 0; } |
| 546 | static constexpr absl::int128 infinity() { return 0; } |
| 547 | static constexpr absl::int128 quiet_NaN() { return 0; } |
| 548 | static constexpr absl::int128 signaling_NaN() { return 0; } |
| 549 | static constexpr absl::int128 denorm_min() { return 0; } |
| 550 | }; |
| 551 | } // namespace std |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 552 | |
| 553 | // -------------------------------------------------------------------------- |
| 554 | // Implementation details follow |
| 555 | // -------------------------------------------------------------------------- |
Abseil Team | 87a4c07 | 2018-06-25 09:18:19 -0700 | [diff] [blame] | 556 | namespace absl { |
Abseil Team | 12bc53e | 2019-12-12 10:36:03 -0800 | [diff] [blame] | 557 | ABSL_NAMESPACE_BEGIN |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 558 | |
Abseil Team | 25274b3 | 2018-01-09 08:46:23 -0800 | [diff] [blame] | 559 | constexpr uint128 MakeUint128(uint64_t high, uint64_t low) { |
| 560 | return uint128(high, low); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | // Assignment from integer types. |
| 564 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 565 | inline uint128& uint128::operator=(int v) { return *this = uint128(v); } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 566 | |
| 567 | inline uint128& uint128::operator=(unsigned int v) { |
| 568 | return *this = uint128(v); |
| 569 | } |
| 570 | |
| 571 | inline uint128& uint128::operator=(long v) { // NOLINT(runtime/int) |
| 572 | return *this = uint128(v); |
| 573 | } |
| 574 | |
| 575 | // NOLINTNEXTLINE(runtime/int) |
| 576 | inline uint128& uint128::operator=(unsigned long v) { |
| 577 | return *this = uint128(v); |
| 578 | } |
| 579 | |
| 580 | // NOLINTNEXTLINE(runtime/int) |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 581 | inline uint128& uint128::operator=(long long v) { return *this = uint128(v); } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 582 | |
| 583 | // NOLINTNEXTLINE(runtime/int) |
| 584 | inline uint128& uint128::operator=(unsigned long long v) { |
| 585 | return *this = uint128(v); |
| 586 | } |
| 587 | |
| 588 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 589 | inline uint128& uint128::operator=(__int128 v) { return *this = uint128(v); } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 590 | |
| 591 | inline uint128& uint128::operator=(unsigned __int128 v) { |
| 592 | return *this = uint128(v); |
| 593 | } |
| 594 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 595 | |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 596 | inline uint128& uint128::operator=(int128 v) { return *this = uint128(v); } |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 597 | |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 598 | // Arithmetic operators. |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 599 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 600 | constexpr uint128 operator<<(uint128 lhs, int amount); |
| 601 | constexpr uint128 operator>>(uint128 lhs, int amount); |
| 602 | constexpr uint128 operator+(uint128 lhs, uint128 rhs); |
| 603 | constexpr uint128 operator-(uint128 lhs, uint128 rhs); |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 604 | uint128 operator*(uint128 lhs, uint128 rhs); |
| 605 | uint128 operator/(uint128 lhs, uint128 rhs); |
| 606 | uint128 operator%(uint128 lhs, uint128 rhs); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 607 | |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 608 | inline uint128& uint128::operator<<=(int amount) { |
| 609 | *this = *this << amount; |
| 610 | return *this; |
| 611 | } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 612 | |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 613 | inline uint128& uint128::operator>>=(int amount) { |
| 614 | *this = *this >> amount; |
| 615 | return *this; |
| 616 | } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 617 | |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 618 | inline uint128& uint128::operator+=(uint128 other) { |
| 619 | *this = *this + other; |
| 620 | return *this; |
| 621 | } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 622 | |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 623 | inline uint128& uint128::operator-=(uint128 other) { |
| 624 | *this = *this - other; |
| 625 | return *this; |
| 626 | } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 627 | |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 628 | inline uint128& uint128::operator*=(uint128 other) { |
| 629 | *this = *this * other; |
| 630 | return *this; |
| 631 | } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 632 | |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 633 | inline uint128& uint128::operator/=(uint128 other) { |
| 634 | *this = *this / other; |
| 635 | return *this; |
| 636 | } |
| 637 | |
| 638 | inline uint128& uint128::operator%=(uint128 other) { |
| 639 | *this = *this % other; |
| 640 | return *this; |
| 641 | } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 642 | |
Abseil Team | 1b8dacc | 2017-12-05 10:58:48 -0800 | [diff] [blame] | 643 | constexpr uint64_t Uint128Low64(uint128 v) { return v.lo_; } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 644 | |
Abseil Team | 1b8dacc | 2017-12-05 10:58:48 -0800 | [diff] [blame] | 645 | constexpr uint64_t Uint128High64(uint128 v) { return v.hi_; } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 646 | |
| 647 | // Constructors from integer types. |
| 648 | |
| 649 | #if defined(ABSL_IS_LITTLE_ENDIAN) |
| 650 | |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 651 | constexpr uint128::uint128(uint64_t high, uint64_t low) : lo_{low}, hi_{high} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 652 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 653 | constexpr uint128::uint128(int v) |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 654 | : lo_{static_cast<uint64_t>(v)}, |
Abseil Team | a4c3fff | 2018-10-29 15:53:34 -0700 | [diff] [blame] | 655 | hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0} {} |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 656 | constexpr uint128::uint128(long v) // NOLINT(runtime/int) |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 657 | : lo_{static_cast<uint64_t>(v)}, |
Abseil Team | a4c3fff | 2018-10-29 15:53:34 -0700 | [diff] [blame] | 658 | hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0} {} |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 659 | constexpr uint128::uint128(long long v) // NOLINT(runtime/int) |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 660 | : lo_{static_cast<uint64_t>(v)}, |
Abseil Team | a4c3fff | 2018-10-29 15:53:34 -0700 | [diff] [blame] | 661 | hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 662 | |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 663 | constexpr uint128::uint128(unsigned int v) : lo_{v}, hi_{0} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 664 | // NOLINTNEXTLINE(runtime/int) |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 665 | constexpr uint128::uint128(unsigned long v) : lo_{v}, hi_{0} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 666 | // NOLINTNEXTLINE(runtime/int) |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 667 | constexpr uint128::uint128(unsigned long long v) : lo_{v}, hi_{0} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 668 | |
| 669 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 670 | constexpr uint128::uint128(__int128 v) |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 671 | : lo_{static_cast<uint64_t>(v & ~uint64_t{0})}, |
| 672 | hi_{static_cast<uint64_t>(static_cast<unsigned __int128>(v) >> 64)} {} |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 673 | constexpr uint128::uint128(unsigned __int128 v) |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 674 | : lo_{static_cast<uint64_t>(v & ~uint64_t{0})}, |
| 675 | hi_{static_cast<uint64_t>(v >> 64)} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 676 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 677 | |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 678 | constexpr uint128::uint128(int128 v) |
| 679 | : lo_{Int128Low64(v)}, hi_{static_cast<uint64_t>(Int128High64(v))} {} |
| 680 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 681 | #elif defined(ABSL_IS_BIG_ENDIAN) |
| 682 | |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 683 | constexpr uint128::uint128(uint64_t high, uint64_t low) : hi_{high}, lo_{low} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 684 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 685 | constexpr uint128::uint128(int v) |
Abseil Team | a4c3fff | 2018-10-29 15:53:34 -0700 | [diff] [blame] | 686 | : hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0}, |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 687 | lo_{static_cast<uint64_t>(v)} {} |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 688 | constexpr uint128::uint128(long v) // NOLINT(runtime/int) |
Abseil Team | a4c3fff | 2018-10-29 15:53:34 -0700 | [diff] [blame] | 689 | : hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0}, |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 690 | lo_{static_cast<uint64_t>(v)} {} |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 691 | constexpr uint128::uint128(long long v) // NOLINT(runtime/int) |
Abseil Team | a4c3fff | 2018-10-29 15:53:34 -0700 | [diff] [blame] | 692 | : hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0}, |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 693 | lo_{static_cast<uint64_t>(v)} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 694 | |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 695 | constexpr uint128::uint128(unsigned int v) : hi_{0}, lo_{v} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 696 | // NOLINTNEXTLINE(runtime/int) |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 697 | constexpr uint128::uint128(unsigned long v) : hi_{0}, lo_{v} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 698 | // NOLINTNEXTLINE(runtime/int) |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 699 | constexpr uint128::uint128(unsigned long long v) : hi_{0}, lo_{v} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 700 | |
| 701 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 702 | constexpr uint128::uint128(__int128 v) |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 703 | : hi_{static_cast<uint64_t>(static_cast<unsigned __int128>(v) >> 64)}, |
| 704 | lo_{static_cast<uint64_t>(v & ~uint64_t{0})} {} |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 705 | constexpr uint128::uint128(unsigned __int128 v) |
Abseil Team | 0fa86ca | 2018-02-02 10:36:07 -0800 | [diff] [blame] | 706 | : hi_{static_cast<uint64_t>(v >> 64)}, |
| 707 | lo_{static_cast<uint64_t>(v & ~uint64_t{0})} {} |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 708 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 709 | |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 710 | constexpr uint128::uint128(int128 v) |
| 711 | : hi_{static_cast<uint64_t>(Int128High64(v))}, lo_{Int128Low64(v)} {} |
| 712 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 713 | #else // byte order |
| 714 | #error "Unsupported byte order: must be little-endian or big-endian." |
| 715 | #endif // byte order |
| 716 | |
| 717 | // Conversion operators to integer types. |
| 718 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 719 | constexpr uint128::operator bool() const { return lo_ || hi_; } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 720 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 721 | constexpr uint128::operator char() const { return static_cast<char>(lo_); } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 722 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 723 | constexpr uint128::operator signed char() const { |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 724 | return static_cast<signed char>(lo_); |
| 725 | } |
| 726 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 727 | constexpr uint128::operator unsigned char() const { |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 728 | return static_cast<unsigned char>(lo_); |
| 729 | } |
| 730 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 731 | constexpr uint128::operator char16_t() const { |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 732 | return static_cast<char16_t>(lo_); |
| 733 | } |
| 734 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 735 | constexpr uint128::operator char32_t() const { |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 736 | return static_cast<char32_t>(lo_); |
| 737 | } |
| 738 | |
Abseil Team | 389ec3f | 2018-12-13 10:30:03 -0800 | [diff] [blame] | 739 | constexpr uint128::operator ABSL_INTERNAL_WCHAR_T() const { |
| 740 | return static_cast<ABSL_INTERNAL_WCHAR_T>(lo_); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | // NOLINTNEXTLINE(runtime/int) |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 744 | constexpr uint128::operator short() const { return static_cast<short>(lo_); } |
| 745 | |
| 746 | constexpr uint128::operator unsigned short() const { // NOLINT(runtime/int) |
| 747 | return static_cast<unsigned short>(lo_); // NOLINT(runtime/int) |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 748 | } |
| 749 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 750 | constexpr uint128::operator int() const { return static_cast<int>(lo_); } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 751 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 752 | constexpr uint128::operator unsigned int() const { |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 753 | return static_cast<unsigned int>(lo_); |
| 754 | } |
| 755 | |
| 756 | // NOLINTNEXTLINE(runtime/int) |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 757 | constexpr uint128::operator long() const { return static_cast<long>(lo_); } |
| 758 | |
| 759 | constexpr uint128::operator unsigned long() const { // NOLINT(runtime/int) |
| 760 | return static_cast<unsigned long>(lo_); // NOLINT(runtime/int) |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 761 | } |
| 762 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 763 | constexpr uint128::operator long long() const { // NOLINT(runtime/int) |
| 764 | return static_cast<long long>(lo_); // NOLINT(runtime/int) |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 765 | } |
| 766 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 767 | constexpr uint128::operator unsigned long long() const { // NOLINT(runtime/int) |
| 768 | return static_cast<unsigned long long>(lo_); // NOLINT(runtime/int) |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 772 | constexpr uint128::operator __int128() const { |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 773 | return (static_cast<__int128>(hi_) << 64) + lo_; |
| 774 | } |
| 775 | |
Abseil Team | 075cf62 | 2017-11-06 12:54:54 -0800 | [diff] [blame] | 776 | constexpr uint128::operator unsigned __int128() const { |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 777 | return (static_cast<unsigned __int128>(hi_) << 64) + lo_; |
| 778 | } |
| 779 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 780 | |
| 781 | // Conversion operators to floating point types. |
| 782 | |
| 783 | inline uint128::operator float() const { |
| 784 | return static_cast<float>(lo_) + std::ldexp(static_cast<float>(hi_), 64); |
| 785 | } |
| 786 | |
| 787 | inline uint128::operator double() const { |
| 788 | return static_cast<double>(lo_) + std::ldexp(static_cast<double>(hi_), 64); |
| 789 | } |
| 790 | |
| 791 | inline uint128::operator long double() const { |
| 792 | return static_cast<long double>(lo_) + |
| 793 | std::ldexp(static_cast<long double>(hi_), 64); |
| 794 | } |
| 795 | |
| 796 | // Comparison operators. |
| 797 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 798 | constexpr bool operator==(uint128 lhs, uint128 rhs) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 799 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 800 | return static_cast<unsigned __int128>(lhs) == |
| 801 | static_cast<unsigned __int128>(rhs); |
| 802 | #else |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 803 | return (Uint128Low64(lhs) == Uint128Low64(rhs) && |
| 804 | Uint128High64(lhs) == Uint128High64(rhs)); |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 805 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 806 | } |
| 807 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 808 | constexpr bool operator!=(uint128 lhs, uint128 rhs) { return !(lhs == rhs); } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 809 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 810 | constexpr bool operator<(uint128 lhs, uint128 rhs) { |
Abseil Team | d936052 | 2020-03-09 12:34:31 -0700 | [diff] [blame] | 811 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 812 | return static_cast<unsigned __int128>(lhs) < |
| 813 | static_cast<unsigned __int128>(rhs); |
| 814 | #else |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 815 | return (Uint128High64(lhs) == Uint128High64(rhs)) |
| 816 | ? (Uint128Low64(lhs) < Uint128Low64(rhs)) |
| 817 | : (Uint128High64(lhs) < Uint128High64(rhs)); |
Abseil Team | d936052 | 2020-03-09 12:34:31 -0700 | [diff] [blame] | 818 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 819 | } |
| 820 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 821 | constexpr bool operator>(uint128 lhs, uint128 rhs) { return rhs < lhs; } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 822 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 823 | constexpr bool operator<=(uint128 lhs, uint128 rhs) { return !(rhs < lhs); } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 824 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 825 | constexpr bool operator>=(uint128 lhs, uint128 rhs) { return !(lhs < rhs); } |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 826 | |
Benjamin Barenblat | 192e959 | 2024-04-18 10:36:51 -0700 | [diff] [blame] | 827 | #ifdef __cpp_impl_three_way_comparison |
| 828 | constexpr absl::strong_ordering operator<=>(uint128 lhs, uint128 rhs) { |
| 829 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 830 | if (auto lhs_128 = static_cast<unsigned __int128>(lhs), |
| 831 | rhs_128 = static_cast<unsigned __int128>(rhs); |
| 832 | lhs_128 < rhs_128) { |
| 833 | return absl::strong_ordering::less; |
| 834 | } else if (lhs_128 > rhs_128) { |
| 835 | return absl::strong_ordering::greater; |
| 836 | } else { |
| 837 | return absl::strong_ordering::equal; |
| 838 | } |
| 839 | #else |
| 840 | if (uint64_t lhs_high = Uint128High64(lhs), rhs_high = Uint128High64(rhs); |
| 841 | lhs_high < rhs_high) { |
| 842 | return absl::strong_ordering::less; |
| 843 | } else if (lhs_high > rhs_high) { |
| 844 | return absl::strong_ordering::greater; |
| 845 | } else if (uint64_t lhs_low = Uint128Low64(lhs), rhs_low = Uint128Low64(rhs); |
| 846 | lhs_low < rhs_low) { |
| 847 | return absl::strong_ordering::less; |
| 848 | } else if (lhs_low > rhs_low) { |
| 849 | return absl::strong_ordering::greater; |
| 850 | } else { |
| 851 | return absl::strong_ordering::equal; |
| 852 | } |
| 853 | #endif |
| 854 | } |
| 855 | #endif |
| 856 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 857 | // Unary operators. |
| 858 | |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 859 | constexpr inline uint128 operator+(uint128 val) { return val; } |
Abseil Team | f729726 | 2021-06-08 18:40:58 -0700 | [diff] [blame] | 860 | |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 861 | constexpr inline int128 operator+(int128 val) { return val; } |
Abseil Team | f729726 | 2021-06-08 18:40:58 -0700 | [diff] [blame] | 862 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 863 | constexpr uint128 operator-(uint128 val) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 864 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 865 | return -static_cast<unsigned __int128>(val); |
| 866 | #else |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 867 | return MakeUint128( |
| 868 | ~Uint128High64(val) + static_cast<unsigned long>(Uint128Low64(val) == 0), |
| 869 | ~Uint128Low64(val) + 1); |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 870 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 871 | } |
| 872 | |
Abseil Team | e38e1aa | 2021-04-22 04:30:35 -0700 | [diff] [blame] | 873 | constexpr inline bool operator!(uint128 val) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 874 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 875 | return !static_cast<unsigned __int128>(val); |
| 876 | #else |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 877 | return !Uint128High64(val) && !Uint128Low64(val); |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 878 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | // Logical operators. |
| 882 | |
Abseil Team | e38e1aa | 2021-04-22 04:30:35 -0700 | [diff] [blame] | 883 | constexpr inline uint128 operator~(uint128 val) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 884 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 885 | return ~static_cast<unsigned __int128>(val); |
| 886 | #else |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 887 | return MakeUint128(~Uint128High64(val), ~Uint128Low64(val)); |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 888 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 889 | } |
| 890 | |
Abseil Team | e38e1aa | 2021-04-22 04:30:35 -0700 | [diff] [blame] | 891 | constexpr inline uint128 operator|(uint128 lhs, uint128 rhs) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 892 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 893 | return static_cast<unsigned __int128>(lhs) | |
| 894 | static_cast<unsigned __int128>(rhs); |
| 895 | #else |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 896 | return MakeUint128(Uint128High64(lhs) | Uint128High64(rhs), |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 897 | Uint128Low64(lhs) | Uint128Low64(rhs)); |
| 898 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 899 | } |
| 900 | |
Abseil Team | e38e1aa | 2021-04-22 04:30:35 -0700 | [diff] [blame] | 901 | constexpr inline uint128 operator&(uint128 lhs, uint128 rhs) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 902 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 903 | return static_cast<unsigned __int128>(lhs) & |
| 904 | static_cast<unsigned __int128>(rhs); |
| 905 | #else |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 906 | return MakeUint128(Uint128High64(lhs) & Uint128High64(rhs), |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 907 | Uint128Low64(lhs) & Uint128Low64(rhs)); |
| 908 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 909 | } |
| 910 | |
Abseil Team | e38e1aa | 2021-04-22 04:30:35 -0700 | [diff] [blame] | 911 | constexpr inline uint128 operator^(uint128 lhs, uint128 rhs) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 912 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 913 | return static_cast<unsigned __int128>(lhs) ^ |
| 914 | static_cast<unsigned __int128>(rhs); |
| 915 | #else |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 916 | return MakeUint128(Uint128High64(lhs) ^ Uint128High64(rhs), |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 917 | Uint128Low64(lhs) ^ Uint128Low64(rhs)); |
| 918 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 919 | } |
| 920 | |
Abseil Team | 1b8dacc | 2017-12-05 10:58:48 -0800 | [diff] [blame] | 921 | inline uint128& uint128::operator|=(uint128 other) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 922 | *this = *this | other; |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 923 | return *this; |
| 924 | } |
| 925 | |
Abseil Team | 1b8dacc | 2017-12-05 10:58:48 -0800 | [diff] [blame] | 926 | inline uint128& uint128::operator&=(uint128 other) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 927 | *this = *this & other; |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 928 | return *this; |
| 929 | } |
| 930 | |
Abseil Team | 1b8dacc | 2017-12-05 10:58:48 -0800 | [diff] [blame] | 931 | inline uint128& uint128::operator^=(uint128 other) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 932 | *this = *this ^ other; |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 933 | return *this; |
| 934 | } |
| 935 | |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 936 | // Arithmetic operators. |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 937 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 938 | constexpr uint128 operator<<(uint128 lhs, int amount) { |
Abseil Team | d936052 | 2020-03-09 12:34:31 -0700 | [diff] [blame] | 939 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 940 | return static_cast<unsigned __int128>(lhs) << amount; |
| 941 | #else |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 942 | // uint64_t shifts of >= 64 are undefined, so we will need some |
| 943 | // special-casing. |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 944 | return amount >= 64 ? MakeUint128(Uint128Low64(lhs) << (amount - 64), 0) |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 945 | : amount == 0 ? lhs |
| 946 | : MakeUint128((Uint128High64(lhs) << amount) | |
| 947 | (Uint128Low64(lhs) >> (64 - amount)), |
| 948 | Uint128Low64(lhs) << amount); |
Abseil Team | d936052 | 2020-03-09 12:34:31 -0700 | [diff] [blame] | 949 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 950 | } |
| 951 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 952 | constexpr uint128 operator>>(uint128 lhs, int amount) { |
Abseil Team | d936052 | 2020-03-09 12:34:31 -0700 | [diff] [blame] | 953 | #ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 954 | return static_cast<unsigned __int128>(lhs) >> amount; |
| 955 | #else |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 956 | // uint64_t shifts of >= 64 are undefined, so we will need some |
| 957 | // special-casing. |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 958 | return amount >= 64 ? MakeUint128(0, Uint128High64(lhs) >> (amount - 64)) |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 959 | : amount == 0 ? lhs |
| 960 | : MakeUint128(Uint128High64(lhs) >> amount, |
| 961 | (Uint128Low64(lhs) >> amount) | |
| 962 | (Uint128High64(lhs) << (64 - amount))); |
Abseil Team | d936052 | 2020-03-09 12:34:31 -0700 | [diff] [blame] | 963 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 964 | } |
| 965 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 966 | #if !defined(ABSL_HAVE_INTRINSIC_INT128) |
| 967 | namespace int128_internal { |
| 968 | constexpr uint128 AddResult(uint128 result, uint128 lhs) { |
| 969 | // check for carry |
| 970 | return (Uint128Low64(result) < Uint128Low64(lhs)) |
| 971 | ? MakeUint128(Uint128High64(result) + 1, Uint128Low64(result)) |
| 972 | : result; |
| 973 | } |
| 974 | } // namespace int128_internal |
| 975 | #endif |
| 976 | |
| 977 | constexpr uint128 operator+(uint128 lhs, uint128 rhs) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 978 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 979 | return static_cast<unsigned __int128>(lhs) + |
| 980 | static_cast<unsigned __int128>(rhs); |
| 981 | #else |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 982 | return int128_internal::AddResult( |
| 983 | MakeUint128(Uint128High64(lhs) + Uint128High64(rhs), |
| 984 | Uint128Low64(lhs) + Uint128Low64(rhs)), |
| 985 | lhs); |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 986 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 987 | } |
| 988 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 989 | #if !defined(ABSL_HAVE_INTRINSIC_INT128) |
| 990 | namespace int128_internal { |
| 991 | constexpr uint128 SubstructResult(uint128 result, uint128 lhs, uint128 rhs) { |
| 992 | // check for carry |
| 993 | return (Uint128Low64(lhs) < Uint128Low64(rhs)) |
| 994 | ? MakeUint128(Uint128High64(result) - 1, Uint128Low64(result)) |
| 995 | : result; |
| 996 | } |
| 997 | } // namespace int128_internal |
| 998 | #endif |
| 999 | |
| 1000 | constexpr uint128 operator-(uint128 lhs, uint128 rhs) { |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 1001 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 1002 | return static_cast<unsigned __int128>(lhs) - |
| 1003 | static_cast<unsigned __int128>(rhs); |
| 1004 | #else |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 1005 | return int128_internal::SubstructResult( |
| 1006 | MakeUint128(Uint128High64(lhs) - Uint128High64(rhs), |
| 1007 | Uint128Low64(lhs) - Uint128Low64(rhs)), |
| 1008 | lhs, rhs); |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 1009 | #endif |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1010 | } |
| 1011 | |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 1012 | inline uint128 operator*(uint128 lhs, uint128 rhs) { |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1013 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 1014 | // TODO(strel) Remove once alignment issues are resolved and unsigned __int128 |
| 1015 | // can be used for uint128 storage. |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 1016 | return static_cast<unsigned __int128>(lhs) * |
| 1017 | static_cast<unsigned __int128>(rhs); |
Ben Niu | 4c015db | 2022-03-18 13:10:10 -0700 | [diff] [blame] | 1018 | #elif defined(_MSC_VER) && defined(_M_X64) && !defined(_M_ARM64EC) |
Abseil Team | 7b46e1d | 2018-11-13 13:22:00 -0800 | [diff] [blame] | 1019 | uint64_t carry; |
| 1020 | uint64_t low = _umul128(Uint128Low64(lhs), Uint128Low64(rhs), &carry); |
| 1021 | return MakeUint128(Uint128Low64(lhs) * Uint128High64(rhs) + |
| 1022 | Uint128High64(lhs) * Uint128Low64(rhs) + carry, |
| 1023 | low); |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1024 | #else // ABSL_HAVE_INTRINSIC128 |
Abseil Team | 3917120 | 2018-02-06 06:42:19 -0800 | [diff] [blame] | 1025 | uint64_t a32 = Uint128Low64(lhs) >> 32; |
| 1026 | uint64_t a00 = Uint128Low64(lhs) & 0xffffffff; |
| 1027 | uint64_t b32 = Uint128Low64(rhs) >> 32; |
| 1028 | uint64_t b00 = Uint128Low64(rhs) & 0xffffffff; |
| 1029 | uint128 result = |
| 1030 | MakeUint128(Uint128High64(lhs) * Uint128Low64(rhs) + |
| 1031 | Uint128Low64(lhs) * Uint128High64(rhs) + a32 * b32, |
| 1032 | a00 * b00); |
| 1033 | result += uint128(a32 * b00) << 32; |
| 1034 | result += uint128(a00 * b32) << 32; |
| 1035 | return result; |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1036 | #endif // ABSL_HAVE_INTRINSIC128 |
| 1037 | } |
| 1038 | |
Abseil Team | 8e088c5 | 2021-08-10 08:52:44 -0700 | [diff] [blame] | 1039 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
| 1040 | inline uint128 operator/(uint128 lhs, uint128 rhs) { |
| 1041 | return static_cast<unsigned __int128>(lhs) / |
| 1042 | static_cast<unsigned __int128>(rhs); |
| 1043 | } |
| 1044 | |
| 1045 | inline uint128 operator%(uint128 lhs, uint128 rhs) { |
| 1046 | return static_cast<unsigned __int128>(lhs) % |
| 1047 | static_cast<unsigned __int128>(rhs); |
| 1048 | } |
| 1049 | #endif |
| 1050 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1051 | // Increment/decrement operators. |
| 1052 | |
| 1053 | inline uint128 uint128::operator++(int) { |
| 1054 | uint128 tmp(*this); |
| 1055 | *this += 1; |
| 1056 | return tmp; |
| 1057 | } |
| 1058 | |
| 1059 | inline uint128 uint128::operator--(int) { |
| 1060 | uint128 tmp(*this); |
| 1061 | *this -= 1; |
| 1062 | return tmp; |
| 1063 | } |
| 1064 | |
| 1065 | inline uint128& uint128::operator++() { |
| 1066 | *this += 1; |
| 1067 | return *this; |
| 1068 | } |
| 1069 | |
| 1070 | inline uint128& uint128::operator--() { |
| 1071 | *this -= 1; |
| 1072 | return *this; |
| 1073 | } |
| 1074 | |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 1075 | constexpr int128 MakeInt128(int64_t high, uint64_t low) { |
| 1076 | return int128(high, low); |
| 1077 | } |
| 1078 | |
| 1079 | // Assignment from integer types. |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 1080 | inline int128& int128::operator=(int v) { return *this = int128(v); } |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 1081 | |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 1082 | inline int128& int128::operator=(unsigned int v) { return *this = int128(v); } |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 1083 | |
| 1084 | inline int128& int128::operator=(long v) { // NOLINT(runtime/int) |
| 1085 | return *this = int128(v); |
| 1086 | } |
| 1087 | |
| 1088 | // NOLINTNEXTLINE(runtime/int) |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 1089 | inline int128& int128::operator=(unsigned long v) { return *this = int128(v); } |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 1090 | |
| 1091 | // NOLINTNEXTLINE(runtime/int) |
Patrick Xia | 65109ec | 2023-04-27 09:23:15 -0700 | [diff] [blame] | 1092 | inline int128& int128::operator=(long long v) { return *this = int128(v); } |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 1093 | |
| 1094 | // NOLINTNEXTLINE(runtime/int) |
| 1095 | inline int128& int128::operator=(unsigned long long v) { |
| 1096 | return *this = int128(v); |
| 1097 | } |
| 1098 | |
| 1099 | // Arithmetic operators. |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 1100 | constexpr int128 operator-(int128 v); |
| 1101 | constexpr int128 operator+(int128 lhs, int128 rhs); |
| 1102 | constexpr int128 operator-(int128 lhs, int128 rhs); |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 1103 | int128 operator*(int128 lhs, int128 rhs); |
| 1104 | int128 operator/(int128 lhs, int128 rhs); |
| 1105 | int128 operator%(int128 lhs, int128 rhs); |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 1106 | constexpr int128 operator|(int128 lhs, int128 rhs); |
| 1107 | constexpr int128 operator&(int128 lhs, int128 rhs); |
| 1108 | constexpr int128 operator^(int128 lhs, int128 rhs); |
| 1109 | constexpr int128 operator<<(int128 lhs, int amount); |
| 1110 | constexpr int128 operator>>(int128 lhs, int amount); |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 1111 | |
| 1112 | inline int128& int128::operator+=(int128 other) { |
| 1113 | *this = *this + other; |
| 1114 | return *this; |
| 1115 | } |
| 1116 | |
| 1117 | inline int128& int128::operator-=(int128 other) { |
| 1118 | *this = *this - other; |
| 1119 | return *this; |
| 1120 | } |
| 1121 | |
| 1122 | inline int128& int128::operator*=(int128 other) { |
| 1123 | *this = *this * other; |
| 1124 | return *this; |
| 1125 | } |
| 1126 | |
| 1127 | inline int128& int128::operator/=(int128 other) { |
| 1128 | *this = *this / other; |
| 1129 | return *this; |
| 1130 | } |
| 1131 | |
| 1132 | inline int128& int128::operator%=(int128 other) { |
| 1133 | *this = *this % other; |
| 1134 | return *this; |
| 1135 | } |
| 1136 | |
| 1137 | inline int128& int128::operator|=(int128 other) { |
| 1138 | *this = *this | other; |
| 1139 | return *this; |
| 1140 | } |
| 1141 | |
| 1142 | inline int128& int128::operator&=(int128 other) { |
| 1143 | *this = *this & other; |
| 1144 | return *this; |
| 1145 | } |
| 1146 | |
| 1147 | inline int128& int128::operator^=(int128 other) { |
| 1148 | *this = *this ^ other; |
| 1149 | return *this; |
| 1150 | } |
| 1151 | |
| 1152 | inline int128& int128::operator<<=(int amount) { |
| 1153 | *this = *this << amount; |
| 1154 | return *this; |
| 1155 | } |
| 1156 | |
| 1157 | inline int128& int128::operator>>=(int amount) { |
| 1158 | *this = *this >> amount; |
| 1159 | return *this; |
| 1160 | } |
| 1161 | |
Abseil Team | a05366d | 2021-08-19 13:34:28 -0700 | [diff] [blame] | 1162 | // Forward declaration for comparison operators. |
| 1163 | constexpr bool operator!=(int128 lhs, int128 rhs); |
| 1164 | |
Abseil Team | 2103fd9 | 2019-11-18 11:02:26 -0800 | [diff] [blame] | 1165 | namespace int128_internal { |
| 1166 | |
| 1167 | // Casts from unsigned to signed while preserving the underlying binary |
| 1168 | // representation. |
| 1169 | constexpr int64_t BitCastToSigned(uint64_t v) { |
| 1170 | // Casting an unsigned integer to a signed integer of the same |
| 1171 | // width is implementation defined behavior if the source value would not fit |
| 1172 | // in the destination type. We step around it with a roundtrip bitwise not |
| 1173 | // operation to make sure this function remains constexpr. Clang, GCC, and |
| 1174 | // MSVC optimize this to a no-op on x86-64. |
| 1175 | return v & (uint64_t{1} << 63) ? ~static_cast<int64_t>(~v) |
| 1176 | : static_cast<int64_t>(v); |
| 1177 | } |
| 1178 | |
| 1179 | } // namespace int128_internal |
| 1180 | |
Abseil Team | ee2e3f6 | 2017-11-07 04:53:20 -0800 | [diff] [blame] | 1181 | #if defined(ABSL_HAVE_INTRINSIC_INT128) |
Abseil Team | fa8c751 | 2019-11-13 08:54:32 -0800 | [diff] [blame] | 1182 | #include "absl/numeric/int128_have_intrinsic.inc" // IWYU pragma: export |
Abseil Team | ee2e3f6 | 2017-11-07 04:53:20 -0800 | [diff] [blame] | 1183 | #else // ABSL_HAVE_INTRINSIC_INT128 |
Abseil Team | fa8c751 | 2019-11-13 08:54:32 -0800 | [diff] [blame] | 1184 | #include "absl/numeric/int128_no_intrinsic.inc" // IWYU pragma: export |
Abseil Team | ee2e3f6 | 2017-11-07 04:53:20 -0800 | [diff] [blame] | 1185 | #endif // ABSL_HAVE_INTRINSIC_INT128 |
| 1186 | |
Abseil Team | 12bc53e | 2019-12-12 10:36:03 -0800 | [diff] [blame] | 1187 | ABSL_NAMESPACE_END |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1188 | } // namespace absl |
| 1189 | |
Abseil Team | 389ec3f | 2018-12-13 10:30:03 -0800 | [diff] [blame] | 1190 | #undef ABSL_INTERNAL_WCHAR_T |
| 1191 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1192 | #endif // ABSL_NUMERIC_INT128_H_ |