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_UTILS_CPP_LIMITS_H 10 #define LLVM_LIBC_UTILS_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: max()27 static constexpr int max() { return INT_MAX; } min()28 static constexpr int min() { return INT_MIN; } 29 }; 30 template <> class NumericLimits<unsigned int> { 31 public: max()32 static constexpr unsigned int max() { return UINT_MAX; } min()33 static constexpr unsigned int min() { return 0; } 34 }; 35 template <> class NumericLimits<long> { 36 public: max()37 static constexpr long max() { return LONG_MAX; } min()38 static constexpr long min() { return LONG_MIN; } 39 }; 40 template <> class NumericLimits<unsigned long> { 41 public: max()42 static constexpr unsigned long max() { return ULONG_MAX; } min()43 static constexpr unsigned long min() { return 0; } 44 }; 45 template <> class NumericLimits<long long> { 46 public: max()47 static constexpr long long max() { return LLONG_MAX; } min()48 static constexpr long long min() { return LLONG_MIN; } 49 }; 50 template <> class NumericLimits<unsigned long long> { 51 public: max()52 static constexpr unsigned long long max() { return ULLONG_MAX; } min()53 static constexpr unsigned long long min() { return 0; } 54 }; 55 template <> class NumericLimits<__uint128_t> { 56 public: max()57 static constexpr __uint128_t max() { return ~__uint128_t(0); } min()58 static constexpr __uint128_t min() { return 0; } 59 }; 60 template <> class NumericLimits<__int128_t> { 61 public: max()62 static constexpr __int128_t max() { return ~__uint128_t(0) >> 1; } min()63 static constexpr __int128_t min() { return __int128_t(1) << 127; } 64 }; 65 66 } // namespace cpp 67 } // namespace __llvm_libc 68 69 #endif // LLVM_LIBC_UTILS_CPP_LIMITS_H 70