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 */ 51 #define BitSizeOf(aItem) (sizeof(aItem) * kBitsPerByte) 52 53 /** 54 * Determines number of byes to represent a given number of bits. 55 * 56 * @param[in] aBitSize The bit-size (number of bits). 57 * 58 * @returns Number of bytes to represent @p aBitSize. 59 * 60 */ 61 #define BytesForBitSize(aBitSize) static_cast<uint8_t>(((aBitSize) + (kBitsPerByte - 1)) / kBitsPerByte) 62 63 /** 64 * Provides a way to query properties of arithmetic types. 65 * 66 * There are no members if `Type` is not a supported arithmetic type. 67 * 68 */ 69 template <typename Type> struct NumericLimits 70 { 71 }; 72 73 // Specialization for different integral types. 74 75 template <> struct NumericLimits<int8_t> 76 { 77 static constexpr int8_t kMin = INT8_MIN; 78 static constexpr int8_t kMax = INT8_MAX; 79 }; 80 81 template <> struct NumericLimits<int16_t> 82 { 83 static constexpr int16_t kMin = INT16_MIN; 84 static constexpr int16_t kMax = INT16_MAX; 85 }; 86 87 template <> struct NumericLimits<int32_t> 88 { 89 static constexpr int32_t kMin = INT32_MIN; 90 static constexpr int32_t kMax = INT32_MAX; 91 }; 92 93 template <> struct NumericLimits<int64_t> 94 { 95 static constexpr int64_t kMin = INT64_MIN; 96 static constexpr int64_t kMax = INT64_MAX; 97 }; 98 99 template <> struct NumericLimits<uint8_t> 100 { 101 static constexpr uint8_t kMin = 0; 102 static constexpr uint8_t kMax = UINT8_MAX; 103 }; 104 105 template <> struct NumericLimits<uint16_t> 106 { 107 static constexpr uint16_t kMin = 0; 108 static constexpr uint16_t kMax = UINT16_MAX; 109 }; 110 111 template <> struct NumericLimits<uint32_t> 112 { 113 static constexpr uint32_t kMin = 0; 114 static constexpr uint32_t kMax = UINT32_MAX; 115 }; 116 117 template <> struct NumericLimits<uint64_t> 118 { 119 static constexpr uint64_t kMin = 0; 120 static constexpr uint64_t kMax = UINT64_MAX; 121 }; 122 123 } // namespace ot 124 125 #endif // NUMERIC_LIMITS_HPP_ 126