1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mse_q7.c
4  * Description:  Mean square error between two Q7 vectors
5  *
6  * $Date:        04 April 2022
7  * $Revision:    V1.10.0
8  *
9  * Target Processor: Cortex-M and Cortex-A cores
10  * -------------------------------------------------------------------- */
11 /*
12  * Copyright (C) 2010-2022 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   @defgroup MSE Mean Square Error
37 
38   Calculates the mean square error between two vectors.
39 
40  */
41 
42 /**
43   @addtogroup MSE
44   @{
45  */
46 
47 /**
48   @brief         Mean square error between two Q7 vectors.
49   @param[in]     pSrcA       points to the first input vector
50   @param[in]     pSrcB       points to the second input vector
51   @param[in]     blockSize   number of samples in input vector
52   @param[out]    pResult     mean square error
53  */
54 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_mse_q7(const q7_t * pSrcA,const q7_t * pSrcB,uint32_t blockSize,q7_t * pResult)55 void arm_mse_q7(
56   const q7_t * pSrcA,
57   const q7_t * pSrcB,
58         uint32_t blockSize,
59         q7_t * pResult)
60 {
61     uint32_t  blkCnt;           /* loop counters */
62     q7x16_t vecSrcA,vecSrcB;
63     q31_t   sum = 0LL;
64 
65    /* Compute 16 outputs at a time */
66     blkCnt = blockSize >> 4U;
67     while (blkCnt > 0U)
68     {
69         vecSrcA = vld1q(pSrcA);
70         vecSrcB = vld1q(pSrcB);
71 
72         vecSrcA = vshrq(vecSrcA,1);
73         vecSrcB = vshrq(vecSrcB,1);
74 
75         vecSrcA = vqsubq(vecSrcA,vecSrcB);
76         /*
77          * sum lanes
78          */
79         sum = vmladavaq(sum, vecSrcA, vecSrcA);
80 
81         blkCnt--;
82         pSrcA += 16;
83         pSrcB += 16;
84     }
85 
86     /*
87      * tail
88      */
89     blkCnt = blockSize & 0xF;
90     if (blkCnt > 0U)
91     {
92         mve_pred16_t p0 = vctp8q(blkCnt);
93         vecSrcA = vld1q(pSrcA);
94         vecSrcB = vld1q(pSrcB);
95 
96         vecSrcA = vshrq(vecSrcA,1);
97         vecSrcB = vshrq(vecSrcB,1);
98 
99         vecSrcA = vqsubq(vecSrcA,vecSrcB);
100 
101         sum = vmladavaq_p(sum, vecSrcA, vecSrcA, p0);
102     }
103 
104     *pResult = (q7_t) __SSAT((q15_t) (sum / blockSize)>>5, 8);
105 }
106 #else
arm_mse_q7(const q7_t * pSrcA,const q7_t * pSrcB,uint32_t blockSize,q7_t * pResult)107 void arm_mse_q7(
108   const q7_t * pSrcA,
109   const q7_t * pSrcB,
110         uint32_t blockSize,
111         q7_t * pResult)
112 {
113         uint32_t blkCnt;                               /* Loop counter */
114         q31_t sum = 0;                                 /* Temporary result storage */
115         q7_t inA,inB;                                       /* Temporary variable to store input value */
116 
117 
118 #if defined (ARM_MATH_LOOPUNROLL)
119 
120   /* Loop unrolling: Compute 4 outputs at a time */
121   blkCnt = blockSize >> 2U;
122 
123   while (blkCnt > 0U)
124   {
125     inA = *pSrcA++ >> 1;
126     inB = *pSrcB++ >> 1;
127     inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
128     sum += ((q15_t) inA * inA);
129 
130     inA = *pSrcA++ >> 1;
131     inB = *pSrcB++ >> 1;
132     inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
133     sum += ((q15_t) inA * inA);
134 
135     inA = *pSrcA++ >> 1;
136     inB = *pSrcB++ >> 1;
137     inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
138     sum += ((q15_t) inA * inA);
139 
140     inA = *pSrcA++ >> 1;
141     inB = *pSrcB++ >> 1;
142     inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
143     sum += ((q15_t) inA * inA);
144 
145     /* Decrement loop counter */
146     blkCnt--;
147   }
148 
149   /* Loop unrolling: Compute remaining outputs */
150   blkCnt = blockSize % 0x4U;
151 
152 #else
153 
154   /* Initialize blkCnt with number of samples */
155   blkCnt = blockSize;
156 
157 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
158 
159   while (blkCnt > 0U)
160   {
161     inA = *pSrcA++ >> 1;
162     inB = *pSrcB++ >> 1;
163 
164     inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
165     sum += ((q15_t) inA * inA);
166 
167     /* Decrement loop counter */
168     blkCnt--;
169   }
170 
171   /* Store result in q7 format */
172   *pResult = (q7_t) __SSAT((q15_t) (sum / blockSize)>>5, 8);;
173 }
174 #endif /* defined(ARM_MATH_MVEI) */
175 
176 /**
177   @} end of MSE group
178  */
179