mathlimits.h 11 KB

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