int128_no_intrinsic.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. // This file contains :int128 implementation details that depend on internal
  16. // representation when ABSL_HAVE_INTRINSIC_INT128 is *not* defined. This file
  17. // is included by int128.h and relies on ABSL_INTERNAL_WCHAR_T being defined.
  18. constexpr uint64_t Int128Low64(int128 v) { return v.lo_; }
  19. constexpr int64_t Int128High64(int128 v) { return v.hi_; }
  20. #if defined(ABSL_IS_LITTLE_ENDIAN)
  21. constexpr int128::int128(int64_t high, uint64_t low) : lo_(low), hi_(high) {}
  22. constexpr int128::int128(int v)
  23. : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
  24. constexpr int128::int128(long v) // NOLINT(runtime/int)
  25. : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
  26. constexpr int128::int128(long long v) // NOLINT(runtime/int)
  27. : lo_{static_cast<uint64_t>(v)}, hi_{v < 0 ? ~int64_t{0} : 0} {}
  28. constexpr int128::int128(unsigned int v) : lo_{v}, hi_{0} {}
  29. // NOLINTNEXTLINE(runtime/int)
  30. constexpr int128::int128(unsigned long v) : lo_{v}, hi_{0} {}
  31. // NOLINTNEXTLINE(runtime/int)
  32. constexpr int128::int128(unsigned long long v) : lo_{v}, hi_{0} {}
  33. constexpr int128::int128(uint128 v)
  34. : lo_{Uint128Low64(v)}, hi_{static_cast<int64_t>(Uint128High64(v))} {}
  35. #elif defined(ABSL_IS_BIG_ENDIAN)
  36. constexpr int128::int128(int64_t high, uint64_t low) : hi_{high}, lo_{low} {}
  37. constexpr int128::int128(int v)
  38. : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
  39. constexpr int128::int128(long v) // NOLINT(runtime/int)
  40. : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
  41. constexpr int128::int128(long long v) // NOLINT(runtime/int)
  42. : hi_{v < 0 ? ~int64_t{0} : 0}, lo_{static_cast<uint64_t>(v)} {}
  43. constexpr int128::int128(unsigned int v) : hi_{0}, lo_{v} {}
  44. // NOLINTNEXTLINE(runtime/int)
  45. constexpr int128::int128(unsigned long v) : hi_{0}, lo_{v} {}
  46. // NOLINTNEXTLINE(runtime/int)
  47. constexpr int128::int128(unsigned long long v) : hi_{0}, lo_{v} {}
  48. constexpr int128::int128(uint128 v)
  49. : hi_{static_cast<int64_t>(Uint128High64(v))}, lo_{Uint128Low64(v)} {}
  50. #else // byte order
  51. #error "Unsupported byte order: must be little-endian or big-endian."
  52. #endif // byte order
  53. constexpr int128::operator bool() const { return lo_ || hi_; }
  54. constexpr int128::operator char() const {
  55. // NOLINTNEXTLINE(runtime/int)
  56. return static_cast<char>(static_cast<long long>(*this));
  57. }
  58. constexpr int128::operator signed char() const {
  59. // NOLINTNEXTLINE(runtime/int)
  60. return static_cast<signed char>(static_cast<long long>(*this));
  61. }
  62. constexpr int128::operator unsigned char() const {
  63. return static_cast<unsigned char>(lo_);
  64. }
  65. constexpr int128::operator char16_t() const {
  66. return static_cast<char16_t>(lo_);
  67. }
  68. constexpr int128::operator char32_t() const {
  69. return static_cast<char32_t>(lo_);
  70. }
  71. constexpr int128::operator ABSL_INTERNAL_WCHAR_T() const {
  72. // NOLINTNEXTLINE(runtime/int)
  73. return static_cast<ABSL_INTERNAL_WCHAR_T>(static_cast<long long>(*this));
  74. }
  75. constexpr int128::operator short() const { // NOLINT(runtime/int)
  76. // NOLINTNEXTLINE(runtime/int)
  77. return static_cast<short>(static_cast<long long>(*this));
  78. }
  79. constexpr int128::operator unsigned short() const { // NOLINT(runtime/int)
  80. return static_cast<unsigned short>(lo_); // NOLINT(runtime/int)
  81. }
  82. constexpr int128::operator int() const {
  83. // NOLINTNEXTLINE(runtime/int)
  84. return static_cast<int>(static_cast<long long>(*this));
  85. }
  86. constexpr int128::operator unsigned int() const {
  87. return static_cast<unsigned int>(lo_);
  88. }
  89. constexpr int128::operator long() const { // NOLINT(runtime/int)
  90. // NOLINTNEXTLINE(runtime/int)
  91. return static_cast<long>(static_cast<long long>(*this));
  92. }
  93. constexpr int128::operator unsigned long() const { // NOLINT(runtime/int)
  94. return static_cast<unsigned long>(lo_); // NOLINT(runtime/int)
  95. }
  96. constexpr int128::operator long long() const { // NOLINT(runtime/int)
  97. // We don't bother checking the value of hi_. If *this < 0, lo_'s high bit
  98. // must be set in order for the value to fit into a long long. Conversely, if
  99. // lo_'s high bit is set, *this must be < 0 for the value to fit.
  100. return int128_internal::BitCastToSigned(lo_);
  101. }
  102. constexpr int128::operator unsigned long long() const { // NOLINT(runtime/int)
  103. return static_cast<unsigned long long>(lo_); // NOLINT(runtime/int)
  104. }
  105. inline int128::operator float() const {
  106. // We must convert the absolute value and then negate as needed, because
  107. // floating point types are typically sign-magnitude. Otherwise, the
  108. // difference between the high and low 64 bits when interpreted as two's
  109. // complement overwhelms the precision of the mantissa.
  110. //
  111. // Also check to make sure we don't negate Int128Min()
  112. return hi_ < 0 && *this != Int128Min()
  113. ? -static_cast<float>(-*this)
  114. : static_cast<float>(lo_) +
  115. std::ldexp(static_cast<float>(hi_), 64);
  116. }
  117. inline int128::operator double() const {
  118. // See comment in int128::operator float() above.
  119. return hi_ < 0 && *this != Int128Min()
  120. ? -static_cast<double>(-*this)
  121. : static_cast<double>(lo_) +
  122. std::ldexp(static_cast<double>(hi_), 64);
  123. }
  124. inline int128::operator long double() const {
  125. // See comment in int128::operator float() above.
  126. return hi_ < 0 && *this != Int128Min()
  127. ? -static_cast<long double>(-*this)
  128. : static_cast<long double>(lo_) +
  129. std::ldexp(static_cast<long double>(hi_), 64);
  130. }
  131. // Comparison operators.
  132. constexpr bool operator==(int128 lhs, int128 rhs) {
  133. return (Int128Low64(lhs) == Int128Low64(rhs) &&
  134. Int128High64(lhs) == Int128High64(rhs));
  135. }
  136. constexpr bool operator!=(int128 lhs, int128 rhs) { return !(lhs == rhs); }
  137. constexpr bool operator<(int128 lhs, int128 rhs) {
  138. return (Int128High64(lhs) == Int128High64(rhs))
  139. ? (Int128Low64(lhs) < Int128Low64(rhs))
  140. : (Int128High64(lhs) < Int128High64(rhs));
  141. }
  142. constexpr bool operator>(int128 lhs, int128 rhs) {
  143. return (Int128High64(lhs) == Int128High64(rhs))
  144. ? (Int128Low64(lhs) > Int128Low64(rhs))
  145. : (Int128High64(lhs) > Int128High64(rhs));
  146. }
  147. constexpr bool operator<=(int128 lhs, int128 rhs) { return !(lhs > rhs); }
  148. constexpr bool operator>=(int128 lhs, int128 rhs) { return !(lhs < rhs); }
  149. #ifdef __cpp_impl_three_way_comparison
  150. constexpr absl::strong_ordering operator<=>(int128 lhs, int128 rhs) {
  151. if (int64_t lhs_high = Int128High64(lhs), rhs_high = Int128High64(rhs);
  152. lhs_high < rhs_high) {
  153. return absl::strong_ordering::less;
  154. } else if (lhs_high > rhs_high) {
  155. return absl::strong_ordering::greater;
  156. } else if (uint64_t lhs_low = Uint128Low64(lhs), rhs_low = Uint128Low64(rhs);
  157. lhs_low < rhs_low) {
  158. return absl::strong_ordering::less;
  159. } else if (lhs_low > rhs_low) {
  160. return absl::strong_ordering::greater;
  161. } else {
  162. return absl::strong_ordering::equal;
  163. }
  164. }
  165. #endif
  166. // Unary operators.
  167. constexpr int128 operator-(int128 v) {
  168. return MakeInt128(~Int128High64(v) + (Int128Low64(v) == 0),
  169. ~Int128Low64(v) + 1);
  170. }
  171. constexpr bool operator!(int128 v) {
  172. return !Int128Low64(v) && !Int128High64(v);
  173. }
  174. constexpr int128 operator~(int128 val) {
  175. return MakeInt128(~Int128High64(val), ~Int128Low64(val));
  176. }
  177. // Arithmetic operators.
  178. namespace int128_internal {
  179. constexpr int128 SignedAddResult(int128 result, int128 lhs) {
  180. // check for carry
  181. return (Int128Low64(result) < Int128Low64(lhs))
  182. ? MakeInt128(Int128High64(result) + 1, Int128Low64(result))
  183. : result;
  184. }
  185. } // namespace int128_internal
  186. constexpr int128 operator+(int128 lhs, int128 rhs) {
  187. return int128_internal::SignedAddResult(
  188. MakeInt128(Int128High64(lhs) + Int128High64(rhs),
  189. Int128Low64(lhs) + Int128Low64(rhs)),
  190. lhs);
  191. }
  192. namespace int128_internal {
  193. constexpr int128 SignedSubstructResult(int128 result, int128 lhs, int128 rhs) {
  194. // check for carry
  195. return (Int128Low64(lhs) < Int128Low64(rhs))
  196. ? MakeInt128(Int128High64(result) - 1, Int128Low64(result))
  197. : result;
  198. }
  199. } // namespace int128_internal
  200. constexpr int128 operator-(int128 lhs, int128 rhs) {
  201. return int128_internal::SignedSubstructResult(
  202. MakeInt128(Int128High64(lhs) - Int128High64(rhs),
  203. Int128Low64(lhs) - Int128Low64(rhs)),
  204. lhs, rhs);
  205. }
  206. inline int128 operator*(int128 lhs, int128 rhs) {
  207. return MakeInt128(
  208. int128_internal::BitCastToSigned(Uint128High64(uint128(lhs) * rhs)),
  209. Uint128Low64(uint128(lhs) * rhs));
  210. }
  211. inline int128 int128::operator++(int) {
  212. int128 tmp(*this);
  213. *this += 1;
  214. return tmp;
  215. }
  216. inline int128 int128::operator--(int) {
  217. int128 tmp(*this);
  218. *this -= 1;
  219. return tmp;
  220. }
  221. inline int128& int128::operator++() {
  222. *this += 1;
  223. return *this;
  224. }
  225. inline int128& int128::operator--() {
  226. *this -= 1;
  227. return *this;
  228. }
  229. constexpr int128 operator|(int128 lhs, int128 rhs) {
  230. return MakeInt128(Int128High64(lhs) | Int128High64(rhs),
  231. Int128Low64(lhs) | Int128Low64(rhs));
  232. }
  233. constexpr int128 operator&(int128 lhs, int128 rhs) {
  234. return MakeInt128(Int128High64(lhs) & Int128High64(rhs),
  235. Int128Low64(lhs) & Int128Low64(rhs));
  236. }
  237. constexpr int128 operator^(int128 lhs, int128 rhs) {
  238. return MakeInt128(Int128High64(lhs) ^ Int128High64(rhs),
  239. Int128Low64(lhs) ^ Int128Low64(rhs));
  240. }
  241. constexpr int128 operator<<(int128 lhs, int amount) {
  242. // int64_t shifts of >= 63 are undefined, so we need some special-casing.
  243. assert(amount >= 0 && amount < 127);
  244. if (amount <= 0) {
  245. return lhs;
  246. } else if (amount < 63) {
  247. return MakeInt128(
  248. (Int128High64(lhs) << amount) |
  249. static_cast<int64_t>(Int128Low64(lhs) >> (64 - amount)),
  250. Int128Low64(lhs) << amount);
  251. } else if (amount == 63) {
  252. return MakeInt128(((Int128High64(lhs) << 32) << 31) |
  253. static_cast<int64_t>(Int128Low64(lhs) >> 1),
  254. (Int128Low64(lhs) << 32) << 31);
  255. } else if (amount == 127) {
  256. return MakeInt128(static_cast<int64_t>(Int128Low64(lhs) << 63), 0);
  257. } else if (amount > 127) {
  258. return MakeInt128(0, 0);
  259. } else {
  260. // amount >= 64 && amount < 127
  261. return MakeInt128(static_cast<int64_t>(Int128Low64(lhs) << (amount - 64)),
  262. 0);
  263. }
  264. }
  265. constexpr int128 operator>>(int128 lhs, int amount) {
  266. // int64_t shifts of >= 63 are undefined, so we need some special-casing.
  267. assert(amount >= 0 && amount < 127);
  268. if (amount <= 0) {
  269. return lhs;
  270. } else if (amount < 63) {
  271. return MakeInt128(
  272. Int128High64(lhs) >> amount,
  273. Int128Low64(lhs) >> amount | static_cast<uint64_t>(Int128High64(lhs))
  274. << (64 - amount));
  275. } else if (amount == 63) {
  276. return MakeInt128((Int128High64(lhs) >> 32) >> 31,
  277. static_cast<uint64_t>(Int128High64(lhs) << 1) |
  278. (Int128Low64(lhs) >> 32) >> 31);
  279. } else if (amount >= 127) {
  280. return MakeInt128((Int128High64(lhs) >> 32) >> 31,
  281. static_cast<uint64_t>((Int128High64(lhs) >> 32) >> 31));
  282. } else {
  283. // amount >= 64 && amount < 127
  284. return MakeInt128(
  285. (Int128High64(lhs) >> 32) >> 31,
  286. static_cast<uint64_t>(Int128High64(lhs) >> (amount - 64)));
  287. }
  288. }