• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   @return        none
62  */
63 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
64 
65 #include "arm_helium_utils.h"
66 
arm_mean_f16(const float16_t * pSrc,uint32_t blockSize,float16_t * pResult)67 void arm_mean_f16(
68   const float16_t * pSrc,
69   uint32_t blockSize,
70   float16_t * pResult)
71 {
72     int32_t  blkCnt;           /* loop counters */
73     f16x8_t vecSrc;
74     f16x8_t sumVec = vdupq_n_f16(0.0f16);
75 
76     blkCnt = blockSize;
77     do {
78         mve_pred16_t p = vctp16q(blkCnt);
79 
80         vecSrc = vldrhq_z_f16((float16_t const *) pSrc, p);
81         sumVec = vaddq_m_f16(sumVec, sumVec, vecSrc, p);
82 
83         blkCnt -= 8;
84         pSrc += 8;
85     }
86     while (blkCnt > 0);
87 
88     *pResult = vecAddAcrossF16Mve(sumVec) / (float16_t) blockSize;
89 }
90 
91 
92 #else
93 
arm_mean_f16(const float16_t * pSrc,uint32_t blockSize,float16_t * pResult)94 void arm_mean_f16(
95   const float16_t * pSrc,
96         uint32_t blockSize,
97         float16_t * pResult)
98 {
99         uint32_t blkCnt;                               /* Loop counter */
100         float16_t sum = 0.0f;                          /* Temporary result storage */
101 
102 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
103 
104   /* Loop unrolling: Compute 4 outputs at a time */
105   blkCnt = blockSize >> 2U;
106 
107   while (blkCnt > 0U)
108   {
109     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
110     sum += *pSrc++;
111 
112     sum += *pSrc++;
113 
114     sum += *pSrc++;
115 
116     sum += *pSrc++;
117 
118     /* Decrement the loop counter */
119     blkCnt--;
120   }
121 
122   /* Loop unrolling: Compute remaining outputs */
123   blkCnt = blockSize % 0x4U;
124 
125 #else
126 
127   /* Initialize blkCnt with number of samples */
128   blkCnt = blockSize;
129 
130 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
131 
132   while (blkCnt > 0U)
133   {
134     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
135     sum += *pSrc++;
136 
137     /* Decrement loop counter */
138     blkCnt--;
139   }
140 
141   /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize  */
142   /* Store result to destination */
143   *pResult = (sum / (float16_t)blockSize);
144 }
145 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
146 
147 /**
148   @} end of mean group
149  */
150 
151 #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
152 
153