• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2020, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file includes definitions for numeric limits.
32  */
33 
34 #ifndef NUMERIC_LIMITS_HPP_
35 #define NUMERIC_LIMITS_HPP_
36 
37 #include <stdint.h>
38 
39 namespace ot {
40 
41 static constexpr uint8_t kBitsPerByte = 8; ///< Number of bits in a byte.
42 
43 /**
44  * Returns the bit-size (number of bits) of a given type or variable.
45  *
46  * @param[in] aItem   The item (type or variable or expression) to get the bit-size of.
47  *
48  * @returns Number of bits of @p aItem.
49  */
50 #define BitSizeOf(aItem) (sizeof(aItem) * kBitsPerByte)
51 
52 /**
53  * Determines number of byes to represent a given number of bits.
54  *
55  * @param[in] aBitSize    The bit-size (number of bits).
56  *
57  * @returns Number of bytes to represent @p aBitSize.
58  */
59 #define BytesForBitSize(aBitSize) static_cast<uint8_t>(((aBitSize) + (kBitsPerByte - 1)) / kBitsPerByte)
60 
61 /**
62  * Provides a way to query properties of arithmetic types.
63  *
64  * There are no members if `Type` is not a supported arithmetic type.
65  */
66 template <typename Type> struct NumericLimits
67 {
68 };
69 
70 // Specialization for different integral types.
71 
72 template <> struct NumericLimits<int8_t>
73 {
74     static constexpr int8_t kMin = INT8_MIN;
75     static constexpr int8_t kMax = INT8_MAX;
76 };
77 
78 template <> struct NumericLimits<int16_t>
79 {
80     static constexpr int16_t kMin = INT16_MIN;
81     static constexpr int16_t kMax = INT16_MAX;
82 };
83 
84 template <> struct NumericLimits<int32_t>
85 {
86     static constexpr int32_t kMin = INT32_MIN;
87     static constexpr int32_t kMax = INT32_MAX;
88 };
89 
90 template <> struct NumericLimits<int64_t>
91 {
92     static constexpr int64_t kMin = INT64_MIN;
93     static constexpr int64_t kMax = INT64_MAX;
94 };
95 
96 template <> struct NumericLimits<uint8_t>
97 {
98     static constexpr uint8_t kMin = 0;
99     static constexpr uint8_t kMax = UINT8_MAX;
100 };
101 
102 template <> struct NumericLimits<uint16_t>
103 {
104     static constexpr uint16_t kMin = 0;
105     static constexpr uint16_t kMax = UINT16_MAX;
106 };
107 
108 template <> struct NumericLimits<uint32_t>
109 {
110     static constexpr uint32_t kMin = 0;
111     static constexpr uint32_t kMax = UINT32_MAX;
112 };
113 
114 template <> struct NumericLimits<uint64_t>
115 {
116     static constexpr uint64_t kMin = 0;
117     static constexpr uint64_t kMax = UINT64_MAX;
118 };
119 
120 } // namespace ot
121 
122 #endif // NUMERIC_LIMITS_HPP_
123