int128.h 12 KB

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