Coverage Report

Created: 2025-10-09 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/numeric/int128_have_intrinsic.inc
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
// This file contains :int128 implementation details that depend on internal
17
// representation when ABSL_HAVE_INTRINSIC_INT128 is defined. This file is
18
// included by int128.h and relies on ABSL_INTERNAL_WCHAR_T being defined.
19
20
namespace int128_internal {
21
22
// Casts from unsigned to signed while preserving the underlying binary
23
// representation.
24
0
constexpr __int128 BitCastToSigned(unsigned __int128 v) {
25
  // Casting an unsigned integer to a signed integer of the same
26
  // width is implementation defined behavior if the source value would not fit
27
  // in the destination type. We step around it with a roundtrip bitwise not
28
  // operation to make sure this function remains constexpr. Clang and GCC
29
  // optimize this to a no-op on x86-64.
30
0
  return v & (static_cast<unsigned __int128>(1) << 127)
31
0
             ? ~static_cast<__int128>(~v)
32
0
             : static_cast<__int128>(v);
33
0
}
34
35
}  // namespace int128_internal
36
37
0
inline int128& int128::operator=(__int128 v) {
38
0
  v_ = v;
39
0
  return *this;
40
0
}
41
42
0
constexpr uint64_t Int128Low64(int128 v) {
43
0
  return static_cast<uint64_t>(v.v_ & ~uint64_t{0});
44
0
}
45
46
0
constexpr int64_t Int128High64(int128 v) {
47
  // Initially cast to unsigned to prevent a right shift on a negative value.
48
0
  return int128_internal::BitCastToSigned(
49
0
      static_cast<uint64_t>(static_cast<unsigned __int128>(v.v_) >> 64));
50
0
}
51
52
constexpr int128::int128(int64_t high, uint64_t low)
53
    // Initially cast to unsigned to prevent a left shift that overflows.
54
0
    : v_(int128_internal::BitCastToSigned(static_cast<unsigned __int128>(high)
55
0
                                           << 64) |
56
0
         low) {}
