mathlimits.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include <google/protobuf/stubs/mathlimits.h>
  35. #include <google/protobuf/stubs/common.h>
  36. namespace google {
  37. namespace protobuf {
  38. // MSVC++ 2005 and older compilers think the header declaration was a
  39. // definition, and erroneously flag these as a duplicate definition.
  40. #if defined(COMPILER_MSVC) || __cpluscplus < 201103L
  41. #define DEF_COMMON_LIMITS(Type)
  42. #define DEF_UNSIGNED_INT_LIMITS(Type)
  43. #define DEF_SIGNED_INT_LIMITS(Type)
  44. #define DEF_PRECISION_LIMITS(Type)
  45. #else
  46. #define DEF_COMMON_LIMITS(Type) \
  47. const bool MathLimits<Type>::kIsSigned; \
  48. const bool MathLimits<Type>::kIsInteger; \
  49. const int MathLimits<Type>::kMin10Exp; \
  50. const int MathLimits<Type>::kMax10Exp;
  51. #define DEF_UNSIGNED_INT_LIMITS(Type) \
  52. DEF_COMMON_LIMITS(Type) \
  53. const Type MathLimits<Type>::kPosMin; \
  54. const Type MathLimits<Type>::kPosMax; \
  55. const Type MathLimits<Type>::kMin; \
  56. const Type MathLimits<Type>::kMax; \
  57. const Type MathLimits<Type>::kEpsilon; \
  58. const Type MathLimits<Type>::kStdError;
  59. #define DEF_SIGNED_INT_LIMITS(Type) \
  60. DEF_UNSIGNED_INT_LIMITS(Type) \
  61. const Type MathLimits<Type>::kNegMin; \
  62. const Type MathLimits<Type>::kNegMax;
  63. #define DEF_PRECISION_LIMITS(Type) \
  64. const int MathLimits<Type>::kPrecisionDigits;
  65. #endif // not COMPILER_MSVC
  66. // http://en.wikipedia.org/wiki/Quadruple_precision_floating-point_format#Double-double_arithmetic
  67. // With some compilers (gcc 4.6.x) on some platforms (powerpc64),
  68. // "long double" is implemented as a pair of double: "double double" format.
  69. // This causes a problem with epsilon (eps).
  70. // eps is the smallest positive number such that 1.0 + eps > 1.0
  71. //
  72. // Normal format: 1.0 + e = 1.0...01 // N-1 zeros for N fraction bits
  73. // D-D format: 1.0 + e = 1.000...0001 // epsilon can be very small
  74. //
  75. // In the normal format, 1.0 + e has to fit in one stretch of bits.
  76. // The maximum rounding error is half of eps.
  77. //
  78. // In the double-double format, 1.0 + e splits across two doubles:
  79. // 1.0 in the high double, e in the low double, and they do not have to
  80. // be contiguous. The maximum rounding error on a value close to 1.0 is
  81. // much larger than eps.
  82. //
  83. // Some code checks for errors by comparing a computed value to a golden
  84. // value +/- some multiple of the maximum rounding error. The maximum
  85. // rounding error is not available so we use eps as an approximation
  86. // instead. That fails when long double is in the double-double format.
  87. // Therefore, we define kStdError as a multiple of
  88. // max(DBL_EPSILON * DBL_EPSILON, kEpsilon) rather than a multiple of kEpsilon.
  89. #define DEF_FP_LIMITS(Type, PREFIX) \
  90. DEF_COMMON_LIMITS(Type) \
  91. const Type MathLimits<Type>::kPosMin = PREFIX##_MIN; \
  92. const Type MathLimits<Type>::kPosMax = PREFIX##_MAX; \
  93. const Type MathLimits<Type>::kMin = -MathLimits<Type>::kPosMax; \
  94. const Type MathLimits<Type>::kMax = MathLimits<Type>::kPosMax; \
  95. const Type MathLimits<Type>::kNegMin = -MathLimits<Type>::kPosMin; \
  96. const Type MathLimits<Type>::kNegMax = -MathLimits<Type>::kPosMax; \
  97. const Type MathLimits<Type>::kEpsilon = PREFIX##_EPSILON; \
  98. /* 32 is 5 bits of mantissa error; should be adequate for common errors */ \
  99. const Type MathLimits<Type>::kStdError = \
  100. 32 * (DBL_EPSILON * DBL_EPSILON > MathLimits<Type>::kEpsilon \
  101. ? DBL_EPSILON * DBL_EPSILON : MathLimits<Type>::kEpsilon); \
  102. DEF_PRECISION_LIMITS(Type) \
  103. const Type MathLimits<Type>::kNaN = HUGE_VAL - HUGE_VAL; \
  104. const Type MathLimits<Type>::kPosInf = HUGE_VAL; \
  105. const Type MathLimits<Type>::kNegInf = -HUGE_VAL;
  106. // The following are *not* casts!
  107. DEF_SIGNED_INT_LIMITS(int8)
  108. DEF_SIGNED_INT_LIMITS(int16) // NOLINT(readability/casting)
  109. DEF_SIGNED_INT_LIMITS(int32) // NOLINT(readability/casting)
  110. DEF_SIGNED_INT_LIMITS(int64) // NOLINT(readability/casting)
  111. DEF_UNSIGNED_INT_LIMITS(uint8)
  112. DEF_UNSIGNED_INT_LIMITS(uint16) // NOLINT(readability/casting)
  113. DEF_UNSIGNED_INT_LIMITS(uint32) // NOLINT(readability/casting)
  114. DEF_UNSIGNED_INT_LIMITS(uint64) // NOLINT(readability/casting)
  115. DEF_SIGNED_INT_LIMITS(long int)
  116. DEF_UNSIGNED_INT_LIMITS(unsigned long int)
  117. DEF_FP_LIMITS(float, FLT)
  118. DEF_FP_LIMITS(double, DBL)
  119. DEF_FP_LIMITS(long double, LDBL);
  120. #undef DEF_COMMON_LIMITS
  121. #undef DEF_SIGNED_INT_LIMITS
  122. #undef DEF_UNSIGNED_INT_LIMITS
  123. #undef DEF_FP_LIMITS
  124. #undef DEF_PRECISION_LIMITS
  125. } // namespace protobuf
  126. } // namespace google