Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 69 additions & 10 deletions src/include/sof/math/numbers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment on lines +58 to +59
*
* \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.
Expand Down Expand Up @@ -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 */
Expand Down
39 changes: 13 additions & 26 deletions src/math/numbers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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;
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
Loading