-
Notifications
You must be signed in to change notification settings - Fork 63
/
ulimits.h
88 lines (77 loc) · 3.91 KB
/
ulimits.h
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
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#pragma once
#include "utypes.h"
namespace ustl {
namespace {
template <typename T> struct __limits_digits { enum { value = sizeof(T)*8 };};
template <typename T> struct __limits_digits10 { enum { value = sizeof(T)*8*643/2136+1 };};
}
/// \class numeric_limits ulimits.h ustl.h
/// \brief Defines numeric limits for a type.
///
template <typename T>
struct numeric_limits {
/// Returns the minimum value for type T.
static inline constexpr T min (void) { return (T(0)); }
/// Returns the minimum value for type T.
static inline constexpr T max (void) { return (T(0)); }
static const bool is_signed = false; ///< True if the type is signed.
static const bool is_integer = false; ///< True if stores an exact value.
static const bool is_integral = false; ///< True if fixed size and cast-copyable.
static const unsigned digits = __limits_digits<T>::value; ///< Number of bits in T
static const unsigned digits10 = __limits_digits10<T>::value; ///< Maximum number of decimal digits in printed version of T
};
#ifndef DOXYGEN_SHOULD_SKIP_THIS
template <typename T>
struct numeric_limits<T*> {
static inline constexpr T* min (void) { return (NULL); }
static inline constexpr T* max (void) { return (reinterpret_cast<T*>(UINTPTR_MAX)); }
static const bool is_signed = false;
static const bool is_integer = true;
static const bool is_integral = true;
static const unsigned digits = __limits_digits<T*>::value;
static const unsigned digits10 = __limits_digits10<T*>::value;
};
#define _NUMERIC_LIMITS(type, minVal, maxVal, bSigned, bInteger, bIntegral) \
template <> \
struct numeric_limits<type> { \
static inline constexpr type min (void) { return (minVal); } \
static inline constexpr type max (void) { return (maxVal); } \
static const bool is_signed = bSigned; \
static const bool is_integer = bInteger; \
static const bool is_integral = bIntegral; \
static const unsigned digits = __limits_digits<type>::value; \
static const unsigned digits10 = __limits_digits10<type>::value; \
}
//--------------------------------------------------------------------------------------
// type min max signed integer integral
//--------------------------------------------------------------------------------------
_NUMERIC_LIMITS (bool, false, true, false, true, true);
_NUMERIC_LIMITS (char, CHAR_MIN, CHAR_MAX, true, true, true);
_NUMERIC_LIMITS (int, INT_MIN, INT_MAX, true, true, true);
_NUMERIC_LIMITS (short, SHRT_MIN, SHRT_MAX, true, true, true);
_NUMERIC_LIMITS (long, LONG_MIN, LONG_MAX, true, true, true);
#if HAVE_THREE_CHAR_TYPES
_NUMERIC_LIMITS (signed char, SCHAR_MIN, SCHAR_MAX, true, true, true);
#endif
_NUMERIC_LIMITS (unsigned char, 0, UCHAR_MAX, false, true, true);
_NUMERIC_LIMITS (unsigned int, 0, UINT_MAX, false, true, true);
_NUMERIC_LIMITS (unsigned short,0, USHRT_MAX, false, true, true);
_NUMERIC_LIMITS (unsigned long, 0, ULONG_MAX, false, true, true);
_NUMERIC_LIMITS (wchar_t, 0, WCHAR_MAX, false, true, true);
_NUMERIC_LIMITS (float, FLT_MIN, FLT_MAX, true, false, true);
_NUMERIC_LIMITS (double, DBL_MIN, DBL_MAX, true, false, true);
_NUMERIC_LIMITS (long double, LDBL_MIN, LDBL_MAX, true, false, true);
#if HAVE_LONG_LONG
_NUMERIC_LIMITS (long long, LLONG_MIN, LLONG_MAX, true, true, true);
_NUMERIC_LIMITS (unsigned long long, 0, ULLONG_MAX, false, true, true);
#endif
//--------------------------------------------------------------------------------------
#endif // DOXYGEN_SHOULD_SKIP_THIS
/// Macro for defining numeric_limits specializations
#define NUMERIC_LIMITS(type, minVal, maxVal, bSigned, bInteger, bIntegral) \
namespace ustl { _NUMERIC_LIMITS (type, minVal, maxVal, bSigned, bInteger, bIntegral); }
} // namespace ustl