int128.h 37 KB

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