int128_no_intrinsic.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. // Unary operators.
  150. constexpr int128 operator-(int128 v) {
  151. return MakeInt128(~Int128High64(v) + (Int128Low64(v) == 0),
  152. ~Int128Low64(v) + 1);
  153. }
  154. constexpr bool operator!(int128 v) {
  155. return !Int128Low64(v) && !Int128High64(v);
  156. }
  157. constexpr int128 operator~(int128 val) {
  158. return MakeInt128(~Int128High64(val), ~Int128Low64(val));
  159. }
  160. // Arithmetic operators.
  161. namespace int128_internal {
  162. constexpr int128 SignedAddResult(int128 result, int128 lhs) {
  163. // check for carry
  164. return (Int128Low64(result) < Int128Low64(lhs))
  165. ? MakeInt128(Int128High64(result) + 1, Int128Low64(result))
  166. : result;
  167. }
  168. } // namespace int128_internal
  169. constexpr int128 operator+(int128 lhs, int128 rhs) {
  170. return int128_internal::SignedAddResult(
  171. MakeInt128(Int128High64(lhs) + Int128High64(rhs),
  172. Int128Low64(lhs) + Int128Low64(rhs)),
  173. lhs);
  174. }
  175. namespace int128_internal {
  176. constexpr int128 SignedSubstructResult(int128 result, int128 lhs, int128 rhs) {
  177. // check for carry
  178. return (Int128Low64(lhs) < Int128Low64(rhs))
  179. ? MakeInt128(Int128High64(result) - 1, Int128Low64(result))
  180. : result;
  181. }
  182. } // namespace int128_internal
  183. constexpr int128 operator-(int128 lhs, int128 rhs) {
  184. return int128_internal::SignedSubstructResult(
  185. MakeInt128(Int128High64(lhs) - Int128High64(rhs),
  186. Int128Low64(lhs) - Int128Low64(rhs)),
  187. lhs, rhs);
  188. }
  189. inline int128 operator*(int128 lhs, int128 rhs) {
  190. return MakeInt128(
  191. int128_internal::BitCastToSigned(Uint128High64(uint128(lhs) * rhs)),
  192. Uint128Low64(uint128(lhs) * rhs));
  193. }
  194. inline int128 int128::operator++(int) {
  195. int128 tmp(*this);
  196. *this += 1;
  197. return tmp;
  198. }
  199. inline int128 int128::operator--(int) {
  200. int128 tmp(*this);
  201. *this -= 1;
  202. return tmp;
  203. }
  204. inline int128& int128::operator++() {
  205. *this += 1;
  206. return *this;
  207. }
  208. inline int128& int128::operator--() {
  209. *this -= 1;
  210. return *this;
  211. }
  212. constexpr int128 operator|(int128 lhs, int128 rhs) {
  213. return MakeInt128(Int128High64(lhs) | Int128High64(rhs),
  214. Int128Low64(lhs) | Int128Low64(rhs));
  215. }
  216. constexpr int128 operator&(int128 lhs, int128 rhs) {
  217. return MakeInt128(Int128High64(lhs) & Int128High64(rhs),
  218. Int128Low64(lhs) & Int128Low64(rhs));
  219. }
  220. constexpr int128 operator^(int128 lhs, int128 rhs) {
  221. return MakeInt128(Int128High64(lhs) ^ Int128High64(rhs),
  222. Int128Low64(lhs) ^ Int128Low64(rhs));
  223. }
  224. constexpr int128 operator<<(int128 lhs, int amount) {
  225. // int64_t shifts of >= 63 are undefined, so we need some special-casing.
  226. assert(amount >= 0 && amount < 127);
  227. if (amount <= 0) {
  228. return lhs;
  229. } else if (amount < 63) {
  230. return MakeInt128(
  231. (Int128High64(lhs) << amount) |
  232. static_cast<int64_t>(Int128Low64(lhs) >> (64 - amount)),
  233. Int128Low64(lhs) << amount);
  234. } else if (amount == 63) {
  235. return MakeInt128(((Int128High64(lhs) << 32) << 31) |
  236. static_cast<int64_t>(Int128Low64(lhs) >> 1),
  237. (Int128Low64(lhs) << 32) << 31);
  238. } else if (amount == 127) {
  239. return MakeInt128(static_cast<int64_t>(Int128Low64(lhs) << 63), 0);
  240. } else if (amount > 127) {
  241. return MakeInt128(0, 0);
  242. } else {
  243. // amount >= 64 && amount < 127
  244. return MakeInt128(static_cast<int64_t>(Int128Low64(lhs) << (amount - 64)),
  245. 0);
  246. }
  247. }
  248. constexpr int128 operator>>(int128 lhs, int amount) {
  249. // int64_t shifts of >= 63 are undefined, so we need some special-casing.
  250. assert(amount >= 0 && amount < 127);
  251. if (amount <= 0) {
  252. return lhs;
  253. } else if (amount < 63) {
  254. return MakeInt128(
  255. Int128High64(lhs) >> amount,
  256. Int128Low64(lhs) >> amount | static_cast<uint64_t>(Int128High64(lhs))
  257. << (64 - amount));
  258. } else if (amount == 63) {
  259. return MakeInt128((Int128High64(lhs) >> 32) >> 31,
  260. static_cast<uint64_t>(Int128High64(lhs) << 1) |
  261. (Int128Low64(lhs) >> 32) >> 31);
  262. } else if (amount >= 127) {
  263. return MakeInt128((Int128High64(lhs) >> 32) >> 31,
  264. static_cast<uint64_t>((Int128High64(lhs) >> 32) >> 31));
  265. } else {
  266. // amount >= 64 && amount < 127
  267. return MakeInt128(
  268. (Int128High64(lhs) >> 32) >> 31,
  269. static_cast<uint64_t>(Int128High64(lhs) >> (amount - 64)));
  270. }
  271. }