Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | /* * Copyright (c) 2019 Facebook. * * SPDX-License-Identifier: Apache-2.0 */ /** * @brief Extra arithmetic and bit manipulation functions. * @defgroup math_extras Math extras * @ingroup utilities * * Portable wrapper functions for a number of arithmetic and bit-counting functions that are often * provided by compiler builtins. If the compiler does not have an appropriate builtin, a portable C * implementation is used instead. * * @{ */ #ifndef ZEPHYR_INCLUDE_SYS_MATH_EXTRAS_H_ #define ZEPHYR_INCLUDE_SYS_MATH_EXTRAS_H_ #include <zephyr/types.h> #include <stdbool.h> #include <stddef.h> /** * @name Unsigned integer addition with overflow detection. * * These functions compute `a + b` and store the result in `*result`, returning * true if the operation overflowed. */ /**@{*/ /** * @brief Add two unsigned 16-bit integers. * @param a First operand. * @param b Second operand. * @param result Pointer to the result. * @return true if the operation overflowed. */ static bool u16_add_overflow(uint16_t a, uint16_t b, uint16_t *result); /** * @brief Add two unsigned 32-bit integers. * @param a First operand. * @param b Second operand. * @param result Pointer to the result. * @return true if the operation overflowed. */ static bool u32_add_overflow(uint32_t a, uint32_t b, uint32_t *result); /** * @brief Add two unsigned 64-bit integers. * @param a First operand. * @param b Second operand. * @param result Pointer to the result. * @return true if the operation overflowed. */ static bool u64_add_overflow(uint64_t a, uint64_t b, uint64_t *result); /** * @brief Add two size_t integers. * @param a First operand. * @param b Second operand. * @param result Pointer to the result. * @return true if the operation overflowed. */ static bool size_add_overflow(size_t a, size_t b, size_t *result); /**@}*/ /** * @name Unsigned integer multiplication with overflow detection. * * These functions compute `a * b` and store the result in `*result`, returning * true if the operation overflowed. */ /**@{*/ /** * @brief Multiply two unsigned 16-bit integers. * @param a First operand. * @param b Second operand. * @param result Pointer to the result. * @return true if the operation overflowed. */ static bool u16_mul_overflow(uint16_t a, uint16_t b, uint16_t *result); /** * @brief Multiply two unsigned 32-bit integers. * @param a First operand. * @param b Second operand. * @param result Pointer to the result. * @return true if the operation overflowed. */ static bool u32_mul_overflow(uint32_t a, uint32_t b, uint32_t *result); /** * @brief Multiply two unsigned 64-bit integers. * @param a First operand. * @param b Second operand. * @param result Pointer to the result. * @return true if the operation overflowed. */ static bool u64_mul_overflow(uint64_t a, uint64_t b, uint64_t *result); /** * @brief Multiply two size_t integers. * @param a First operand. * @param b Second operand. * @param result Pointer to the result. * @return true if the operation overflowed. */ static bool size_mul_overflow(size_t a, size_t b, size_t *result); /**@}*/ /** * @name Count leading zeros. * * Count the number of leading zero bits in the bitwise representation of `x`. * When `x = 0`, this is the size of `x` in bits. */ /**@{*/ /** * @brief Count the number of leading zero bits in a 32-bit integer. * @param x Integer to count leading zeros in. * @return Number of leading zero bits in `x`. */ static int u32_count_leading_zeros(uint32_t x); /** * @brief Count the number of leading zero bits in a 64-bit integer. * @param x Integer to count leading zeros in. * @return Number of leading zero bits in `x`. */ static int u64_count_leading_zeros(uint64_t x); /**@}*/ /** * @name Count trailing zeros. * * Count the number of trailing zero bits in the bitwise representation of `x`. * When `x = 0`, this is the size of `x` in bits. */ /**@{*/ /** * @brief Count the number of trailing zero bits in a 32-bit integer. * @param x Integer to count trailing zeros in. * @return Number of trailing zero bits in `x`. */ static int u32_count_trailing_zeros(uint32_t x); /** * @brief Count the number of trailing zero bits in a 64-bit integer. * @param x Integer to count trailing zeros in. * @return Number of trailing zero bits in `x`. */ static int u64_count_trailing_zeros(uint64_t x); /**@}*/ /**@}*/ #include <zephyr/sys/math_extras_impl.h> #endif /* ZEPHYR_INCLUDE_SYS_MATH_EXTRAS_H_ */ |