诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

308 lines
13 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. // All Rights Reserved.
  31. //
  32. // Author: Maxim Lifantsev
  33. //
  34. // Useful integer and floating point limits and type traits.
  35. //
  36. // This partially replaces/duplictes numeric_limits<> from <limits>.
  37. // We get a Google-style class that we have a greater control over
  38. // and thus can add new features to it or fix whatever happens to be broken in
  39. // numeric_limits for the compilers we use.
  40. //
  41. #ifndef UTIL_MATH_MATHLIMITS_H__
  42. #define UTIL_MATH_MATHLIMITS_H__
  43. // Note that for Windows we do something different because it does not support
  44. // the plain isinf and isnan.
  45. #if __cplusplus >= 201103L
  46. // GCC 4.9 has a bug that makes isinf and isnan ambigious when both <math.h>
  47. // and <cmath> get pulled into the same translation unit. We use the ones in
  48. // std:: namespace explicitly for C++11
  49. #include <cmath>
  50. #define GOOGLE_PROTOBUF_USE_STD_CMATH
  51. #elif _GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
  52. // libstdc++ <cmath> header undefines the global macros and put functions in
  53. // std:: namespace even before C++11. Use the ones in std:: instead too.
  54. #include <cmath>
  55. #define GOOGLE_PROTOBUF_USE_STD_CMATH
  56. #else
  57. #include <math.h>
  58. #endif
  59. #include <string.h>
  60. #include <cfloat>
  61. #include <google/protobuf/stubs/common.h>
  62. #include <google/protobuf/port_def.inc>
  63. // ========================================================================= //
  64. // Useful integer and floating point limits and type traits.
  65. // This is just for the documentation;
  66. // real members are defined in our specializations below.
  67. namespace google {
  68. namespace protobuf {
  69. template<typename T> struct MathLimits {
  70. // Type name.
  71. typedef T Type;
  72. // Unsigned version of the Type with the same byte size.
  73. // Same as Type for floating point and unsigned types.
  74. typedef T UnsignedType;
  75. // If the type supports negative values.
  76. static const bool kIsSigned;
  77. // If the type supports only integer values.
  78. static const bool kIsInteger;
  79. // Magnitude-wise smallest representable positive value.
  80. static const Type kPosMin;
  81. // Magnitude-wise largest representable positive value.
  82. static const Type kPosMax;
  83. // Smallest representable value.
  84. static const Type kMin;
  85. // Largest representable value.
  86. static const Type kMax;
  87. // Magnitude-wise smallest representable negative value.
  88. // Present only if kIsSigned.
  89. static const Type kNegMin;
  90. // Magnitude-wise largest representable negative value.
  91. // Present only if kIsSigned.
  92. static const Type kNegMax;
  93. // Smallest integer x such that 10^x is representable.
  94. static const int kMin10Exp;
  95. // Largest integer x such that 10^x is representable.
  96. static const int kMax10Exp;
  97. // Smallest positive value such that Type(1) + kEpsilon != Type(1)
  98. static const Type kEpsilon;
  99. // Typical rounding error that is enough to cover
  100. // a few simple floating-point operations.
  101. // Slightly larger than kEpsilon to account for a few rounding errors.
  102. // Is zero if kIsInteger.
  103. static const Type kStdError;
  104. // Number of decimal digits of mantissa precision.
  105. // Present only if !kIsInteger.
  106. static const int kPrecisionDigits;
  107. // Not a number, i.e. result of 0/0.
  108. // Present only if !kIsInteger.
  109. static const Type kNaN;
  110. // Positive infinity, i.e. result of 1/0.
  111. // Present only if !kIsInteger.
  112. static const Type kPosInf;
  113. // Negative infinity, i.e. result of -1/0.
  114. // Present only if !kIsInteger.
  115. static const Type kNegInf;
  116. // NOTE: Special floating point values behave
  117. // in a special (but mathematically-logical) way
  118. // in terms of (in)equalty comparison and mathematical operations
  119. // -- see out unittest for examples.
  120. // Special floating point value testers.
  121. // Present in integer types for convenience.
  122. static bool IsFinite(const Type x);
  123. static bool IsNaN(const Type x);
  124. static bool IsInf(const Type x);
  125. static bool IsPosInf(const Type x);
  126. static bool IsNegInf(const Type x);
  127. };
  128. // ========================================================================= //
  129. // All #define-s below are simply to refactor the declarations of
  130. // MathLimits template specializations.
  131. // They are all #undef-ined below.
  132. // The hoop-jumping in *_INT_(MAX|MIN) below is so that the compiler does not
  133. // get an overflow while computing the constants.
  134. #define SIGNED_INT_MAX(Type) \
  135. (((Type(1) << (sizeof(Type)*8 - 2)) - 1) + (Type(1) << (sizeof(Type)*8 - 2)))
  136. #define SIGNED_INT_MIN(Type) \
  137. (-(Type(1) << (sizeof(Type)*8 - 2)) - (Type(1) << (sizeof(Type)*8 - 2)))
  138. #define UNSIGNED_INT_MAX(Type) \
  139. (((Type(1) << (sizeof(Type)*8 - 1)) - 1) + (Type(1) << (sizeof(Type)*8 - 1)))
  140. // Compile-time selected log10-related constants for integer types.
  141. #define SIGNED_MAX_10_EXP(Type) \
  142. (sizeof(Type) == 1 ? 2 : ( \
  143. sizeof(Type) == 2 ? 4 : ( \
  144. sizeof(Type) == 4 ? 9 : ( \
  145. sizeof(Type) == 8 ? 18 : -1))))
  146. #define UNSIGNED_MAX_10_EXP(Type) \
  147. (sizeof(Type) == 1 ? 2 : ( \
  148. sizeof(Type) == 2 ? 4 : ( \
  149. sizeof(Type) == 4 ? 9 : ( \
  150. sizeof(Type) == 8 ? 19 : -1))))
  151. #define DECL_INT_LIMIT_FUNCS \
  152. static bool IsFinite(const Type /*x*/) { return true; } \
  153. static bool IsNaN(const Type /*x*/) { return false; } \
  154. static bool IsInf(const Type /*x*/) { return false; } \
  155. static bool IsPosInf(const Type /*x*/) { return false; } \
  156. static bool IsNegInf(const Type /*x*/) { return false; }
  157. #define DECL_SIGNED_INT_LIMITS(IntType, UnsignedIntType) \
  158. template <> \
  159. struct PROTOBUF_EXPORT MathLimits<IntType> { \
  160. typedef IntType Type; \
  161. typedef UnsignedIntType UnsignedType; \
  162. static const bool kIsSigned = true; \
  163. static const bool kIsInteger = true; \
  164. static const Type kPosMin = 1; \
  165. static const Type kPosMax = SIGNED_INT_MAX(Type); \
  166. static const Type kMin = SIGNED_INT_MIN(Type); \
  167. static const Type kMax = kPosMax; \
  168. static const Type kNegMin = -1; \
  169. static const Type kNegMax = kMin; \
  170. static const int kMin10Exp = 0; \
  171. static const int kMax10Exp = SIGNED_MAX_10_EXP(Type); \
  172. static const Type kEpsilon = 1; \
  173. static const Type kStdError = 0; \
  174. DECL_INT_LIMIT_FUNCS \
  175. };
  176. #define DECL_UNSIGNED_INT_LIMITS(IntType) \
  177. template <> \
  178. struct PROTOBUF_EXPORT MathLimits<IntType> { \
  179. typedef IntType Type; \
  180. typedef IntType UnsignedType; \
  181. static const bool kIsSigned = false; \
  182. static const bool kIsInteger = true; \
  183. static const Type kPosMin = 1; \
  184. static const Type kPosMax = UNSIGNED_INT_MAX(Type); \
  185. static const Type kMin = 0; \
  186. static const Type kMax = kPosMax; \
  187. static const int kMin10Exp = 0; \
  188. static const int kMax10Exp = UNSIGNED_MAX_10_EXP(Type); \
  189. static const Type kEpsilon = 1; \
  190. static const Type kStdError = 0; \
  191. DECL_INT_LIMIT_FUNCS \
  192. };
  193. DECL_SIGNED_INT_LIMITS(signed char, unsigned char)
  194. DECL_SIGNED_INT_LIMITS(signed short int, unsigned short int)
  195. DECL_SIGNED_INT_LIMITS(signed int, unsigned int)
  196. DECL_SIGNED_INT_LIMITS(signed long int, unsigned long int)
  197. DECL_SIGNED_INT_LIMITS(signed long long int, unsigned long long int)
  198. DECL_UNSIGNED_INT_LIMITS(unsigned char)
  199. DECL_UNSIGNED_INT_LIMITS(unsigned short int)
  200. DECL_UNSIGNED_INT_LIMITS(unsigned int)
  201. DECL_UNSIGNED_INT_LIMITS(unsigned long int)
  202. DECL_UNSIGNED_INT_LIMITS(unsigned long long int)
  203. #undef DECL_SIGNED_INT_LIMITS
  204. #undef DECL_UNSIGNED_INT_LIMITS
  205. #undef SIGNED_INT_MAX
  206. #undef SIGNED_INT_MIN
  207. #undef UNSIGNED_INT_MAX
  208. #undef SIGNED_MAX_10_EXP
  209. #undef UNSIGNED_MAX_10_EXP
  210. #undef DECL_INT_LIMIT_FUNCS
  211. // For non-Windows builds we use the std:: versions of isinf and isnan if they
  212. // are available; see the comment about <cmath> at the top of this file for the
  213. // details on why we need to do this.
  214. #ifdef GOOGLE_PROTOBUF_USE_STD_CMATH
  215. #define ISINF std::isinf
  216. #define ISNAN std::isnan
  217. #else
  218. #define ISINF isinf
  219. #define ISNAN isnan
  220. #endif
  221. // ========================================================================= //
  222. #if defined(_WIN32) && !defined(__MINGW32__) // Lacks built-in isnan() and isinf()
  223. #define DECL_FP_LIMIT_FUNCS \
  224. static bool IsFinite(const Type x) { return _finite(x); } \
  225. static bool IsNaN(const Type x) { return _isnan(x); } \
  226. static bool IsInf(const Type x) { return (_fpclass(x) & (_FPCLASS_NINF | _FPCLASS_PINF)) != 0; } \
  227. static bool IsPosInf(const Type x) { return _fpclass(x) == _FPCLASS_PINF; } \
  228. static bool IsNegInf(const Type x) { return _fpclass(x) == _FPCLASS_NINF; }
  229. #else
  230. #define DECL_FP_LIMIT_FUNCS \
  231. static bool IsFinite(const Type x) { return !ISINF(x) && !ISNAN(x); } \
  232. static bool IsNaN(const Type x) { return ISNAN(x); } \
  233. static bool IsInf(const Type x) { return ISINF(x); } \
  234. static bool IsPosInf(const Type x) { return ISINF(x) && x > 0; } \
  235. static bool IsNegInf(const Type x) { return ISINF(x) && x < 0; }
  236. #endif
  237. // We can't put floating-point constant values in the header here because
  238. // such constants are not considered to be primitive-type constants by gcc.
  239. // CAVEAT: Hence, they are going to be initialized only during
  240. // the global objects construction time.
  241. #define DECL_FP_LIMITS(FP_Type, PREFIX) \
  242. template <> \
  243. struct PROTOBUF_EXPORT MathLimits<FP_Type> { \
  244. typedef FP_Type Type; \
  245. typedef FP_Type UnsignedType; \
  246. static const bool kIsSigned = true; \
  247. static const bool kIsInteger = false; \
  248. static const Type kPosMin; \
  249. static const Type kPosMax; \
  250. static const Type kMin; \
  251. static const Type kMax; \
  252. static const Type kNegMin; \
  253. static const Type kNegMax; \
  254. static const int kMin10Exp = PREFIX##_MIN_10_EXP; \
  255. static const int kMax10Exp = PREFIX##_MAX_10_EXP; \
  256. static const Type kEpsilon; \
  257. static const Type kStdError; \
  258. static const int kPrecisionDigits = PREFIX##_DIG; \
  259. static const Type kNaN; \
  260. static const Type kPosInf; \
  261. static const Type kNegInf; \
  262. DECL_FP_LIMIT_FUNCS \
  263. };
  264. DECL_FP_LIMITS(float, FLT)
  265. DECL_FP_LIMITS(double, DBL)
  266. DECL_FP_LIMITS(long double, LDBL)
  267. #undef ISINF
  268. #undef ISNAN
  269. #undef DECL_FP_LIMITS
  270. #undef DECL_FP_LIMIT_FUNCS
  271. // ========================================================================= //
  272. } // namespace protobuf
  273. } // namespace google
  274. #include <google/protobuf/port_undef.inc>
  275. #endif // UTIL_MATH_MATHLIMITS_H__