summaryrefslogtreecommitdiff
path: root/libc/src/__support/CPP/limits.h
blob: 225bf6ee7770d45f327f8a0bf05ab063a3026e16 (plain)
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
//===-- A self contained equivalent of std::limits --------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_LIMITS_H
#define LLVM_LIBC_SRC_SUPPORT_CPP_LIMITS_H

#include <limits.h>

namespace __llvm_libc {
namespace cpp {

template <class T> class numeric_limits {
public:
  static constexpr T max();
  static constexpr T min();
};

// TODO: Add numeric_limits specializations as needed for new types.

template <> class numeric_limits<int> {
public:
  static constexpr int max() { return INT_MAX; }
  static constexpr int min() { return INT_MIN; }
};
template <> class numeric_limits<unsigned int> {
public:
  static constexpr unsigned int max() { return UINT_MAX; }
  static constexpr unsigned int min() { return 0; }
};
template <> class numeric_limits<long> {
public:
  static constexpr long max() { return LONG_MAX; }
  static constexpr long min() { return LONG_MIN; }
};
template <> class numeric_limits<unsigned long> {
public:
  static constexpr unsigned long max() { return ULONG_MAX; }
  static constexpr unsigned long min() { return 0; }
};
template <> class numeric_limits<long long> {
public:
  static constexpr long long max() { return LLONG_MAX; }
  static constexpr long long min() { return LLONG_MIN; }
};
template <> class numeric_limits<unsigned long long> {
public:
  static constexpr unsigned long long max() { return ULLONG_MAX; }
  static constexpr unsigned long long min() { return 0; }
};
template <> class numeric_limits<short> {
public:
  static constexpr short max() { return SHRT_MAX; }
  static constexpr short min() { return SHRT_MIN; }
};
template <> class numeric_limits<unsigned short> {
public:
  static constexpr unsigned short max() { return USHRT_MAX; }
  static constexpr unsigned short min() { return 0; }
};
template <> class numeric_limits<char> {
public:
  static constexpr char max() { return CHAR_MAX; }
  static constexpr char min() { return CHAR_MIN; }
};
template <> class numeric_limits<unsigned char> {
public:
  static constexpr unsigned char max() { return UCHAR_MAX; }
  static constexpr unsigned char min() { return 0; }
};
#ifdef __SIZEOF_INT128__
// On platform where UInt128 resolves to __uint128_t, this specialization
// provides the limits of UInt128.
template <> class numeric_limits<__uint128_t> {
public:
  static constexpr __uint128_t max() { return ~__uint128_t(0); }
  static constexpr __uint128_t min() { return 0; }
};
#endif

} // namespace cpp
} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_SUPPORT_CPP_LIMITS_H