1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mean_f16.c
4  * Description:  Mean value 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   @defgroup mean Mean
40 
41   Calculates the mean of the input vector. Mean is defined as the average of the elements in the vector.
42   The underlying algorithm is used:
43 
44   <pre>
45       Result = (pSrc[0] + pSrc[1] + pSrc[2] + ... + pSrc[blockSize-1]) / blockSize;
46   </pre>
47 
48   There are separate functions for floating-point, Q31, Q15, and Q7 data types.
49  */
50 
51 /**
52   @addtogroup mean
53   @{
54  */
55 
56 /**
57   @brief         Mean value of a floating-point vector.
58   @param[in]     pSrc       points to the input vector.
59   @param[in]     blockSize  number of samples in input vector.
60   @param[out]    pResult    mean value returned here.
61  */
62 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
63 
64 #include "arm_helium_utils.h"
65 
arm_mean_f16(const float16_t * pSrc,uint32_t blockSize,float16_t * pResult)66 void arm_mean_f16(
67   const float16_t * pSrc,
68   uint32_t blockSize,
69   float16_t * pResult)
70 {
71     int32_t  blkCnt;           /* loop counters */
72     f16x8_t vecSrc;
73     f16x8_t sumVec = vdupq_n_f16(0.0f16);
74 
75     blkCnt = blockSize;
76     do {
77         mve_pred16_t p = vctp16q(blkCnt);
78 
79         vecSrc = vldrhq_z_f16((float16_t const *) pSrc, p);
80         sumVec = vaddq_m_f16(sumVec, sumVec, vecSrc, p);
81 
82         blkCnt -= 8;
83         pSrc += 8;
84     }
85     while (blkCnt > 0);
86 
87     *pResult = (_Float16)vecAddAcrossF16Mve(sumVec) / (_Float16) blockSize;
88 }
89 
90 
91 #else
92 
arm_mean_f16(const float16_t * pSrc,uint32_t blockSize,float16_t * pResult)93 void arm_mean_f16(
94   const float16_t * pSrc,
95         uint32_t blockSize,
96         float16_t * pResult)
97 {
98         uint32_t blkCnt;                               /* Loop counter */
99         float16_t sum = 0.0f;                          /* Temporary result storage */
100 
101 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
102 
103   /* Loop unrolling: Compute 4 outputs at a time */
104   blkCnt = blockSize >> 2U;
105 
106   while (blkCnt > 0U)
107   {
108     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
109     sum += (_Float16)*pSrc++;
110 
111     sum += (_Float16)*pSrc++;
112 
113     sum += (_Float16)*pSrc++;
114 
115     sum += (_Float16)*pSrc++;
116 
117     /* Decrement the loop counter */
118     blkCnt--;
119   }
120 
121   /* Loop unrolling: Compute remaining outputs */
122   blkCnt = blockSize % 0x4U;
123 
124 #else
125 
126   /* Initialize blkCnt with number of samples */
127   blkCnt = blockSize;
128 
129 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
130 
131   while (blkCnt > 0U)
132   {
133     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
134     sum += (_Float16)*pSrc++;
135 
136     /* Decrement loop counter */
137     blkCnt--;
138   }
139 
140   /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize  */
141   /* Store result to destination */
142   *pResult = ((_Float16)sum / (_Float16)blockSize);
143 }
144 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
145 
146 /**
147   @} end of mean group
148  */
149 
150 #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
151 
152