1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mat_add_q15.c
4  * Description:  Q15 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         Q15 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 Q15 range [0x8000 0x7FFF] are saturated.
52  */
53 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
54 
arm_mat_add_q15(const arm_matrix_instance_q15 * pSrcA,const arm_matrix_instance_q15 * pSrcB,arm_matrix_instance_q15 * pDst)55 arm_status arm_mat_add_q15(
56   const arm_matrix_instance_q15 * pSrcA,
57   const arm_matrix_instance_q15 * pSrcB,
58         arm_matrix_instance_q15 * pDst)
59 {
60     uint32_t        numSamples;       /* total number of elements in the matrix  */
61     q15_t          *pDataA, *pDataB, *pDataDst;
62     q15x8_t       vecA, vecB, vecDst = { 0 };
63     q15_t const   *pSrcAVec;
64     q15_t const   *pSrcBVec;
65     uint32_t        blkCnt;           /* loop counters */
66     arm_status status;                             /* status of matrix addition */
67 
68     pDataA = pSrcA->pData;
69     pDataB = pSrcB->pData;
70     pDataDst = pDst->pData;
71     pSrcAVec = (q15_t const *) pDataA;
72     pSrcBVec = (q15_t const *) pDataB;
73 
74   #ifdef ARM_MATH_MATRIX_CHECK
75 
76   /* Check for matrix mismatch condition */
77   if ((pSrcA->numRows != pSrcB->numRows) ||
78       (pSrcA->numCols != pSrcB->numCols) ||
79       (pSrcA->numRows != pDst->numRows)  ||
80       (pSrcA->numCols != pDst->numCols)    )
81   {
82     /* Set status as ARM_MATH_SIZE_MISMATCH */
83     status = ARM_MATH_SIZE_MISMATCH;
84   }
85   else
86 
87 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
88 
89   {
90 
91     /*
92      * Total number of samples in the input matrix
93      */
94     numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
95     blkCnt = numSamples >> 3;
96     while (blkCnt > 0U)
97     {
98         /* C(m,n) = A(m,n) + B(m,n) */
99         /* Add and then store the results in the destination buffer. */
100         vecA = vld1q(pSrcAVec); pSrcAVec += 8;
101         vecB = vld1q(pSrcBVec); pSrcBVec += 8;
102         vecDst = vqaddq(vecA, vecB);
103         vst1q(pDataDst, vecDst);  pDataDst += 8;
104         /*
105          * Decrement the blockSize loop counter
106          */
107         blkCnt--;
108     }
109     /*
110      * tail
111      * (will be merged thru tail predication)
112      */
113     blkCnt = numSamples & 7;
114     if (blkCnt > 0U)
115     {
116         mve_pred16_t p0 = vctp16q(blkCnt);
117         vecA = vld1q(pSrcAVec); pSrcAVec += 8;
118         vecB = vld1q(pSrcBVec); pSrcBVec += 8;
119         vecDst = vqaddq_m(vecDst, vecA, vecB, p0);
120         vstrhq_p(pDataDst, vecDst, p0);
121     }
122   /* Set status as ARM_MATH_SUCCESS */
123     status = ARM_MATH_SUCCESS;
124   }
125 
126   /* Return to application */
127   return (status);
128 }
129 
130 #else
arm_mat_add_q15(const arm_matrix_instance_q15 * pSrcA,const arm_matrix_instance_q15 * pSrcB,arm_matrix_instance_q15 * pDst)131 arm_status arm_mat_add_q15(
132   const arm_matrix_instance_q15 * pSrcA,
133   const arm_matrix_instance_q15 * pSrcB,
134         arm_matrix_instance_q15 * pDst)
135 {
136         q15_t *pInA = pSrcA->pData;                    /* input data matrix pointer A */
137         q15_t *pInB = pSrcB->pData;                    /* input data matrix pointer B */
138         q15_t *pOut = pDst->pData;                     /* output data matrix pointer */
139 
140         uint32_t numSamples;                           /* total number of elements in the matrix */
141         uint32_t blkCnt;                               /* loop counters */
142         arm_status status;                             /* status of matrix addition */
143 
144 #ifdef ARM_MATH_MATRIX_CHECK
145 
146   /* Check for matrix mismatch condition */
147   if ((pSrcA->numRows != pSrcB->numRows) ||
148       (pSrcA->numCols != pSrcB->numCols) ||
149       (pSrcA->numRows != pDst->numRows)  ||
150       (pSrcA->numCols != pDst->numCols)    )
151   {
152     /* Set status as ARM_MATH_SIZE_MISMATCH */
153     status = ARM_MATH_SIZE_MISMATCH;
154   }
155   else
156 
157 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
158 
159   {
160     /* Total number of samples in input matrix */
161     numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
162 
163 #if defined (ARM_MATH_LOOPUNROLL)
164 
165     /* Loop unrolling: Compute 4 outputs at a time */
166     blkCnt = numSamples >> 2U;
167 
168     while (blkCnt > 0U)
169     {
170       /* C(m,n) = A(m,n) + B(m,n) */
171 
172       /* Add, saturate and store result in destination buffer. */
173 #if defined (ARM_MATH_DSP)
174       write_q15x2_ia (&pOut, __QADD16(read_q15x2_ia (&pInA), read_q15x2_ia (&pInB)));
175 
176       write_q15x2_ia (&pOut, __QADD16(read_q15x2_ia (&pInA), read_q15x2_ia (&pInB)));
177 #else
178       *pOut++ = (q15_t) __SSAT(((q31_t) *pInA++ + *pInB++), 16);
179 
180       *pOut++ = (q15_t) __SSAT(((q31_t) *pInA++ + *pInB++), 16);
181 
182       *pOut++ = (q15_t) __SSAT(((q31_t) *pInA++ + *pInB++), 16);
183 
184       *pOut++ = (q15_t) __SSAT(((q31_t) *pInA++ + *pInB++), 16);
185 #endif
186 
187       /* Decrement loop counter */
188       blkCnt--;
189     }
190 
191     /* Loop unrolling: Compute remaining outputs */
192     blkCnt = numSamples % 0x4U;
193 
194 #else
195 
196     /* Initialize blkCnt with number of samples */
197     blkCnt = numSamples;
198 
199 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
200 
201     while (blkCnt > 0U)
202     {
203       /* C(m,n) = A(m,n) + B(m,n) */
204 
205       /* Add, saturate and store result in destination buffer. */
206 #if defined (ARM_MATH_DSP)
207       *pOut++ = (q15_t) __QADD16(*pInA++, *pInB++);
208 #else
209       *pOut++ = (q15_t) __SSAT(((q31_t) *pInA++ + *pInB++), 16);
210 #endif
211 
212       /* Decrement loop counter */
213       blkCnt--;
214     }
215 
216     /* Set status as ARM_MATH_SUCCESS */
217     status = ARM_MATH_SUCCESS;
218   }
219 
220   /* Return to application */
221   return (status);
222 }
223 #endif /* defined(ARM_MATH_MVEI) */
224 
225 /**
226   @} end of MatrixAdd group
227  */
228