57
58
59
0
constexpr int128::int128(int v) : v_{v} {}
60
61
constexpr int128::int128(long v) : v_{v} {}       // NOLINT(runtime/int)
62
63
constexpr int128::int128(long long v) : v_{v} {}  // NOLINT(runtime/int)
64
65
0
constexpr int128::int128(__int128 v) : v_{v} {}
66
67
constexpr int128::int128(unsigned int v) : v_{v} {}
68
69
constexpr int128::int128(unsigned long v) : v_{v} {}  // NOLINT(runtime/int)
70
71
// NOLINTNEXTLINE(runtime/int)
72
constexpr int128::int128(unsigned long long v) : v_{v} {}
73
74
constexpr int128::int128(unsigned __int128 v) : v_{static_cast<__int128>(v)} {}
75
76
constexpr int128::int128(float v) : v_{static_cast<__int128>(v)} {}
77
78
constexpr int128::int128(double v) : v_{static_cast<__int128>(v)} {}
79
80
constexpr int128::int128(long double v) : v_{static_cast<__int128>(v)} {}
81
82
constexpr int128::int128(uint128 v) : v_{static_cast<__int128>(v)} {}
83
84
0
constexpr int128::operator bool() const { return static_cast<bool>(v_); }
85
86
0
constexpr int128::operator char() const { return static_cast<char>(v_); }
87
88
0
constexpr int128::operator signed char() const {
89
0
  return static_cast<signed char>(v_);
90
0
}
91
92
0
constexpr int128::operator unsigned char() const {
93
0
  return static_cast<unsigned char>(v_);
94
0
}
95
96
0
constexpr int128::operator char16_t() const {
97
0
  return static_cast<char16_t>(v_);
98
0
}
99
100
0
constexpr int128::operator char32_t() const {
101
0
  return static_cast<char32_t>(v_);
102
0
}
103
104
0
constexpr int128::operator ABSL_INTERNAL_WCHAR_T() const {
105
0
  return static_cast<ABSL_INTERNAL_WCHAR_T>(v_);
106
0
}
107
108
0
constexpr int128::operator short() const {  // NOLINT(runtime/int)
109
0
  return static_cast<short>(v_);            // NOLINT(runtime/int)
110
0
}
111
112
0
constexpr int128::operator unsigned short() const {  // NOLINT(runtime/int)
113
0
  return static_cast<unsigned short>(v_);            // NOLINT(runtime/int)
114
0
}
115
116
0
constexpr int128::operator int() const { return static_cast<int>(v_); }
117
118
0
constexpr int128::operator unsigned int() const {
119
0
  return static_cast<unsigned int>(v_);
120
0
}
121
122
0
constexpr int128::operator long() const {  // NOLINT(runtime/int)
123
0
  return static_cast<long>(v_);            // NOLINT(runtime/int)
124
0
}
125
126
0
constexpr int128::operator unsigned long() const {  // NOLINT(runtime/int)
127
0
  return static_cast<unsigned long>(v_);            // NOLINT(runtime/int)
128
0
}
129
130
0
constexpr int128::operator long long() const {  // NOLINT(runtime/int)
131
0
  return static_cast<long long>(v_);            // NOLINT(runtime/int)
132
0
}
133
134
0
constexpr int128::operator unsigned long long() const {  // NOLINT(runtime/int)
135
0
  return static_cast<unsigned long long>(v_);            // NOLINT(runtime/int)
136
0
}
137
138
0
constexpr int128::operator __int128() const { return v_; }
139
140
0
constexpr int128::operator unsigned __int128() const {
141
0
  return static_cast<unsigned __int128>(v_);
142
0
}
143
144
// Clang on PowerPC sometimes produces incorrect __int128 to floating point
145
// conversions. In that case, we do the conversion with a similar implementation
146
// to the conversion operators in int128_no_intrinsic.inc.
147
#if defined(__clang__) && !defined(__ppc64__)
148
0
constexpr int128::operator float() const { return static_cast<float>(v_); }
149
150
0
constexpr int128::operator double() const { return static_cast<double>(v_); }
151
152
0
constexpr int128::operator long double() const {
153
0
  return static_cast<long double>(v_);
154
0
}
155
156
#else   // Clang on PowerPC
157
158
constexpr int128::operator float() const {
159
  // We must convert the absolute value and then negate as needed, because
160
  // floating point types are typically sign-magnitude. Otherwise, the
161
  // difference between the high and low 64 bits when interpreted as two's
162
  // complement overwhelms the precision of the mantissa.
163
  //
164
  // Also check to make sure we don't negate Int128Min()
165
  constexpr float pow_2_64 = 18446744073709551616.0f;
166
  return v_ < 0 && *this != Int128Min()
167
             ? -static_cast<float>(-*this)
168
             : static_cast<float>(Int128Low64(*this)) +
169
                   static_cast<float>(Int128High64(*this)) * pow_2_64;
170
}
171
172
constexpr int128::operator double() const {
173
  // See comment in int128::operator float() above.
174
  constexpr double pow_2_64 = 18446744073709551616.0;
175
  return v_ < 0 && *this != Int128Min()
176
             ? -static_cast<double>(-*this)
177
             : static_cast<double>(Int128Low64(*this)) +
178
                   static_cast<double>(Int128High64(*this)) * pow_2_64;
179
}
180
181
constexpr int128::operator long double() const {
182
  // See comment in int128::operator float() above.
183
  constexpr long double pow_2_64 = 18446744073709551616.0L;
184
  return v_ < 0 && *this != Int128Min()
185
             ? -static_cast<long double>(-*this)
186
             : static_cast<long double>(Int128Low64(*this)) +
187
                   static_cast<long double>(Int128High64(*this)) * pow_2_64;
188
}
189
#endif  // Clang on PowerPC
190
191
// Comparison operators.
192
193
0
constexpr bool operator==(int128 lhs, int128 rhs) {
194
0
  return static_cast<__int128>(lhs) == static_cast<__int128>(rhs);
195
0
}
196
197
0
constexpr bool operator!=(int128 lhs, int128 rhs) {
198
0
  return static_cast<__int128>(lhs) != static_cast<__int128>(rhs);
199
0
}
200
201
0
constexpr bool operator<(int128 lhs, int128 rhs) {
202
0
  return static_cast<__int128>(lhs) < static_cast<__int128>(rhs);
203
0
}
204
205
0
constexpr bool operator>(int128 lhs, int128 rhs) {
206
0
  return static_cast<__int128>(lhs) > static_cast<__int128>(rhs);
207
0
}
208
209
0
constexpr bool operator<=(int128 lhs, int128 rhs) {
210
0
  return static_cast<__int128>(lhs) <= static_cast<__int128>(rhs);
211
0
}
212
213
0
constexpr bool operator>=(int128 lhs, int128 rhs) {
214
0
  return static_cast<__int128>(lhs) >= static_cast<__int128>(rhs);
215
0
}
216
217
#ifdef __cpp_impl_three_way_comparison
218
constexpr absl::strong_ordering operator<=>(int128 lhs, int128 rhs) {
219
  if (auto lhs_128 = static_cast<__int128>(lhs),
220
      rhs_128 = static_cast<__int128>(rhs);
221
      lhs_128 < rhs_128) {
222
    return absl::strong_ordering::less;
223
  } else if (lhs_128 > rhs_128) {
224
    return absl::strong_ordering::greater;
225
  } else {
226
    return absl::strong_ordering::equal;
227
  }
228
}
229
#endif
230
231
// Unary operators.
232
233
0
constexpr int128 operator-(int128 v) { return -static_cast<__int128>(v); }
234
235
0
constexpr bool operator!(int128 v) { return !static_cast<__int128>(v); }
236
237
0
constexpr int128 operator~(int128 val) { return ~static_cast<__int128>(val); }
238
239
// Arithmetic operators.
240
241
0
constexpr int128 operator+(int128 lhs, int128 rhs) {
242
0
  return static_cast<__int128>(lhs) + static_cast<__int128>(rhs);
243
0
}
244
245
0
constexpr int128 operator-(int128 lhs, int128 rhs) {
246
0
  return static_cast<__int128>(lhs) - static_cast<__int128>(rhs);
247
0
}
248
249
#if defined(ABSL_HAVE_INTRINSIC_INT128)
250
0
constexpr int128 operator*(int128 lhs, int128 rhs) {
251
0
  return static_cast<__int128>(lhs) * static_cast<__int128>(rhs);
252
0
}
253
254
0
constexpr int128 operator/(int128 lhs, int128 rhs) {
255
0
  return static_cast<__int128>(lhs) / static_cast<__int128>(rhs);
256
0
}
257
258
0
constexpr int128 operator%(int128 lhs, int128 rhs) {
259
0
  return static_cast<__int128>(lhs) % static_cast<__int128>(rhs);
260
0
}
261
#endif  // ABSL_HAVE_INTRINSIC_INT128
262
263
0
inline int128 int128::operator++(int) {
264
0
  int128 tmp(*this);
265
0
  ++v_;
266
0
  return tmp;
267
0
}
268
269
0
inline int128 int128::operator--(int) {
270
0
  int128 tmp(*this);
271
0
  --v_;
272
0
  return tmp;
273
0
}
274
275
0
inline int128& int128::operator++() {
276
0
  ++v_;
277
0
  return *this;
278
0
}
279
280
0
inline int128& int128::operator--() {
281
0
  --v_;
282
0
  return *this;
283
0
}
284
285
0
constexpr int128 operator|(int128 lhs, int128 rhs) {
286
0
  return static_cast<__int128>(lhs) | static_cast<__int128>(rhs);
287
0
}
288
289
0
constexpr int128 operator&(int128 lhs, int128 rhs) {
290
0
  return static_cast<__int128>(lhs) & static_cast<__int128>(rhs);
291
0
}
292
293
0
constexpr int128 operator^(int128 lhs, int128 rhs) {
294
0
  return static_cast<__int128>(lhs) ^ static_cast<__int128>(rhs);
295
0
}
296
297
0
constexpr int128 operator<<(int128 lhs, int amount) {
298
0
  return static_cast<__int128>(lhs) << amount;
299
0
}
300
301
0
constexpr int128 operator>>(int128 lhs, int amount) {
302
0
  return static_cast<__int128>(lhs) >> amount;
303
0
}