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 *not* defined. This file
18// is included by int128.h and relies on ABSL_INTERNAL_WCHAR_T being defined.
19
20constexpr uint64_t Int128Low64(int128 v) { return v.lo_; }
21
22constexpr int64_t Int128High64(int128 v) { return v.hi_; }
23
24#if defined(ABSL_IS_LITTLE_ENDIAN)
25
26constexpr int128::int128(int64_t high, uint64_t low) :
27    lo_(low), hi_(high) {}
28
29constexpr int128::int128(int v)
30    : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
31constexpr int128::int128(long v)  // NOLINT(runtime/int)
32    : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
33constexpr int128::int128(long long v)  // NOLINT(runtime/int)
34    : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
35
36constexpr int128::int128(unsigned int v) : lo_{v}, hi_{0} {}
37// NOLINTNEXTLINE(runtime/int)
38constexpr int128::int128(unsigned long v) : lo_{v}, hi_{0} {}
39// NOLINTNEXTLINE(runtime/int)
40constexpr int128::int128(unsigned long long v) : lo_{v}, hi_{0} {}
41
42constexpr int128::int128(uint128 v)
43    : lo_{Uint128Low64(v)}, hi_{static_cast<int64_t>(Uint128High64(v))} {}
44
45#elif defined(ABSL_IS_BIG_ENDIAN)
46
47constexpr int128::int128(int64_t high, uint64_t low) :
48    hi_{high}, lo_{low} {}
49
50constexpr int128::int128(int v)
51    : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
52constexpr int128::int128(long v)  // NOLINT(runtime/int)
53    : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
54constexpr int128::int128(long long v)  // NOLINT(runtime/int)
55    : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
56
57constexpr int128::int128(unsigned int v) : hi_{0}, lo_{v} {}
58// NOLINTNEXTLINE(runtime/int)
59constexpr int128::int128(unsigned long v) : hi_{0}, lo_{v} {}
60// NOLINTNEXTLINE(runtime/int)
61constexpr int128::int128(unsigned long long v) : hi_{0}, lo_{v} {}
62
63constexpr int128::int128(uint128 v)
64    : hi_{static_cast<int64_t>(Uint128High64(v))}, lo_{Uint128Low64(v)} {}
65
66#else  // byte order
67#error "Unsupported byte order: must be little-endian or big-endian."
68#endif  // byte order
69
70constexpr int128::operator bool() const { return lo_ || hi_; }
71
72constexpr int128::operator char() const {
73  // NOLINTNEXTLINE(runtime/int)
74  return static_cast<char>(static_cast<long long>(*this));
75}
76
77constexpr int128::operator signed char() const {
78  // NOLINTNEXTLINE(runtime/int)
79  return static_cast<signed char>(static_cast<long long>(*this));
80}
81
82constexpr int128::operator unsigned char() const {
83  return static_cast<unsigned char>(lo_);
84}
85
86constexpr int128::operator char16_t() const {
87  return static_cast<char16_t>(lo_);
88}
89
90constexpr int128::operator char32_t() const {
91  return static_cast<char32_t>(lo_);
92}
93
94constexpr int128::operator ABSL_INTERNAL_WCHAR_T() const {
95  // NOLINTNEXTLINE(runtime/int)
96  return static_cast<ABSL_INTERNAL_WCHAR_T>(static_cast<long long>(*this));
97}
98
99constexpr int128::operator short() const {  // NOLINT(runtime/int)
100  // NOLINTNEXTLINE(runtime/int)
101  return static_cast<short>(static_cast<long long>(*this));
102}
103
104constexpr int128::operator unsigned short() const {  // NOLINT(runtime/int)
105  return static_cast<unsigned short>(lo_);           // NOLINT(runtime/int)
106}
107
108constexpr int128::operator int() const {
109  // NOLINTNEXTLINE(runtime/int)
110  return static_cast<int>(static_cast<long long>(*this));
111}
112
113constexpr int128::operator unsigned int() const {
114  return static_cast<unsigned int>(lo_);
115}
116
117constexpr int128::operator long() const {  // NOLINT(runtime/int)
118  // NOLINTNEXTLINE(runtime/int)
119  return static_cast<long>(static_cast<long long>(*this));
120}
121
122constexpr int128::operator unsigned long() const {  // NOLINT(runtime/int)
123  return static_cast<unsigned long>(lo_);           // NOLINT(runtime/int)
124}
125
126constexpr int128::operator long long() const {  // NOLINT(runtime/int)
127  // We don't bother checking the value of hi_. If *this < 0, lo_'s high bit
128  // must be set in order for the value to fit into a long long. Conversely, if
129  // lo_'s high bit is set, *this must be < 0 for the value to fit.
130  return int128_internal::BitCastToSigned(lo_);
131}
132
133constexpr int128::operator unsigned long long() const {  // NOLINT(runtime/int)
134  return static_cast<unsigned long long>(lo_);           // NOLINT(runtime/int)
135}
136
137// Forward declaration for conversion operators to floating point types.
138int128 operator-(int128 v);
139bool operator!=(int128 lhs, int128 rhs);
140
141inline int128::operator float() const {
142  // We must convert the absolute value and then negate as needed, because
143  // floating point types are typically sign-magnitude. Otherwise, the
144  // difference between the high and low 64 bits when interpreted as two's
145  // complement overwhelms the precision of the mantissa.
146  //
147  // Also check to make sure we don't negate Int128Min()
148  return hi_ < 0 && *this != Int128Min()
149             ? -static_cast<float>(-*this)
150             : static_cast<float>(lo_) +
151                   std::ldexp(static_cast<float>(hi_), 64);
152}
153
154inline int128::operator double() const {
155  // See comment in int128::operator float() above.
156  return hi_ < 0 && *this != Int128Min()
157             ? -static_cast<double>(-*this)
158             : static_cast<double>(lo_) +
159                   std::ldexp(static_cast<double>(hi_), 64);
160}
161
162inline int128::operator long double() const {
163  // See comment in int128::operator float() above.
164  return hi_ < 0 && *this != Int128Min()
165             ? -static_cast<long double>(-*this)
166             : static_cast<long double>(lo_) +
167                   std::ldexp(static_cast<long double>(hi_), 64);
168}
169
170// Comparison operators.
171
172inline bool operator==(int128 lhs, int128 rhs) {
173  return (Int128Low64(lhs) == Int128Low64(rhs) &&
174          Int128High64(lhs) == Int128High64(rhs));
175}
176
177inline bool operator!=(int128 lhs, int128 rhs) {
178  return !(lhs == rhs);
179}
180
181inline bool operator<(int128 lhs, int128 rhs) {
182  return (Int128High64(lhs) == Int128High64(rhs))
183             ? (Int128Low64(lhs) < Int128Low64(rhs))
184             : (Int128High64(lhs) < Int128High64(rhs));
185}
186
187inline bool operator>(int128 lhs, int128 rhs) {
188  return (Int128High64(lhs) == Int128High64(rhs))
189             ? (Int128Low64(lhs) > Int128Low64(rhs))
190             : (Int128High64(lhs) > Int128High64(rhs));
191}
192
193inline bool operator<=(int128 lhs, int128 rhs) {
194  return !(lhs > rhs);
195}
196
197inline bool operator>=(int128 lhs, int128 rhs) {
198  return !(lhs < rhs);
199}
200
201// Unary operators.
202
203inline int128 operator-(int128 v) {
204  int64_t hi = ~Int128High64(v);
205  uint64_t lo = ~Int128Low64(v) + 1;
206  if (lo == 0) ++hi;  // carry
207  return MakeInt128(hi, lo);
208}
209
210inline bool operator!(int128 v) {
211  return !Int128Low64(v) && !Int128High64(v);
212}
213
214inline int128 operator~(int128 val) {
215  return MakeInt128(~Int128High64(val), ~Int128Low64(val));
216}
217
218// Arithmetic operators.
219
220inline int128 operator+(int128 lhs, int128 rhs) {
221  int128 result = MakeInt128(Int128High64(lhs) + Int128High64(rhs),
222                             Int128Low64(lhs) + Int128Low64(rhs));
223  if (Int128Low64(result) < Int128Low64(lhs)) {  // check for carry
224    return MakeInt128(Int128High64(result) + 1, Int128Low64(result));
225  }
226  return result;
227}
228
229inline int128 operator-(int128 lhs, int128 rhs) {
230  int128 result = MakeInt128(Int128High64(lhs) - Int128High64(rhs),
231                             Int128Low64(lhs) - Int128Low64(rhs));
232  if (Int128Low64(lhs) < Int128Low64(rhs)) {  // check for carry
233    return MakeInt128(Int128High64(result) - 1, Int128Low64(result));
234  }
235  return result;
236}
237
238inline int128 operator*(int128 lhs, int128 rhs) {
239  uint128 result = uint128(lhs) * rhs;
240  return MakeInt128(int128_internal::BitCastToSigned(Uint128High64(result)),
241                    Uint128Low64(result));
242}
243
244inline int128 int128::operator++(int) {
245  int128 tmp(*this);
246  *this += 1;
247  return tmp;
248}
249
250inline int128 int128::operator--(int) {
251  int128 tmp(*this);
252  *this -= 1;
253  return tmp;
254}
255
256inline int128& int128::operator++() {
257  *this += 1;
258  return *this;
259}
260
261inline int128& int128::operator--() {
262  *this -= 1;
263  return *this;
264}
265
266inline int128 operator|(int128 lhs, int128 rhs) {
267  return MakeInt128(Int128High64(lhs) | Int128High64(rhs),
268                    Int128Low64(lhs) | Int128Low64(rhs));
269}
270
271inline int128 operator&(int128 lhs, int128 rhs) {
272  return MakeInt128(Int128High64(lhs) & Int128High64(rhs),
273                    Int128Low64(lhs) & Int128Low64(rhs));
274}
275
276inline int128 operator^(int128 lhs, int128 rhs) {
277  return MakeInt128(Int128High64(lhs) ^ Int128High64(rhs),
278                    Int128Low64(lhs) ^ Int128Low64(rhs));
279}
280
281inline int128 operator<<(int128 lhs, int amount) {
282  // uint64_t shifts of >= 64 are undefined, so we need some special-casing.
283  if (amount < 64) {
284    if (amount != 0) {
285      return MakeInt128(
286          (Int128High64(lhs) << amount) |
287              static_cast<int64_t>(Int128Low64(lhs) >> (64 - amount)),
288          Int128Low64(lhs) << amount);
289    }
290    return lhs;
291  }
292  return MakeInt128(static_cast<int64_t>(Int128Low64(lhs) << (amount - 64)), 0);
293}
294
295inline int128 operator>>(int128 lhs, int amount) {
296  // uint64_t shifts of >= 64 are undefined, so we need some special-casing.
297  if (amount < 64) {
298    if (amount != 0) {
299      return MakeInt128(
300          Int128High64(lhs) >> amount,
301          (Int128Low64(lhs) >> amount) |
302              (static_cast<uint64_t>(Int128High64(lhs)) << (64 - amount)));
303    }
304    return lhs;
305  }
306  return MakeInt128(0,
307                    static_cast<uint64_t>(Int128High64(lhs) >> (amount - 64)));
308}
309