Mstdlib-1.24.0
m_math.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2015 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_MATH_H__
25#define __M_MATH_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31
32/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
33
34__BEGIN_DECLS
35
36/*! \addtogroup m_math Math
37 * \ingroup mstdlib_base
38 *
39 * Mathmatic calculations and conversions.
40 *
41 * @{
42 */
43
44#define M_MIN(a,b) ((a)<(b)?(a):(b))
45#define M_MAX(a,b) ((a)>(b)?(a):(b))
46#define M_ABS(a) ((a)<0?((a)*-1):(a))
47#define M_CLAMP(x,l,h) M_MIN(h,M_MAX(l,x))
48
49
50/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
51
52/*! Calculate the exponent of an integer x^y (num^exp).
53 *
54 * \param[in] num Base number.
55 * \param[in] exponent Exponent.
56 *
57 * \return num ^ exp
58 */
59M_API M_uint64 M_uint64_exp(M_uint64 num, int exponent);
60
61
62/*! Round an integer with implied decimals.
63 *
64 * \param[in] num Number to round.
65 * \param[in] currentDecimals Current number of implied decimal places.
66 * \param[in] wantedDecimals Desired number of decimal places in output. Must be <= currentDecimals.
67 *
68 * \return num rounded to wantedDecimals (output will have wantedDecimals implied decimals).
69 */
70M_API M_uint64 M_uint64_prec_round(M_uint64 num, int currentDecimals, int wantedDecimals);
71
72
73/*! Round an integer value up to a given multiple.
74 *
75 * \param[in] n integer value from which to determine the next multiple.
76 * \param[in] mul integer multiple to use.
77 *
78 * \return the next value greater than or equal to n that is evenly divisible by mul.
79 */
80M_API M_uint64 M_uint64_round_up_to_nearest_multiple(M_uint64 n, M_uint64 mul);
81
82
83/*! Determine if 32bit integer is a power of two.
84 *
85 * \param[in] n integer to check
86 *
87 * \return M_TRUE if power of 2, M_FALSE otherwise
88 */
89M_API M_bool M_uint32_is_power_of_two(M_uint32 n);
90
91
92/*! Determine if 64bit integer is a power of two.
93 *
94 * \param[in] n integer to check
95 *
96 * \return M_TRUE if power of 2, M_FALSE otherwise
97 */
98M_API M_bool M_uint64_is_power_of_two(M_uint64 n);
99
100
101/*! Determine if size_t is a power of two.
102 *
103 * \param[in] n integer to check
104 *
105 * \return M_TRUE if power of 2, M_FALSE otherwise
106 */
107M_API M_bool M_size_t_is_power_of_two(size_t n);
108
109
110/*! Round a 32bit integer value up to the next power of two.
111 *
112 * \param[in] n integer value from which to determine the next power of two.
113 *
114 * \return the next value greater than or equal to n that is a power of two.
115 */
116M_API M_uint32 M_uint32_round_up_to_power_of_two(M_uint32 n);
117
118
119/*! Round a 64bit integer value up to the next power of two.
120 *
121 * \param[in] n integer value from which to determine the next power of two.
122 *
123 * \return the next value greater than or equal to n that is a power of two.
124 */
125M_API M_uint64 M_uint64_round_up_to_power_of_two(M_uint64 n);
126
127
128/*! Round a size_t value up to the next power of two.
129 *
130 * \param[in] n integer value from which to determine the next power of two.
131 *
132 * \return the next value greater than or equal to n that is a power of two.
133 */
135
136
137/*! Round a 32bit integer value down to the last power of two.
138 *
139 * \param[in] n integer value from which to determine the next power of two.
140 *
141 * \return the next value greater than or equal to n that is a power of two.
142 */
143M_API M_uint32 M_uint32_round_down_to_power_of_two(M_uint32 n);
144
145
146/*! Round a 64bit integer value down to the last power of two.
147 *
148 * \param[in] n integer value from which to determine the next power of two.
149 *
150 * \return the next value greater than or equal to n that is a power of two.
151 */
152M_API M_uint64 M_uint64_round_down_to_power_of_two(M_uint64 n);
153
154
155/*! Round a size_t value down to the last power of two.
156 *
157 * \param[in] n integer value from which to determine the next power of two.
158 *
159 * \return the next value greater than or equal to n that is a power of two.
160 */
162
163
164/*! Get the log2 of a 32bit integer. n is rounded down if not power of 2, can
165 * use M_uint32_round_up_to_power_of_two() to round n up first.
166 * \param[in] n integer value to get the log2 for
167 * \return log2 of n
168 */
169M_API M_uint8 M_uint32_log2(M_uint32 n);
170
171
172/*! Get the log2 of a 64bit integer. n is rounded down if not power of 2, can
173 * use M_uint64_round_up_to_power_of_two() to round n up first.
174 * \param[in] n integer value to get the log2 for
175 * \return log2 of n
176 */
177M_API M_uint8 M_uint64_log2(M_uint64 n);
178
179
180/*! Increase the number of bits while keeping the sign.
181 *
182 * \param[in] x Value to sign extend
183 * \param[in] num_bits The number of bits to extend from
184 * \return sign extended value
185 *
186 */
187M_API M_int64 M_sign_extend(M_uint64 x, size_t num_bits);
188
189
190/*! Count number of decimal digits in an integer.
191 *
192 * \param[in] num Number to count digits.
193 *
194 * \return number of digits in integer.
195 */
196M_API int M_uint64_count_digits(M_uint64 num);
197
198
199/*! Count number of set bits in a single byte.
200 *
201 * \param[in] x value to count bits in
202 * \return number of set (1) bits in \a x
203 */
204M_API M_uint8 M_uint8_popcount(M_uint8 x);
205
206/*! Count number of set bits in a 64bit integer.
207 *
208 * \param[in] num value to count bits in
209 * \return number of set (1) bits in \a num
210 */
211M_API M_uint8 M_uint64_popcount(M_uint64 num);
212
213
214/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
215
216/*! Floating point modulus.
217 *
218 * Splits floating point number into integer and fractional parts.
219 *
220 * \param[in] x Number to split.
221 * \param[out] iptr Integer part of number.
222 *
223 * \return Fractional part of number.
224 */
225M_API double M_math_modf(double x, double *iptr);
226
227
228/*! Floating point rounding.
229 *
230 * \param[in] x Number to round.
231 *
232 * \return Number rounded.
233 */
234M_API double M_math_round(double x);
235
236/*! @} */
237
238__END_DECLS
239
240#endif /* __M_MATH_H__ */
M_uint64 M_uint64_prec_round(M_uint64 num, int currentDecimals, int wantedDecimals)
M_uint8 M_uint32_log2(M_uint32 n)
double M_math_round(double x)
size_t M_size_t_round_up_to_power_of_two(size_t n)
M_int64 M_sign_extend(M_uint64 x, size_t num_bits)
double M_math_modf(double x, double *iptr)
M_uint64 M_uint64_round_up_to_nearest_multiple(M_uint64 n, M_uint64 mul)
M_uint32 M_uint32_round_down_to_power_of_two(M_uint32 n)
M_bool M_uint64_is_power_of_two(M_uint64 n)
M_uint8 M_uint64_log2(M_uint64 n)
size_t M_size_t_round_down_to_power_of_two(size_t n)
M_uint32 M_uint32_round_up_to_power_of_two(M_uint32 n)
M_uint64 M_uint64_exp(M_uint64 num, int exponent)
M_bool M_size_t_is_power_of_two(size_t n)
int M_uint64_count_digits(M_uint64 num)
M_uint8 M_uint8_popcount(M_uint8 x)
M_uint8 M_uint64_popcount(M_uint64 num)
M_uint64 M_uint64_round_up_to_power_of_two(M_uint64 n)
M_uint64 M_uint64_round_down_to_power_of_two(M_uint64 n)
M_bool M_uint32_is_power_of_two(M_uint32 n)