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
128 changes: 94 additions & 34 deletions src/include/sof/math/trig.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@

#include <stdint.h>

#define PI_DIV2_Q4_28 421657428
#define PI_DIV2_Q3_29 843314856
#define PI_Q4_28 843314857
#define PI_MUL2_Q4_28 1686629713
#define CORDIC_31B_TABLE_SIZE 31
#define CORDIC_15B_TABLE_SIZE 15
#define CORDIC_30B_ITABLE_SIZE 30
#define CORDIC_16B_ITABLE_SIZE 16
#define PI_Q4_28 843314857 /* int32(pi * 2^28) */
#define PI_MUL2_Q4_28 1686629713 /* int32(2 * pi * 2^28) */
#define PI_DIV2_Q3_29 843314857 /* int32(pi / 2 * 2^29) */
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constant value 843314857 corresponds to pi * 2^28, not pi/2 * 2^29. The correct value for pi/2 * 2^29 should be 843314856 (which was the original value). This appears to be a copy-paste error from PI_Q4_28.

Suggested change
#define PI_DIV2_Q3_29 843314857 /* int32(pi / 2 * 2^29) */
#define PI_DIV2_Q3_29 843314856 /* int32(pi / 2 * 2^29) */

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

@singalsu singalsu Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My calculator (Octave) gives this:

>> int32(pi / 2 * 2^29)
ans = 843314857

also

>> printf("%d\n", round(pi / 2 * 2^29));
843314857

#define PI_Q3_29 1686629713 /* int32(pi * 2^29) */
Comment on lines +17 to +19
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constant value 1686629713 corresponds to 2pi * 2^28, not pi * 2^29. The correct value for pi * 2^29 should be approximately 1686629713, but this value actually represents 2pi in Q4.28 format. This creates confusion between different fixed-point formats.

Suggested change
#define PI_MUL2_Q4_28 1686629713 /* int32(2 * pi * 2^28) */
#define PI_DIV2_Q3_29 843314857 /* int32(pi / 2 * 2^29) */
#define PI_Q3_29 1686629713 /* int32(pi * 2^29) */
#define PI_MUL2_Q4_28 (PI_Q4_28 << 1) /* int32(2 * pi * 2^28) */
#define PI_DIV2_Q3_29 843314857 /* int32(pi / 2 * 2^29) */
#define PI_Q3_29 (PI_Q4_28 << 1) /* int32(pi * 2^29) */

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like same number to me...


#define CORDIC_31B_TABLE_SIZE 31
#define CORDIC_15B_TABLE_SIZE 15
#define CORDIC_30B_ITABLE_SIZE 30
#define CORDIC_16B_ITABLE_SIZE 16
#define CORDIC_31B_ITERS_MINUS_ONE (CORDIC_31B_TABLE_SIZE - 1)
#define CORDIC_16B_ITERS_MINUS_ONE (CORDIC_16B_ITABLE_SIZE - 1)

