You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

int128.h 44 kB

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