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