int128.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. // -----------------------------------------------------------------------------
  17. // File: int128.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This header file defines 128-bit integer types, `uint128` and `int128`.
  21. #ifndef ABSL_NUMERIC_INT128_H_
  22. #define ABSL_NUMERIC_INT128_H_
  23. #include <cassert>
  24. #include <cmath>
  25. #include <cstdint>
  26. #include <cstring>
  27. #include <iosfwd>
  28. #include <limits>
  29. #include <utility>
  30. #include "absl/base/config.h"
  31. #include "absl/base/macros.h"
  32. #include "absl/base/port.h"
  33. #if defined(_MSC_VER)
  34. // In very old versions of MSVC and when the /Zc:wchar_t flag is off, wchar_t is
  35. // a typedef for unsigned short. Otherwise wchar_t is mapped to the __wchar_t
  36. // builtin type. We need to make sure not to define operator wchar_t()
  37. // alongside operator unsigned short() in these instances.
  38. #define ABSL_INTERNAL_WCHAR_T __wchar_t
  39. #if defined(_M_X64)
  40. #include <intrin.h>
  41. #pragma intrinsic(_umul128)
  42. #endif // defined(_M_X64)
  43. #else // defined(_MSC_VER)
  44. #define ABSL_INTERNAL_WCHAR_T wchar_t
  45. #endif // defined(_MSC_VER)
  46. namespace absl {
  47. ABSL_NAMESPACE_BEGIN
  48. class int128;
  49. // uint128
  50. //
  51. // An unsigned 128-bit integer type. The API is meant to mimic an intrinsic type
  52. // as closely as is practical, including exhibiting undefined behavior in
  53. // analogous cases (e.g. division by zero). This type is intended to be a
  54. // drop-in replacement once C++ supports an intrinsic `uint128_t` type; when
  55. // that occurs, existing well-behaved uses of `uint128` will continue to work
  56. // using that new type.
  57. //
  58. // Note: code written with this type will continue to compile once `uint128_t`
  59. // is introduced, provided the replacement helper functions
  60. // `Uint128(Low|High)64()` and `MakeUint128()` are made.
  61. //
  62. // A `uint128` supports the following:
  63. //
  64. // * Implicit construction from integral types
  65. // * Explicit conversion to integral types
  66. //
  67. // Additionally, if your compiler supports `__int128`, `uint128` is
  68. // interoperable with that type. (Abseil checks for this compatibility through
  69. // the `ABSL_HAVE_INTRINSIC_INT128` macro.)
  70. //
  71. // However, a `uint128` differs from intrinsic integral types in the following
  72. // ways:
  73. //
  74. // * Errors on implicit conversions that do not preserve value (such as
  75. // loss of precision when converting to float values).
  76. // * Requires explicit construction from and conversion to floating point
  77. // types.
  78. // * Conversion to integral types requires an explicit static_cast() to
  79. // mimic use of the `-Wnarrowing` compiler flag.
  80. // * The alignment requirement of `uint128` may differ from that of an
  81. // intrinsic 128-bit integer type depending on platform and build
  82. // configuration.
  83. //
  84. // Example:
  85. //
  86. // float y = absl::Uint128Max(); // Error. uint128 cannot be implicitly
  87. // // converted to float.
  88. //
  89. // absl::uint128 v;
  90. // uint64_t i = v; // Error
  91. // uint64_t i = static_cast<uint64_t>(v); // OK
  92. //
  93. class
  94. #if defined(ABSL_HAVE_INTRINSIC_INT128)
  95. alignas(unsigned __int128)
  96. #endif // ABSL_HAVE_INTRINSIC_INT128
  97. uint128 {
  98. public:
  99. uint128() = default;
  100. // Constructors from arithmetic types
  101. constexpr uint128(int v); // NOLINT(runtime/explicit)
  102. constexpr uint128(unsigned int v); // NOLINT(runtime/explicit)
  103. constexpr uint128(long v); // NOLINT(runtime/int)
  104. constexpr uint128(unsigned long v); // NOLINT(runtime/int)
  105. constexpr uint128(long long v); // NOLINT(runtime/int)
  106. constexpr uint128(unsigned long long v); // NOLINT(runtime/int)
  107. #ifdef ABSL_HAVE_INTRINSIC_INT128
  108. constexpr uint128(__int128 v); // NOLINT(runtime/explicit)
  109. constexpr uint128(unsigned __int128 v); // NOLINT(runtime/explicit)
  110. #endif // ABSL_HAVE_INTRINSIC_INT128
  111. constexpr uint128(int128 v); // NOLINT(runtime/explicit)
  112. explicit uint128(float v);
  113. explicit uint128(double v);
  114. explicit uint128(long double v);
  115. // Assignment operators from arithmetic types
  116. uint128& operator=(int v);
  117. uint128& operator=(unsigned int v);
  118. uint128& operator=(long v); // NOLINT(runtime/int)
  119. uint128& operator=(unsigned long v); // NOLINT(runtime/int)
  120. uint128& operator=(long long v); // NOLINT(runtime/int)
  121. uint128& operator=(unsigned long long v); // NOLINT(runtime/int)
  122. #ifdef ABSL_HAVE_INTRINSIC_INT128
  123. uint128& operator=(__int128 v);
  124. uint128& operator=(unsigned __int128 v);
  125. #endif // ABSL_HAVE_INTRINSIC_INT128
  126. uint128& operator=(int128 v);
  127. // Conversion operators to other arithmetic types
  128. constexpr explicit operator bool() const;
  129. constexpr explicit operator char() const;
  130. constexpr explicit operator signed char() const;
  131. constexpr explicit operator unsigned char() const;
  132. constexpr explicit operator char16_t() const;
  133. constexpr explicit operator char32_t() const;
  134. constexpr explicit operator ABSL_INTERNAL_WCHAR_T() const;
  135. constexpr explicit operator short() const; // NOLINT(runtime/int)
  136. // NOLINTNEXTLINE(runtime/int)
  137. constexpr explicit operator unsigned short() const;
  138. constexpr explicit operator int() const;
  139. constexpr explicit operator unsigned int() const;
  140. constexpr explicit operator long() const; // NOLINT(runtime/int)
  141. // NOLINTNEXTLINE(runtime/int)
  142. constexpr explicit operator unsigned long() const;
  143. // NOLINTNEXTLINE(runtime/int)
  144. constexpr explicit operator long long() const;
  145. // NOLINTNEXTLINE(runtime/int)
  146. constexpr explicit operator unsigned long long() const;
  147. #ifdef ABSL_HAVE_INTRINSIC_INT128
  148. constexpr explicit operator __int128() const;
  149. constexpr explicit operator unsigned __int128() const;
  150. #endif // ABSL_HAVE_INTRINSIC_INT128
  151. explicit operator float() const;
  152. explicit operator double() const;
  153. explicit operator long double() const;
  154. // Trivial copy constructor, assignment operator and destructor.
  155. // Arithmetic operators.
  156. uint128& operator+=(uint128 other);
  157. uint128& operator-=(uint128 other);
  158. uint128& operator*=(uint128 other);
  159. // Long division/modulo for uint128.
  160. uint128& operator/=(uint128 other);
  161. uint128& operator%=(uint128 other);
  162. uint128 operator++(int);
  163. uint128 operator--(int);
  164. uint128& operator<<=(int);
  165. uint128& operator>>=(int);
  166. uint128& operator&=(uint128 other);
  167. uint128& operator|=(uint128 other);
  168. uint128& operator^=(uint128 other);
  169. uint128& operator++();
  170. uint128& operator--();
  171. // Uint128Low64()
  172. //
  173. // Returns the lower 64-bit value of a `uint128` value.
  174. friend constexpr uint64_t Uint128Low64(uint128 v);
  175. // Uint128High64()
  176. //
  177. // Returns the higher 64-bit value of a `uint128` value.
  178. friend constexpr uint64_t Uint128High64(uint128 v);
  179. // MakeUInt128()
  180. //
  181. // Constructs a `uint128` numeric value from two 64-bit unsigned integers.
  182. // Note that this factory function is the only way to construct a `uint128`
  183. // from integer values greater than 2^64.
  184. //
  185. // Example:
  186. //
  187. // absl::uint128 big = absl::MakeUint128(1, 0);
  188. friend constexpr uint128 MakeUint128(uint64_t high, uint64_t low);
  189. // Uint128Max()
  190. //
  191. // Returns the highest value for a 128-bit unsigned integer.
  192. friend constexpr uint128 Uint128Max();
  193. // Support for absl::Hash.
  194. template <typename H>
  195. friend H AbslHashValue(H h, uint128 v) {
  196. return H::combine(std::move(h), Uint128High64(v), Uint128Low64(v));
  197. }
  198. private:
  199. constexpr uint128(uint64_t high, uint64_t low);
  200. // TODO(strel) Update implementation to use __int128 once all users of
  201. // uint128 are fixed to not depend on alignof(uint128) == 8. Also add
  202. // alignas(16) to class definition to keep alignment consistent across
  203. // platforms.
  204. #if defined(ABSL_IS_LITTLE_ENDIAN)
  205. uint64_t lo_;
  206. uint64_t hi_;
  207. #elif defined(ABSL_IS_BIG_ENDIAN)
  208. uint64_t hi_;
  209. uint64_t lo_;
  210. #else // byte order
  211. #error "Unsupported byte order: must be little-endian or big-endian."
  212. #endif // byte order
  213. };
  214. // Prefer to use the constexpr `Uint128Max()`.
  215. //
  216. // TODO(absl-team) deprecate kuint128max once migration tool is released.
  217. ABSL_DLL extern const uint128 kuint128max;
  218. // allow uint128 to be logged
  219. std::ostream& operator<<(std::ostream& os, uint128 v);
  220. // TODO(strel) add operator>>(std::istream&, uint128)
  221. constexpr uint128 Uint128Max() {
  222. return uint128((std::numeric_limits<uint64_t>::max)(),
  223. (std::numeric_limits<uint64_t>::max)());
  224. }
  225. ABSL_NAMESPACE_END
  226. } // namespace absl
  227. // Specialized numeric_limits for uint128.
  228. namespace std {
  229. template <>
  230. class numeric_limits<absl::uint128> {
  231. public:
  232. static constexpr bool is_specialized = true;
  233. static constexpr bool is_signed = false;
  234. static constexpr bool is_integer = true;
  235. static constexpr bool is_exact = true;
  236. static constexpr bool has_infinity = false;
  237. static constexpr bool has_quiet_NaN = false;
  238. static constexpr bool has_signaling_NaN = false;
  239. static constexpr float_denorm_style has_denorm = denorm_absent;
  240. static constexpr bool has_denorm_loss = false;
  241. static constexpr float_round_style round_style = round_toward_zero;
  242. static constexpr bool is_iec559 = false;
  243. static constexpr bool is_bounded = true;
  244. static constexpr bool is_modulo = true;
  245. static constexpr int digits = 128;
  246. static constexpr int digits10 = 38;
  247. static constexpr int max_digits10 = 0;
  248. static constexpr int radix = 2;
  249. static constexpr int min_exponent = 0;
  250. static constexpr int min_exponent10 = 0;
  251. static constexpr int max_exponent = 0;
  252. static constexpr int max_exponent10 = 0;
  253. #ifdef ABSL_HAVE_INTRINSIC_INT128
  254. static constexpr bool traps = numeric_limits<unsigned __int128>::traps;
  255. #else // ABSL_HAVE_INTRINSIC_INT128
  256. static constexpr bool traps = numeric_limits<uint64_t>::traps;
  257. #endif // ABSL_HAVE_INTRINSIC_INT128
  258. static constexpr bool tinyness_before = false;
  259. static constexpr absl::uint128 (min)() { return 0; }
  260. static constexpr absl::uint128 lowest() { return 0; }
  261. static constexpr absl::uint128 (max)() { return absl::Uint128Max(); }
  262. static constexpr absl::uint128 epsilon() { return 0; }
  263. static constexpr absl::uint128 round_error() { return 0; }
  264. static constexpr absl::uint128 infinity() { return 0; }
  265. static constexpr absl::uint128 quiet_NaN() { return 0; }
  266. static constexpr absl::uint128 signaling_NaN() { return 0; }
  267. static constexpr absl::uint128 denorm_min() { return 0; }
  268. };
  269. } // namespace std
  270. namespace absl {
  271. ABSL_NAMESPACE_BEGIN
  272. // int128
  273. //
  274. // A signed 128-bit integer type. The API is meant to mimic an intrinsic
  275. // integral type as closely as is practical, including exhibiting undefined
  276. // behavior in analogous cases (e.g. division by zero).
  277. //
  278. // An `int128` supports the following:
  279. //
  280. // * Implicit construction from integral types
  281. // * Explicit conversion to integral types
  282. //
  283. // However, an `int128` differs from intrinsic integral types in the following
  284. // ways:
  285. //
  286. // * It is not implicitly convertible to other integral types.
  287. // * Requires explicit construction from and conversion to floating point
  288. // types.
  289. // Additionally, if your compiler supports `__int128`, `int128` is
  290. // interoperable with that type. (Abseil checks for this compatibility through
  291. // the `ABSL_HAVE_INTRINSIC_INT128` macro.)
  292. //
  293. // The design goal for `int128` is that it will be compatible with a future
  294. // `int128_t`, if that type becomes a part of the standard.
  295. //
  296. // Example:
  297. //
  298. // float y = absl::int128(17); // Error. int128 cannot be implicitly
  299. // // converted to float.
  300. //
  301. // absl::int128 v;
  302. // int64_t i = v; // Error
  303. // int64_t i = static_cast<int64_t>(v); // OK
  304. //
  305. class int128 {
  306. public:
  307. int128() = default;
  308. // Constructors from arithmetic types
  309. constexpr int128(int v); // NOLINT(runtime/explicit)
  310. constexpr int128(unsigned int v); // NOLINT(runtime/explicit)
  311. constexpr int128(long v); // NOLINT(runtime/int)
  312. constexpr int128(unsigned long v); // NOLINT(runtime/int)
  313. constexpr int128(long long v); // NOLINT(runtime/int)
  314. constexpr int128(unsigned long long v); // NOLINT(runtime/int)
  315. #ifdef ABSL_HAVE_INTRINSIC_INT128
  316. constexpr int128(__int128 v); // NOLINT(runtime/explicit)
  317. constexpr explicit int128(unsigned __int128 v);
  318. #endif // ABSL_HAVE_INTRINSIC_INT128
  319. constexpr explicit int128(uint128 v);
  320. explicit int128(float v);
  321. explicit int128(double v);
  322. explicit int128(long double v);
  323. // Assignment operators from arithmetic types
  324. int128& operator=(int v);
  325. int128& operator=(unsigned int v);
  326. int128& operator=(long v); // NOLINT(runtime/int)
  327. int128& operator=(unsigned long v); // NOLINT(runtime/int)
  328. int128& operator=(long long v); // NOLINT(runtime/int)
  329. int128& operator=(unsigned long long v); // NOLINT(runtime/int)
  330. #ifdef ABSL_HAVE_INTRINSIC_INT128
  331. int128& operator=(__int128 v);
  332. #endif // ABSL_HAVE_INTRINSIC_INT128
  333. // Conversion operators to other arithmetic types
  334. constexpr explicit operator bool() const;
  335. constexpr explicit operator char() const;
  336. constexpr explicit operator signed char() const;
  337. constexpr explicit operator unsigned char() const;
  338. constexpr explicit operator char16_t() const;
  339. constexpr explicit operator char32_t() const;
  340. constexpr explicit operator ABSL_INTERNAL_WCHAR_T() const;
  341. constexpr explicit operator short() const; // NOLINT(runtime/int)
  342. // NOLINTNEXTLINE(runtime/int)
  343. constexpr explicit operator unsigned short() const;
  344. constexpr explicit operator int() const;
  345. constexpr explicit operator unsigned int() const;
  346. constexpr explicit operator long() const; // NOLINT(runtime/int)
  347. // NOLINTNEXTLINE(runtime/int)
  348. constexpr explicit operator unsigned long() const;
  349. // NOLINTNEXTLINE(runtime/int)
  350. constexpr explicit operator long long() const;
  351. // NOLINTNEXTLINE(runtime/int)
  352. constexpr explicit operator unsigned long long() const;
  353. #ifdef ABSL_HAVE_INTRINSIC_INT128
  354. constexpr explicit operator __int128() const;
  355. constexpr explicit operator unsigned __int128() const;
  356. #endif // ABSL_HAVE_INTRINSIC_INT128
  357. explicit operator float() const;
  358. explicit operator double() const;
  359. explicit operator long double() const;
  360. // Trivial copy constructor, assignment operator and destructor.
  361. // Arithmetic operators
  362. int128& operator+=(int128 other);
  363. int128& operator-=(int128 other);
  364. int128& operator*=(int128 other);
  365. int128& operator/=(int128 other);
  366. int128& operator%=(int128 other);
  367. int128 operator++(int); // postfix increment: i++
  368. int128 operator--(int); // postfix decrement: i--
  369. int128& operator++(); // prefix increment: ++i
  370. int128& operator--(); // prefix decrement: --i
  371. int128& operator&=(int128 other);
  372. int128& operator|=(int128 other);
  373. int128& operator^=(int128 other);
  374. int128& operator<<=(int amount);
  375. int128& operator>>=(int amount);
  376. // Int128Low64()
  377. //
  378. // Returns the lower 64-bit value of a `int128` value.
  379. friend constexpr uint64_t Int128Low64(int128 v);
  380. // Int128High64()
  381. //
  382. // Returns the higher 64-bit value of a `int128` value.
  383. friend constexpr int64_t Int128High64(int128 v);
  384. // MakeInt128()
  385. //
  386. // Constructs a `int128` numeric value from two 64-bit integers. Note that
  387. // signedness is conveyed in the upper `high` value.
  388. //
  389. // (absl::int128(1) << 64) * high + low
  390. //
  391. // Note that this factory function is the only way to construct a `int128`
  392. // from integer values greater than 2^64 or less than -2^64.
  393. //
  394. // Example:
  395. //
  396. // absl::int128 big = absl::MakeInt128(1, 0);
  397. // absl::int128 big_n = absl::MakeInt128(-1, 0);
  398. friend constexpr int128 MakeInt128(int64_t high, uint64_t low);
  399. // Int128Max()
  400. //
  401. // Returns the maximum value for a 128-bit signed integer.
  402. friend constexpr int128 Int128Max();
  403. // Int128Min()
  404. //
  405. // Returns the minimum value for a 128-bit signed integer.
  406. friend constexpr int128 Int128Min();
  407. // Support for absl::Hash.
  408. template <typename H>
  409. friend H AbslHashValue(H h, int128 v) {
  410. return H::combine(std::move(h), Int128High64(v), Int128Low64(v));
  411. }
  412. private:
  413. constexpr int128(int64_t high, uint64_t low);
  414. #if defined(ABSL_HAVE_INTRINSIC_INT128)
  415. __int128 v_;
  416. #else // ABSL_HAVE_INTRINSIC_INT128
  417. #if defined(ABSL_IS_LITTLE_ENDIAN)
  418. uint64_t lo_;
  419. int64_t hi_;
  420. #elif defined(ABSL_IS_BIG_ENDIAN)
  421. int64_t hi_;
  422. uint64_t lo_;
  423. #else // byte order
  424. #error "Unsupported byte order: must be little-endian or big-endian."
  425. #endif // byte order
  426. #endif // ABSL_HAVE_INTRINSIC_INT128
  427. };
  428. std::ostream& operator<<(std::ostream& os, int128 v);
  429. // TODO(absl-team) add operator>>(std::istream&, int128)
  430. constexpr int128 Int128Max() {
  431. return int128((std::numeric_limits<int64_t>::max)(),
  432. (std::numeric_limits<uint64_t>::max)());
  433. }
  434. constexpr int128 Int128Min() {
  435. return int128((std::numeric_limits<int64_t>::min)(), 0);
  436. }
  437. ABSL_NAMESPACE_END
  438. } // namespace absl
  439. // Specialized numeric_limits for int128.
  440. namespace std {
  441. template <>
  442. class numeric_limits<absl::int128> {
  443. public:
  444. static constexpr bool is_specialized = true;
  445. static constexpr bool is_signed = true;
  446. static constexpr bool is_integer = true;
  447. static constexpr bool is_exact = true;
  448. static constexpr bool has_infinity = false;
  449. static constexpr bool has_quiet_NaN = false;
  450. static constexpr bool has_signaling_NaN = false;
  451. static constexpr float_denorm_style has_denorm = denorm_absent;
  452. static constexpr bool has_denorm_loss = false;
  453. static constexpr float_round_style round_style = round_toward_zero;
  454. static constexpr bool is_iec559 = false;
  455. static constexpr bool is_bounded = true;
  456. static constexpr bool is_modulo = false;
  457. static constexpr int digits = 127;
  458. static constexpr int digits10 = 38;
  459. static constexpr int max_digits10 = 0;
  460. static constexpr int radix = 2;
  461. static constexpr int min_exponent = 0;
  462. static constexpr int min_exponent10 = 0;
  463. static constexpr int max_exponent = 0;
  464. static constexpr int max_exponent10 = 0;
  465. #ifdef ABSL_HAVE_INTRINSIC_INT128
  466. static constexpr bool traps = numeric_limits<__int128>::traps;
  467. #else // ABSL_HAVE_INTRINSIC_INT128
  468. static constexpr bool traps = numeric_limits<uint64_t>::traps;
  469. #endif // ABSL_HAVE_INTRINSIC_INT128
  470. static constexpr bool tinyness_before = false;
  471. static constexpr absl::int128 (min)() { return absl::Int128Min(); }
  472. static constexpr absl::int128 lowest() { return absl::Int128Min(); }
  473. static constexpr absl::int128 (max)() { return absl::Int128Max(); }
  474. static constexpr absl::int128 epsilon() { return 0; }
  475. static constexpr absl::int128 round_error() { return 0; }
  476. static constexpr absl::int128 infinity() { return 0; }
  477. static constexpr absl::int128 quiet_NaN() { return 0; }
  478. static constexpr absl::int128 signaling_NaN() { return 0; }
  479. static constexpr absl::int128 denorm_min() { return 0; }
  480. };
  481. } // namespace std
  482. // --------------------------------------------------------------------------
  483. // Implementation details follow
  484. // --------------------------------------------------------------------------
  485. namespace absl {
  486. ABSL_NAMESPACE_BEGIN
  487. constexpr uint128 MakeUint128(uint64_t high, uint64_t low) {
  488. return uint128(high, low);
  489. }
  490. // Assignment from integer types.
  491. inline uint128& uint128::operator=(int v) { return *this = uint128(v); }
  492. inline uint128& uint128::operator=(unsigned int v) {
  493. return *this = uint128(v);
  494. }
  495. inline uint128& uint128::operator=(long v) { // NOLINT(runtime/int)
  496. return *this = uint128(v);
  497. }
  498. // NOLINTNEXTLINE(runtime/int)
  499. inline uint128& uint128::operator=(unsigned long v) {
  500. return *this = uint128(v);
  501. }
  502. // NOLINTNEXTLINE(runtime/int)
  503. inline uint128& uint128::operator=(long long v) {
  504. return *this = uint128(v);
  505. }
  506. // NOLINTNEXTLINE(runtime/int)
  507. inline uint128& uint128::operator=(unsigned long long v) {
  508. return *this = uint128(v);
  509. }
  510. #ifdef ABSL_HAVE_INTRINSIC_INT128
  511. inline uint128& uint128::operator=(__int128 v) {
  512. return *this = uint128(v);
  513. }
  514. inline uint128& uint128::operator=(unsigned __int128 v) {
  515. return *this = uint128(v);
  516. }
  517. #endif // ABSL_HAVE_INTRINSIC_INT128
  518. inline uint128& uint128::operator=(int128 v) {
  519. return *this = uint128(v);
  520. }
  521. // Arithmetic operators.
  522. uint128 operator<<(uint128 lhs, int amount);
  523. uint128 operator>>(uint128 lhs, int amount);
  524. uint128 operator+(uint128 lhs, uint128 rhs);
  525. uint128 operator-(uint128 lhs, uint128 rhs);
  526. uint128 operator*(uint128 lhs, uint128 rhs);
  527. uint128 operator/(uint128 lhs, uint128 rhs);
  528. uint128 operator%(uint128 lhs, uint128 rhs);
  529. inline uint128& uint128::operator<<=(int amount) {
  530. *this = *this << amount;
  531. return *this;
  532. }
  533. inline uint128& uint128::operator>>=(int amount) {
  534. *this = *this >> amount;
  535. return *this;
  536. }
  537. inline uint128& uint128::operator+=(uint128 other) {
  538. *this = *this + other;
  539. return *this;
  540. }
  541. inline uint128& uint128::operator-=(uint128 other) {
  542. *this = *this - other;
  543. return *this;
  544. }
  545. inline uint128& uint128::operator*=(uint128 other) {
  546. *this = *this * other;
  547. return *this;
  548. }
  549. inline uint128& uint128::operator/=(uint128 other) {
  550. *this = *this / other;
  551. return *this;
  552. }
  553. inline uint128& uint128::operator%=(uint128 other) {
  554. *this = *this % other;
  555. return *this;
  556. }
  557. constexpr uint64_t Uint128Low64(uint128 v) { return v.lo_; }
  558. constexpr uint64_t Uint128High64(uint128 v) { return v.hi_; }
  559. // Constructors from integer types.
  560. #if defined(ABSL_IS_LITTLE_ENDIAN)
  561. constexpr uint128::uint128(uint64_t high, uint64_t low)
  562. : lo_{low}, hi_{high} {}
  563. constexpr uint128::uint128(int v)
  564. : lo_{static_cast<uint64_t>(v)},
  565. hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0} {}
  566. constexpr uint128::uint128(long v) // NOLINT(runtime/int)
  567. : lo_{static_cast<uint64_t>(v)},
  568. hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0} {}
  569. constexpr uint128::uint128(long long v) // NOLINT(runtime/int)
  570. : lo_{static_cast<uint64_t>(v)},
  571. hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0} {}
  572. constexpr uint128::uint128(unsigned int v) : lo_{v}, hi_{0} {}
  573. // NOLINTNEXTLINE(runtime/int)
  574. constexpr uint128::uint128(unsigned long v) : lo_{v}, hi_{0} {}
  575. // NOLINTNEXTLINE(runtime/int)
  576. constexpr uint128::uint128(unsigned long long v) : lo_{v}, hi_{0} {}
  577. #ifdef ABSL_HAVE_INTRINSIC_INT128
  578. constexpr uint128::uint128(__int128 v)
  579. : lo_{static_cast<uint64_t>(v & ~uint64_t{0})},
  580. hi_{static_cast<uint64_t>(static_cast<unsigned __int128>(v) >> 64)} {}
  581. constexpr uint128::uint128(unsigned __int128 v)
  582. : lo_{static_cast<uint64_t>(v & ~uint64_t{0})},
  583. hi_{static_cast<uint64_t>(v >> 64)} {}
  584. #endif // ABSL_HAVE_INTRINSIC_INT128
  585. constexpr uint128::uint128(int128 v)
  586. : lo_{Int128Low64(v)}, hi_{static_cast<uint64_t>(Int128High64(v))} {}
  587. #elif defined(ABSL_IS_BIG_ENDIAN)
  588. constexpr uint128::uint128(uint64_t high, uint64_t low)
  589. : hi_{high}, lo_{low} {}
  590. constexpr uint128::uint128(int v)
  591. : hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0},
  592. lo_{static_cast<uint64_t>(v)} {}
  593. constexpr uint128::uint128(long v) // NOLINT(runtime/int)
  594. : hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0},
  595. lo_{static_cast<uint64_t>(v)} {}
  596. constexpr uint128::uint128(long long v) // NOLINT(runtime/int)
  597. : hi_{v < 0 ? (std::numeric_limits<uint64_t>::max)() : 0},
  598. lo_{static_cast<uint64_t>(v)} {}
  599. constexpr uint128::uint128(unsigned int v) : hi_{0}, lo_{v} {}
  600. // NOLINTNEXTLINE(runtime/int)
  601. constexpr uint128::uint128(unsigned long v) : hi_{0}, lo_{v} {}
  602. // NOLINTNEXTLINE(runtime/int)
  603. constexpr uint128::uint128(unsigned long long v) : hi_{0}, lo_{v} {}
  604. #ifdef ABSL_HAVE_INTRINSIC_INT128
  605. constexpr uint128::uint128(__int128 v)
  606. : hi_{static_cast<uint64_t>(static_cast<unsigned __int128>(v) >> 64)},
  607. lo_{static_cast<uint64_t>(v & ~uint64_t{0})} {}
  608. constexpr uint128::uint128(unsigned __int128 v)
  609. : hi_{static_cast<uint64_t>(v >> 64)},
  610. lo_{static_cast<uint64_t>(v & ~uint64_t{0})} {}
  611. #endif // ABSL_HAVE_INTRINSIC_INT128
  612. constexpr uint128::uint128(int128 v)
  613. : hi_{static_cast<uint64_t>(Int128High64(v))}, lo_{Int128Low64(v)} {}
  614. #else // byte order
  615. #error "Unsupported byte order: must be little-endian or big-endian."
  616. #endif // byte order
  617. // Conversion operators to integer types.
  618. constexpr uint128::operator bool() const { return lo_ || hi_; }
  619. constexpr uint128::operator char() const { return static_cast<char>(lo_); }
  620. constexpr uint128::operator signed char() const {
  621. return static_cast<signed char>(lo_);
  622. }
  623. constexpr uint128::operator unsigned char() const {
  624. return static_cast<unsigned char>(lo_);
  625. }
  626. constexpr uint128::operator char16_t() const {
  627. return static_cast<char16_t>(lo_);
  628. }
  629. constexpr uint128::operator char32_t() const {
  630. return static_cast<char32_t>(lo_);
  631. }
  632. constexpr uint128::operator ABSL_INTERNAL_WCHAR_T() const {
  633. return static_cast<ABSL_INTERNAL_WCHAR_T>(lo_);
  634. }
  635. // NOLINTNEXTLINE(runtime/int)
  636. constexpr uint128::operator short() const { return static_cast<short>(lo_); }
  637. constexpr uint128::operator unsigned short() const { // NOLINT(runtime/int)
  638. return static_cast<unsigned short>(lo_); // NOLINT(runtime/int)
  639. }
  640. constexpr uint128::operator int() const { return static_cast<int>(lo_); }
  641. constexpr uint128::operator unsigned int() const {
  642. return static_cast<unsigned int>(lo_);
  643. }
  644. // NOLINTNEXTLINE(runtime/int)
  645. constexpr uint128::operator long() const { return static_cast<long>(lo_); }
  646. constexpr uint128::operator unsigned long() const { // NOLINT(runtime/int)
  647. return static_cast<unsigned long>(lo_); // NOLINT(runtime/int)
  648. }
  649. constexpr uint128::operator long long() const { // NOLINT(runtime/int)
  650. return static_cast<long long>(lo_); // NOLINT(runtime/int)
  651. }
  652. constexpr uint128::operator unsigned long long() const { // NOLINT(runtime/int)
  653. return static_cast<unsigned long long>(lo_); // NOLINT(runtime/int)
  654. }
  655. #ifdef ABSL_HAVE_INTRINSIC_INT128
  656. constexpr uint128::operator __int128() const {
  657. return (static_cast<__int128>(hi_) << 64) + lo_;
  658. }
  659. constexpr uint128::operator unsigned __int128() const {
  660. return (static_cast<unsigned __int128>(hi_) << 64) + lo_;
  661. }
  662. #endif // ABSL_HAVE_INTRINSIC_INT128
  663. // Conversion operators to floating point types.
  664. inline uint128::operator float() const {
  665. return static_cast<float>(lo_) + std::ldexp(static_cast<float>(hi_), 64);
  666. }
  667. inline uint128::operator double() const {
  668. return static_cast<double>(lo_) + std::ldexp(static_cast<double>(hi_), 64);
  669. }
  670. inline uint128::operator long double() const {
  671. return static_cast<long double>(lo_) +
  672. std::ldexp(static_cast<long double>(hi_), 64);
  673. }
  674. // Comparison operators.
  675. inline bool operator==(uint128 lhs, uint128 rhs) {
  676. return (Uint128Low64(lhs) == Uint128Low64(rhs) &&
  677. Uint128High64(lhs) == Uint128High64(rhs));
  678. }
  679. inline bool operator!=(uint128 lhs, uint128 rhs) {
  680. return !(lhs == rhs);
  681. }
  682. inline bool operator<(uint128 lhs, uint128 rhs) {
  683. #ifdef ABSL_HAVE_INTRINSIC_INT128
  684. return static_cast<unsigned __int128>(lhs) <
  685. static_cast<unsigned __int128>(rhs);
  686. #else
  687. return (Uint128High64(lhs) == Uint128High64(rhs))
  688. ? (Uint128Low64(lhs) < Uint128Low64(rhs))
  689. : (Uint128High64(lhs) < Uint128High64(rhs));
  690. #endif
  691. }
  692. inline bool operator>(uint128 lhs, uint128 rhs) { return rhs < lhs; }
  693. inline bool operator<=(uint128 lhs, uint128 rhs) { return !(rhs < lhs); }
  694. inline bool operator>=(uint128 lhs, uint128 rhs) { return !(lhs < rhs); }
  695. // Unary operators.
  696. inline uint128 operator-(uint128 val) {
  697. uint64_t hi = ~Uint128High64(val);
  698. uint64_t lo = ~Uint128Low64(val) + 1;
  699. if (lo == 0) ++hi; // carry
  700. return MakeUint128(hi, lo);
  701. }
  702. inline bool operator!(uint128 val) {
  703. return !Uint128High64(val) && !Uint128Low64(val);
  704. }
  705. // Logical operators.
  706. inline uint128 operator~(uint128 val) {
  707. return MakeUint128(~Uint128High64(val), ~Uint128Low64(val));
  708. }
  709. inline uint128 operator|(uint128 lhs, uint128 rhs) {
  710. return MakeUint128(Uint128High64(lhs) | Uint128High64(rhs),
  711. Uint128Low64(lhs) | Uint128Low64(rhs));
  712. }
  713. inline uint128 operator&(uint128 lhs, uint128 rhs) {
  714. return MakeUint128(Uint128High64(lhs) & Uint128High64(rhs),
  715. Uint128Low64(lhs) & Uint128Low64(rhs));
  716. }
  717. inline uint128 operator^(uint128 lhs, uint128 rhs) {
  718. return MakeUint128(Uint128High64(lhs) ^ Uint128High64(rhs),
  719. Uint128Low64(lhs) ^ Uint128Low64(rhs));
  720. }
  721. inline uint128& uint128::operator|=(uint128 other) {
  722. hi_ |= other.hi_;
  723. lo_ |= other.lo_;
  724. return *this;
  725. }
  726. inline uint128& uint128::operator&=(uint128 other) {
  727. hi_ &= other.hi_;
  728. lo_ &= other.lo_;
  729. return *this;
  730. }
  731. inline uint128& uint128::operator^=(uint128 other) {
  732. hi_ ^= other.hi_;
  733. lo_ ^= other.lo_;
  734. return *this;
  735. }
  736. // Arithmetic operators.
  737. inline uint128 operator<<(uint128 lhs, int amount) {
  738. #ifdef ABSL_HAVE_INTRINSIC_INT128
  739. return static_cast<unsigned __int128>(lhs) << amount;
  740. #else
  741. // uint64_t shifts of >= 64 are undefined, so we will need some
  742. // special-casing.
  743. if (amount < 64) {
  744. if (amount != 0) {
  745. return MakeUint128(
  746. (Uint128High64(lhs) << amount) | (Uint128Low64(lhs) >> (64 - amount)),
  747. Uint128Low64(lhs) << amount);
  748. }
  749. return lhs;
  750. }
  751. return MakeUint128(Uint128Low64(lhs) << (amount - 64), 0);
  752. #endif
  753. }
  754. inline uint128 operator>>(uint128 lhs, int amount) {
  755. #ifdef ABSL_HAVE_INTRINSIC_INT128
  756. return static_cast<unsigned __int128>(lhs) >> amount;
  757. #else
  758. // uint64_t shifts of >= 64 are undefined, so we will need some
  759. // special-casing.
  760. if (amount < 64) {
  761. if (amount != 0) {
  762. return MakeUint128(Uint128High64(lhs) >> amount,
  763. (Uint128Low64(lhs) >> amount) |
  764. (Uint128High64(lhs) << (64 - amount)));
  765. }
  766. return lhs;
  767. }
  768. return MakeUint128(0, Uint128High64(lhs) >> (amount - 64));
  769. #endif
  770. }
  771. inline uint128 operator+(uint128 lhs, uint128 rhs) {
  772. uint128 result = MakeUint128(Uint128High64(lhs) + Uint128High64(rhs),
  773. Uint128Low64(lhs) + Uint128Low64(rhs));
  774. if (Uint128Low64(result) < Uint128Low64(lhs)) { // check for carry
  775. return MakeUint128(Uint128High64(result) + 1, Uint128Low64(result));
  776. }
  777. return result;
  778. }
  779. inline uint128 operator-(uint128 lhs, uint128 rhs) {
  780. uint128 result = MakeUint128(Uint128High64(lhs) - Uint128High64(rhs),
  781. Uint128Low64(lhs) - Uint128Low64(rhs));
  782. if (Uint128Low64(lhs) < Uint128Low64(rhs)) { // check for carry
  783. return MakeUint128(Uint128High64(result) - 1, Uint128Low64(result));
  784. }
  785. return result;
  786. }
  787. inline uint128 operator*(uint128 lhs, uint128 rhs) {
  788. #if defined(ABSL_HAVE_INTRINSIC_INT128)
  789. // TODO(strel) Remove once alignment issues are resolved and unsigned __int128
  790. // can be used for uint128 storage.
  791. return static_cast<unsigned __int128>(lhs) *
  792. static_cast<unsigned __int128>(rhs);
  793. #elif defined(_MSC_VER) && defined(_M_X64)
  794. uint64_t carry;
  795. uint64_t low = _umul128(Uint128Low64(lhs), Uint128Low64(rhs), &carry);
  796. return MakeUint128(Uint128Low64(lhs) * Uint128High64(rhs) +
  797. Uint128High64(lhs) * Uint128Low64(rhs) + carry,
  798. low);
  799. #else // ABSL_HAVE_INTRINSIC128
  800. uint64_t a32 = Uint128Low64(lhs) >> 32;
  801. uint64_t a00 = Uint128Low64(lhs) & 0xffffffff;
  802. uint64_t b32 = Uint128Low64(rhs) >> 32;
  803. uint64_t b00 = Uint128Low64(rhs) & 0xffffffff;
  804. uint128 result =
  805. MakeUint128(Uint128High64(lhs) * Uint128Low64(rhs) +
  806. Uint128Low64(lhs) * Uint128High64(rhs) + a32 * b32,
  807. a00 * b00);
  808. result += uint128(a32 * b00) << 32;
  809. result += uint128(a00 * b32) << 32;
  810. return result;
  811. #endif // ABSL_HAVE_INTRINSIC128
  812. }
  813. // Increment/decrement operators.
  814. inline uint128 uint128::operator++(int) {
  815. uint128 tmp(*this);
  816. *this += 1;
  817. return tmp;
  818. }
  819. inline uint128 uint128::operator--(int) {
  820. uint128 tmp(*this);
  821. *this -= 1;
  822. return tmp;
  823. }
  824. inline uint128& uint128::operator++() {
  825. *this += 1;
  826. return *this;
  827. }
  828. inline uint128& uint128::operator--() {
  829. *this -= 1;
  830. return *this;
  831. }
  832. constexpr int128 MakeInt128(int64_t high, uint64_t low) {
  833. return int128(high, low);
  834. }
  835. // Assignment from integer types.
  836. inline int128& int128::operator=(int v) {
  837. return *this = int128(v);
  838. }
  839. inline int128& int128::operator=(unsigned int v) {
  840. return *this = int128(v);
  841. }
  842. inline int128& int128::operator=(long v) { // NOLINT(runtime/int)
  843. return *this = int128(v);
  844. }
  845. // NOLINTNEXTLINE(runtime/int)
  846. inline int128& int128::operator=(unsigned long v) {
  847. return *this = int128(v);
  848. }
  849. // NOLINTNEXTLINE(runtime/int)
  850. inline int128& int128::operator=(long long v) {
  851. return *this = int128(v);
  852. }
  853. // NOLINTNEXTLINE(runtime/int)
  854. inline int128& int128::operator=(unsigned long long v) {
  855. return *this = int128(v);
  856. }
  857. // Arithmetic operators.
  858. int128 operator+(int128 lhs, int128 rhs);
  859. int128 operator-(int128 lhs, int128 rhs);
  860. int128 operator*(int128 lhs, int128 rhs);
  861. int128 operator/(int128 lhs, int128 rhs);
  862. int128 operator%(int128 lhs, int128 rhs);
  863. int128 operator|(int128 lhs, int128 rhs);
  864. int128 operator&(int128 lhs, int128 rhs);
  865. int128 operator^(int128 lhs, int128 rhs);
  866. int128 operator<<(int128 lhs, int amount);
  867. int128 operator>>(int128 lhs, int amount);
  868. inline int128& int128::operator+=(int128 other) {
  869. *this = *this + other;
  870. return *this;
  871. }
  872. inline int128& int128::operator-=(int128 other) {
  873. *this = *this - other;
  874. return *this;
  875. }
  876. inline int128& int128::operator*=(int128 other) {
  877. *this = *this * other;
  878. return *this;
  879. }
  880. inline int128& int128::operator/=(int128 other) {
  881. *this = *this / other;
  882. return *this;
  883. }
  884. inline int128& int128::operator%=(int128 other) {
  885. *this = *this % other;
  886. return *this;
  887. }
  888. inline int128& int128::operator|=(int128 other) {
  889. *this = *this | other;
  890. return *this;
  891. }
  892. inline int128& int128::operator&=(int128 other) {
  893. *this = *this & other;
  894. return *this;
  895. }
  896. inline int128& int128::operator^=(int128 other) {
  897. *this = *this ^ other;
  898. return *this;
  899. }
  900. inline int128& int128::operator<<=(int amount) {
  901. *this = *this << amount;
  902. return *this;
  903. }
  904. inline int128& int128::operator>>=(int amount) {
  905. *this = *this >> amount;
  906. return *this;
  907. }
  908. namespace int128_internal {
  909. // Casts from unsigned to signed while preserving the underlying binary
  910. // representation.
  911. constexpr int64_t BitCastToSigned(uint64_t v) {
  912. // Casting an unsigned integer to a signed integer of the same
  913. // width is implementation defined behavior if the source value would not fit
  914. // in the destination type. We step around it with a roundtrip bitwise not
  915. // operation to make sure this function remains constexpr. Clang, GCC, and
  916. // MSVC optimize this to a no-op on x86-64.
  917. return v & (uint64_t{1} << 63) ? ~static_cast<int64_t>(~v)
  918. : static_cast<int64_t>(v);
  919. }
  920. } // namespace int128_internal
  921. #if defined(ABSL_HAVE_INTRINSIC_INT128)
  922. #include "absl/numeric/int128_have_intrinsic.inc" // IWYU pragma: export
  923. #else // ABSL_HAVE_INTRINSIC_INT128
  924. #include "absl/numeric/int128_no_intrinsic.inc" // IWYU pragma: export
  925. #endif // ABSL_HAVE_INTRINSIC_INT128
  926. ABSL_NAMESPACE_END
  927. } // namespace absl
  928. #undef ABSL_INTERNAL_WCHAR_T
  929. #endif // ABSL_NUMERIC_INT128_H_