1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mean_q31.c
4  * Description:  Mean value of a Q31 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 mean
37   @{
38  */
39 
40 /**
41   @brief         Mean value of a Q31 vector.
42   @param[in]     pSrc       points to the input vector
43   @param[in]     blockSize  number of samples in input vector
44   @param[out]    pResult    mean value returned here
45 
46   @par           Scaling and Overflow Behavior
47                    The function is implemented using a 64-bit internal accumulator.
48                    The input is represented in 1.31 format and is accumulated in a 64-bit
49                    accumulator in 33.31 format.
50                    There is no risk of internal overflow with this approach, and the
51                    full precision of intermediate result is preserved.
52                    Finally, the accumulator is truncated to yield a result of 1.31 format.
53  */
54 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_mean_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)55 ARM_DSP_ATTRIBUTE void arm_mean_q31(
56   const q31_t * pSrc,
57         uint32_t blockSize,
58         q31_t * pResult)
59 {
60     uint32_t  blkCnt;           /* loop counters */
61     q31x4_t vecSrc;
62     q63_t     sum = 0LL;
63 
64 
65     /* Compute 4 outputs at a time */
66     blkCnt = blockSize >> 2U;
67     while (blkCnt > 0U)
68     {
69 
70         vecSrc = vldrwq_s32(pSrc);
71         /*
72          * sum lanes
73          */
74         sum = vaddlvaq(sum, vecSrc);
75 
76         blkCnt --;
77         pSrc += 4;
78     }
79 
80     /* Tail */
81     blkCnt = blockSize & 0x3;
82 
83     while (blkCnt > 0U)
84     {
85       /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
86       sum += *pSrc++;
87       blkCnt --;
88     }
89 
90     *pResult = arm_div_int64_to_int32(sum, blockSize);
91 }
92 #else
arm_mean_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)93 ARM_DSP_ATTRIBUTE void arm_mean_q31(
94   const q31_t * pSrc,
95         uint32_t blockSize,
96         q31_t * pResult)
97 {
98         uint32_t blkCnt;                               /* Loop counter */
99         q63_t sum = 0;                                 /* Temporary result storage */
100 
101 #if defined (ARM_MATH_LOOPUNROLL)
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 += *pSrc++;
110 
111     sum += *pSrc++;
112 
113     sum += *pSrc++;
114 
115     sum += *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 += *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 = (q31_t) (sum / blockSize);
143 }
144 #endif /* defined(ARM_MATH_MVEI) */
145 
146 /**
147   @} end of mean group
148  */
149