mathlimits.h 11 KB

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