• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mse_q31.c
4  * Description:  Mean square error between two Q31 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 /**
37   @addtogroup MSE
38   @{
39  */
40 
41 /**
42   @brief         Mean square error between two Q31 vectors.
43   @param[in]     pSrcA       points to the first input vector
44   @param[in]     pSrcB       points to the second input vector
45   @param[in]     blockSize  number of samples in input vector
46   @param[out]    pResult    mean square error
47   @return        none
48  */
49 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_mse_q31(const q31_t * pSrcA,const q31_t * pSrcB,uint32_t blockSize,q31_t * pResult)50 void arm_mse_q31(
51   const q31_t * pSrcA,
52   const q31_t * pSrcB,
53         uint32_t blockSize,
54         q31_t * pResult)
55 {
56     uint32_t  blkCnt;           /* loop counters */
57     q31x4_t vecSrcA,vecSrcB;
58     q63_t   sum = 0LL;
59 
60    /* Compute 4 outputs at a time */
61     blkCnt = blockSize >> 2U;
62     while (blkCnt > 0U)
63     {
64         vecSrcA = vld1q(pSrcA);
65         vecSrcB = vld1q(pSrcB);
66 
67         vecSrcA = vshrq(vecSrcA,1);
68         vecSrcB = vshrq(vecSrcB,1);
69 
70 
71         vecSrcA = vqsubq(vecSrcA,vecSrcB);
72         /*
73          * sum lanes
74          */
75         sum = vrmlaldavhaq(sum, vecSrcA, vecSrcA);
76 
77         blkCnt--;
78         pSrcA += 4;
79         pSrcB += 4;
80     }
81 
82     /*
83      * tail
84      */
85     blkCnt = blockSize & 3;
86     if (blkCnt > 0U)
87     {
88         mve_pred16_t p0 = vctp32q(blkCnt);
89         vecSrcA = vld1q(pSrcA);
90         vecSrcB = vld1q(pSrcB);
91 
92         vecSrcA = vshrq(vecSrcA,1);
93         vecSrcB = vshrq(vecSrcB,1);
94 
95         vecSrcA = vqsubq(vecSrcA,vecSrcB);
96 
97         sum = vrmlaldavhaq_p(sum, vecSrcA, vecSrcA, p0);
98     }
99 
100 
101     *pResult = (q31_t) ((sum / blockSize)>>21);
102 
103 }
104 #else
arm_mse_q31(const q31_t * pSrcA,const q31_t * pSrcB,uint32_t blockSize,q31_t * pResult)105 void arm_mse_q31(
106   const q31_t * pSrcA,
107   const q31_t * pSrcB,
108         uint32_t blockSize,
109         q31_t * pResult)
110 {
111         uint32_t blkCnt;                               /* Loop counter */
112         q63_t sum = 0;                                 /* Temporary result storage */
113 
114         q31_t inA32,inB32;                                    /* Temporary variable to store packed input value */
115 
116 #if defined (ARM_MATH_LOOPUNROLL)
117 
118   /* Loop unrolling: Compute 4 outputs at a time */
119   blkCnt = blockSize >> 2U;
120 
121   while (blkCnt > 0U)
122   {
123     inA32 = *pSrcA++ >> 1;
124     inB32 = *pSrcB++ >> 1;
125     inA32 = __QSUB(inA32, inB32);
126     sum += ((q63_t) inA32 * inA32) >> 14U;
127 
128     inA32 = *pSrcA++ >> 1;
129     inB32 = *pSrcB++ >> 1;
130     inA32 = __QSUB(inA32, inB32);
131     sum += ((q63_t) inA32 * inA32) >> 14U;
132 
133     inA32 = *pSrcA++ >> 1;
134     inB32 = *pSrcB++ >> 1;
135     inA32 = __QSUB(inA32, inB32);
136     sum += ((q63_t) inA32 * inA32) >> 14U;
137 
138     inA32 = *pSrcA++ >> 1;
139     inB32 = *pSrcB++ >> 1;
140     inA32 = __QSUB(inA32, inB32);
141     sum += ((q63_t) inA32 * inA32) >> 14U;
142 
143 
144     /* Decrement loop counter */
145     blkCnt--;
146   }
147 
148   /* Loop unrolling: Compute remaining outputs */
149   blkCnt = blockSize % 0x4U;
150 
151 #else
152 
153   /* Initialize blkCnt with number of samples */
154   blkCnt = blockSize;
155 
156 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
157 
158   while (blkCnt > 0U)
159   {
160     inA32 = *pSrcA++ >> 1;
161     inB32 = *pSrcB++ >> 1;
162     inA32 = __QSUB(inA32, inB32);
163     sum += ((q63_t) inA32 * inA32) >> 14U;
164 
165     /* Decrement loop counter */
166     blkCnt--;
167   }
168 
169   /* Store result in q31 format */
170   *pResult = (q31_t) ((sum / blockSize)>>15);
171 }
172 #endif /* defined(ARM_MATH_MVEI) */
173 
174 /**
175   @} end of MSE group
176  */
177