int128.h 12 KB

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