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 __llvm_libc
{
17 template <class T
> class numeric_limits
{
19 static constexpr T
max();
20 static constexpr T
min();
23 // TODO: Add numeric_limits specializations as needed for new types.
25 template <> class numeric_limits
<int> {
27 static constexpr int max() { return INT_MAX
; }
28 static constexpr int min() { return INT_MIN
; }
30 template <> class numeric_limits
<unsigned int> {
32 static constexpr unsigned int max() { return UINT_MAX
; }
33 static constexpr unsigned int min() { return 0; }
35 template <> class numeric_limits
<long> {
37 static constexpr long max() { return LONG_MAX
; }
38 static constexpr long min() { return LONG_MIN
; }
40 template <> class numeric_limits
<unsigned long> {
42 static constexpr unsigned long max() { return ULONG_MAX
; }
43 static constexpr unsigned long min() { return 0; }
45 template <> class numeric_limits
<long long> {
47 static constexpr long long max() { return LLONG_MAX
; }
48 static constexpr long long min() { return LLONG_MIN
; }
50 template <> class numeric_limits
<unsigned long long> {
52 static constexpr unsigned long long max() { return ULLONG_MAX
; }
53 static constexpr unsigned long long min() { return 0; }
55 template <> class numeric_limits
<short> {
57 static constexpr short max() { return SHRT_MAX
; }
58 static constexpr short min() { return SHRT_MIN
; }
60 template <> class numeric_limits
<unsigned short> {
62 static constexpr unsigned short max() { return USHRT_MAX
; }
63 static constexpr unsigned short min() { return 0; }
65 template <> class numeric_limits
<char> {
67 static constexpr char max() { return CHAR_MAX
; }
68 static constexpr char min() { return CHAR_MIN
; }
70 template <> class numeric_limits
<unsigned char> {
72 static constexpr unsigned char max() { return UCHAR_MAX
; }
73 static constexpr unsigned char min() { return 0; }
75 #ifdef __SIZEOF_INT128__
76 // On platform where UInt128 resolves to __uint128_t, this specialization
77 // provides the limits of UInt128.
78 template <> class numeric_limits
<__uint128_t
> {
80 static constexpr __uint128_t
max() { return ~__uint128_t(0); }
81 static constexpr __uint128_t
min() { return 0; }
86 } // namespace __llvm_libc
88 #endif // LLVM_LIBC_SRC_SUPPORT_CPP_LIMITS_H