1 /*
2 * Copyright (c) 2022, 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 generic number utility functions (min, max, clamp).
32 */
33
34 #ifndef NUM_UTILS_HPP_
35 #define NUM_UTILS_HPP_
36
37 #include "common/numeric_limits.hpp"
38 #include "common/type_traits.hpp"
39
40 namespace ot {
41
42 /**
43 * This template function returns the minimum of two given values.
44 *
45 * Uses `operator<` to compare the values.
46 *
47 * @tparam Type The value type.
48 *
49 * @param[in] aFirst The first value.
50 * @param[in] aSecond The second value.
51 *
52 * @returns The minimum of @p aFirst and @p aSecond.
53 *
54 */
Min(Type aFirst,Type aSecond)55 template <typename Type> Type Min(Type aFirst, Type aSecond) { return (aFirst < aSecond) ? aFirst : aSecond; }
56
57 /**
58 * This template function returns the maximum of two given values.
59 *
60 * Uses `operator<` to compare the values.
61 *
62 * @tparam Type The value type.
63 *
64 * @param[in] aFirst The first value.
65 * @param[in] aSecond The second value.
66 *
67 * @returns The maximum of @p aFirst and @p aSecond.
68 *
69 */
Max(Type aFirst,Type aSecond)70 template <typename Type> Type Max(Type aFirst, Type aSecond) { return (aFirst < aSecond) ? aSecond : aFirst; }
71
72 /**
73 * This template function returns clamped version of a given value to a given closed range [min, max].
74 *
75 * Uses `operator<` to compare the values. The behavior is undefined if the value of @p aMin is greater than @p aMax.
76 *
77 * @tparam Type The value type.
78 *
79 * @param[in] aValue The value to clamp.
80 * @param[in] aMin The minimum value.
81 * @param[in] aMax The maximum value.
82 *
83 * @returns The clamped version of @aValue to the closed range [@p aMin, @p aMax].
84 *
85 */
Clamp(Type aValue,Type aMin,Type aMax)86 template <typename Type> Type Clamp(Type aValue, Type aMin, Type aMax)
87 {
88 Type value = Max(aValue, aMin);
89
90 return Min(value, aMax);
91 }
92
93 /**
94 * This template function returns a clamped version of given integer to a `uint8_t`.
95 *
96 * If @p aValue is greater than max value of a `uint8_t`, the max value is returned.
97 *
98 * @tparam UintType The value type (MUST be `uint16_t`, `uint32_t`, or `uint64_t`).
99 *
100 * @param[in] aValue The value to clamp.
101 *
102 * @returns The clamped version of @p aValue to `uint8_t`.
103 *
104 */
ClampToUint8(UintType aValue)105 template <typename UintType> uint8_t ClampToUint8(UintType aValue)
106 {
107 static_assert(TypeTraits::IsSame<UintType, uint16_t>::kValue || TypeTraits::IsSame<UintType, uint32_t>::kValue ||
108 TypeTraits::IsSame<UintType, uint64_t>::kValue,
109 "UintType must be `uint16_t, `uint32_t`, or `uint64_t`");
110
111 return static_cast<uint8_t>(Min(aValue, static_cast<UintType>(NumericLimits<uint8_t>::kMax)));
112 }
113
114 /**
115 * This template function returns a clamped version of given integer to a `uint16_t`.
116 *
117 * If @p aValue is greater than max value of a `uint16_t`, the max value is returned.
118 *
119 * @tparam UintType The value type (MUST be `uint32_t`, or `uint64_t`).
120 *
121 * @param[in] aValue The value to clamp.
122 *
123 * @returns The clamped version of @p aValue to `uint16_t`.
124 *
125 */
ClampToUint16(UintType aValue)126 template <typename UintType> uint16_t ClampToUint16(UintType aValue)
127 {
128 static_assert(TypeTraits::IsSame<UintType, uint32_t>::kValue || TypeTraits::IsSame<UintType, uint64_t>::kValue,
129 "UintType must be `uint32_t` or `uint64_t`");
130
131 return static_cast<uint16_t>(Min(aValue, static_cast<UintType>(NumericLimits<uint16_t>::kMax)));
132 }
133
134 /**
135 * Returns a clamped version of given integer to a `int8_t`.
136 *
137 * If @p aValue is smaller than min value of a `int8_t`, the min value of `int8_t` is returned.
138 * If @p aValue is larger than max value of a `int8_t`, the max value of `int8_t` is returned.
139 *
140 * @tparam IntType The value type (MUST be `int16_t`, `int32_t`, or `int64_t`).
141 *
142 * @param[in] aValue The value to clamp.
143 *
144 * @returns The clamped version of @p aValue to `int8_t`.
145 *
146 */
ClampToInt8(IntType aValue)147 template <typename IntType> int8_t ClampToInt8(IntType aValue)
148 {
149 static_assert(TypeTraits::IsSame<IntType, int16_t>::kValue || TypeTraits::IsSame<IntType, int32_t>::kValue ||
150 TypeTraits::IsSame<IntType, int64_t>::kValue,
151 "IntType must be `int16_t, `int32_t`, or `int64_t`");
152
153 return static_cast<int8_t>(Clamp(aValue, static_cast<IntType>(NumericLimits<int8_t>::kMin),
154 static_cast<IntType>(NumericLimits<int8_t>::kMax)));
155 }
156
157 /**
158 * This template function performs a three-way comparison between two values.
159 *
160 * @tparam Type The value type.
161 *
162 * @param[in] aFirst The first value.
163 * @param[in] aSecond The second value.
164 *
165 * @retval 1 If @p aFirst > @p aSecond.
166 * @retval 0 If @p aFirst == @p aSecond.
167 * @retval -1 If @p aFirst < @p aSecond.
168 *
169 */
ThreeWayCompare(Type aFirst,Type aSecond)170 template <typename Type> int ThreeWayCompare(Type aFirst, Type aSecond)
171 {
172 return (aFirst == aSecond) ? 0 : ((aFirst > aSecond) ? 1 : -1);
173 }
174
175 /**
176 * This is template specialization of three-way comparison between two boolean values.
177 *
178 * @param[in] aFirst The first boolean value.
179 * @param[in] aSecond The second boolean value.
180 *
181 * @retval 1 If @p aFirst is true and @p aSecond is false (true > false).
182 * @retval 0 If both @p aFirst and @p aSecond are true, or both are false (they are equal).
183 * @retval -1 If @p aFirst is false and @p aSecond is true (false < true).
184 *
185 */
ThreeWayCompare(bool aFirst,bool aSecond)186 template <> inline int ThreeWayCompare(bool aFirst, bool aSecond)
187 {
188 return (aFirst == aSecond) ? 0 : (aFirst ? 1 : -1);
189 }
190
191 /**
192 * This template function divides two numbers and rounds the result to the closest integer.
193 *
194 * @tparam IntType The integer type.
195 *
196 * @param[in] aDividend The dividend value.
197 * @param[in] aDivisor The divisor value.
198 *
199 * @return The result of division and rounding to the closest integer.
200 *
201 */
DivideAndRoundToClosest(IntType aDividend,IntType aDivisor)202 template <typename IntType> inline IntType DivideAndRoundToClosest(IntType aDividend, IntType aDivisor)
203 {
204 return (aDividend + (aDivisor / 2)) / aDivisor;
205 }
206
207 /**
208 * Casts a given `uint32_t` to `unsigned long`.
209 *
210 * @param[in] aUint32 A `uint32_t` value.
211 *
212 * @returns The @p aUint32 value as `unsigned long`.
213 *
214 */
ToUlong(uint32_t aUint32)215 inline unsigned long ToUlong(uint32_t aUint32) { return static_cast<unsigned long>(aUint32); }
216
217 /**
218 * Counts the number of `1` bits in the binary representation of a given unsigned int bit-mask value.
219 *
220 * @tparam UintType The unsigned int type (MUST be `uint8_t`, uint16_t`, uint32_t`, or `uint64_t`).
221 *
222 * @param[in] aMask A bit mask.
223 *
224 * @returns The number of `1` bits in @p aMask.
225 *
226 */
CountBitsInMask(UintType aMask)227 template <typename UintType> uint8_t CountBitsInMask(UintType aMask)
228 {
229 static_assert(TypeTraits::IsSame<UintType, uint8_t>::kValue || TypeTraits::IsSame<UintType, uint16_t>::kValue ||
230 TypeTraits::IsSame<UintType, uint32_t>::kValue || TypeTraits::IsSame<UintType, uint64_t>::kValue,
231 "UintType must be `uint8_t`, `uint16_t`, `uint32_t`, or `uint64_t`");
232
233 uint8_t count = 0;
234
235 while (aMask != 0)
236 {
237 aMask &= aMask - 1;
238 count++;
239 }
240
241 return count;
242 }
243
244 } // namespace ot
245
246 #endif // NUM_UTILS_HPP_
247