1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_power_f32.c
4  * Description:  Sum of the squares of the elements of a floating-point 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   @defgroup power Power
37 
38   Calculates the sum of the squares of the elements in the input vector.
39   The underlying algorithm is used:
40 
41   <pre>
42       Result = pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + pSrc[2] * pSrc[2] + ... + pSrc[blockSize-1] * pSrc[blockSize-1];
43   </pre>
44 
45   There are separate functions for floating point, Q31, Q15, and Q7 data types.
46 
47   Since the result is not divided by the length, those functions are in fact computing
48   something which is more an energy than a power.
49 
50  */
51 
52 /**
53   @addtogroup power
54   @{
55  */
56 
57 /**
58   @brief         Sum of the squares of the elements of a floating-point vector.
59   @param[in]     pSrc       points to the input vector
60   @param[in]     blockSize  number of samples in input vector
61   @param[out]    pResult    sum of the squares value returned here
62   @return        none
63  */
64 #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
65 
66 #include "arm_helium_utils.h"
67 
arm_power_f32(const float32_t * pSrc,uint32_t blockSize,float32_t * pResult)68 void arm_power_f32(
69   const float32_t * pSrc,
70   uint32_t blockSize,
71   float32_t * pResult)
72 {
73     uint32_t        blkCnt;     /* loop counters */
74     f32x4_t         vecSrc;
75     f32x4_t         sumVec = vdupq_n_f32(0.0f);
76     float32_t       sum = 0.0f;
77     float32_t in;
78 
79     /* Compute 4 outputs at a time */
80     blkCnt = blockSize >> 2U;
81     while (blkCnt > 0U)
82     {
83         vecSrc = vldrwq_f32(pSrc);
84         /*
85          * sum lanes
86          */
87         sumVec = vfmaq(sumVec, vecSrc, vecSrc);
88 
89         blkCnt --;
90         pSrc += 4;
91     }
92     sum = vecAddAcrossF32Mve(sumVec);
93 
94     /*
95      * tail
96      */
97     blkCnt = blockSize & 0x3;
98     while (blkCnt > 0U)
99     {
100       /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
101 
102       /* Compute Power and store result in a temporary variable, sum. */
103       in = *pSrc++;
104       sum += in * in;
105 
106       /* Decrement loop counter */
107       blkCnt--;
108     }
109 
110     *pResult = sum;
111 }
112 #else
113 #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_power_f32(const float32_t * pSrc,uint32_t blockSize,float32_t * pResult)114 void arm_power_f32(
115   const float32_t * pSrc,
116   uint32_t blockSize,
117   float32_t * pResult)
118 {
119   float32_t sum = 0.0f;                          /* accumulator */
120   float32_t in;                                  /* Temporary variable to store input value */
121   uint32_t blkCnt;                               /* loop counter */
122 
123   float32x4_t sumV = vdupq_n_f32(0.0f);                          /* Temporary result storage */
124   float32x2_t sumV2;
125   float32x4_t inV;
126 
127   blkCnt = blockSize >> 2U;
128 
129   /* Compute 4 outputs at a time.
130    ** a second loop below computes the remaining 1 to 3 samples. */
131   while (blkCnt > 0U)
132   {
133     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
134     /* Compute Power and then store the result in a temporary variable, sum. */
135     inV = vld1q_f32(pSrc);
136     sumV = vmlaq_f32(sumV, inV, inV);
137     pSrc += 4;
138 
139     /* Decrement the loop counter */
140     blkCnt--;
141   }
142   sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV));
143   sum = vget_lane_f32(sumV2, 0) + vget_lane_f32(sumV2, 1);
144 
145   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
146    ** No loop unrolling is used. */
147   blkCnt = blockSize % 0x4U;
148 
149   while (blkCnt > 0U)
150   {
151     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
152     /* compute power and then store the result in a temporary variable, sum. */
153     in = *pSrc++;
154     sum += in * in;
155 
156     /* Decrement the loop counter */
157     blkCnt--;
158   }
159 
160   /* Store the result to the destination */
161   *pResult = sum;
162 }
163 #else
arm_power_f32(const float32_t * pSrc,uint32_t blockSize,float32_t * pResult)164 void arm_power_f32(
165   const float32_t * pSrc,
166         uint32_t blockSize,
167         float32_t * pResult)
168 {
169         uint32_t blkCnt;                               /* Loop counter */
170         float32_t sum = 0.0f;                          /* Temporary result storage */
171         float32_t in;                                  /* Temporary variable to store input value */
172 
173 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
174 
175   /* Loop unrolling: Compute 4 outputs at a time */
176   blkCnt = blockSize >> 2U;
177 
178   while (blkCnt > 0U)
179   {
180     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
181 
182     /* Compute Power and store result in a temporary variable, sum. */
183     in = *pSrc++;
184     sum += in * in;
185 
186     in = *pSrc++;
187     sum += in * in;
188 
189     in = *pSrc++;
190     sum += in * in;
191 
192     in = *pSrc++;
193     sum += in * in;
194 
195     /* Decrement loop counter */
196     blkCnt--;
197   }
198 
199   /* Loop unrolling: Compute remaining outputs */
200   blkCnt = blockSize % 0x4U;
201 
202 #else
203 
204   /* Initialize blkCnt with number of samples */
205   blkCnt = blockSize;
206 
207 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
208 
209   while (blkCnt > 0U)
210   {
211     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
212 
213     /* Compute Power and store result in a temporary variable, sum. */
214     in = *pSrc++;
215     sum += in * in;
216 
217     /* Decrement loop counter */
218     blkCnt--;
219   }
220 
221   /* Store result to destination */
222   *pResult = sum;
223 }
224 #endif /* #if defined(ARM_MATH_NEON) */
225 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
226 
227 /**
228   @} end of power group
229  */
230