]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/dsp/cmsis_dsp/FilteringFunctions/arm_conv_opt_q7.c
1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
4 * $Date: 17. January 2013
7 * Project: CMSIS DSP Library
8 * Title: arm_conv_opt_q7.c
10 * Description: Convolution of Q7 sequences.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
23 * - Neither the name of ARM LIMITED nor the names of its contributors
24 * may be used to endorse or promote products derived from this
25 * software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 * -------------------------------------------------------------------- */
44 * @ingroup groupFilters
53 * @brief Convolution of Q7 sequences.
54 * @param[in] *pSrcA points to the first input sequence.
55 * @param[in] srcALen length of the first input sequence.
56 * @param[in] *pSrcB points to the second input sequence.
57 * @param[in] srcBLen length of the second input sequence.
58 * @param[out] *pDst points to the location where the output result is written. Length srcALen+srcBLen-1.
59 * @param[in] *pScratch1 points to scratch buffer(of type q15_t) of size max(srcALen, srcBLen) + 2*min(srcALen, srcBLen) - 2.
60 * @param[in] *pScratch2 points to scratch buffer (of type q15_t) of size min(srcALen, srcBLen).
64 * If the silicon does not support unaligned memory access enable the macro UNALIGNED_SUPPORT_DISABLE
65 * In this case input, output, scratch1 and scratch2 buffers should be aligned by 32-bit
68 * <b>Scaling and Overflow Behavior:</b>
71 * The function is implemented using a 32-bit internal accumulator.
72 * Both the inputs are represented in 1.7 format and multiplications yield a 2.14 result.
73 * The 2.14 intermediate results are accumulated in a 32-bit accumulator in 18.14 format.
74 * This approach provides 17 guard bits and there is no risk of overflow as long as <code>max(srcALen, srcBLen)<131072</code>.
75 * The 18.14 result is then truncated to 18.7 format by discarding the low 7 bits and then saturated to 1.7 format.
89 q15_t
*pScr2
, *pScr1
; /* Intermediate pointers for scratch pointers */
90 q15_t x4
; /* Temporary input variable */
91 q7_t
*pIn1
, *pIn2
; /* inputA and inputB pointer */
92 uint32_t j
, k
, blkCnt
, tapCnt
; /* loop counter */
93 q7_t
*px
; /* Temporary input1 pointer */
94 q15_t
*py
; /* Temporary input2 pointer */
95 q31_t acc0
, acc1
, acc2
, acc3
; /* Accumulator */
96 q31_t x1
, x2
, x3
, y1
; /* Temporary input variables */
97 q7_t
*pOut
= pDst
; /* output pointer */
98 q7_t out0
, out1
, out2
, out3
; /* temporary variables */
100 /* The algorithm implementation is based on the lengths of the inputs. */
101 /* srcB is always made to slide across srcA. */
102 /* So srcBLen is always considered as shorter or equal to srcALen */
103 if(srcALen
>= srcBLen
)
105 /* Initialization of inputA pointer */
108 /* Initialization of inputB pointer */
113 /* Initialization of inputA pointer */
116 /* Initialization of inputB pointer */
119 /* srcBLen is always considered as shorter or equal to srcALen */
125 /* pointer to take end of scratch2 buffer */
128 /* points to smaller length sequence */
129 px
= pIn2
+ srcBLen
- 1;
131 /* Apply loop unrolling and do 4 Copies simultaneously. */
134 /* First part of the processing with loop unrolling copies 4 data points at a time.
135 ** a second loop below copies for the remaining 1 to 3 samples. */
138 /* copy second buffer in reversal manner */
148 /* Decrement the loop counter */
152 /* If the count is not a multiple of 4, copy remaining samples here.
153 ** No loop unrolling is used. */
158 /* copy second buffer in reversal manner for remaining samples */
162 /* Decrement the loop counter */
166 /* Initialze temporary scratch pointer */
169 /* Fill (srcBLen - 1u) zeros in scratch buffer */
170 arm_fill_q15(0, pScr1
, (srcBLen
- 1u));
172 /* Update temporary scratch pointer */
173 pScr1
+= (srcBLen
- 1u);
175 /* Copy (srcALen) samples in scratch buffer */
176 /* Apply loop unrolling and do 4 Copies simultaneously. */
179 /* First part of the processing with loop unrolling copies 4 data points at a time.
180 ** a second loop below copies for the remaining 1 to 3 samples. */
183 /* copy second buffer in reversal manner */
184 x4
= (q15_t
) * pIn1
++;
186 x4
= (q15_t
) * pIn1
++;
188 x4
= (q15_t
) * pIn1
++;
190 x4
= (q15_t
) * pIn1
++;
193 /* Decrement the loop counter */
197 /* If the count is not a multiple of 4, copy remaining samples here.
198 ** No loop unrolling is used. */
203 /* copy second buffer in reversal manner for remaining samples */
204 x4
= (q15_t
) * pIn1
++;
207 /* Decrement the loop counter */
211 #ifndef UNALIGNED_SUPPORT_DISABLE
213 /* Fill (srcBLen - 1u) zeros at end of scratch buffer */
214 arm_fill_q15(0, pScr1
, (srcBLen
- 1u));
217 pScr1
+= (srcBLen
- 1u);
221 /* Apply loop unrolling and do 4 Copies simultaneously. */
222 k
= (srcBLen
- 1u) >> 2u;
224 /* First part of the processing with loop unrolling copies 4 data points at a time.
225 ** a second loop below copies for the remaining 1 to 3 samples. */
228 /* copy second buffer in reversal manner */
234 /* Decrement the loop counter */
238 /* If the count is not a multiple of 4, copy remaining samples here.
239 ** No loop unrolling is used. */
240 k
= (srcBLen
- 1u) % 0x4u
;
244 /* copy second buffer in reversal manner for remaining samples */
247 /* Decrement the loop counter */
253 /* Temporary pointer for scratch2 */
256 /* Initialization of pIn2 pointer */
261 /* Actual convolution process starts here */
262 blkCnt
= (srcALen
+ srcBLen
- 1u) >> 2;
266 /* Initialze temporary scratch pointer as scratch1 */
269 /* Clear Accumlators */
275 /* Read two samples from scratch1 buffer */
276 x1
= *__SIMD32(pScr1
)++;
278 /* Read next two samples from scratch1 buffer */
279 x2
= *__SIMD32(pScr1
)++;
281 tapCnt
= (srcBLen
) >> 2u;
286 /* Read four samples from smaller buffer */
287 y1
= _SIMD32_OFFSET(pScr2
);
289 /* multiply and accumlate */
290 acc0
= __SMLAD(x1
, y1
, acc0
);
291 acc2
= __SMLAD(x2
, y1
, acc2
);
293 /* pack input data */
294 #ifndef ARM_MATH_BIG_ENDIAN
295 x3
= __PKHBT(x2
, x1
, 0);
297 x3
= __PKHBT(x1
, x2
, 0);
300 /* multiply and accumlate */
301 acc1
= __SMLADX(x3
, y1
, acc1
);
303 /* Read next two samples from scratch1 buffer */
304 x1
= *__SIMD32(pScr1
)++;
306 /* pack input data */
307 #ifndef ARM_MATH_BIG_ENDIAN
308 x3
= __PKHBT(x1
, x2
, 0);
310 x3
= __PKHBT(x2
, x1
, 0);
313 acc3
= __SMLADX(x3
, y1
, acc3
);
315 /* Read four samples from smaller buffer */
316 y1
= _SIMD32_OFFSET(pScr2
+ 2u);
318 acc0
= __SMLAD(x2
, y1
, acc0
);
320 acc2
= __SMLAD(x1
, y1
, acc2
);
322 acc1
= __SMLADX(x3
, y1
, acc1
);
324 x2
= *__SIMD32(pScr1
)++;
326 #ifndef ARM_MATH_BIG_ENDIAN
327 x3
= __PKHBT(x2
, x1
, 0);
329 x3
= __PKHBT(x1
, x2
, 0);
332 acc3
= __SMLADX(x3
, y1
, acc3
);
337 /* Decrement the loop counter */
343 /* Update scratch pointer for remaining samples of smaller length sequence */
347 /* apply same above for remaining samples of smaller length sequence */
348 tapCnt
= (srcBLen
) & 3u;
353 /* accumlate the results */
354 acc0
+= (*pScr1
++ * *pScr2
);
355 acc1
+= (*pScr1
++ * *pScr2
);
356 acc2
+= (*pScr1
++ * *pScr2
);
357 acc3
+= (*pScr1
++ * *pScr2
++);
361 /* Decrement the loop counter */
367 /* Store the result in the accumulator in the destination buffer. */
368 out0
= (q7_t
) (__SSAT(acc0
>> 7u, 8));
369 out1
= (q7_t
) (__SSAT(acc1
>> 7u, 8));
370 out2
= (q7_t
) (__SSAT(acc2
>> 7u, 8));
371 out3
= (q7_t
) (__SSAT(acc3
>> 7u, 8));
373 *__SIMD32(pOut
)++ = __PACKq7(out0
, out1
, out2
, out3
);
375 /* Initialization of inputB pointer */
383 blkCnt
= (srcALen
+ srcBLen
- 1u) & 0x3;
385 /* Calculate convolution for remaining samples of Bigger length sequence */
388 /* Initialze temporary scratch pointer as scratch1 */
391 /* Clear Accumlators */
394 tapCnt
= (srcBLen
) >> 1u;
398 acc0
+= (*pScr1
++ * *pScr2
++);
399 acc0
+= (*pScr1
++ * *pScr2
++);
401 /* Decrement the loop counter */
405 tapCnt
= (srcBLen
) & 1u;
407 /* apply same above for remaining samples of smaller length sequence */
411 /* accumlate the results */
412 acc0
+= (*pScr1
++ * *pScr2
++);
414 /* Decrement the loop counter */
420 /* Store the result in the accumulator in the destination buffer. */
421 *pOut
++ = (q7_t
) (__SSAT(acc0
>> 7u, 8));
423 /* Initialization of inputB pointer */
434 * @} end of Conv group