• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <limits.h>
17
18#include "pw_polyfill/standard_library/namespace.h"
19
20_PW_POLYFILL_BEGIN_NAMESPACE_STD
21
22template <typename T>
23struct numeric_limits {
24  static constexpr bool is_specialized = false;
25};
26
27// Only a few of the numeric_limits methods are implemented.
28#define _PW_LIMITS_SPECIALIZATION(                               \
29    type, val_signed, val_int, min_value, max_value)             \
30  template <>                                                    \
31  struct numeric_limits<type> {                                  \
32    static constexpr bool is_specialized = true;                 \
33                                                                 \
34    static constexpr bool is_signed = (val_signed);              \
35    static constexpr bool is_integer = (val_int);                \
36                                                                 \
37    static constexpr type min() noexcept { return (min_value); } \
38    static constexpr type max() noexcept { return (max_value); } \
39  }
40
41#define _PW_INTEGRAL_LIMIT(type, sname, uname)            \
42  _PW_LIMITS_SPECIALIZATION(                              \
43      signed type, true, true, sname##_MIN, sname##_MAX); \
44  _PW_LIMITS_SPECIALIZATION(unsigned type, false, true, 0u, uname##_MAX)
45
46_PW_LIMITS_SPECIALIZATION(bool, false, true, false, true);
47_PW_LIMITS_SPECIALIZATION(char, char(-1) < char(0), true, CHAR_MIN, CHAR_MAX);
48
49_PW_INTEGRAL_LIMIT(char, SCHAR, UCHAR);
50_PW_INTEGRAL_LIMIT(short, SHRT, USHRT);
51_PW_INTEGRAL_LIMIT(int, INT, UINT);
52_PW_INTEGRAL_LIMIT(long, LONG, ULONG);
53
54#ifndef LLONG_MIN
55#define LLONG_MIN ((long long)(~0ull ^ (~0ull >> 1)))
56#define LLONG_MAX ((long long)(~0ull >> 1))
57
58#define ULLONG_MIN (0ull)
59#define ULLONG_MAX (~0ull)
60#endif  // LLONG_MIN
61
62_PW_INTEGRAL_LIMIT(long long, LLONG, ULLONG);
63
64#undef _PW_LIMITS_SPECIALIZATION
65#undef _PW_INTEGRAL_LIMIT
66
67_PW_POLYFILL_END_NAMESPACE_STD
68