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