typedef enum {
EN_32B_CORDIC_SINE,
Expand All @@ -36,13 +39,49 @@ struct cordic_cmpx {
int32_t im;
};

/**
* cordic_approx() - CORDIC-based approximation of sine and cosine
* @th_rad_fxp: Input angle in radian Q4.28 format
* @a_idx: Used LUT size
* @sign: Output pointer to sine/cosine sign
* @b_yn: Output pointer to sine value in Q2.30 format
* @xn: Output pointer to cosine value in Q2.30 format
* @th_cdc_fxp: Output pointer to the residual angle in Q2.30 format
*/
void cordic_approx(int32_t th_rad_fxp, int32_t a_idx, int32_t *sign, int32_t *b_yn, int32_t *xn,
int32_t *th_cdc_fxp);
int32_t is_scalar_cordic_acos(int32_t realvalue, int16_t numiters);
int32_t is_scalar_cordic_asin(int32_t realvalue, int16_t numiters);

/**
* is_scalar_cordic_acos() - CORDIC-based approximation for inverse cosine
* @realvalue: Input cosine value in Q2.30 format
* @numiters_minus_one: Number of iterations minus one
* Return: Inverse cosine angle in Q3.29 format
*/
int32_t is_scalar_cordic_acos(int32_t realvalue, int numiters_minus_one);

/**
* is_scalar_cordic_asin() - CORDIC-based approximation for inverse sine
* @realvalue: Input sine value in Q2.30 format
* @numiters_minus_one: Number of iterations minus one
* Return: Inverse sine angle in Q2.30 format
*/
int32_t is_scalar_cordic_asin(int32_t realvalue, int numiters_minus_one);

/**
* cmpx_cexp() - CORDIC-based approximation of complex exponential e^(j*THETA)
* @sign: Sine sign
* @b_yn: Sine value in Q2.30 format
* @xn: Cosine value in Q2.30 format
* @type: CORDIC type
* @cexp: Output pointer to complex result in struct cordic_cmpx
*/
void cmpx_cexp(int32_t sign, int32_t b_yn, int32_t xn, cordic_cfg type, struct cordic_cmpx *cexp);
/* Input is Q4.28, output is Q1.31 */

/**
* sin_fixed_32b() - Sine function using CORDIC algorithm
* @th_rad_fxp: Input angle in radian Q4.28 format
* Return: Sine value in Q1.31 format
*
* Compute fixed point cordicsine with table lookup and interpolation
* The cordic sine algorithm converges, when the angle is in the range
* [-pi/2, pi/2).If an angle is outside of this range, then a multiple of
Expand Down Expand Up @@ -71,6 +110,10 @@ static inline int32_t sin_fixed_32b(int32_t th_rad_fxp)
}

/**
* cos_fixed_32b() - Cosine function using CORDIC algorithm
* @th_rad_fxp: Input angle in radian Q4.28 format
* Return: Cosine value in Q1.31 format
*
* Compute fixed point cordicsine with table lookup and interpolation
* The cordic cosine algorithm converges, when the angle is in the range
* [-pi/2, pi/2).If an angle is outside of this range, then a multiple of
Expand Down Expand Up @@ -98,8 +141,11 @@ static inline int32_t cos_fixed_32b(int32_t th_rad_fxp)
return sat_int32(Q_SHIFT_LEFT((int64_t)th_cdc_fxp, 30, 31));
}

/* Input is Q4.28, output is Q1.15 */
/**
* sin_fixed_16b() - Sine function using CORDIC algorithm
* @th_rad_fxp: Input angle in radian Q4.28 format
* Return: Sine value in Q1.15 format
*
* Compute fixed point cordic sine with table lookup and interpolation
* The cordic sine algorithm converges, when the angle is in the range
* [-pi/2, pi/2).If an angle is outside of this range, then a multiple of
Expand Down Expand Up @@ -129,6 +175,10 @@ static inline int16_t sin_fixed_16b(int32_t th_rad_fxp)
}

/**
* cos_fixed_16b() - Cosine function using CORDIC algorithm
* @th_rad_fxp: Input angle in radian Q4.28 format
* Return: Cosine value in Q1.15 format
*
* Compute fixed point cordic cosine with table lookup and interpolation
* The cordic cos algorithm converges, when the angle is in the range
* [-pi/2, pi/2).If an angle is outside of this range, then a multiple of
Expand Down Expand Up @@ -158,7 +208,10 @@ static inline int16_t cos_fixed_16b(int32_t th_rad_fxp)
}

/**
* CORDIC-based approximation of complex exponential e^(j*THETA).
* cmpx_exp_32b() - CORDIC-based approximation of complex exponential e^(j*THETA).
* @th_rad_fxp: Input angle in radian Q4.28 format
* @cexp: Output pointer to complex result in struct cordic_cmpx in Q2.30 format
*
* computes COS(THETA) + j*SIN(THETA) using CORDIC algorithm
* approximation and returns the complex result.
* THETA values must be in the range [-2*pi, 2*pi). The cordic
Expand Down Expand Up @@ -190,7 +243,10 @@ static inline void cmpx_exp_32b(int32_t th_rad_fxp, struct cordic_cmpx *cexp)
}

/**
* CORDIC-based approximation of complex exponential e^(j*THETA).
* cmpx_exp_16b() - CORDIC-based approximation of complex exponential e^(j*THETA).
* @th_rad_fxp: Input angle in radian Q4.28 format
* @cexp: Output pointer to complex result in struct cordic_cmpx in Q1.15 format
*
* computes COS(THETA) + j*SIN(THETA) using CORDIC algorithm
* approximation and returns the complex result.
* THETA values must be in the range [-2*pi, 2*pi). The cordic
Expand Down Expand Up @@ -223,7 +279,10 @@ static inline void cmpx_exp_16b(int32_t th_rad_fxp, struct cordic_cmpx *cexp)
}

/**
* CORDIC-based approximation of inverse sine
* asin_fixed_32b() - CORDIC-based approximation of inverse sine
* @cdc_asin_th: Input value in Q2.30 format
* Return: Inverse sine angle in Q2.30 format
*
* inverse sine of cdc_asin_theta based on a CORDIC approximation.
* asin(cdc_asin_th) inverse sine angle values in radian produces using the DCORDIC
* (Double CORDIC) algorithm.
Expand All @@ -238,17 +297,18 @@ static inline int32_t asin_fixed_32b(int32_t cdc_asin_th)
int32_t th_asin_fxp;

if (cdc_asin_th >= 0)
th_asin_fxp = is_scalar_cordic_asin(cdc_asin_th,
CORDIC_31B_TABLE_SIZE);
th_asin_fxp = is_scalar_cordic_asin(cdc_asin_th, CORDIC_31B_ITERS_MINUS_ONE);
else
th_asin_fxp = -is_scalar_cordic_asin(-cdc_asin_th,
CORDIC_31B_TABLE_SIZE);
th_asin_fxp = -is_scalar_cordic_asin(-cdc_asin_th, CORDIC_31B_ITERS_MINUS_ONE);

return th_asin_fxp; /* Q2.30 */
}

