int128.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // This file is based on the uint128 implementation of protobuf at
  2. // https://github.com/protocolbuffers/protobuf/blob/1e88936fce10cf773cb72b44c6a7f48b38c7578b/src/google/protobuf/stubs/int128.h
  3. //
  4. // Protocol Buffers - Google's data interchange format
  5. // Copyright 2008 Google Inc. All rights reserved.
  6. // https://developers.google.com/protocol-buffers/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #pragma once
  34. #include <c10/macros/Export.h>
  35. #include <cstdint>
  36. #include <iosfwd>
  37. namespace c10 {
  38. struct uint128_pod;
  39. // TODO(xiaofeng): Define GOOGLE_PROTOBUF_HAS_CONSTEXPR when constexpr is
  40. // available.
  41. #ifdef GOOGLE_PROTOBUF_HAS_CONSTEXPR
  42. #define UINT128_CONSTEXPR constexpr
  43. #else
  44. #define UINT128_CONSTEXPR
  45. #endif
  46. class uint128;
  47. inline uint128& operator<<=(uint128& self, int amount);
  48. // An unsigned 128-bit integer type. Thread-compatible.
  49. class C10_API uint128 {
  50. public:
  51. UINT128_CONSTEXPR uint128(); // Sets to 0, but don't trust on this behavior.
  52. UINT128_CONSTEXPR uint128(uint64_t top, uint64_t bottom);
  53. #ifndef SWIG
  54. UINT128_CONSTEXPR uint128(int bottom);
  55. UINT128_CONSTEXPR uint128(uint32_t bottom); // Top 96 bits = 0
  56. #endif
  57. UINT128_CONSTEXPR uint128(uint64_t bottom); // hi_ = 0
  58. UINT128_CONSTEXPR uint128(const uint128_pod& val);
  59. // Trivial copy constructor, assignment operator and destructor.
  60. void Initialize(uint64_t top, uint64_t bottom);
  61. // Arithmetic operators.
  62. uint128& operator+=(const uint128& b);
  63. uint128& operator-=(const uint128& b);
  64. uint128& operator*=(const uint128& b);
  65. // Long division/modulo for uint128.
  66. uint128& operator/=(const uint128& b);
  67. uint128& operator%=(const uint128& b);
  68. uint128 operator++(int);
  69. uint128 operator--(int);
  70. // Make msvc happy with using operator<<= from DivModImpl
  71. // which is a static function, and linker complained about missing
  72. // static version of this overload
  73. friend uint128& operator<<=(uint128&, int);
  74. uint128& operator>>=(int);
  75. uint128& operator&=(const uint128& b);
  76. uint128& operator|=(const uint128& b);
  77. uint128& operator^=(const uint128& b);
  78. uint128& operator++();
  79. uint128& operator--();
  80. friend uint64_t Uint128Low64(const uint128& v);
  81. friend uint64_t Uint128High64(const uint128& v);
  82. // We add "std::" to avoid including all of port.h.
  83. C10_API friend std::ostream& operator<<(std::ostream& o, const uint128& b);
  84. private:
  85. static void DivModImpl(
  86. uint128 dividend,
  87. uint128 divisor,
  88. uint128* quotient_ret,
  89. uint128* remainder_ret);
  90. // Little-endian memory order optimizations can benefit from
  91. // having lo_ first, hi_ last.
  92. // See util/endian/endian.h and Load128/Store128 for storing a uint128.
  93. uint64_t lo_;
  94. uint64_t hi_;
  95. // Not implemented, just declared for catching automatic type conversions.
  96. uint128(uint8_t);
  97. uint128(uint16_t);
  98. uint128(float v);
  99. uint128(double v);
  100. };
  101. // This is a POD form of uint128 which can be used for static variables which
  102. // need to be operated on as uint128.
  103. struct uint128_pod {
  104. // Note: The ordering of fields is different than 'class uint128' but the
  105. // same as its 2-arg constructor. This enables more obvious initialization
  106. // of static instances, which is the primary reason for this struct in the
  107. // first place. This does not seem to defeat any optimizations wrt
  108. // operations involving this struct.
  109. uint64_t hi;
  110. uint64_t lo;
  111. };
  112. C10_API extern const uint128_pod kuint128max;
  113. // allow uint128 to be logged
  114. C10_API extern std::ostream& operator<<(std::ostream& o, const uint128& b);
  115. // Methods to access low and high pieces of 128-bit value.
  116. // Defined externally from uint128 to facilitate conversion
  117. // to native 128-bit types when compilers support them.
  118. inline uint64_t Uint128Low64(const uint128& v) {
  119. return v.lo_;
  120. }
  121. inline uint64_t Uint128High64(const uint128& v) {
  122. return v.hi_;
  123. }
  124. // TODO: perhaps it would be nice to have int128, a signed 128-bit type?
  125. // --------------------------------------------------------------------------
  126. // Implementation details follow
  127. // --------------------------------------------------------------------------
  128. inline bool operator==(const uint128& lhs, const uint128& rhs) {
  129. return (
  130. Uint128Low64(lhs) == Uint128Low64(rhs) &&
  131. Uint128High64(lhs) == Uint128High64(rhs));
  132. }
  133. inline bool operator!=(const uint128& lhs, const uint128& rhs) {
  134. return !(lhs == rhs);
  135. }
  136. inline UINT128_CONSTEXPR uint128::uint128() : lo_(0), hi_(0) {}
  137. inline UINT128_CONSTEXPR uint128::uint128(uint64_t top, uint64_t bottom)
  138. : lo_(bottom), hi_(top) {}
  139. inline UINT128_CONSTEXPR uint128::uint128(const uint128_pod& v)
  140. : lo_(v.lo), hi_(v.hi) {}
  141. inline UINT128_CONSTEXPR uint128::uint128(uint64_t bottom)
  142. : lo_(bottom), hi_(0) {}
  143. #ifndef SWIG
  144. inline UINT128_CONSTEXPR uint128::uint128(uint32_t bottom)
  145. : lo_(bottom), hi_(0) {}
  146. inline UINT128_CONSTEXPR uint128::uint128(int bottom)
  147. : lo_(bottom), hi_(static_cast<int64_t>((bottom < 0) ? -1 : 0)) {}
  148. #endif
  149. #undef UINT128_CONSTEXPR
  150. inline void uint128::Initialize(uint64_t top, uint64_t bottom) {
  151. hi_ = top;
  152. lo_ = bottom;
  153. }
  154. // Comparison operators.
  155. #define CMP128(op) \
  156. inline bool operator op(const uint128& lhs, const uint128& rhs) { \
  157. return (Uint128High64(lhs) == Uint128High64(rhs)) \
  158. ? (Uint128Low64(lhs) op Uint128Low64(rhs)) \
  159. : (Uint128High64(lhs) op Uint128High64(rhs)); \
  160. }
  161. CMP128(<)
  162. CMP128(>)
  163. CMP128(>=)
  164. CMP128(<=)
  165. #undef CMP128
  166. // Unary operators
  167. inline uint128 operator-(const uint128& val) {
  168. const uint64_t hi_flip = ~Uint128High64(val);
  169. const uint64_t lo_flip = ~Uint128Low64(val);
  170. const uint64_t lo_add = lo_flip + 1;
  171. if (lo_add < lo_flip) {
  172. return uint128(hi_flip + 1, lo_add);
  173. }
  174. return uint128(hi_flip, lo_add);
  175. }
  176. inline bool operator!(const uint128& val) {
  177. return !Uint128High64(val) && !Uint128Low64(val);
  178. }
  179. // Logical operators.
  180. inline uint128 operator~(const uint128& val) {
  181. return uint128(~Uint128High64(val), ~Uint128Low64(val));
  182. }
  183. #define LOGIC128(op) \
  184. inline uint128 operator op(const uint128& lhs, const uint128& rhs) { \
  185. return uint128( \
  186. Uint128High64(lhs) op Uint128High64(rhs), \
  187. Uint128Low64(lhs) op Uint128Low64(rhs)); \
  188. }
  189. LOGIC128(|)
  190. LOGIC128(&)
  191. LOGIC128(^)
  192. #undef LOGIC128
  193. #define LOGICASSIGN128(op) \
  194. inline uint128& uint128::operator op(const uint128 & other) { \
  195. hi_ op other.hi_; \
  196. lo_ op other.lo_; \
  197. return *this; \
  198. }
  199. LOGICASSIGN128(|=)
  200. LOGICASSIGN128(&=)
  201. LOGICASSIGN128(^=)
  202. #undef LOGICASSIGN128
  203. // Shift operators.
  204. inline uint128 operator<<(const uint128& val, int amount) {
  205. // uint64_t shifts of >= 64 are undefined, so we will need some
  206. // special-casing.
  207. if (amount < 64) {
  208. if (amount == 0) {
  209. return val;
  210. }
  211. uint64_t new_hi =
  212. (Uint128High64(val) << amount) | (Uint128Low64(val) >> (64 - amount));
  213. uint64_t new_lo = Uint128Low64(val) << amount;
  214. return uint128(new_hi, new_lo);
  215. } else if (amount < 128) {
  216. return uint128(Uint128Low64(val) << (amount - 64), 0);
  217. } else {
  218. return uint128(0, 0);
  219. }
  220. }
  221. inline uint128 operator>>(const uint128& val, int amount) {
  222. // uint64_t shifts of >= 64 are undefined, so we will need some
  223. // special-casing.
  224. if (amount < 64) {
  225. if (amount == 0) {
  226. return val;
  227. }
  228. uint64_t new_hi = Uint128High64(val) >> amount;
  229. uint64_t new_lo =
  230. (Uint128Low64(val) >> amount) | (Uint128High64(val) << (64 - amount));
  231. return uint128(new_hi, new_lo);
  232. } else if (amount < 128) {
  233. return uint128(0, Uint128High64(val) >> (amount - 64));
  234. } else {
  235. return uint128(0, 0);
  236. }
  237. }
  238. inline uint128& operator<<=(uint128& self, int amount) {
  239. // uint64_t shifts of >= 64 are undefined, so we will need some
  240. // special-casing.
  241. if (amount < 64) {
  242. if (amount != 0) {
  243. self.hi_ = (self.hi_ << amount) | (self.lo_ >> (64 - amount));
  244. self.lo_ = self.lo_ << amount;
  245. }
  246. } else if (amount < 128) {
  247. self.hi_ = self.lo_ << (amount - 64);
  248. self.lo_ = 0;
  249. } else {
  250. self.hi_ = 0;
  251. self.lo_ = 0;
  252. }
  253. return self;
  254. }
  255. inline uint128& uint128::operator>>=(int amount) {
  256. // uint64_t shifts of >= 64 are undefined, so we will need some
  257. // special-casing.
  258. if (amount < 64) {
  259. if (amount != 0) {
  260. lo_ = (lo_ >> amount) | (hi_ << (64 - amount));
  261. hi_ = hi_ >> amount;
  262. }
  263. } else if (amount < 128) {
  264. lo_ = hi_ >> (amount - 64);
  265. hi_ = 0;
  266. } else {
  267. lo_ = 0;
  268. hi_ = 0;
  269. }
  270. return *this;
  271. }
  272. inline uint128 operator+(const uint128& lhs, const uint128& rhs) {
  273. return uint128(lhs) += rhs;
  274. }
  275. inline uint128 operator-(const uint128& lhs, const uint128& rhs) {
  276. return uint128(lhs) -= rhs;
  277. }
  278. inline uint128 operator*(const uint128& lhs, const uint128& rhs) {
  279. return uint128(lhs) *= rhs;
  280. }
  281. inline uint128 operator/(const uint128& lhs, const uint128& rhs) {
  282. return uint128(lhs) /= rhs;
  283. }
  284. inline uint128 operator%(const uint128& lhs, const uint128& rhs) {
  285. return uint128(lhs) %= rhs;
  286. }
  287. inline uint128& uint128::operator+=(const uint128& b) {
  288. hi_ += b.hi_;
  289. uint64_t lolo = lo_ + b.lo_;
  290. if (lolo < lo_)
  291. ++hi_;
  292. lo_ = lolo;
  293. return *this;
  294. }
  295. inline uint128& uint128::operator-=(const uint128& b) {
  296. hi_ -= b.hi_;
  297. if (b.lo_ > lo_)
  298. --hi_;
  299. lo_ -= b.lo_;
  300. return *this;
  301. }
  302. inline uint128& uint128::operator*=(const uint128& b) {
  303. uint64_t a96 = hi_ >> 32;
  304. uint64_t a64 = hi_ & 0xffffffffu;
  305. uint64_t a32 = lo_ >> 32;
  306. uint64_t a00 = lo_ & 0xffffffffu;
  307. uint64_t b96 = b.hi_ >> 32;
  308. uint64_t b64 = b.hi_ & 0xffffffffu;
  309. uint64_t b32 = b.lo_ >> 32;
  310. uint64_t b00 = b.lo_ & 0xffffffffu;
  311. // multiply [a96 .. a00] x [b96 .. b00]
  312. // terms higher than c96 disappear off the high side
  313. // terms c96 and c64 are safe to ignore carry bit
  314. uint64_t c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96;
  315. uint64_t c64 = a64 * b00 + a32 * b32 + a00 * b64;
  316. this->hi_ = (c96 << 32) + c64;
  317. this->lo_ = 0;
  318. // add terms after this one at a time to capture carry
  319. *this += uint128(a32 * b00) << 32;
  320. *this += uint128(a00 * b32) << 32;
  321. *this += a00 * b00;
  322. return *this;
  323. }
  324. inline uint128 uint128::operator++(int) {
  325. uint128 tmp(*this);
  326. *this += 1;
  327. return tmp;
  328. }
  329. inline uint128 uint128::operator--(int) {
  330. uint128 tmp(*this);
  331. *this -= 1;
  332. return tmp;
  333. }
  334. inline uint128& uint128::operator++() {
  335. *this += 1;
  336. return *this;
  337. }
  338. inline uint128& uint128::operator--() {
  339. *this -= 1;
  340. return *this;
  341. }
  342. } // namespace c10