int128_no_intrinsic.inc 11 KB

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