1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_power_f16.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_f16.h"
30 
31 #if defined(ARM_FLOAT16_SUPPORTED)
32 
33 
34 /**
35   @ingroup groupStats
36  */
37 
38 
39 
40 /**
41   @addtogroup power
42   @{
43  */
44 
45 /**
46   @brief         Sum of the squares of the elements of a floating-point vector.
47   @param[in]     pSrc       points to the input vector
48   @param[in]     blockSize  number of samples in input vector
49   @param[out]    pResult    sum of the squares value returned here
50  */
51 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
52 
53 #include "arm_helium_utils.h"
54 
arm_power_f16(const float16_t * pSrc,uint32_t blockSize,float16_t * pResult)55 void arm_power_f16(
56   const float16_t * pSrc,
57   uint32_t blockSize,
58   float16_t * pResult)
59 {
60     int32_t         blkCnt;     /* loop counters */
61     f16x8_t         vecSrc;
62     f16x8_t         sumVec = vdupq_n_f16(0.0f);
63 
64 
65     blkCnt = blockSize;
66     do {
67         mve_pred16_t    p = vctp16q(blkCnt);
68 
69         vecSrc = vldrhq_z_f16((float16_t const *) pSrc, p);
70         /*
71          * sum lanes
72          */
73         sumVec = vfmaq_m(sumVec, vecSrc, vecSrc, p);
74 
75         blkCnt -= 8;
76         pSrc += 8;
77     }
78     while (blkCnt > 0);
79 
80     *pResult = vecAddAcrossF16Mve(sumVec);
81 }
82 #else
83 
arm_power_f16(const float16_t * pSrc,uint32_t blockSize,float16_t * pResult)84 void arm_power_f16(
85   const float16_t * pSrc,
86         uint32_t blockSize,
87         float16_t * pResult)
88 {
89         uint32_t blkCnt;                               /* Loop counter */
90         _Float16 sum = 0.0f16;                          /* Temporary result storage */
91         _Float16 in;                                  /* Temporary variable to store input value */
92 
93 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
94 
95   /* Loop unrolling: Compute 4 outputs at a time */
96   blkCnt = blockSize >> 2U;
97 
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     in = *pSrc++;
107     sum += in * in;
108 
109     in = *pSrc++;
110     sum += in * in;
111 
112     in = *pSrc++;
113     sum += in * in;
114 
115     /* Decrement loop counter */
116     blkCnt--;
117   }
118 
119   /* Loop unrolling: Compute remaining outputs */
120   blkCnt = blockSize % 0x4U;
121 
122 #else
123 
124   /* Initialize blkCnt with number of samples */
125   blkCnt = blockSize;
126 
127 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
128 
129   while (blkCnt > 0U)
130   {
131     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
132 
133     /* Compute Power and store result in a temporary variable, sum. */
134     in = *pSrc++;
135     sum += in * in;
136 
137     /* Decrement loop counter */
138     blkCnt--;
139   }
140 
141   /* Store result to destination */
142   *pResult = sum;
143 }
144 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
145 
146 /**
147   @} end of power group
148  */
149 
150 #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
151 
152