Sound Open Firmware
Loading...
Searching...
No Matches
icomplex32.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// Copyright(c) 2020-2026 Intel Corporation.
4//
5// Author: Amery Song <chao.song@intel.com>
6// Keyon Jie <yang.jie@linux.intel.com>
7
8#include <sof/audio/format.h>
9#include <sof/math/exp_fcn.h>
10#include <sof/math/log.h>
11#include <sof/math/trig.h>
12#include <sof/common.h>
13#include <stdint.h>
14
15#ifndef __SOF_ICOMPLEX32_H__
16#define __SOF_ICOMPLEX32_H__
17
23struct icomplex32 {
24 int32_t real;
25 int32_t imag;
26};
27
33struct ipolar32 {
34 int32_t magnitude;
35 int32_t angle;
36};
37
38/*
39 * These helpers are optimized for FFT calculation only.
40 * e.g. _add/sub() assume the output won't be saturate so no check needed,
41 * and _mul() assumes Q1.31 * Q1.31 so the output will be shifted to be Q1.31.
42 */
43
44static inline void icomplex32_add(const struct icomplex32 *in1, const struct icomplex32 *in2,
45 struct icomplex32 *out)
46{
47 out->real = in1->real + in2->real;
48 out->imag = in1->imag + in2->imag;
49}
50
51static inline void icomplex32_adds(const struct icomplex32 *in1, const struct icomplex32 *in2,
52 struct icomplex32 *out)
53{
54 out->real = sat_int32((int64_t)in1->real + in2->real);
55 out->imag = sat_int32((int64_t)in1->imag + in2->imag);
56}
57
58static inline void icomplex32_sub(const struct icomplex32 *in1, const struct icomplex32 *in2,
59 struct icomplex32 *out)
60{
61 out->real = in1->real - in2->real;
62 out->imag = in1->imag - in2->imag;
63}
64
65static inline void icomplex32_mul(const struct icomplex32 *in1, const struct icomplex32 *in2,
66 struct icomplex32 *out)
67{
68 out->real = ((int64_t)in1->real * in2->real - (int64_t)in1->imag * in2->imag) >> 31;
69 out->imag = ((int64_t)in1->real * in2->imag + (int64_t)in1->imag * in2->real) >> 31;
70}
71
72/* complex conjugate */
73static inline void icomplex32_conj(struct icomplex32 *comp)
74{
75 comp->imag = SATP_INT32((int64_t)-1 * comp->imag);
76}
77
78/* shift a complex n bits, n > 0: left shift, n < 0: right shift */
79static inline void icomplex32_shift(const struct icomplex32 *input, int32_t n,
80 struct icomplex32 *output)
81{
82 if (n > 0) {
83 /* need saturation handling */
84 output->real = SATP_INT32(SATM_INT32((int64_t)input->real << n));
85 output->imag = SATP_INT32(SATM_INT32((int64_t)input->imag << n));
86 } else {
87 output->real = input->real >> -n;
88 output->imag = input->imag >> -n;
89 }
90}
91
101void sofm_icomplex32_to_polar(struct icomplex32 *complex, struct ipolar32 *polar);
102
111void sofm_ipolar32_to_complex(struct ipolar32 *polar, struct icomplex32 *complex);
112
113#endif /* __SOF_ICOMPLEX32_H__ */
#define SATP_INT32(x)
Definition format.h:109
#define SATM_INT32(x)
Definition format.h:110
static int32_t sat_int32(int64_t x)
Definition format_generic.h:13
static void icomplex32_mul(const struct icomplex32 *in1, const struct icomplex32 *in2, struct icomplex32 *out)
Definition icomplex32.h:65
static void icomplex32_conj(struct icomplex32 *comp)
Definition icomplex32.h:73
void sofm_ipolar32_to_complex(struct ipolar32 *polar, struct icomplex32 *complex)
sofm_ipolar32_to_complex() - Convert complex number from polar to normal (re, im) format.
static void icomplex32_add(const struct icomplex32 *in1, const struct icomplex32 *in2, struct icomplex32 *out)
Definition icomplex32.h:44
static void icomplex32_adds(const struct icomplex32 *in1, const struct icomplex32 *in2, struct icomplex32 *out)
Definition icomplex32.h:51
static void icomplex32_sub(const struct icomplex32 *in1, const struct icomplex32 *in2, struct icomplex32 *out)
Definition icomplex32.h:58
void sofm_icomplex32_to_polar(struct icomplex32 *complex, struct ipolar32 *polar)
sofm_icomplex32_to_polar() - Convert (re, im) complex number to polar.
static void icomplex32_shift(const struct icomplex32 *input, int32_t n, struct icomplex32 *output)
Definition icomplex32.h:79
struct icomplex32 - Storage for a normal complex number.
Definition icomplex32.h:23
int32_t imag
Definition icomplex32.h:25
int32_t real
Definition icomplex32.h:24
struct ipolar32 - Storage for complex number in polar format.
Definition icomplex32.h:33
int32_t magnitude
Definition icomplex32.h:34
int32_t angle
Definition icomplex32.h:35