int128.cc 14 KB

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