-
Notifications
You must be signed in to change notification settings - Fork 0
/
limits
471 lines (399 loc) · 18.2 KB
/
limits
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//模板特化技巧
//通过宏定义,来处理static const 类型的变量的定义;
/*
* Copyright (c) 1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/* NOTE: This is not portable code. Parts of numeric_limits<> are
* inherently machine-dependent, and this file is written for the MIPS
* architecture and the SGI MIPSpro C++ compiler. Parts of it (in
* particular, some of the characteristics of floating-point types)
* are almost certainly incorrect for any other platform.
*/
#ifndef __SGI_CPP_LIMITS
#define __SGI_CPP_LIMITS
#include <limits.h>
#include <float.h>
#include <stl_config.h>
__STL_BEGIN_NAMESPACE
enum float_round_style {
round_indeterminate = -1,
round_toward_zero = 0,
round_to_nearest = 1,
round_toward_infinity = 2,
round_toward_neg_infinity = 3
};
enum float_denorm_style {
denorm_indeterminate = -1,
denorm_absent = 0,
denorm_present = 1
};
// The C++ standard (section 18.2.1) requires that some of the members of
// numeric_limits be static const data members that are given constant-
// initializers within the class declaration. On compilers where the
// __STL_STATIC_CONST_INIT_BUG macro is defined, it is impossible to write
// a standard-conforming numeric_limits class.
//
// There are two possible workarounds: either initialize the data
// members outside the class, or change them from data members to
// enums. Neither workaround is satisfactory: the former makes it
// impossible to use the data members in constant-expressions, and the
// latter means they have the wrong type and that it is impossible to
// take their addresses. We choose the former workaround.
#ifdef __STL_STATIC_CONST_INIT_BUG
# define __STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \//反斜杠符号"\"起到强制换行的作用
enum { __mem_name = __mem_value }
#else /* __STL_STATIC_CONST_INIT_BUG */
# define __STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
static const __mem_type __mem_name = __mem_value
#endif /* __STL_STATIC_CONST_INIT_BUG */
// Base class for all specializations of numeric_limits.
//_Numeric_limits_base类
template <class __number>
class _Numeric_limits_base {
public:
__STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);
static __number min() __STL_NOTHROW { return __number(); }
static __number max() __STL_NOTHROW { return __number(); }
__STL_DECLARE_LIMITS_MEMBER(int, digits, 0);
__STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);
__STL_DECLARE_LIMITS_MEMBER(bool, is_signed, false);
__STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
__STL_DECLARE_LIMITS_MEMBER(bool, is_exact, false);
__STL_DECLARE_LIMITS_MEMBER(int, radix, 0);
static __number epsilon() __STL_NOTHROW { return __number(); }
static __number round_error() __STL_NOTHROW { return __number(); }
__STL_DECLARE_LIMITS_MEMBER(int, min_exponent, 0);
__STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
__STL_DECLARE_LIMITS_MEMBER(int, max_exponent, 0);
__STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);
__STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, false);
__STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, false);
__STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
__STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
has_denorm,
denorm_absent);
__STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
static __number infinity() __STL_NOTHROW { return __number(); }
static __number quiet_NaN() __STL_NOTHROW { return __number(); }
static __number signaling_NaN() __STL_NOTHROW { return __number(); }
static __number denorm_min() __STL_NOTHROW { return __number(); }
__STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, false);
__STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
__STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, false);
__STL_DECLARE_LIMITS_MEMBER(bool, traps, false);
__STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
__STL_DECLARE_LIMITS_MEMBER(float_round_style,
round_style,
round_toward_zero);
};
#ifdef __STL_STATIC_CONST_INIT_BUG
# define __STL_DEFINE_NUMERIC_BASE_MEMBER(__type, __mem)
#else /* __STL_STATIC_CONST_INIT_BUG */
# define __STL_DEFINE_NUMERIC_BASE_MEMBER(__type, __mem) \
template <class __number> \
const __type _Numeric_limits_base<__number>:: __mem
#endif /* __STL_STATIC_CONST_INIT_BUG */
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, is_specialized);
__STL_DEFINE_NUMERIC_BASE_MEMBER(int, digits);
__STL_DEFINE_NUMERIC_BASE_MEMBER(int, digits10);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, is_signed);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, is_integer);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, is_exact);
__STL_DEFINE_NUMERIC_BASE_MEMBER(int, radix);
__STL_DEFINE_NUMERIC_BASE_MEMBER(int, min_exponent);
__STL_DEFINE_NUMERIC_BASE_MEMBER(int, max_exponent);
__STL_DEFINE_NUMERIC_BASE_MEMBER(int, min_exponent10);
__STL_DEFINE_NUMERIC_BASE_MEMBER(int, max_exponent10);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, has_infinity);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, has_quiet_NaN);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, has_signaling_NaN);
__STL_DEFINE_NUMERIC_BASE_MEMBER(float_denorm_style, has_denorm);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, has_denorm_loss);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, is_iec559);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, is_bounded);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, is_modulo);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, traps);
__STL_DEFINE_NUMERIC_BASE_MEMBER(bool, tinyness_before);
__STL_DEFINE_NUMERIC_BASE_MEMBER(float_round_style, round_style);
// Base class for integers.
//_Integer_limits类;
template <class _Int,
_Int __imin,
_Int __imax,
int __idigits = -1>
class _Integer_limits : public _Numeric_limits_base<_Int>
{
public:
__STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
static _Int min() __STL_NOTHROW { return __imin; }
static _Int max() __STL_NOTHROW { return __imax; }
__STL_DECLARE_LIMITS_MEMBER(int,
digits,
(__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
- (__imin == 0 ? 0 : 1)
: __idigits);
__STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000);
// log 2 = 0.301029995664...
__STL_DECLARE_LIMITS_MEMBER(bool, is_signed, __imin != 0);
__STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
__STL_DECLARE_LIMITS_MEMBER(bool, is_exact, true);
__STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
__STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
__STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
};
#ifdef __STL_STATIC_CONST_INIT_BUG
# define __STL_DEFINE_INTEGER_LIMITS_MEMBER(__type, __mem)
#else /* __STL_STATIC_CONST_INIT_BUG */
# define __STL_DEFINE_INTEGER_LIMITS_MEMBER(__type, __mem) \
template <class _Int, _Int __imin, _Int __imax, int __idigits> \
const __type _Integer_limits<_Int, __imin, __imax, __idigits>:: __mem
#endif /* __STL_STATIC_CONST_INIT_BUG */
__STL_DEFINE_INTEGER_LIMITS_MEMBER(bool, is_specialized);
__STL_DEFINE_INTEGER_LIMITS_MEMBER(int, digits);
__STL_DEFINE_INTEGER_LIMITS_MEMBER(int, digits10);
__STL_DEFINE_INTEGER_LIMITS_MEMBER(bool, is_signed);
__STL_DEFINE_INTEGER_LIMITS_MEMBER(bool, is_integer);
__STL_DEFINE_INTEGER_LIMITS_MEMBER(bool, is_exact);
__STL_DEFINE_INTEGER_LIMITS_MEMBER(int, radix);
__STL_DEFINE_INTEGER_LIMITS_MEMBER(bool, is_bounded);
__STL_DEFINE_INTEGER_LIMITS_MEMBER(bool, is_modulo);
// Base class for floating-point numbers.
//_Floating_limits类;
template <class __number,
int __Digits, int __Digits10,
int __MinExp, int __MaxExp,
int __MinExp10, int __MaxExp10,
unsigned int __InfinityWord,
unsigned int __QNaNWord, unsigned int __SNaNWord,
bool __IsIEC559,
float_round_style __RoundStyle>
class _Floating_limits : public _Numeric_limits_base<__number>
{
public:
__STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);
__STL_DECLARE_LIMITS_MEMBER(int, digits, __Digits);
__STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);
__STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);
__STL_DECLARE_LIMITS_MEMBER(int, radix, 2);
__STL_DECLARE_LIMITS_MEMBER(int, min_exponent, __MinExp);
__STL_DECLARE_LIMITS_MEMBER(int, max_exponent, __MaxExp);
__STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
__STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);
__STL_DECLARE_LIMITS_MEMBER(bool, has_infinity, true);
__STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN, true);
__STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, true);
__STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
has_denorm,
denorm_indeterminate);
__STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss, false);
static __number infinity() __STL_NOTHROW {
static const unsigned int _S_inf[sizeof(__number) / sizeof(int)] =
{ __InfinityWord };
return *reinterpret_cast<const __number*>(&_S_inf);
}
static __number quiet_NaN() __STL_NOTHROW {
static const unsigned int _S_nan[sizeof(__number) / sizeof(int)] =
{ __QNaNWord };
return *reinterpret_cast<const __number*>(&_S_nan);
}
static __number signaling_NaN() __STL_NOTHROW {
static const unsigned int _S_nan[sizeof(__number) / sizeof(int)] =
{ __SNaNWord };
return *reinterpret_cast<const __number*>(&_S_nan);
}
__STL_DECLARE_LIMITS_MEMBER(bool, is_iec559, __IsIEC559);
__STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
__STL_DECLARE_LIMITS_MEMBER(bool, traps, true);
__STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before, false);
__STL_DECLARE_LIMITS_MEMBER(float_round_style, round_style, __RoundStyle);
};
#ifdef __STL_STATIC_CONST_INIT_BUG
# define __STL_DEFINE_FLOAT_LIMITS_MEMBER(__type, __mem)
#else /* __STL_STATIC_CONST_INIT_BUG */
# define __STL_DEFINE_FLOAT_LIMITS_MEMBER(__type, __mem) \
template <class __Num, int __Dig, int __Dig10, \
int __MnX, int __MxX, int __MnX10, int __MxX10, \
unsigned int __Inf, unsigned int __QNaN, unsigned int __SNaN, \
bool __IsIEEE, float_round_style __Sty> \
const __type _Floating_limits<__Num, __Dig, __Dig10, \
__MnX, __MxX, __MnX10, __MxX10, \
__Inf, __QNaN, __SNaN,__IsIEEE, __Sty>:: __mem
#endif /* __STL_STATIC_CONST_INIT_BUG */
__STL_DEFINE_FLOAT_LIMITS_MEMBER(bool, is_specialized);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(int, digits);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(int, digits10);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(bool, is_signed);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(int, radix);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(int, min_exponent);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(int, max_exponent);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(int, min_exponent10);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(int, max_exponent10);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(bool, has_infinity);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(bool, has_quiet_NaN);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(bool, has_signaling_NaN);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(float_denorm_style, has_denorm);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(bool, has_denorm_loss);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(bool, is_iec559);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(bool, is_bounded);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(bool, traps);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(bool, tinyness_before);
__STL_DEFINE_FLOAT_LIMITS_MEMBER(float_round_style, round_style);
#undef __STL_DECLARE_NUMERIC_LIMITS_MEMBER
#undef __STL_DEFINE_NUMERIC_BASE_MEMBER
#undef __STL_DEFINE_INTEGER_LIMITS_MEMBER
#undef __STL_DEFINE_FLOAT_LIMITS_MEMBER
// Class numeric_limits
// The unspecialized class.
//numeric_limits类;
template<class T>
class numeric_limits : public _Numeric_limits_base<T> {};
// Specializations for all built-in integral types.
#ifndef __STL_NO_BOOL
//模板特化技术,针对bool,char,unsigned char,wchar_t,short,unsigned
//short,int,unsigned int,long,unsigned long,long long,unsigned long long;
__STL_TEMPLATE_NULL
class numeric_limits<bool>
: public _Integer_limits<bool, false, true, 0>
{};
#endif /* __STL_NO_BOOL */
__STL_TEMPLATE_NULL
class numeric_limits<char>
: public _Integer_limits<char, CHAR_MIN, CHAR_MAX>
{};
__STL_TEMPLATE_NULL
class numeric_limits<signed char>
: public _Integer_limits<signed char, SCHAR_MIN, SCHAR_MAX>
{};
__STL_TEMPLATE_NULL
class numeric_limits<unsigned char>
: public _Integer_limits<unsigned char, 0, UCHAR_MAX>
{};
#ifdef __STL_HAS_WCHAR_T
__STL_TEMPLATE_NULL
class numeric_limits<wchar_t>
: public _Integer_limits<wchar_t, INT_MIN, INT_MAX>
{};
#endif
__STL_TEMPLATE_NULL
class numeric_limits<short>
: public _Integer_limits<short, SHRT_MIN, SHRT_MAX>
{};
__STL_TEMPLATE_NULL
class numeric_limits<unsigned short>
: public _Integer_limits<unsigned short, 0, USHRT_MAX>
{};
__STL_TEMPLATE_NULL
class numeric_limits<int>
: public _Integer_limits<int, INT_MIN, INT_MAX>
{};
__STL_TEMPLATE_NULL
class numeric_limits<unsigned int>
: public _Integer_limits<unsigned int, 0, UINT_MAX>
{};
__STL_TEMPLATE_NULL
class numeric_limits<long>
: public _Integer_limits<long, LONG_MIN, LONG_MAX>
{};
__STL_TEMPLATE_NULL
class numeric_limits<unsigned long>
: public _Integer_limits<unsigned long, 0, ULONG_MAX>
{};
#ifdef __STL_LONG_LONG
// Some compilers have long long, but don't define the
// LONGLONG_MIN and LONGLONG_MAX macros in limits.h. This
// assumes that long long is 64 bits.
#if !defined(LONGLONG_MIN) && !defined(LONGLONG_MAX) \
&& !defined(ULONGLONG_MAX)
#define ULONGLONG_MAX 0xffffffffffffffffLLU
#define LONGLONG_MAX 0x7fffffffffffffffLL
#define LONGLONG_MIN (-LONGLONG_MAX - 1)
#endif
__STL_TEMPLATE_NULL
class numeric_limits<long long>
: public _Integer_limits<long long, LONGLONG_MIN, LONGLONG_MAX>
{};
__STL_TEMPLATE_NULL
class numeric_limits<unsigned long long>
: public _Integer_limits<unsigned long long, 0, ULONGLONG_MAX>
{};
#endif /* __STL_LONG_LONG */
// Specializations for all built-in floating-point type.
//numeric_limits<float>类;
__STL_TEMPLATE_NULL class numeric_limits<float>
: public _Floating_limits<float,
FLT_MANT_DIG, // Binary digits of precision
FLT_DIG, // Decimal digits of precision
FLT_MIN_EXP, // Minimum exponent
FLT_MAX_EXP, // Maximum exponent
FLT_MIN_10_EXP, // Minimum base 10 exponent
FLT_MAX_10_EXP, // Maximum base 10 exponent
0x7f800000u, // First word of +infinity
0x7f810000u, // First word of quiet NaN
0x7fc10000u, // First word of signaling NaN
true, // conforms to iec559
round_to_nearest>
{
public:
static float min() __STL_NOTHROW { return FLT_MIN; }
static float denorm_min() __STL_NOTHROW { return FLT_MIN; }
static float max() __STL_NOTHROW { return FLT_MAX; }
static float epsilon() __STL_NOTHROW { return FLT_EPSILON; }
static float round_error() __STL_NOTHROW { return 0.5f; } // Units: ulps.
};
//numeric_limits<double>类;
__STL_TEMPLATE_NULL class numeric_limits<double>
: public _Floating_limits<double,
DBL_MANT_DIG, // Binary digits of precision
DBL_DIG, // Decimal digits of precision
DBL_MIN_EXP, // Minimum exponent
DBL_MAX_EXP, // Maximum exponent
DBL_MIN_10_EXP, // Minimum base 10 exponent
DBL_MAX_10_EXP, // Maximum base 10 exponent
0x7ff00000u, // First word of +infinity
0x7ff10000u, // First word of quiet NaN
0x7ff90000u, // First word of signaling NaN
true, // conforms to iec559
round_to_nearest>
{
public:
static double min() __STL_NOTHROW { return DBL_MIN; }
static double denorm_min() __STL_NOTHROW { return DBL_MIN; }
static double max() __STL_NOTHROW { return DBL_MAX; }
static double epsilon() __STL_NOTHROW { return DBL_EPSILON; }
static double round_error() __STL_NOTHROW { return 0.5; } // Units: ulps.
};
__STL_TEMPLATE_NULL class numeric_limits<long double>
: public _Floating_limits<long double,
LDBL_MANT_DIG, // Binary digits of precision
LDBL_DIG, // Decimal digits of precision
LDBL_MIN_EXP, // Minimum exponent
LDBL_MAX_EXP, // Maximum exponent
LDBL_MIN_10_EXP,// Minimum base 10 exponent
LDBL_MAX_10_EXP,// Maximum base 10 exponent
0x7ff00000u, // First word of +infinity
0x7ff10000u, // First word of quiet NaN
0x7ff90000u, // First word of signaling NaN
false, // Doesn't conform to iec559
round_to_nearest>
{
public:
static long double min() __STL_NOTHROW { return LDBL_MIN; }
static long double denorm_min() __STL_NOTHROW { return LDBL_MIN; }
static long double max() __STL_NOTHROW { return LDBL_MAX; }
static long double epsilon() __STL_NOTHROW { return LDBL_EPSILON; }
static long double round_error() __STL_NOTHROW { return 4; } // Units: ulps.
};
__STL_END_NAMESPACE
#endif /* __SGI_CPP_LIMITS */
// Local Variables:
// mode:C++
// End: