1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mat_add_q31.c
4 * Description: Q31 matrix addition
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/matrix_functions.h"
30
31 /**
32 @ingroup groupMatrix
33 */
34
35 /**
36 @addtogroup MatrixAdd
37 @{
38 */
39
40 /**
41 @brief Q31 matrix addition.
42 @param[in] pSrcA points to first input matrix structure
43 @param[in] pSrcB points to second input matrix structure
44 @param[out] pDst points to output matrix structure
45 @return execution status
46 - \ref ARM_MATH_SUCCESS : Operation successful
47 - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
48
49 @par Scaling and Overflow Behavior
50 The function uses saturating arithmetic.
51 Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] are saturated.
52 */
53 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_mat_add_q31(const arm_matrix_instance_q31 * pSrcA,const arm_matrix_instance_q31 * pSrcB,arm_matrix_instance_q31 * pDst)54 arm_status arm_mat_add_q31(
55 const arm_matrix_instance_q31 * pSrcA,
56 const arm_matrix_instance_q31 * pSrcB,
57 arm_matrix_instance_q31 * pDst)
58 {
59 arm_status status; /* status of matrix addition */
60 uint32_t numSamples; /* total number of elements in the matrix */
61 q31_t *pDataA, *pDataB, *pDataDst;
62 q31x4_t vecA, vecB, vecDst;
63 q31_t const *pSrcAVec;
64 q31_t const *pSrcBVec;
65 uint32_t blkCnt; /* loop counters */
66
67 pDataA = pSrcA->pData;
68 pDataB = pSrcB->pData;
69 pDataDst = pDst->pData;
70 pSrcAVec = (q31_t const *) pDataA;
71 pSrcBVec = (q31_t const *) pDataB;
72
73 #ifdef ARM_MATH_MATRIX_CHECK
74
75 /* Check for matrix mismatch condition */
76 if ((pSrcA->numRows != pSrcB->numRows) ||
77 (pSrcA->numCols != pSrcB->numCols) ||
78 (pSrcA->numRows != pDst->numRows) ||
79 (pSrcA->numCols != pDst->numCols) )
80 {
81 /* Set status as ARM_MATH_SIZE_MISMATCH */
82 status = ARM_MATH_SIZE_MISMATCH;
83 }
84 else
85 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
86 {
87 /*
88 * Total number of samples in the input matrix
89 */
90 numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
91 blkCnt = numSamples >> 2;
92 while (blkCnt > 0U)
93 {
94 /* C(m,n) = A(m,n) + B(m,n) */
95 /* Add and then store the results in the destination buffer. */
96 vecA = vld1q(pSrcAVec);
97 pSrcAVec += 4;
98 vecB = vld1q(pSrcBVec);
99 pSrcBVec += 4;
100 vecDst = vqaddq(vecA, vecB);
101 vst1q(pDataDst, vecDst);
102 pDataDst += 4;
103 /*
104 * Decrement the blockSize loop counter
105 */
106 blkCnt--;
107 }
108 /*
109 * tail
110 */
111 blkCnt = numSamples & 3;
112 if (blkCnt > 0U)
113 {
114 mve_pred16_t p0 = vctp32q(blkCnt);
115 vecA = vld1q(pSrcAVec);
116 pSrcAVec += 4;
117 vecB = vld1q(pSrcBVec);
118 pSrcBVec += 4;
119 vecDst = vqaddq_m(vecDst, vecA, vecB, p0);
120 vstrwq_p(pDataDst, vecDst, p0);
121 }
122 status = ARM_MATH_SUCCESS;
123 }
124
125 /* Return to application */
126 return (status);
127 }
128
129 #else
arm_mat_add_q31(const arm_matrix_instance_q31 * pSrcA,const arm_matrix_instance_q31 * pSrcB,arm_matrix_instance_q31 * pDst)130 arm_status arm_mat_add_q31(
131 const arm_matrix_instance_q31 * pSrcA,
132 const arm_matrix_instance_q31 * pSrcB,
133 arm_matrix_instance_q31 * pDst)
134 {
135 q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */
136 q31_t *pInB = pSrcB->pData; /* input data matrix pointer B */
137 q31_t *pOut = pDst->pData; /* output data matrix pointer */
138
139 uint32_t numSamples; /* total number of elements in the matrix */
140 uint32_t blkCnt; /* loop counters */
141 arm_status status; /* status of matrix addition */
142
143 #ifdef ARM_MATH_MATRIX_CHECK
144
145 /* Check for matrix mismatch condition */
146 if ((pSrcA->numRows != pSrcB->numRows) ||
147 (pSrcA->numCols != pSrcB->numCols) ||
148 (pSrcA->numRows != pDst->numRows) ||
149 (pSrcA->numCols != pDst->numCols) )
150 {
151 /* Set status as ARM_MATH_SIZE_MISMATCH */
152 status = ARM_MATH_SIZE_MISMATCH;
153 }
154 else
155
156 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
157
158 {
159 /* Total number of samples in input matrix */
160 numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
161
162 #if defined (ARM_MATH_LOOPUNROLL)
163
164 /* Loop unrolling: Compute 4 outputs at a time */
165 blkCnt = numSamples >> 2U;
166
167 while (blkCnt > 0U)
168 {
169 /* C(m,n) = A(m,n) + B(m,n) */
170
171 /* Add, saturate and store result in destination buffer. */
172 *pOut++ = __QADD(*pInA++, *pInB++);
173
174 *pOut++ = __QADD(*pInA++, *pInB++);
175
176 *pOut++ = __QADD(*pInA++, *pInB++);
177
178 *pOut++ = __QADD(*pInA++, *pInB++);
179
180 /* Decrement loop counter */
181 blkCnt--;
182 }
183
184 /* Loop unrolling: Compute remaining outputs */
185 blkCnt = numSamples % 0x4U;
186
187 #else
188
189 /* Initialize blkCnt with number of samples */
190 blkCnt = numSamples;
191
192 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
193
194 while (blkCnt > 0U)
195 {
196 /* C(m,n) = A(m,n) + B(m,n) */
197
198 /* Add, saturate and store result in destination buffer. */
199 *pOut++ = __QADD(*pInA++, *pInB++);
200
201 /* Decrement loop counter */
202 blkCnt--;
203 }
204
205 /* Set status as ARM_MATH_SUCCESS */
206 status = ARM_MATH_SUCCESS;
207 }
208
209 /* Return to application */
210 return (status);
211 }
212 #endif /* defined(ARM_MATH_MVEI) */
213
214 /**
215 @} end of MatrixAdd group
216 */
217