1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_cmplx_conj_q15.c
4  * Description:  Q15 complex conjugate
5  *
6  * $Date:        23 April 2021
7  * $Revision:    V1.9.0
8  *
9  * Target Processor: Cortex-M and Cortex-A cores
10  * -------------------------------------------------------------------- */
11 /*
12  * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
13  *
14  * SPDX-License-Identifier: Apache-2.0
15  *
16  * Licensed under the Apache License, Version 2.0 (the License); you may
17  * not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19  *
20  * www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */
28 
29 #include "dsp/complex_math_functions.h"
30 
31 /**
32   @ingroup groupCmplxMath
33  */
34 
35 /**
36   @addtogroup cmplx_conj
37   @{
38  */
39 
40 /**
41   @brief         Q15 complex conjugate.
42   @param[in]     pSrc        points to the input vector
43   @param[out]    pDst        points to the output vector
44   @param[in]     numSamples  number of samples in each vector
45 
46   @par           Scaling and Overflow Behavior
47                    The function uses saturating arithmetic.
48                    The Q15 value -1 (0x8000) is saturated to the maximum allowable positive value 0x7FFF.
49  */
50 
51 
52 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_cmplx_conj_q15(const q15_t * pSrc,q15_t * pDst,uint32_t numSamples)53 ARM_DSP_ATTRIBUTE void arm_cmplx_conj_q15(
54   const q15_t * pSrc,
55         q15_t * pDst,
56         uint32_t numSamples)
57 {
58     uint32_t blockSize = numSamples * CMPLX_DIM;   /* loop counters */
59     uint32_t blkCnt;
60     q31_t in1;
61 
62     q15x8x2_t vecSrc;
63     q15x8_t zero;
64 
65     zero = vdupq_n_s16(0);
66 
67     /* Compute 8 real samples at a time */
68     blkCnt = blockSize >> 4U;
69     while (blkCnt > 0U)
70     {
71         vecSrc = vld2q(pSrc);
72         vecSrc.val[1] = vqsubq(zero, vecSrc.val[1]);
73         vst2q(pDst,vecSrc);
74         /*
75          * Decrement the blkCnt loop counter
76          * Advance vector source and destination pointers
77          */
78         pSrc += 16;
79         pDst += 16;
80         blkCnt --;
81     }
82 
83      /* Tail */
84     blkCnt = (blockSize & 0xF) >> 1;
85 
86     while (blkCnt > 0U)
87     {
88       /* C[0] + jC[1] = A[0]+ j(-1)A[1] */
89 
90       /* Calculate Complex Conjugate and store result in destination buffer. */
91       *pDst++ =  *pSrc++;
92       in1 = *pSrc++;
93       *pDst++ = __SSAT(-in1, 16);
94 
95       /* Decrement loop counter */
96       blkCnt--;
97     }
98 }
99 #else
arm_cmplx_conj_q15(const q15_t * pSrc,q15_t * pDst,uint32_t numSamples)100 ARM_DSP_ATTRIBUTE void arm_cmplx_conj_q15(
101   const q15_t * pSrc,
102         q15_t * pDst,
103         uint32_t numSamples)
104 {
105         uint32_t blkCnt;                               /* Loop counter */
106         q31_t in1;                                     /* Temporary input variable */
107 
108 #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
109         q31_t in2, in3, in4;                           /* Temporary input variables */
110 #endif
111 
112 
113 #if defined (ARM_MATH_LOOPUNROLL)
114 
115   /* Loop unrolling: Compute 4 outputs at a time */
116   blkCnt = numSamples >> 2U;
117 
118   while (blkCnt > 0U)
119   {
120     /* C[0] + jC[1] = A[0]+ j(-1)A[1] */
121 
122     /* Calculate Complex Conjugate and store result in destination buffer. */
123 
124 #if defined (ARM_MATH_DSP)
125     in1 = read_q15x2_ia (&pSrc);
126     in2 = read_q15x2_ia (&pSrc);
127     in3 = read_q15x2_ia (&pSrc);
128     in4 = read_q15x2_ia (&pSrc);
129 
130 #ifndef ARM_MATH_BIG_ENDIAN
131     in1 = __QASX(0, in1);
132     in2 = __QASX(0, in2);
133     in3 = __QASX(0, in3);
134     in4 = __QASX(0, in4);
135 #else
136     in1 = __QSAX(0, in1);
137     in2 = __QSAX(0, in2);
138     in3 = __QSAX(0, in3);
139     in4 = __QSAX(0, in4);
140 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
141 
142     in1 = ((uint32_t) in1 >> 16) | ((uint32_t) in1 << 16);
143     in2 = ((uint32_t) in2 >> 16) | ((uint32_t) in2 << 16);
144     in3 = ((uint32_t) in3 >> 16) | ((uint32_t) in3 << 16);
145     in4 = ((uint32_t) in4 >> 16) | ((uint32_t) in4 << 16);
146 
147     write_q15x2_ia (&pDst, in1);
148     write_q15x2_ia (&pDst, in2);
149     write_q15x2_ia (&pDst, in3);
150     write_q15x2_ia (&pDst, in4);
151 #else
152     *pDst++ =  *pSrc++;
153     in1 = *pSrc++;
154     *pDst++ = (in1 == (q15_t) 0x8000) ? (q15_t) 0x7fff : -in1;
155 
156     *pDst++ =  *pSrc++;
157     in1 = *pSrc++;
158     *pDst++ = (in1 == (q15_t) 0x8000) ? (q15_t) 0x7fff : -in1;
159 
160     *pDst++ =  *pSrc++;
161     in1 = *pSrc++;
162     *pDst++ = (in1 == (q15_t) 0x8000) ? (q15_t) 0x7fff : -in1;
163 
164     *pDst++ =  *pSrc++;
165     in1 = *pSrc++;
166     *pDst++ = (in1 == (q15_t) 0x8000) ? (q15_t) 0x7fff : -in1;
167 
168 #endif /* #if defined (ARM_MATH_DSP) */
169 
170     /* Decrement loop counter */
171     blkCnt--;
172   }
173 
174   /* Loop unrolling: Compute remaining outputs */
175   blkCnt = numSamples % 0x4U;
176 
177 #else
178 
179   /* Initialize blkCnt with number of samples */
180   blkCnt = numSamples;
181 
182 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
183 
184   while (blkCnt > 0U)
185   {
186     /* C[0] + jC[1] = A[0]+ j(-1)A[1] */
187 
188     /* Calculate Complex Conjugate and store result in destination buffer. */
189     *pDst++ =  *pSrc++;
190     in1 = *pSrc++;
191 #if defined (ARM_MATH_DSP)
192     *pDst++ = __SSAT(-in1, 16);
193 #else
194     *pDst++ = (in1 == (q15_t) 0x8000) ? (q15_t) 0x7fff : -in1;
195 #endif
196 
197     /* Decrement loop counter */
198     blkCnt--;
199   }
200 
201 }
202 #endif /* defined(ARM_MATH_MVEI) */
203 
204 /**
205   @} end of cmplx_conj group
206  */
207