Sound Open Firmware
Loading...
Searching...
No Matches
trig.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 *
3 * Copyright(c) 2016 Intel Corporation. All rights reserved.
4 *
5 * Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
6 * Liam Girdwood <liam.r.girdwood@linux.intel.com>
7 * Keyon Jie <yang.jie@linux.intel.com>
8 * Shriram Shastry <malladi.sastry@linux.intel.com>
9 */
10
11#ifndef __SOF_MATH_TRIG_H__
12#define __SOF_MATH_TRIG_H__
13
14#include <stdint.h>
15
16#define PI_Q4_28 843314857 /* int32(pi * 2^28) */
17#define PI_MUL2_Q4_28 1686629713 /* int32(2 * pi * 2^28) */
18#define PI_DIV2_Q3_29 843314857 /* int32(pi / 2 * 2^29) */
19#define PI_Q3_29 1686629713 /* int32(pi * 2^29) */
20
21#define CORDIC_31B_TABLE_SIZE 31
22#define CORDIC_15B_TABLE_SIZE 15
23#define CORDIC_30B_ITABLE_SIZE 30
24#define CORDIC_16B_ITABLE_SIZE 16
25#define CORDIC_31B_ITERATIONS (CORDIC_31B_TABLE_SIZE - 1)
26#define CORDIC_16B_ITERATIONS (CORDIC_16B_ITABLE_SIZE - 1)
27
36
38 int32_t re;
39 int32_t im;
40};
41
51void cordic_approx(int32_t th_rad_fxp, int32_t a_idx, int32_t *sign, int32_t *b_yn, int32_t *xn,
52 int32_t *th_cdc_fxp);
53
60int32_t is_scalar_cordic_acos(int32_t realvalue, int numiters_minus_one);
61
68int32_t is_scalar_cordic_asin(int32_t realvalue, int numiters_minus_one);
69
78void cmpx_cexp(int32_t sign, int32_t b_yn, int32_t xn, cordic_cfg type, struct cordic_cmpx *cexp);
79
99static inline int32_t sin_fixed_32b(int32_t th_rad_fxp)
100{
101 int32_t th_cdc_fxp;
102 int32_t sign;
103 int32_t b_yn;
104 int32_t xn;
105 cordic_approx(th_rad_fxp, CORDIC_31B_TABLE_SIZE, &sign, &b_yn, &xn, &th_cdc_fxp);
106
107 th_cdc_fxp = sign * b_yn;
108 /*convert Q2.30 to Q1.31 format*/
109 return sat_int32(Q_SHIFT_LEFT((int64_t)th_cdc_fxp, 30, 31));
110}
111
131static inline int32_t cos_fixed_32b(int32_t th_rad_fxp)
132{
133 int32_t th_cdc_fxp;
134 int32_t sign;
135 int32_t b_yn;
136 int32_t xn;
137 cordic_approx(th_rad_fxp, CORDIC_31B_TABLE_SIZE, &sign, &b_yn, &xn, &th_cdc_fxp);
138
139 th_cdc_fxp = sign * xn;
140 /*convert Q2.30 to Q1.31 format*/
141 return sat_int32(Q_SHIFT_LEFT((int64_t)th_cdc_fxp, 30, 31));
142}
143
163static inline int16_t sin_fixed_16b(int32_t th_rad_fxp)
164{
165 int32_t th_cdc_fxp;
166 int32_t sign;
167 int32_t b_yn;
168 int32_t xn;
169
170 cordic_approx(th_rad_fxp, CORDIC_15B_TABLE_SIZE, &sign, &b_yn, &xn, &th_cdc_fxp);
171
172 th_cdc_fxp = sign * b_yn;
173 /*convert Q2.30 to Q1.15 format*/
174 return sat_int16(Q_SHIFT_RND(th_cdc_fxp, 30, 15));
175}
176
196static inline int16_t cos_fixed_16b(int32_t th_rad_fxp)
197{
198 int32_t th_cdc_fxp;
199 int32_t sign;
200 int32_t b_yn;
201 int32_t xn;
202
203 cordic_approx(th_rad_fxp, CORDIC_15B_TABLE_SIZE, &sign, &b_yn, &xn, &th_cdc_fxp);
204
205 th_cdc_fxp = sign * xn;
206 /*convert Q2.30 to Q1.15 format*/
207 return sat_int16(Q_SHIFT_RND(th_cdc_fxp, 30, 15));
208}
209
233static inline void cmpx_exp_32b(int32_t th_rad_fxp, struct cordic_cmpx *cexp)
234{
235 int32_t th_cdc_fxp;
236 int32_t sign;
237 int32_t b_yn;
238 int32_t xn;
239
240 cordic_approx(th_rad_fxp, CORDIC_31B_TABLE_SIZE, &sign, &b_yn, &xn, &th_cdc_fxp);
241 cmpx_cexp(sign, b_yn, xn, EN_32B_CORDIC_CEXP, cexp);
242 /* return the complex(re & im) result in Q2.30*/
243}
244
268static inline void cmpx_exp_16b(int32_t th_rad_fxp, struct cordic_cmpx *cexp)
269{
270 int32_t th_cdc_fxp;
271 int32_t sign;
272 int32_t b_yn;
273 int32_t xn;
274
275 /* compute coeff from angles */
276 cordic_approx(th_rad_fxp, CORDIC_15B_TABLE_SIZE, &sign, &b_yn, &xn, &th_cdc_fxp);
277 cmpx_cexp(sign, b_yn, xn, EN_16B_CORDIC_CEXP, cexp);
278 /* return the complex(re & im) result in Q1.15*/
279}
280
295static inline int32_t asin_fixed_32b(int32_t cdc_asin_th)
296{
297 int32_t th_asin_fxp;
298
299 if (cdc_asin_th >= 0)
300 th_asin_fxp = is_scalar_cordic_asin(cdc_asin_th, CORDIC_31B_ITERATIONS);
301 else
302 th_asin_fxp = -is_scalar_cordic_asin(-cdc_asin_th, CORDIC_31B_ITERATIONS);
303
304 return th_asin_fxp; /* Q2.30 */
305}
306
320static inline int32_t acos_fixed_32b(int32_t cdc_acos_th)
321{
322 int32_t th_acos_fxp;
323
324 if (cdc_acos_th >= 0)
325 th_acos_fxp = is_scalar_cordic_acos(cdc_acos_th, CORDIC_31B_ITERATIONS);
326 else
327 th_acos_fxp = PI_Q3_29 - is_scalar_cordic_acos(-cdc_acos_th, CORDIC_31B_ITERATIONS);
328
329 return th_acos_fxp; /* Q3.29 */
330}
331
347static inline int16_t asin_fixed_16b(int32_t cdc_asin_th)
348{
349 int32_t th_asin_fxp;
350
351 if (cdc_asin_th >= 0)
352 th_asin_fxp = is_scalar_cordic_asin(cdc_asin_th, CORDIC_16B_ITERATIONS);
353 else
354 th_asin_fxp = -is_scalar_cordic_asin(-cdc_asin_th, CORDIC_16B_ITERATIONS);
355 /*convert Q2.30 to Q2.14 format*/
356 return sat_int16(Q_SHIFT_RND(th_asin_fxp, 30, 14));
357}
358
373static inline int16_t acos_fixed_16b(int32_t cdc_acos_th)
374{
375 int32_t th_acos_fxp;
376
377 if (cdc_acos_th >= 0)
378 th_acos_fxp = is_scalar_cordic_acos(cdc_acos_th, CORDIC_16B_ITERATIONS);
379 else
380 th_acos_fxp = PI_Q3_29 - is_scalar_cordic_acos(-cdc_acos_th, CORDIC_16B_ITERATIONS);
381
382 /*convert Q3.29 to Q3.13 format*/
383 return sat_int16(Q_SHIFT_RND(th_acos_fxp, 29, 13));
384}
385
398int32_t sofm_atan2_32b(int32_t y, int32_t x);
399
400#endif /* __SOF_MATH_TRIG_H__ */
#define Q_SHIFT_LEFT(x, src_q, dst_q)
Definition format.h:82
#define Q_SHIFT_RND(x, src_q, dst_q)
Definition format.h:78
static int16_t sat_int16(int32_t x)
Definition format_generic.h:33
static int32_t sat_int32(int64_t x)
Definition format_generic.h:13
Definition trig.h:37
int32_t im
Definition trig.h:39
int32_t re
Definition trig.h:38
uint32_t type
Definition tlv.h:0
static void cmpx_exp_16b(int32_t th_rad_fxp, struct cordic_cmpx *cexp)
cmpx_exp_16b() - CORDIC-based approximation of complex exponential e^(j*THETA).
Definition trig.h:268
#define CORDIC_16B_ITERATIONS
Definition trig.h:26
static int16_t cos_fixed_16b(int32_t th_rad_fxp)
cos_fixed_16b() - Cosine function using CORDIC algorithm
Definition trig.h:196
cordic_cfg
Definition trig.h:28
@ EN_32B_CORDIC_SINE
Definition trig.h:29
@ EN_16B_CORDIC_CEXP
Definition trig.h:34
@ EN_16B_CORDIC_COSINE
Definition trig.h:33
@ EN_32B_CORDIC_COSINE
Definition trig.h:30
@ EN_32B_CORDIC_CEXP
Definition trig.h:31
@ EN_16B_CORDIC_SINE
Definition trig.h:32
int32_t sofm_atan2_32b(int32_t y, int32_t x)
sofm_atan2_32b() - Four-quadrant arctangent using degree-9 Remez minimax polynomial
int32_t is_scalar_cordic_asin(int32_t realvalue, int numiters_minus_one)
is_scalar_cordic_asin() - CORDIC-based approximation for inverse sine
#define CORDIC_31B_ITERATIONS
Definition trig.h:25
static int32_t cos_fixed_32b(int32_t th_rad_fxp)
cos_fixed_32b() - Cosine function using CORDIC algorithm
Definition trig.h:131
static void cmpx_exp_32b(int32_t th_rad_fxp, struct cordic_cmpx *cexp)
cmpx_exp_32b() - CORDIC-based approximation of complex exponential e^(j*THETA).
Definition trig.h:233
#define CORDIC_15B_TABLE_SIZE
Definition trig.h:22
int32_t is_scalar_cordic_acos(int32_t realvalue, int numiters_minus_one)
is_scalar_cordic_acos() - CORDIC-based approximation for inverse cosine
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)
cordic_approx() - CORDIC-based approximation of sine and cosine
static int32_t sin_fixed_32b(int32_t th_rad_fxp)
sin_fixed_32b() - Sine function using CORDIC algorithm
Definition trig.h:99
#define PI_Q3_29
Definition trig.h:19
static int32_t acos_fixed_32b(int32_t cdc_acos_th)
acos_fixed_32b() - CORDIC-based approximation of inverse cosine
Definition trig.h:320
#define CORDIC_31B_TABLE_SIZE
Definition trig.h:21
static int16_t acos_fixed_16b(int32_t cdc_acos_th)
acos_fixed_16b() - CORDIC-based approximation of inverse cosine
Definition trig.h:373
void cmpx_cexp(int32_t sign, int32_t b_yn, int32_t xn, cordic_cfg type, struct cordic_cmpx *cexp)
cmpx_cexp() - CORDIC-based approximation of complex exponential e^(j*THETA)
static int32_t asin_fixed_32b(int32_t cdc_asin_th)
asin_fixed_32b() - CORDIC-based approximation of inverse sine
Definition trig.h:295
static int16_t sin_fixed_16b(int32_t th_rad_fxp)
sin_fixed_16b() - Sine function using CORDIC algorithm
Definition trig.h:163
static int16_t asin_fixed_16b(int32_t cdc_asin_th)
asin_fixed_16b() - CORDIC-based approximation of inverse sine
Definition trig.h:347