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