1 /* 2 * Arm SCP/MCP Software 3 * Copyright (c) 2015-2021, Arm Limited and Contributors. All rights reserved. 4 * 5 * SPDX-License-Identifier: BSD-3-Clause 6 */ 7 8 #ifndef FWK_MATH_H 9 #define FWK_MATH_H 10 11 #include <limits.h> 12 #include <stdint.h> 13 14 /*! 15 * \ingroup GroupLibFramework 16 * \defgroup GroupMath Math 17 * 18 * \details Math helper functions to implement several common operations in an 19 * efficient way. 20 * 21 * \{ 22 */ 23 24 /*! 25 * \brief Raise two to an exponent. 26 * 27 * \param exp The exponent to raise to. 28 * 29 * \return Two raised to \p exp. 30 * 31 * \note The type of the value returned by this macro is that of the \p exp 32 * parameter. 33 */ 34 #define fwk_math_pow2(exp) (((__typeof__(exp))1) << (exp)) 35 36 /*! 37 * \brief Count the leading zeros of an unsigned integer. 38 * 39 * \param num Operation input. The result is undefined if \p num is \c 0. 40 * 41 * \return Number of leading zeroes in the \p num parameter. 42 * 43 * \note The type of the value returned by this macro is that of the \p num 44 * parameter. 45 */ 46 #define fwk_math_clz(num) _Generic((num), \ 47 unsigned int: __builtin_clz, \ 48 unsigned long: __builtin_clzl, \ 49 unsigned long long: __builtin_clzll)(num) 50 51 /*! 52 * \brief Calculate the binary logarithm (log2) of an integer value. 53 * 54 * \param num Operation input. The result is undefined if \p num is \c 0. 55 * 56 * \return The binary logarithm of \p num. 57 * 58 * \note The type of the value returned by this macro is that of the \p num 59 * parameter. 60 * \warning If \p num is not a power of two, the result of this call is rounded 61 * down to the next whole number. 62 */ 63 #define fwk_math_log2(num) \ 64 ((sizeof(num) * CHAR_BIT) - fwk_math_clz(num) - 1) 65 66 /*! 67 * \} 68 */ 69 70 #endif /* FWK_MATH_H */ 71