int128.cc 14 KB

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