| 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 "hdr/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 | #include "src/__support/macros/config.h" |
| 17 | #include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128 |
| 18 | |
| 19 | namespace LIBC_NAMESPACE_DECL { |
| 20 | namespace cpp { |
| 21 | |
| 22 | namespace internal { |
| 23 | |
| 24 | template <typename T, T min_value, T max_value> struct integer_impl { |
| 25 | static_assert(cpp::is_integral_v<T>); |
| 26 | LIBC_INLINE static constexpr T max() { return max_value; } |
| 27 | LIBC_INLINE static constexpr T min() { return min_value; } |
| 28 | LIBC_INLINE_VAR static constexpr int digits = |
| 29 | CHAR_BIT * sizeof(T) - cpp::is_signed_v<T>; |
| 30 | }; |
| 31 | |
| 32 | } // namespace internal |
| 33 | |
| 34 | template <class T> struct numeric_limits {}; |
| 35 | |
| 36 | // TODO: Add numeric_limits specializations as needed for new types. |
| 37 | template <> |
| 38 | struct numeric_limits<short> |
| 39 | : public internal::integer_impl<short, SHRT_MIN, SHRT_MAX> {}; |
| 40 | |
| 41 | template <> |
| 42 | struct numeric_limits<unsigned short> |
| 43 | : public internal::integer_impl<unsigned short, 0, USHRT_MAX> {}; |
| 44 | |
| 45 | template <> |
| 46 | struct numeric_limits<int> |
| 47 | : public internal::integer_impl<int, INT_MIN, INT_MAX> {}; |
| 48 | |
| 49 | template <> |
| 50 | struct numeric_limits<unsigned int> |
| 51 | : public internal::integer_impl<unsigned int, 0, UINT_MAX> {}; |
| 52 | |
| 53 | template <> |
| 54 | struct numeric_limits<long> |
| 55 | : public internal::integer_impl<long, LONG_MIN, LONG_MAX> {}; |
| 56 | |
| 57 | template <> |
| 58 | struct numeric_limits<unsigned long> |
| 59 | : public internal::integer_impl<unsigned long, 0, ULONG_MAX> {}; |
| 60 | |
| 61 | template <> |
| 62 | struct numeric_limits<long long> |
| 63 | : public internal::integer_impl<long long, LLONG_MIN, LLONG_MAX> {}; |
| 64 | |
| 65 | template <> |
| 66 | struct numeric_limits<unsigned long long> |
| 67 | : public internal::integer_impl<unsigned long long, 0, ULLONG_MAX> {}; |
| 68 | |
| 69 | template <> |
| 70 | struct numeric_limits<char> |
| 71 | : public internal::integer_impl<char, CHAR_MIN, CHAR_MAX> {}; |
| 72 | |
| 73 | template <> |
| 74 | struct numeric_limits<signed char> |
| 75 | : public internal::integer_impl<signed char, SCHAR_MIN, SCHAR_MAX> {}; |
| 76 | |
| 77 | template <> |
| 78 | struct numeric_limits<unsigned char> |
| 79 | : public internal::integer_impl<unsigned char, 0, UCHAR_MAX> {}; |
| 80 | |
| 81 | #ifdef LIBC_TYPES_HAS_INT128 |
| 82 | // On platform where UInt128 resolves to __uint128_t, this specialization |
| 83 | // provides the limits of UInt128. |
| 84 | template <> |
| 85 | struct numeric_limits<__uint128_t> |
| 86 | : public internal::integer_impl<__uint128_t, 0, ~__uint128_t(0)> {}; |
| 87 | #endif |
| 88 | |
| 89 | } // namespace cpp |
| 90 | } // namespace LIBC_NAMESPACE_DECL |
| 91 | |
| 92 | #endif // LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H |
| 93 | |