Coverage Report

Created: 2025-10-10 07:13

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