诸暨麻将添加redis
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

193 lines
6.6 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. #include <google/protobuf/stubs/int128.h>
  31. #include <iomanip>
  32. #include <ostream> // NOLINT(readability/streams)
  33. #include <sstream>
  34. #include <google/protobuf/stubs/logging.h>
  35. #include <google/protobuf/port_def.inc>
  36. namespace google {
  37. namespace protobuf {
  38. const uint128_pod kuint128max = {
  39. static_cast<uint64>(PROTOBUF_LONGLONG(0xFFFFFFFFFFFFFFFF)),
  40. static_cast<uint64>(PROTOBUF_LONGLONG(0xFFFFFFFFFFFFFFFF))
  41. };
  42. // Returns the 0-based position of the last set bit (i.e., most significant bit)
  43. // in the given uint64. The argument may not be 0.
  44. //
  45. // For example:
  46. // Given: 5 (decimal) == 101 (binary)
  47. // Returns: 2
  48. #define STEP(T, n, pos, sh) \
  49. do { \
  50. if ((n) >= (static_cast<T>(1) << (sh))) { \
  51. (n) = (n) >> (sh); \
  52. (pos) |= (sh); \
  53. } \
  54. } while (0)
  55. static inline int Fls64(uint64 n) {
  56. GOOGLE_DCHECK_NE(0, n);
  57. int pos = 0;
  58. STEP(uint64, n, pos, 0x20);
  59. uint32 n32 = n;
  60. STEP(uint32, n32, pos, 0x10);
  61. STEP(uint32, n32, pos, 0x08);
  62. STEP(uint32, n32, pos, 0x04);
  63. return pos + ((PROTOBUF_ULONGLONG(0x3333333322221100) >> (n32 << 2)) & 0x3);
  64. }
  65. #undef STEP
  66. // Like Fls64() above, but returns the 0-based position of the last set bit
  67. // (i.e., most significant bit) in the given uint128. The argument may not be 0.
  68. static inline int Fls128(uint128 n) {
  69. if (uint64 hi = Uint128High64(n)) {
  70. return Fls64(hi) + 64;
  71. }
  72. return Fls64(Uint128Low64(n));
  73. }
  74. void uint128::DivModImpl(uint128 dividend, uint128 divisor,
  75. uint128* quotient_ret, uint128* remainder_ret) {
  76. if (divisor == 0) {
  77. GOOGLE_LOG(FATAL) << "Division or mod by zero: dividend.hi=" << dividend.hi_
  78. << ", lo=" << dividend.lo_;
  79. } else if (dividend < divisor) {
  80. *quotient_ret = 0;
  81. *remainder_ret = dividend;
  82. return;
  83. } else {
  84. int dividend_bit_length = Fls128(dividend);
  85. int divisor_bit_length = Fls128(divisor);
  86. int difference = dividend_bit_length - divisor_bit_length;
  87. uint128 quotient = 0;
  88. while (difference >= 0) {
  89. quotient <<= 1;
  90. uint128 shifted_divisor = divisor << difference;
  91. if (shifted_divisor <= dividend) {
  92. dividend -= shifted_divisor;
  93. quotient += 1;
  94. }
  95. difference -= 1;
  96. }
  97. //record the final quotient and remainder
  98. *quotient_ret = quotient;
  99. *remainder_ret = dividend;
  100. }
  101. }
  102. uint128& uint128::operator/=(const uint128& divisor) {
  103. uint128 quotient = 0;
  104. uint128 remainder = 0;
  105. DivModImpl(*this, divisor, &quotient, &remainder);
  106. *this = quotient;
  107. return *this;
  108. }
  109. uint128& uint128::operator%=(const uint128& divisor) {
  110. uint128 quotient = 0;
  111. uint128 remainder = 0;
  112. DivModImpl(*this, divisor, &quotient, &remainder);
  113. *this = remainder;
  114. return *this;
  115. }
  116. std::ostream& operator<<(std::ostream& o, const uint128& b) {
  117. std::ios_base::fmtflags flags = o.flags();
  118. // Select a divisor which is the largest power of the base < 2^64.
  119. uint128 div;
  120. std::streamsize div_base_log;
  121. switch (flags & std::ios::basefield) {
  122. case std::ios::hex:
  123. div =
  124. static_cast<uint64>(PROTOBUF_ULONGLONG(0x1000000000000000)); // 16^15
  125. div_base_log = 15;
  126. break;
  127. case std::ios::oct:
  128. div = static_cast<uint64>(
  129. PROTOBUF_ULONGLONG(01000000000000000000000)); // 8^21
  130. div_base_log = 21;
  131. break;
  132. default: // std::ios::dec
  133. div = static_cast<uint64>(
  134. PROTOBUF_ULONGLONG(10000000000000000000)); // 10^19
  135. div_base_log = 19;
  136. break;
  137. }
  138. // Now piece together the uint128 representation from three chunks of
  139. // the original value, each less than "div" and therefore representable
  140. // as a uint64.
  141. std::ostringstream os;
  142. std::ios_base::fmtflags copy_mask =
  143. std::ios::basefield | std::ios::showbase | std::ios::uppercase;
  144. os.setf(flags & copy_mask, copy_mask);
  145. uint128 high = b;
  146. uint128 low;
  147. uint128::DivModImpl(high, div, &high, &low);
  148. uint128 mid;
  149. uint128::DivModImpl(high, div, &high, &mid);
  150. if (high.lo_ != 0) {
  151. os << high.lo_;
  152. os << std::noshowbase << std::setfill('0') << std::setw(div_base_log);
  153. os << mid.lo_;
  154. os << std::setw(div_base_log);
  155. } else if (mid.lo_ != 0) {
  156. os << mid.lo_;
  157. os << std::noshowbase << std::setfill('0') << std::setw(div_base_log);
  158. }
  159. os << low.lo_;
  160. std::string rep = os.str();
  161. // Add the requisite padding.
  162. std::streamsize width = o.width(0);
  163. if (width > rep.size()) {
  164. if ((flags & std::ios::adjustfield) == std::ios::left) {
  165. rep.append(width - rep.size(), o.fill());
  166. } else {
  167. rep.insert(static_cast<std::string::size_type>(0),
  168. width - rep.size(), o.fill());
  169. }
  170. }
  171. // Stream the final representation in a single "<<" call.
  172. return o << rep;
  173. }
  174. } // namespace protobuf
  175. } // namespace google