int128.h 39 KB

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