1 | //===-- A self contained equivalent of std::limits --------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H |
10 | #define LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H |
11 | |
12 | #include "include/llvm-libc-macros/limits-macros.h" // CHAR_BIT |
13 | #include "src/__support/CPP/type_traits/is_integral.h" |
14 | #include "src/__support/CPP/type_traits/is_signed.h" |
15 | #include "src/__support/macros/attributes.h" // LIBC_INLINE |
16 | |
17 | namespace LIBC_NAMESPACE { |
18 | namespace cpp { |
19 | |
20 | namespace internal { |
21 | |
22 | template <typename T, T min_value, T max_value> struct integer_impl { |
23 | static_assert(cpp::is_integral_v<T>); |
24 | LIBC_INLINE static constexpr T max() { return max_value; } |
25 | LIBC_INLINE static constexpr T min() { return min_value; } |
26 | LIBC_INLINE_VAR static constexpr int digits = |
27 | CHAR_BIT * sizeof(T) - cpp::is_signed_v<T>; |
28 | }; |
29 | |
30 | } // namespace internal |
31 | |
32 | template <class T> struct numeric_limits {}; |
33 | |
34 | // TODO: Add numeric_limits specializations as needed for new types. |
35 | template <> |
36 | struct numeric_limits<short> |
37 | : public internal::integer_impl<short, SHRT_MIN, SHRT_MAX> {}; |
38 | |
39 | template <> |
40 | struct numeric_limits<unsigned short> |
41 | : public internal::integer_impl<unsigned short, 0, USHRT_MAX> {}; |
42 | |
43 | template <> |
44 | struct numeric_limits<int> |
45 | : public internal::integer_impl<int, INT_MIN, INT_MAX> {}; |
46 | |
47 | template <> |
48 | struct numeric_limits<unsigned int> |
49 | : public internal::integer_impl<unsigned int, 0, UINT_MAX> {}; |
50 | |
51 | template <> |
52 | struct numeric_limits<long> |
53 | : public internal::integer_impl<long, LONG_MIN, LONG_MAX> {}; |
54 | |
55 | template <> |
56 | struct numeric_limits<unsigned long> |
57 | : public internal::integer_impl<unsigned long, 0, ULONG_MAX> {}; |
58 | |
59 | template <> |
60 | struct numeric_limits<long long> |
61 | : public internal::integer_impl<long long, LLONG_MIN, LLONG_MAX> {}; |
62 | |
63 | template <> |
64 | struct numeric_limits<unsigned long long> |
65 | : public internal::integer_impl<unsigned long long, 0, ULLONG_MAX> {}; |
66 | |
67 | template <> |
68 | struct numeric_limits<char> |
69 | : public internal::integer_impl<char, CHAR_MIN, CHAR_MAX> {}; |
70 | |
71 | template <> |
72 | struct numeric_limits<signed char> |
73 | : public internal::integer_impl<signed char, SCHAR_MIN, SCHAR_MAX> {}; |
74 | |
75 | template <> |
76 | struct numeric_limits<unsigned char> |
77 | : public internal::integer_impl<unsigned char, 0, UCHAR_MAX> {}; |
78 | |
79 | #ifdef __SIZEOF_INT128__ |
80 | // On platform where UInt128 resolves to __uint128_t, this specialization |
81 | // provides the limits of UInt128. |
82 | template <> |
83 | struct numeric_limits<__uint128_t> |
84 | : public internal::integer_impl<__uint128_t, 0, ~__uint128_t(0)> {}; |
85 | #endif |
86 | |
87 | } // namespace cpp |
88 | } // namespace LIBC_NAMESPACE |
89 | |
90 | #endif // LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H |
91 | |