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 <limits.h> 13 14 namespace __llvm_libc { 15 namespace cpp { 16 17 template <class T> class NumericLimits { 18 public: 19 static constexpr T max(); 20 static constexpr T min(); 21 }; 22 23 // TODO: Add NumericLimits specializations as needed for new types. 24 25 template <> class NumericLimits<int> { 26 public: 27 static constexpr int max() { return INT_MAX; } 28 static constexpr int min() { return INT_MIN; } 29 }; 30 template <> class NumericLimits<unsigned int> { 31 public: 32 static constexpr unsigned int max() { return UINT_MAX; } 33 static constexpr unsigned int min() { return 0; } 34 }; 35 template <> class NumericLimits<long> { 36 public: 37 static constexpr long max() { return LONG_MAX; } 38 static constexpr long min() { return LONG_MIN; } 39 }; 40 template <> class NumericLimits<unsigned long> { 41 public: 42 static constexpr unsigned long max() { return ULONG_MAX; } 43 static constexpr unsigned long min() { return 0; } 44 }; 45 template <> class NumericLimits<long long> { 46 public: 47 static constexpr long long max() { return LLONG_MAX; } 48 static constexpr long long min() { return LLONG_MIN; } 49 }; 50 template <> class NumericLimits<unsigned long long> { 51 public: 52 static constexpr unsigned long long max() { return ULLONG_MAX; } 53 static constexpr unsigned long long min() { return 0; } 54 }; 55 template <> class NumericLimits<short> { 56 public: 57 static constexpr short max() { return SHRT_MAX; } 58 static constexpr short min() { return SHRT_MIN; } 59 }; 60 template <> class NumericLimits<unsigned short> { 61 public: 62 static constexpr unsigned short max() { return USHRT_MAX; } 63 static constexpr unsigned short min() { return 0; } 64 }; 65 template <> class NumericLimits<char> { 66 public: 67 static constexpr char max() { return CHAR_MAX; } 68 static constexpr char min() { return CHAR_MIN; } 69 }; 70 template <> class NumericLimits<unsigned char> { 71 public: 72 static constexpr unsigned char max() { return UCHAR_MAX; } 73 static constexpr unsigned char min() { return 0; } 74 }; 75 #ifdef __SIZEOF_INT128__ 76 template <> class NumericLimits<__uint128_t> { 77 public: 78 static constexpr __uint128_t max() { return ~__uint128_t(0); } 79 static constexpr __uint128_t min() { return 0; } 80 }; 81 template <> class NumericLimits<__int128_t> { 82 public: 83 static constexpr __int128_t max() { return ~__uint128_t(0) >> 1; } 84 static constexpr __int128_t min() { return __int128_t(1) << 127; } 85 }; 86 #endif 87 } // namespace cpp 88 } // namespace __llvm_libc 89 90 #endif // LLVM_LIBC_SRC_SUPPORT_CPP_LIMITS_H 91