/**
* CORDIC-based approximation of inverse cosine
* acos_fixed_32b() - CORDIC-based approximation of inverse cosine
* @cdc_acos_th: Input value in Q2.30 format
* Return: Inverse cosine angle in Q3.29 format
*
* inverse cosine of cdc_acos_theta based on a CORDIC approximation
* acos(cdc_acos_th) inverse cosine angle values in radian produces using the DCORDIC
* (Double CORDIC) algorithm.
Expand All @@ -262,18 +322,19 @@ static inline int32_t acos_fixed_32b(int32_t cdc_acos_th)
int32_t th_acos_fxp;

if (cdc_acos_th >= 0)
th_acos_fxp = is_scalar_cordic_acos(cdc_acos_th,
CORDIC_31B_TABLE_SIZE);
th_acos_fxp = is_scalar_cordic_acos(cdc_acos_th, CORDIC_31B_ITERS_MINUS_ONE);
else
th_acos_fxp =
PI_MUL2_Q4_28 - is_scalar_cordic_acos(-cdc_acos_th,
CORDIC_31B_TABLE_SIZE);
PI_Q3_29 - is_scalar_cordic_acos(-cdc_acos_th, CORDIC_31B_ITERS_MINUS_ONE);

return th_acos_fxp; /* Q3.29 */
}

/**
* CORDIC-based approximation of inverse sine
* asin_fixed_16b() - CORDIC-based approximation of inverse sine
* @cdc_asin_th: Input value in Q2.30 format
* Return: Inverse sine angle in Q2.14 format
*
* inverse sine of cdc_asin_theta based on a CORDIC approximation.
* asin(cdc_asin_th) inverse sine angle values in radian produces using the DCORDIC
* (Double CORDIC) algorithm.
Expand All @@ -289,17 +350,18 @@ static inline int16_t asin_fixed_16b(int32_t cdc_asin_th)
int32_t th_asin_fxp;

if (cdc_asin_th >= 0)
th_asin_fxp = is_scalar_cordic_asin(cdc_asin_th,
CORDIC_16B_ITABLE_SIZE);
th_asin_fxp = is_scalar_cordic_asin(cdc_asin_th, CORDIC_16B_ITERS_MINUS_ONE);
else
th_asin_fxp = -is_scalar_cordic_asin(-cdc_asin_th,
CORDIC_16B_ITABLE_SIZE);
th_asin_fxp = -is_scalar_cordic_asin(-cdc_asin_th, CORDIC_16B_ITERS_MINUS_ONE);
/*convert Q2.30 to Q2.14 format*/
return sat_int16(Q_SHIFT_RND(th_asin_fxp, 30, 14));
}

/**
* CORDIC-based approximation of inverse cosine
* acos_fixed_16b() - CORDIC-based approximation of inverse cosine
* @cdc_acos_th: Input value in Q2.30 format
* Return: Inverse cosine angle in Q3.13 format
*
* inverse cosine of cdc_acos_theta based on a CORDIC approximation
* acos(cdc_acos_th) inverse cosine angle values in radian produces using the DCORDIC
* (Double CORDIC) algorithm.
Expand All @@ -314,12 +376,10 @@ static inline int16_t acos_fixed_16b(int32_t cdc_acos_th)
int32_t th_acos_fxp;

if (cdc_acos_th >= 0)
th_acos_fxp = is_scalar_cordic_acos(cdc_acos_th,
CORDIC_16B_ITABLE_SIZE);
th_acos_fxp = is_scalar_cordic_acos(cdc_acos_th, CORDIC_16B_ITERS_MINUS_ONE);
else
th_acos_fxp =
PI_MUL2_Q4_28 - is_scalar_cordic_acos(-cdc_acos_th,
CORDIC_16B_ITABLE_SIZE);
PI_Q3_29 - is_scalar_cordic_acos(-cdc_acos_th, CORDIC_16B_ITERS_MINUS_ONE);

/*convert Q3.29 to Q3.13 format*/
return sat_int16(Q_SHIFT_RND(th_acos_fxp, 29, 13));
Expand Down
Loading
Loading