Path: blob/main/contrib/llvm-project/libc/src/__support/CPP/limits.h
213799 views
//===-- A self contained equivalent of std::limits --------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H9#define LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H1011#include "hdr/limits_macros.h" // CHAR_BIT12#include "src/__support/CPP/type_traits/is_integral.h"13#include "src/__support/CPP/type_traits/is_signed.h"14#include "src/__support/macros/attributes.h" // LIBC_INLINE15#include "src/__support/macros/config.h"16#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT1281718namespace LIBC_NAMESPACE_DECL {19namespace cpp {2021namespace internal {2223template <typename T, T min_value, T max_value> struct integer_impl {24static_assert(cpp::is_integral_v<T>);25LIBC_INLINE static constexpr T max() { return max_value; }26LIBC_INLINE static constexpr T min() { return min_value; }27LIBC_INLINE_VAR static constexpr int digits =28CHAR_BIT * sizeof(T) - cpp::is_signed_v<T>;29};3031} // namespace internal3233template <class T> struct numeric_limits {};3435// TODO: Add numeric_limits specializations as needed for new types.36template <>37struct numeric_limits<short>38: public internal::integer_impl<short, SHRT_MIN, SHRT_MAX> {};3940template <>41struct numeric_limits<unsigned short>42: public internal::integer_impl<unsigned short, 0, USHRT_MAX> {};4344template <>45struct numeric_limits<int>46: public internal::integer_impl<int, INT_MIN, INT_MAX> {};4748template <>49struct numeric_limits<unsigned int>50: public internal::integer_impl<unsigned int, 0, UINT_MAX> {};5152template <>53struct numeric_limits<long>54: public internal::integer_impl<long, LONG_MIN, LONG_MAX> {};5556template <>57struct numeric_limits<unsigned long>58: public internal::integer_impl<unsigned long, 0, ULONG_MAX> {};5960template <>61struct numeric_limits<long long>62: public internal::integer_impl<long long, LLONG_MIN, LLONG_MAX> {};6364template <>65struct numeric_limits<unsigned long long>66: public internal::integer_impl<unsigned long long, 0, ULLONG_MAX> {};6768template <>69struct numeric_limits<char>70: public internal::integer_impl<char, CHAR_MIN, CHAR_MAX> {};7172template <>73struct numeric_limits<signed char>74: public internal::integer_impl<signed char, SCHAR_MIN, SCHAR_MAX> {};7576template <>77struct numeric_limits<unsigned char>78: public internal::integer_impl<unsigned char, 0, UCHAR_MAX> {};7980#ifdef LIBC_TYPES_HAS_INT12881// On platform where UInt128 resolves to __uint128_t, this specialization82// provides the limits of UInt128.83template <>84struct numeric_limits<__uint128_t>85: public internal::integer_impl<__uint128_t, 0, ~__uint128_t(0)> {};86#endif8788} // namespace cpp89} // namespace LIBC_NAMESPACE_DECL9091#endif // LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H929394