诸暨麻将添加redis
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

388 righe
12 KiB

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