From 82f109318065760f66c4f0bd5679f7dcdbfa0b40 Mon Sep 17 00:00:00 2001 From: Tomasz Leman Date: Fri, 4 Dec 2026 17:38:07 +0100 Subject: [PATCH] math: numbers: use const array arguments and add Doxygen comments find_equal_int16(), find_min_int16() and find_max_abs_int32() only read their input vector. Declare it const, so callers with constant data do not have to cast the qualifier away. Existing callers are not affected, the functions are currently only called from the unit tests. Document gcd(), find_equal_int16(), find_min_int16(), find_max_abs_int32(), norm_int32() and crc32() with Doxygen comments in numbers.h, including what was not described before: the size idx[] needs and the meaning of a zero or negative max_results in find_equal_int16(), the minimum vector length of the find functions, the result of norm_int32() for -1 and the use of the base argument of crc32(). The comments in numbers.c now refer to the header. The comment of find_max_abs_int32() said that the smallest negative value is saturated. That is not what the code does: the negation of INT32_MIN is done in 32 bits and overflows. Document the value as not supported instead of changing the function here. Make the input vectors of the find_*() unit tests const, to build the tests against the new prototypes. No functional change. Closes #11590 Assisted-by: Copilot:claude-fable-5.2 Signed-off-by: Tomasz Leman --- src/include/sof/math/numbers.h | 79 ++++++++++++++++--- src/math/numbers.c | 39 +++------ .../arithmetic/test_find_equal_int16_ztest.c | 4 +- .../test_find_max_abs_int32_ztest.c | 4 +- .../arithmetic/test_find_min_int16_ztest.c | 4 +- 5 files changed, 88 insertions(+), 42 deletions(-) diff --git a/src/include/sof/math/numbers.h b/src/include/sof/math/numbers.h index a73bff4c7934..3037d1d8d2ff 100644 --- a/src/include/sof/math/numbers.h +++ b/src/include/sof/math/numbers.h @@ -51,7 +51,18 @@ /* Zephyr added gcd() in 2025/Nov to sys/util.h */ #ifndef gcd #define USE_SOF_GCD 1 -int gcd(int a, int b); /* Calculate greatest common divisor for a and b */ +/** + * \brief Calculate the greatest common divisor of two integers + * + * If one of the arguments is zero, the other one is returned as such, so + * gcd(0, 0) returns 0. Otherwise the signs of the arguments are ignored and + * the result is positive. + * + * \param[in] a first value + * \param[in] b second value + * \return greatest common divisor of a and b + */ +int gcd(int a, int b); #endif /* This is a divide function that returns ceil of the quotient. @@ -102,21 +113,69 @@ static inline void cross_product_s16(int32_t *px, int32_t *py, int32_t *pz, *pz = (int32_t)a * e - (int32_t)b * d; } -/* Find indices of equal values in a vector of integer values */ -int find_equal_int16(int16_t idx[], int16_t vec[], int n, int vec_length, - int max_results); +/** + * \brief Find the indices of the values equal to n in a vector + * + * The indices are stored to idx[] in ascending order. The search stops when + * max_results matches have been found. With max_results set to 1 only the + * first match is returned, and idx can point to a single variable. + * + * \param[out] idx indices of the matching values, with room for + * max_results entries, or for vec_length entries + * if max_results is zero or negative + * \param[in] vec vector to search from + * \param[in] n value to search for + * \param[in] vec_length number of values in vec + * \param[in] max_results maximum number of matches to return, zero or + * negative to return all matches + * \return number of matches stored to idx + */ +int find_equal_int16(int16_t idx[], const int16_t vec[], int n, int vec_length, + int max_results); -/* Return the smallest value found in a vector */ -int16_t find_min_int16(int16_t vec[], int vec_length); +/** + * \brief Find the smallest value in a vector + * + * \param[in] vec vector of values + * \param[in] vec_length number of values in vec, at least one + * \return smallest value in vec + */ +int16_t find_min_int16(const int16_t vec[], int vec_length); -/* Return the largest absolute value found in a vector */ -int32_t find_max_abs_int32(int32_t vec[], int vec_length); +/** + * \brief Find the largest absolute value in a vector + * + * \note The value INT32_MIN is not supported in vec, its negation overflows. + * + * \param[in] vec vector of values + * \param[in] vec_length number of values in vec, at least one + * \return largest absolute value in vec + */ +int32_t find_max_abs_int32(const int32_t vec[], int vec_length); -/* Count the left shift amount to normalize a 32 bit signed integer value - * without causing overflow. Input value 0 will result to 31. +/** + * \brief Count the left shift that normalizes a 32 bit signed integer + * + * The result is the number of bits the value can be shifted left without + * overflow, i.e. the number of redundant sign bits. + * + * \param[in] val value to normalize + * \return shift amount, from 0 to 31. Both 0 and -1 return 31. */ int norm_int32(int32_t val); +/** + * \brief Calculate the CRC-32 checksum of a block of data + * + * Uses the reversed polynomial 0xEDB88320. To continue the checksum of data + * that is processed in several blocks, pass the result of the previous block + * as base. + * + * \param[in] base initial checksum value, zero to start a new checksum + * \param[in] data data to calculate the checksum for + * \param[in] bytes size of the data in bytes + * \return CRC-32 checksum + */ uint32_t crc32(uint32_t base, const void *data, uint32_t bytes); /* merges two 16-bit values into a single 32-bit value */ diff --git a/src/math/numbers.c b/src/math/numbers.c index df4f822c749a..64f9ee4ca450 100644 --- a/src/math/numbers.c +++ b/src/math/numbers.c @@ -18,12 +18,9 @@ /* see numbers.h */ #ifdef USE_SOF_GCD -/* This function returns the greatest common divisor of two numbers - * If both parameters are 0, gcd(0, 0) returns 0 - * If first parameters is 0 or second parameter is 0, gcd(0, b) returns b - * and gcd(a, 0) returns a, because everything divides 0. +/* See numbers.h for the description. If one of the parameters is 0, the + * other one is returned, because everything divides 0. */ - int gcd(int a, int b) { if (a == 0) @@ -81,15 +78,9 @@ EXPORT_SYMBOL(gcd); #if CONFIG_NUMBERS_VECTOR_FIND -/* This function searches from vec[] (of length vec_length) integer values - * of n. The indexes to equal values is returned in idx[]. The function - * returns the number of found matches. The max_results should be set to - * 0 (or negative) or vec_length get all the matches. The max_result can be set - * to 1 to receive only the first match in ascending order. It avoids need - * for an array for idx. - */ -int find_equal_int16(int16_t idx[], int16_t vec[], int n, int vec_length, - int max_results) +/* See numbers.h for the description */ +int find_equal_int16(int16_t idx[], const int16_t vec[], int n, int vec_length, + int max_results) { int nresults = 0; int i; @@ -105,8 +96,8 @@ int find_equal_int16(int16_t idx[], int16_t vec[], int n, int vec_length, return nresults; } -/* Return the smallest value found in the vector */ -int16_t find_min_int16(int16_t vec[], int vec_length) +/* See numbers.h for the description */ +int16_t find_min_int16(const int16_t vec[], int vec_length) { int i; int min = vec[0]; @@ -117,10 +108,8 @@ int16_t find_min_int16(int16_t vec[], int vec_length) return min; } -/* Return the largest absolute value found in the vector. Note that - * smallest negative value need to be saturated to preset as int32_t. - */ -int32_t find_max_abs_int32(int32_t vec[], int vec_length) +/* See numbers.h for the description */ +int32_t find_max_abs_int32(const int32_t vec[], int vec_length) { int i; int64_t amax = (vec[0] > 0) ? vec[0] : -vec[0]; @@ -137,9 +126,7 @@ int32_t find_max_abs_int32(int32_t vec[], int vec_length) #if CONFIG_NUMBERS_NORM -/* Count the left shift amount to normalize a 32 bit signed integer value - * without causing overflow. Input value 0 will result to 31. - */ +/* See numbers.h for the description */ int norm_int32(int32_t val) { int c = 0; @@ -157,10 +144,10 @@ EXPORT_SYMBOL(norm_int32); #endif /* CONFIG_NORM */ -/** - * Basic CRC-32 implementation, based on pseudo-code from +/* Basic CRC-32 implementation, based on pseudo-code from * https://en.wikipedia.org/wiki/Cyclic_redundancy_check#CRC-32_algorithm - * 0xEDB88320 is the reversed polynomial representation + * 0xEDB88320 is the reversed polynomial representation. See numbers.h for + * the description. */ uint32_t crc32(uint32_t base, const void *data, uint32_t bytes) { diff --git a/test/ztest/unit/math/basic/arithmetic/test_find_equal_int16_ztest.c b/test/ztest/unit/math/basic/arithmetic/test_find_equal_int16_ztest.c index 268b031eb3e2..a5c2e0024e39 100644 --- a/test/ztest/unit/math/basic/arithmetic/test_find_equal_int16_ztest.c +++ b/test/ztest/unit/math/basic/arithmetic/test_find_equal_int16_ztest.c @@ -21,7 +21,7 @@ ZTEST(math_arithmetic_suite, test_math_numbers_find_equal_int16_matches) { int16_t r[4]; - int16_t vec[] = {5, 123, 5, 10, 123, 500, 123}; + const int16_t vec[] = {5, 123, 5, 10, 123, 500, 123}; int16_t template[] = {1, 4, 6}; int r_num = find_equal_int16(r, vec, 123, 7, 4); @@ -40,7 +40,7 @@ ZTEST(math_arithmetic_suite, test_math_numbers_find_equal_int16_matches) ZTEST(math_arithmetic_suite, test_math_numbers_find_equal_int16_no_matches) { int16_t r[4]; - int16_t vec[] = {1, 2, 3, 4, 5}; + const int16_t vec[] = {1, 2, 3, 4, 5}; int r_num = find_equal_int16(r, vec, 0, 5, 4); diff --git a/test/ztest/unit/math/basic/arithmetic/test_find_max_abs_int32_ztest.c b/test/ztest/unit/math/basic/arithmetic/test_find_max_abs_int32_ztest.c index 7b978188c9a7..a66964fde6df 100644 --- a/test/ztest/unit/math/basic/arithmetic/test_find_max_abs_int32_ztest.c +++ b/test/ztest/unit/math/basic/arithmetic/test_find_max_abs_int32_ztest.c @@ -21,7 +21,7 @@ */ ZTEST(math_arithmetic_suite, test_math_numbers_find_max_abs_int32_negative_max) { - int32_t vec[] = {-100, 99, 98, 50}; + const int32_t vec[] = {-100, 99, 98, 50}; int32_t r = find_max_abs_int32(vec, ARRAY_SIZE(vec)); zassert_equal(r, 100, "find_max_abs_int32([-100, 99, 98, 50]) should return 100"); @@ -35,7 +35,7 @@ ZTEST(math_arithmetic_suite, test_math_numbers_find_max_abs_int32_negative_max) */ ZTEST(math_arithmetic_suite, test_math_numbers_find_max_abs_int32_positive_max) { - int32_t vec[] = {-100, 99, 98, 50, 101}; + const int32_t vec[] = {-100, 99, 98, 50, 101}; int32_t r = find_max_abs_int32(vec, ARRAY_SIZE(vec)); zassert_equal(r, 101, "find_max_abs_int32([-100, 99, 98, 50, 101]) should return 101"); diff --git a/test/ztest/unit/math/basic/arithmetic/test_find_min_int16_ztest.c b/test/ztest/unit/math/basic/arithmetic/test_find_min_int16_ztest.c index ac9225081a43..267e73560d91 100644 --- a/test/ztest/unit/math/basic/arithmetic/test_find_min_int16_ztest.c +++ b/test/ztest/unit/math/basic/arithmetic/test_find_min_int16_ztest.c @@ -20,7 +20,7 @@ */ ZTEST(math_arithmetic_suite, test_math_numbers_find_min_int16_for_2_equals_2) { - int16_t vec[] = {2}; + const int16_t vec[] = {2}; int r = find_min_int16(vec, ARRAY_SIZE(vec)); zassert_equal(r, 2, "find_min_int16([2]) should return 2"); @@ -33,7 +33,7 @@ ZTEST(math_arithmetic_suite, test_math_numbers_find_min_int16_for_2_equals_2) */ ZTEST(math_arithmetic_suite, test_math_numbers_find_min_int16_for_5_2_3_4_1_equals_1) { - int16_t vec[] = {5, 2, 3, 4, 1}; + const int16_t vec[] = {5, 2, 3, 4, 1}; int r = find_min_int16(vec, ARRAY_SIZE(vec)); zassert_equal(r, 1, "find_min_int16([5, 2, 3, 4, 1]) should return 1");