1 //===-- A self contained equivalent of std::limits --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H
10 #define LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H
14 namespace LIBC_NAMESPACE
{
17 // Some older gcc distributions don't define these for 32 bit targets.
19 constexpr size_t LLONG_BIT_WIDTH
= sizeof(long long) * 8;
20 constexpr long long LLONG_MAX
= ~0LL ^ (1LL << (LLONG_BIT_WIDTH
- 1));
21 constexpr long long LLONG_MIN
= 1LL << (LLONG_BIT_WIDTH
- 1);
22 constexpr unsigned long long ULLONG_MAX
= ~0ULL;
25 template <class T
> class numeric_limits
{
27 static constexpr T
max();
28 static constexpr T
min();
31 // TODO: Add numeric_limits specializations as needed for new types.
33 template <> class numeric_limits
<int> {
35 static constexpr int max() { return INT_MAX
; }
36 static constexpr int min() { return INT_MIN
; }
38 template <> class numeric_limits
<unsigned int> {
40 static constexpr unsigned int max() { return UINT_MAX
; }
41 static constexpr unsigned int min() { return 0; }
43 template <> class numeric_limits
<long> {
45 static constexpr long max() { return LONG_MAX
; }
46 static constexpr long min() { return LONG_MIN
; }
48 template <> class numeric_limits
<unsigned long> {
50 static constexpr unsigned long max() { return ULONG_MAX
; }
51 static constexpr unsigned long min() { return 0; }
53 template <> class numeric_limits
<long long> {
55 static constexpr long long max() { return LLONG_MAX
; }
56 static constexpr long long min() { return LLONG_MIN
; }
58 template <> class numeric_limits
<unsigned long long> {
60 static constexpr unsigned long long max() { return ULLONG_MAX
; }
61 static constexpr unsigned long long min() { return 0; }
63 template <> class numeric_limits
<short> {
65 static constexpr short max() { return SHRT_MAX
; }
66 static constexpr short min() { return SHRT_MIN
; }
68 template <> class numeric_limits
<unsigned short> {
70 static constexpr unsigned short max() { return USHRT_MAX
; }
71 static constexpr unsigned short min() { return 0; }
73 template <> class numeric_limits
<char> {
75 static constexpr char max() { return CHAR_MAX
; }
76 static constexpr char min() { return CHAR_MIN
; }
78 template <> class numeric_limits
<signed char> {
80 static constexpr signed char max() { return SCHAR_MAX
; }
81 static constexpr signed char min() { return SCHAR_MIN
; }
83 template <> class numeric_limits
<unsigned char> {
85 static constexpr unsigned char max() { return UCHAR_MAX
; }
86 static constexpr unsigned char min() { return 0; }
88 #ifdef __SIZEOF_INT128__
89 // On platform where UInt128 resolves to __uint128_t, this specialization
90 // provides the limits of UInt128.
91 template <> class numeric_limits
<__uint128_t
> {
93 static constexpr __uint128_t
max() { return ~__uint128_t(0); }
94 static constexpr __uint128_t
min() { return 0; }
99 } // namespace LIBC_NAMESPACE
101 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H