1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_power_q7.c
4 * Description: Sum of the squares of the elements of a Q7 vector
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/statistics_functions.h"
30
31 /**
32 @ingroup groupStats
33 */
34
35 /**
36 @addtogroup power
37 @{
38 */
39
40 /**
41 @brief Sum of the squares of the elements of a Q7 vector.
42 @param[in] pSrc points to the input vector
43 @param[in] blockSize number of samples in input vector
44 @param[out] pResult sum of the squares value returned here
45
46 @par Scaling and Overflow Behavior
47 The function is implemented using a 32-bit internal accumulator.
48 The input is represented in 1.7 format.
49 Intermediate multiplication yields a 2.14 format, and this
50 result is added without saturation to an accumulator in 18.14 format.
51 With 17 guard bits in the accumulator, there is no risk of overflow, and the
52 full precision of the intermediate multiplication is preserved.
53 Finally, the return result is in 18.14 format.
54 */
55 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_power_q7(const q7_t * pSrc,uint32_t blockSize,q31_t * pResult)56 ARM_DSP_ATTRIBUTE void arm_power_q7(
57 const q7_t * pSrc,
58 uint32_t blockSize,
59 q31_t * pResult)
60 {
61 uint32_t blkCnt; /* loop counters */
62 q7x16_t vecSrc;
63 q31_t sum = 0LL;
64 q7_t in;
65
66 /* Compute 16 outputs at a time */
67 blkCnt = blockSize >> 4U;
68 while (blkCnt > 0U)
69 {
70 vecSrc = vldrbq_s8(pSrc);
71 /*
72 * sum lanes
73 */
74 sum = vmladavaq(sum, vecSrc, vecSrc);
75
76 blkCnt--;
77 pSrc += 16;
78 }
79
80 /*
81 * tail
82 */
83 blkCnt = blockSize & 0xF;
84 while (blkCnt > 0U)
85 {
86 /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
87
88 /* Compute Power and store result in a temporary variable, sum. */
89 in = *pSrc++;
90 sum += ((q15_t) in * in);
91
92 /* Decrement loop counter */
93 blkCnt--;
94 }
95
96 *pResult = sum;
97 }
98 #else
arm_power_q7(const q7_t * pSrc,uint32_t blockSize,q31_t * pResult)99 ARM_DSP_ATTRIBUTE void arm_power_q7(
100 const q7_t * pSrc,
101 uint32_t blockSize,
102 q31_t * pResult)
103 {
104 uint32_t blkCnt; /* Loop counter */
105 q31_t sum = 0; /* Temporary result storage */
106 q7_t in; /* Temporary variable to store input value */
107
108 #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
109 q31_t in32; /* Temporary variable to store packed input value */
110 q31_t in1, in2; /* Temporary variables to store input value */
111 #endif
112
113 #if defined (ARM_MATH_LOOPUNROLL)
114
115 /* Loop unrolling: Compute 4 outputs at a time */
116 blkCnt = blockSize >> 2U;
117
118 while (blkCnt > 0U)
119 {
120 /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
121
122 /* Compute Power and store result in a temporary variable, sum. */
123 #if defined (ARM_MATH_DSP)
124 in32 = read_q7x4_ia (&pSrc);
125
126 in1 = __SXTB16(__ROR(in32, 8));
127 in2 = __SXTB16(in32);
128
129 /* calculate power and accumulate to accumulator */
130 sum = __SMLAD(in1, in1, sum);
131 sum = __SMLAD(in2, in2, sum);
132 #else
133 in = *pSrc++;
134 sum += ((q15_t) in * in);
135
136 in = *pSrc++;
137 sum += ((q15_t) in * in);
138
139 in = *pSrc++;
140 sum += ((q15_t) in * in);
141
142 in = *pSrc++;
143 sum += ((q15_t) in * in);
144 #endif /* #if defined (ARM_MATH_DSP) */
145
146 /* Decrement loop counter */
147 blkCnt--;
148 }
149
150 /* Loop unrolling: Compute remaining outputs */
151 blkCnt = blockSize % 0x4U;
152
153 #else
154
155 /* Initialize blkCnt with number of samples */
156 blkCnt = blockSize;
157
158 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
159
160 while (blkCnt > 0U)
161 {
162 /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
163
164 /* Compute Power and store result in a temporary variable, sum. */
165 in = *pSrc++;
166 sum += ((q15_t) in * in);
167
168 /* Decrement loop counter */
169 blkCnt--;
170 }
171
172 /* Store result in 18.14 format */
173 *pResult = sum;
174 }
175 #endif /* defined(ARM_MATH_MVEI) */
176
177 /**
178 @} end of power group
179 */
180