LCOV - code coverage report
Current view: top level - zephyr/sys - math_extras.h Coverage Total Hit
Test: new.info Lines: 92.3 % 13 12
Test Date: 2025-03-11 06:50:38

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

Generated by: LCOV version 2.0-1