aboutsummaryrefslogtreecommitdiff
path: root/libc/src/__support/CPP/limits.h
blob: 76158b2926b5e5882087284035716b614c6b5247 (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
//===-- A self contained equivalent of <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 "hdr/limits_macros.h" // CHAR_BIT
#include "src/__support/CPP/type_traits/is_integral.h"
#include "src/__support/CPP/type_traits/is_signed.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE

namespace LIBC_NAMESPACE_DECL {
namespace cpp {

namespace internal {

template <typename T, bool is_integral> struct numeric_limits_impl {};

template <typename T> struct numeric_limits_impl<T, true> {
  LIBC_INLINE_VAR static constexpr bool is_signed = T(-1) < T(0);

  LIBC_INLINE_VAR static constexpr int digits =
      (CHAR_BIT * sizeof(T)) - is_signed;

  LIBC_INLINE static constexpr T min() {
    if constexpr (is_signed) {
      return T(T(1) << digits);
    } else {
      return 0;
    }
  }

  LIBC_INLINE static constexpr T max() {
    if constexpr (is_signed) {
      return T(T(~0) ^ min());
    } else {
      return T(~0);
    }
  }
};

} // namespace internal

template <typename T>
struct numeric_limits
    : public internal::numeric_limits_impl<T, is_integral_v<T>> {};

} // namespace cpp
} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC___SUPPORT_CPP_LIMITS_H