int128.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <iosfwd>
  36. namespace c10 {
  37. struct uint128_pod;
  38. // TODO(xiaofeng): Define GOOGLE_PROTOBUF_HAS_CONSTEXPR when constexpr is
  39. // available.
  40. #ifdef GOOGLE_PROTOBUF_HAS_CONSTEXPR
  41. #define UINT128_CONSTEXPR constexpr
  42. #else
  43. #define UINT128_CONSTEXPR
  44. #endif
  45. class uint128;
  46. static inline uint128& operator<<=(uint128& self, int amount);
  47. // An unsigned 128-bit integer type. Thread-compatible.
  48. class C10_API uint128 {
  49. public:
  50. UINT128_CONSTEXPR uint128(); // Sets to 0, but don't trust on this behavior.
  51. UINT128_CONSTEXPR uint128(uint64_t top, uint64_t bottom);
  52. #ifndef SWIG
  53. UINT128_CONSTEXPR uint128(int bottom);
  54. UINT128_CONSTEXPR uint128(uint32_t bottom); // Top 96 bits = 0
  55. #endif
  56. UINT128_CONSTEXPR uint128(uint64_t bottom); // hi_ = 0
  57. UINT128_CONSTEXPR uint128(const uint128_pod& val);
  58. // Trivial copy constructor, assignment operator and destructor.
  59. void Initialize(uint64_t top, uint64_t bottom);
  60. // Arithmetic operators.
  61. uint128& operator+=(const uint128& b);
  62. uint128& operator-=(const uint128& b);
  63. uint128& operator*=(const uint128& b);
  64. // Long division/modulo for uint128.
  65. uint128& operator/=(const uint128& b);
  66. uint128& operator%=(const uint128& b);
  67. uint128 operator++(int);
  68. uint128 operator--(int);
  69. // Make msvc happy with using operator<<= from DivModImpl
  70. // which is a static function, and linker complained about missing
  71. // static version of this overload
  72. friend uint128& operator<<=(uint128&, int);
  73. uint128& operator>>=(int);
  74. uint128& operator&=(const uint128& b);
  75. uint128& operator|=(const uint128& b);
  76. uint128& operator^=(const uint128& b);
  77. uint128& operator++();
  78. uint128& operator--();
  79. friend uint64_t Uint128Low64(const uint128& v);
  80. friend uint64_t Uint128High64(const uint128& v);
  81. // We add "std::" to avoid including all of port.h.
  82. C10_API friend std::ostream& operator<<(std::ostream& o, const uint128& b);
  83. private:
  84. static void DivModImpl(
  85. uint128 dividend,
  86. uint128 divisor,
  87. uint128* quotient_ret,
  88. uint128* remainder_ret);
  89. // Little-endian memory order optimizations can benefit from
  90. // having lo_ first, hi_ last.
  91. // See util/endian/endian.h and Load128/Store128 for storing a uint128.
  92. uint64_t lo_;
  93. uint64_t hi_;
  94. // Not implemented, just declared for catching automatic type conversions.
  95. uint128(uint8_t);
  96. uint128(uint16_t);
  97. uint128(float v);
  98. uint128(double v);
  99. };
  100. // This is a POD form of uint128 which can be used for static variables which
  101. // need to be operated on as uint128.
  102. struct uint128_pod {
  103. // Note: The ordering of fields is different than 'class uint128' but the
  104. // same as its 2-arg constructor. This enables more obvious initialization
  105. // of static instances, which is the primary reason for this struct in the
  106. // first place. This does not seem to defeat any optimizations wrt
  107. // operations involving this struct.
  108. uint64_t hi;
  109. uint64_t lo;
  110. };
  111. C10_API extern const uint128_pod kuint128max;
  112. // allow uint128 to be logged
  113. C10_API extern std::ostream& operator<<(std::ostream& o, const uint128& b);
  114. // Methods to access low and high pieces of 128-bit value.
  115. // Defined externally from uint128 to facilitate conversion
  116. // to native 128-bit types when compilers support them.
  117. inline uint64_t Uint128Low64(const uint128& v) {
  118. return v.lo_;
  119. }
  120. inline uint64_t Uint128High64(const uint128& v) {
  121. return v.hi_;
  122. }
  123. // TODO: perhaps it would be nice to have int128, a signed 128-bit type?
  124. // --------------------------------------------------------------------------
  125. // Implementation details follow
  126. // --------------------------------------------------------------------------
  127. inline bool operator==(const uint128& lhs, const uint128& rhs) {
  128. return (
  129. Uint128Low64(lhs) == Uint128Low64(rhs) &&
  130. Uint128High64(lhs) == Uint128High64(rhs));
  131. }
  132. inline bool operator!=(const uint128& lhs, const uint128& rhs) {
  133. return !(lhs == rhs);
  134. }
  135. C10_API inline UINT128_CONSTEXPR uint128::uint128() : lo_(0), hi_(0) {}
  136. C10_API inline UINT128_CONSTEXPR uint128::uint128(uint64_t top, uint64_t bottom)
  137. : lo_(bottom), hi_(top) {}
  138. C10_API inline UINT128_CONSTEXPR uint128::uint128(const uint128_pod& v)
  139. : lo_(v.lo), hi_(v.hi) {}
  140. C10_API inline UINT128_CONSTEXPR uint128::uint128(uint64_t bottom)
  141. : lo_(bottom), hi_(0) {}
  142. #ifndef SWIG
  143. C10_API inline UINT128_CONSTEXPR uint128::uint128(uint32_t bottom)
  144. : lo_(bottom), hi_(0) {}
  145. C10_API inline UINT128_CONSTEXPR uint128::uint128(int bottom)
  146. : lo_(bottom), hi_(static_cast<int64_t>((bottom < 0) ? -1 : 0)) {}
  147. #endif
  148. #undef UINT128_CONSTEXPR
  149. C10_API inline void uint128::Initialize(uint64_t top, uint64_t bottom) {
  150. hi_ = top;
  151. lo_ = bottom;
  152. }
  153. // Comparison operators.
  154. #define CMP128(op) \
  155. inline bool operator op(const uint128& lhs, const uint128& rhs) { \
  156. return (Uint128High64(lhs) == Uint128High64(rhs)) \
  157. ? (Uint128Low64(lhs) op Uint128Low64(rhs)) \
  158. : (Uint128High64(lhs) op Uint128High64(rhs)); \
  159. }
  160. CMP128(<)
  161. CMP128(>)
  162. CMP128(>=)
  163. CMP128(<=)
  164. #undef CMP128
  165. // Unary operators
  166. inline uint128 operator-(const uint128& val) {
  167. const uint64_t hi_flip = ~Uint128High64(val);
  168. const uint64_t lo_flip = ~Uint128Low64(val);
  169. const uint64_t lo_add = lo_flip + 1;
  170. if (lo_add < lo_flip) {
  171. return uint128(hi_flip + 1, lo_add);
  172. }
  173. return uint128(hi_flip, lo_add);
  174. }
  175. inline bool operator!(const uint128& val) {
  176. return !Uint128High64(val) && !Uint128Low64(val);
  177. }
  178. // Logical operators.
  179. inline uint128 operator~(const uint128& val) {
  180. return uint128(~Uint128High64(val), ~Uint128Low64(val));
  181. }
  182. #define LOGIC128(op) \
  183. inline uint128 operator op(const uint128& lhs, const uint128& rhs) { \
  184. return uint128( \
  185. Uint128High64(lhs) op Uint128High64(rhs), \
  186. Uint128Low64(lhs) op Uint128Low64(rhs)); \
  187. }
  188. LOGIC128(|)
  189. LOGIC128(&)
  190. LOGIC128(^)
  191. #undef LOGIC128
  192. #define LOGICASSIGN128(op) \
  193. C10_API inline uint128& uint128::operator op(const uint128& other) { \
  194. hi_ op other.hi_; \
  195. lo_ op other.lo_; \
  196. return *this; \
  197. }
  198. LOGICASSIGN128(|=)
  199. LOGICASSIGN128(&=)
  200. LOGICASSIGN128(^=)
  201. #undef LOGICASSIGN128
  202. // Shift operators.
  203. inline uint128 operator<<(const uint128& val, int amount) {
  204. // uint64_t shifts of >= 64 are undefined, so we will need some
  205. // special-casing.
  206. if (amount < 64) {
  207. if (amount == 0) {
  208. return val;
  209. }
  210. uint64_t new_hi =
  211. (Uint128High64(val) << amount) | (Uint128Low64(val) >> (64 - amount));
  212. uint64_t new_lo = Uint128Low64(val) << amount;
  213. return uint128(new_hi, new_lo);
  214. } else if (amount < 128) {
  215. return uint128(Uint128Low64(val) << (amount - 64), 0);
  216. } else {
  217. return uint128(0, 0);
  218. }
  219. }
  220. inline uint128 operator>>(const uint128& val, int amount) {
  221. // uint64_t shifts of >= 64 are undefined, so we will need some
  222. // special-casing.
  223. if (amount < 64) {
  224. if (amount == 0) {
  225. return val;
  226. }
  227. uint64_t new_hi = Uint128High64(val) >> amount;
  228. uint64_t new_lo =
  229. (Uint128Low64(val) >> amount) | (Uint128High64(val) << (64 - amount));
  230. return uint128(new_hi, new_lo);
  231. } else if (amount < 128) {
  232. return uint128(0, Uint128High64(val) >> (amount - 64));
  233. } else {
  234. return uint128(0, 0);
  235. }
  236. }
  237. static inline uint128& operator<<=(uint128& self, int amount) {
  238. // uint64_t shifts of >= 64 are undefined, so we will need some
  239. // special-casing.
  240. if (amount < 64) {
  241. if (amount != 0) {
  242. self.hi_ = (self.hi_ << amount) | (self.lo_ >> (64 - amount));
  243. self.lo_ = self.lo_ << amount;
  244. }
  245. } else if (amount < 128) {
  246. self.hi_ = self.lo_ << (amount - 64);
  247. self.lo_ = 0;
  248. } else {
  249. self.hi_ = 0;
  250. self.lo_ = 0;
  251. }
  252. return self;
  253. }
  254. C10_API inline uint128& uint128::operator>>=(int amount) {
  255. // uint64_t shifts of >= 64 are undefined, so we will need some
  256. // special-casing.
  257. if (amount < 64) {
  258. if (amount != 0) {
  259. lo_ = (lo_ >> amount) | (hi_ << (64 - amount));
  260. hi_ = hi_ >> amount;
  261. }
  262. } else if (amount < 128) {
  263. lo_ = hi_ >> (amount - 64);
  264. hi_ = 0;
  265. } else {
  266. lo_ = 0;
  267. hi_ = 0;
  268. }
  269. return *this;
  270. }
  271. inline uint128 operator+(const uint128& lhs, const uint128& rhs) {
  272. return uint128(lhs) += rhs;
  273. }
  274. inline uint128 operator-(const uint128& lhs, const uint128& rhs) {
  275. return uint128(lhs) -= rhs;
  276. }
  277. inline uint128 operator*(const uint128& lhs, const uint128& rhs) {
  278. return uint128(lhs) *= rhs;
  279. }
  280. inline uint128 operator/(const uint128& lhs, const uint128& rhs) {
  281. return uint128(lhs) /= rhs;
  282. }
  283. inline uint128 operator%(const uint128& lhs, const uint128& rhs) {
  284. return uint128(lhs) %= rhs;
  285. }
  286. C10_API inline uint128& uint128::operator+=(const uint128& b) {
  287. hi_ += b.hi_;
  288. uint64_t lolo = lo_ + b.lo_;
  289. if (lolo < lo_)
  290. ++hi_;
  291. lo_ = lolo;
  292. return *this;
  293. }
  294. C10_API inline uint128& uint128::operator-=(const uint128& b) {
  295. hi_ -= b.hi_;
  296. if (b.lo_ > lo_)
  297. --hi_;
  298. lo_ -= b.lo_;
  299. return *this;
  300. }
  301. C10_API inline uint128& uint128::operator*=(const uint128& b) {
  302. uint64_t a96 = hi_ >> 32;
  303. uint64_t a64 = hi_ & 0xffffffffu;
  304. uint64_t a32 = lo_ >> 32;
  305. uint64_t a00 = lo_ & 0xffffffffu;
  306. uint64_t b96 = b.hi_ >> 32;
  307. uint64_t b64 = b.hi_ & 0xffffffffu;
  308. uint64_t b32 = b.lo_ >> 32;
  309. uint64_t b00 = b.lo_ & 0xffffffffu;
  310. // multiply [a96 .. a00] x [b96 .. b00]
  311. // terms higher than c96 disappear off the high side
  312. // terms c96 and c64 are safe to ignore carry bit
  313. uint64_t c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96;
  314. uint64_t c64 = a64 * b00 + a32 * b32 + a00 * b64;
  315. this->hi_ = (c96 << 32) + c64;
  316. this->lo_ = 0;
  317. // add terms after this one at a time to capture carry
  318. *this += uint128(a32 * b00) << 32;
  319. *this += uint128(a00 * b32) << 32;
  320. *this += a00 * b00;
  321. return *this;
  322. }
  323. C10_API inline uint128 uint128::operator++(int) {
  324. uint128 tmp(*this);
  325. *this += 1;
  326. return tmp;
  327. }
  328. C10_API inline uint128 uint128::operator--(int) {
  329. uint128 tmp(*this);
  330. *this -= 1;
  331. return tmp;
  332. }
  333. C10_API inline uint128& uint128::operator++() {
  334. *this += 1;
  335. return *this;
  336. }
  337. C10_API inline uint128& uint128::operator--() {
  338. *this -= 1;
  339. return *this;
  340. }
  341. } // namespace c10