/src/abseil-cpp/absl/numeric/int128.cc
Line | Count | Source |
1 | | // Copyright 2017 The Abseil Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "absl/numeric/int128.h" |
16 | | |
17 | | #include <stddef.h> |
18 | | |
19 | | #include <cassert> |
20 | | #include <cstdint> |
21 | | #include <iomanip> |
22 | | #include <ios> |
23 | | #include <limits> |
24 | | #include <ostream> // NOLINT(readability/streams) |
25 | | #include <sstream> |
26 | | #include <string> |
27 | | #include <type_traits> |
28 | | |
29 | | #include "absl/base/attributes.h" |
30 | | #include "absl/base/config.h" |
31 | | #include "absl/base/optimization.h" |
32 | | #include "absl/numeric/bits.h" |
33 | | |
34 | | namespace absl { |
35 | | ABSL_NAMESPACE_BEGIN |
36 | | |
37 | | namespace { |
38 | | |
39 | | // Returns the 0-based position of the last set bit (i.e., most significant bit) |
40 | | // in the given uint128. The argument is not 0. |
41 | | // |
42 | | // For example: |
43 | | // Given: 5 (decimal) == 101 (binary) |
44 | | // Returns: 2 |
45 | 0 | inline ABSL_ATTRIBUTE_ALWAYS_INLINE int Fls128(uint128 n) { |
46 | 0 | if (uint64_t hi = Uint128High64(n)) { |
47 | 0 | ABSL_ASSUME(hi != 0); |
48 | 0 | return 127 - countl_zero(hi); |
49 | 0 | } |
50 | 0 | const uint64_t low = Uint128Low64(n); |
51 | 0 | ABSL_ASSUME(low != 0); |
52 | 0 | return 63 - countl_zero(low); |
53 | 0 | } |
54 | | |
55 | | // Long division/modulo for uint128 implemented using the shift-subtract |
56 | | // division algorithm adapted from: |
57 | | // https://stackoverflow.com/questions/5386377/division-without-using |
58 | | inline void DivModImpl(uint128 dividend, uint128 divisor, uint128* quotient_ret, |
59 | 0 | uint128* remainder_ret) { |
60 | 0 | assert(divisor != 0); |
61 | | |
62 | 0 | if (divisor > dividend) { |
63 | 0 | *quotient_ret = 0; |
64 | 0 | *remainder_ret = dividend; |
65 | 0 | return; |
66 | 0 | } |
67 | | |
68 | 0 | if (divisor == dividend) { |
69 | 0 | *quotient_ret = 1; |
70 | 0 | *remainder_ret = 0; |
71 | 0 | return; |
72 | 0 | } |
73 | | |
74 | 0 | uint128 denominator = divisor; |
75 | 0 | uint128 quotient = 0; |
76 | | |
77 | | // Left aligns the MSB of the denominator and the dividend. |
78 | 0 | const int shift = Fls128(dividend) - Fls128(denominator); |
79 | 0 | denominator <<= shift; |
80 | | |
81 | | // Uses shift-subtract algorithm to divide dividend by denominator. The |
82 | | // remainder will be left in dividend. |
83 | 0 | for (int i = 0; i <= shift; ++i) { |
84 | 0 | quotient <<= 1; |
85 | 0 | if (dividend >= denominator) { |
86 | 0 | dividend -= denominator; |
87 | 0 | quotient |= 1; |
88 | 0 | } |
89 | 0 | denominator >>= 1; |
90 | 0 | } |
91 | |
|
92 | 0 | *quotient_ret = quotient; |
93 | 0 | *remainder_ret = dividend; |
94 | 0 | } |
95 | | |
96 | | template <typename T> |
97 | 0 | uint128 MakeUint128FromFloat(T v) { |
98 | 0 | static_assert(std::is_floating_point_v<T>); |
99 | | |
100 | | // Rounding behavior is towards zero, same as for built-in types. |
101 | | |
102 | | // Undefined behavior if v is NaN or cannot fit into uint128. |
103 | 0 | assert(std::isfinite(v) && v > -1 && |
104 | 0 | (std::numeric_limits<T>::max_exponent <= 128 || |
105 | 0 | v < std::ldexp(static_cast<T>(1), 128))); |
106 | | |
107 | 0 | if (v >= std::ldexp(static_cast<T>(1), 64)) { |
108 | 0 | uint64_t hi = static_cast<uint64_t>(std::ldexp(v, -64)); |
109 | 0 | uint64_t lo = static_cast<uint64_t>(v - std::ldexp(static_cast<T>(hi), 64)); |
110 | 0 | return MakeUint128(hi, lo); |
111 | 0 | } |
112 | | |
113 | 0 | return MakeUint128(0, static_cast<uint64_t>(v)); |
114 | 0 | } Unexecuted instantiation: int128.cc:absl::uint128 absl::(anonymous namespace)::MakeUint128FromFloat<float>(float) Unexecuted instantiation: int128.cc:absl::uint128 absl::(anonymous namespace)::MakeUint128FromFloat<double>(double) Unexecuted instantiation: int128.cc:absl::uint128 absl::(anonymous namespace)::MakeUint128FromFloat<long double>(long double) |
115 | | |
116 | | #if defined(__clang__) && (__clang_major__ < 9) && !defined(__SSE3__) |
117 | | // Workaround for clang bug: https://bugs.llvm.org/show_bug.cgi?id=38289 |
118 | | // Casting from long double to uint64_t is miscompiled and drops bits. |
119 | | // It is more work, so only use when we need the workaround. |
120 | | uint128 MakeUint128FromFloat(long double v) { |
121 | | // Go 50 bits at a time, that fits in a double |
122 | | static_assert(std::numeric_limits<double>::digits >= 50); |
123 | | static_assert(std::numeric_limits<long double>::digits <= 150); |
124 | | // Undefined behavior if v is not finite or cannot fit into uint128. |
125 | | assert(std::isfinite(v) && v > -1 && v < std::ldexp(1.0L, 128)); |
126 | | |
127 | | v = std::ldexp(v, -100); |
128 | | uint64_t w0 = static_cast<uint64_t>(static_cast<double>(std::trunc(v))); |
129 | | v = std::ldexp(v - static_cast<double>(w0), 50); |
130 | | uint64_t w1 = static_cast<uint64_t>(static_cast<double>(std::trunc(v))); |
131 | | v = std::ldexp(v - static_cast<double>(w1), 50); |
132 | | uint64_t w2 = static_cast<uint64_t>(static_cast<double>(std::trunc(v))); |
133 | | return (static_cast<uint128>(w0) << 100) | (static_cast<uint128>(w1) << 50) | |
134 | | static_cast<uint128>(w2); |
135 | | } |
136 | | #endif // __clang__ && (__clang_major__ < 9) && !__SSE3__ |
137 | | } // namespace |
138 | | |
139 | 0 | uint128::uint128(float v) : uint128(MakeUint128FromFloat(v)) {} |
140 | 0 | uint128::uint128(double v) : uint128(MakeUint128FromFloat(v)) {} |
141 | 0 | uint128::uint128(long double v) : uint128(MakeUint128FromFloat(v)) {} |
142 | | |
143 | | #if !defined(ABSL_HAVE_INTRINSIC_INT128) |
144 | | uint128 operator/(uint128 lhs, uint128 rhs) { |
145 | | uint128 quotient = 0; |
146 | | uint128 remainder = 0; |
147 | | DivModImpl(lhs, rhs, "ient, &remainder); |
148 | | return quotient; |
149 | | } |
150 | | |
151 | | uint128 operator%(uint128 lhs, uint128 rhs) { |
152 | | uint128 quotient = 0; |
153 | | uint128 remainder = 0; |
154 | | DivModImpl(lhs, rhs, "ient, &remainder); |
155 | | return remainder; |
156 | | } |
157 | | #endif // !defined(ABSL_HAVE_INTRINSIC_INT128) |
158 | | |
159 | | namespace { |
160 | | |
161 | 0 | std::string Uint128ToFormattedString(uint128 v, std::ios_base::fmtflags flags) { |
162 | | // Select a divisor which is the largest power of the base < 2^64. |
163 | 0 | uint128 div; |
164 | 0 | int div_base_log; |
165 | 0 | switch (flags & std::ios::basefield) { |
166 | 0 | case std::ios::hex: |
167 | 0 | div = 0x1000000000000000; // 16^15 |
168 | 0 | div_base_log = 15; |
169 | 0 | break; |
170 | 0 | case std::ios::oct: |
171 | 0 | div = 01000000000000000000000; // 8^21 |
172 | 0 | div_base_log = 21; |
173 | 0 | break; |
174 | 0 | default: // std::ios::dec |
175 | 0 | div = 10000000000000000000u; // 10^19 |
176 | 0 | div_base_log = 19; |
177 | 0 | break; |
178 | 0 | } |
179 | | |
180 | | // Now piece together the uint128 representation from three chunks of the |
181 | | // original value, each less than "div" and therefore representable as a |
182 | | // uint64_t. |
183 | 0 | std::ostringstream os; |
184 | 0 | std::ios_base::fmtflags copy_mask = |
185 | 0 | std::ios::basefield | std::ios::showbase | std::ios::uppercase; |
186 | 0 | os.setf(flags & copy_mask, copy_mask); |
187 | 0 | uint128 high = v; |
188 | 0 | uint128 low; |
189 | 0 | DivModImpl(high, div, &high, &low); |
190 | 0 | uint128 mid; |
191 | 0 | DivModImpl(high, div, &high, &mid); |
192 | 0 | if (Uint128Low64(high) != 0) { |
193 | 0 | os << Uint128Low64(high); |
194 | 0 | os << std::noshowbase << std::setfill('0') << std::setw(div_base_log); |
195 | 0 | os << Uint128Low64(mid); |
196 | 0 | os << std::setw(div_base_log); |
197 | 0 | } else if (Uint128Low64(mid) != 0) { |
198 | 0 | os << Uint128Low64(mid); |
199 | 0 | os << std::noshowbase << std::setfill('0') << std::setw(div_base_log); |
200 | 0 | } |
201 | 0 | os << Uint128Low64(low); |
202 | 0 | return os.str(); |
203 | 0 | } |
204 | | |
205 | | } // namespace |
206 | | |
207 | 0 | std::string uint128::ToString() const { |
208 | 0 | return Uint128ToFormattedString(*this, std::ios_base::dec); |
209 | 0 | } |
210 | | |
211 | 0 | std::ostream& operator<<(std::ostream& os, uint128 v) { |
212 | 0 | std::ios_base::fmtflags flags = os.flags(); |
213 | 0 | std::string rep = Uint128ToFormattedString(v, flags); |
214 | | |
215 | | // Add the requisite padding. |
216 | 0 | std::streamsize width = os.width(0); |
217 | 0 | if (static_cast<size_t>(width) > rep.size()) { |
218 | 0 | const size_t count = static_cast<size_t>(width) - rep.size(); |
219 | 0 | std::ios::fmtflags adjustfield = flags & std::ios::adjustfield; |
220 | 0 | if (adjustfield == std::ios::left) { |
221 | 0 | rep.append(count, os.fill()); |
222 | 0 | } else if (adjustfield == std::ios::internal && |
223 | 0 | (flags & std::ios::showbase) && |
224 | 0 | (flags & std::ios::basefield) == std::ios::hex && v != 0) { |
225 | 0 | rep.insert(size_t{2}, count, os.fill()); |
226 | 0 | } else { |
227 | 0 | rep.insert(size_t{0}, count, os.fill()); |
228 | 0 | } |
229 | 0 | } |
230 | |
|
231 | 0 | return os << rep; |
232 | 0 | } |
233 | | |
234 | | namespace { |
235 | | |
236 | 0 | uint128 UnsignedAbsoluteValue(int128 v) { |
237 | | // Cast to uint128 before possibly negating because -Int128Min() is undefined. |
238 | 0 | return Int128High64(v) < 0 ? -uint128(v) : uint128(v); |
239 | 0 | } |
240 | | |
241 | | } // namespace |
242 | | |
243 | | #if !defined(ABSL_HAVE_INTRINSIC_INT128) |
244 | | namespace { |
245 | | |
246 | | template <typename T> |
247 | | int128 MakeInt128FromFloat(T v) { |
248 | | // Conversion when v is NaN or cannot fit into int128 would be undefined |
249 | | // behavior if using an intrinsic 128-bit integer. |
250 | | assert(std::isfinite(v) && (std::numeric_limits<T>::max_exponent <= 127 || |
251 | | (v >= -std::ldexp(static_cast<T>(1), 127) && |
252 | | v < std::ldexp(static_cast<T>(1), 127)))); |
253 | | |
254 | | // We must convert the absolute value and then negate as needed, because |
255 | | // floating point types are typically sign-magnitude. Otherwise, the |
256 | | // difference between the high and low 64 bits when interpreted as two's |
257 | | // complement overwhelms the precision of the mantissa. |
258 | | uint128 result = v < 0 ? -MakeUint128FromFloat(-v) : MakeUint128FromFloat(v); |
259 | | return MakeInt128(int128_internal::BitCastToSigned(Uint128High64(result)), |
260 | | Uint128Low64(result)); |
261 | | } |
262 | | |
263 | | } // namespace |
264 | | |
265 | | int128::int128(float v) : int128(MakeInt128FromFloat(v)) {} |
266 | | int128::int128(double v) : int128(MakeInt128FromFloat(v)) {} |
267 | | int128::int128(long double v) : int128(MakeInt128FromFloat(v)) {} |
268 | | |
269 | | int128 operator/(int128 lhs, int128 rhs) { |
270 | | assert(lhs != Int128Min() || rhs != -1); // UB on two's complement. |
271 | | |
272 | | uint128 quotient = 0; |
273 | | uint128 remainder = 0; |
274 | | DivModImpl(UnsignedAbsoluteValue(lhs), UnsignedAbsoluteValue(rhs), |
275 | | "ient, &remainder); |
276 | | if ((Int128High64(lhs) < 0) != (Int128High64(rhs) < 0)) quotient = -quotient; |
277 | | return MakeInt128(int128_internal::BitCastToSigned(Uint128High64(quotient)), |
278 | | Uint128Low64(quotient)); |
279 | | } |
280 | | |
281 | | int128 operator%(int128 lhs, int128 rhs) { |
282 | | assert(lhs != Int128Min() || rhs != -1); // UB on two's complement. |
283 | | |
284 | | uint128 quotient = 0; |
285 | | uint128 remainder = 0; |
286 | | DivModImpl(UnsignedAbsoluteValue(lhs), UnsignedAbsoluteValue(rhs), |
287 | | "ient, &remainder); |
288 | | if (Int128High64(lhs) < 0) remainder = -remainder; |
289 | | return MakeInt128(int128_internal::BitCastToSigned(Uint128High64(remainder)), |
290 | | Uint128Low64(remainder)); |
291 | | } |
292 | | #endif // ABSL_HAVE_INTRINSIC_INT128 |
293 | | |
294 | 0 | std::string int128::ToString() const { |
295 | 0 | std::string rep; |
296 | 0 | if (Int128High64(*this) < 0) rep = "-"; |
297 | 0 | rep.append(Uint128ToFormattedString(UnsignedAbsoluteValue(*this), |
298 | 0 | std::ios_base::dec)); |
299 | 0 | return rep; |
300 | 0 | } |
301 | | |
302 | 0 | std::ostream& operator<<(std::ostream& os, int128 v) { |
303 | 0 | std::ios_base::fmtflags flags = os.flags(); |
304 | 0 | std::string rep; |
305 | | |
306 | | // Add the sign if needed. |
307 | 0 | bool print_as_decimal = |
308 | 0 | (flags & std::ios::basefield) == std::ios::dec || |
309 | 0 | (flags & std::ios::basefield) == std::ios_base::fmtflags(); |
310 | 0 | if (print_as_decimal) { |
311 | 0 | if (Int128High64(v) < 0) { |
312 | 0 | rep = "-"; |
313 | 0 | } else if (flags & std::ios::showpos) { |
314 | 0 | rep = "+"; |
315 | 0 | } |
316 | 0 | } |
317 | |
|
318 | 0 | rep.append(Uint128ToFormattedString( |
319 | 0 | print_as_decimal ? UnsignedAbsoluteValue(v) : uint128(v), os.flags())); |
320 | | |
321 | | // Add the requisite padding. |
322 | 0 | std::streamsize width = os.width(0); |
323 | 0 | if (static_cast<size_t>(width) > rep.size()) { |
324 | 0 | const size_t count = static_cast<size_t>(width) - rep.size(); |
325 | 0 | switch (flags & std::ios::adjustfield) { |
326 | 0 | case std::ios::left: |
327 | 0 | rep.append(count, os.fill()); |
328 | 0 | break; |
329 | 0 | case std::ios::internal: |
330 | 0 | if (print_as_decimal && (rep[0] == '+' || rep[0] == '-')) { |
331 | 0 | rep.insert(size_t{1}, count, os.fill()); |
332 | 0 | } else if ((flags & std::ios::basefield) == std::ios::hex && |
333 | 0 | (flags & std::ios::showbase) && v != 0) { |
334 | 0 | rep.insert(size_t{2}, count, os.fill()); |
335 | 0 | } else { |
336 | 0 | rep.insert(size_t{0}, count, os.fill()); |
337 | 0 | } |
338 | 0 | break; |
339 | 0 | default: // std::ios::right |
340 | 0 | rep.insert(size_t{0}, count, os.fill()); |
341 | 0 | break; |
342 | 0 | } |
343 | 0 | } |
344 | | |
345 | 0 | return os << rep; |
346 | 0 | } |
347 | | |
348 | | ABSL_NAMESPACE_END |
349 | | } // namespace absl |