1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mean_q7.c
4  * Description:  Mean value of a Q7 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 Q7 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 32-bit internal accumulator.
48                    The input is represented in 1.7 format and is accumulated in a 32-bit
49                    accumulator in 25.7 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.7 format.
53  */
54 
55 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
56 
arm_mean_q7(const q7_t * pSrc,uint32_t blockSize,q7_t * pResult)57 ARM_DSP_ATTRIBUTE void arm_mean_q7(
58   const q7_t * pSrc,
59         uint32_t blockSize,
60         q7_t * pResult)
61 {
62     uint32_t  blkCnt;           /* loop counters */
63     q7x16_t vecSrc;
64     q31_t     sum = 0L;
65 
66 
67     blkCnt = blockSize >> 4;
68     while (blkCnt > 0U)
69     {
70         vecSrc = vldrbq_s8(pSrc);
71         /*
72          * sum lanes
73          */
74         sum = vaddvaq(sum, vecSrc);
75 
76         blkCnt--;
77         pSrc += 16;
78     }
79 
80     blkCnt = blockSize & 0xF;
81     while (blkCnt > 0U)
82     {
83       /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
84       sum += *pSrc++;
85 
86       /* Decrement loop counter */
87       blkCnt--;
88     }
89 
90     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize  */
91     /* Store the result to the destination */
92     *pResult = (q7_t) (sum / (int32_t) blockSize);
93 }
94 #else
arm_mean_q7(const q7_t * pSrc,uint32_t blockSize,q7_t * pResult)95 ARM_DSP_ATTRIBUTE void arm_mean_q7(
96   const q7_t * pSrc,
97         uint32_t blockSize,
98         q7_t * pResult)
99 {
100         uint32_t blkCnt;                               /* Loop counter */
101         q31_t sum = 0;                                 /* Temporary result storage */
102 
103 #if defined (ARM_MATH_LOOPUNROLL)
104         q31_t in;
105 #endif
106 
107 #if defined (ARM_MATH_LOOPUNROLL)
108 
109   /* Loop unrolling: Compute 4 outputs at a time */
110   blkCnt = blockSize >> 2U;
111 
112   while (blkCnt > 0U)
113   {
114     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
115     in = read_q7x4_ia (&pSrc);
116     sum += ((in << 24U) >> 24U);
117     sum += ((in << 16U) >> 24U);
118     sum += ((in <<  8U) >> 24U);
119     sum +=  (in >> 24U);
120 
121     /* Decrement the loop counter */
122     blkCnt--;
123   }
124 
125   /* Loop unrolling: Compute remaining outputs */
126   blkCnt = blockSize % 0x4U;
127 
128 #else
129 
130   /* Initialize blkCnt with number of samples */
131   blkCnt = blockSize;
132 
133 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
134 
135   while (blkCnt > 0U)
136   {
137     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
138     sum += *pSrc++;
139 
140     /* Decrement loop counter */
141     blkCnt--;
142   }
143 
144   /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize  */
145   /* Store result to destination */
146   *pResult = (q7_t) (sum / (int32_t) blockSize);
147 }
148 #endif /* defined(ARM_MATH_MVEI) */
149 
150 /**
151   @} end of mean group
152  */
153