• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mat_sub_f32.c
4  * Description:  Floating-point matrix subtraction
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   @defgroup MatrixSub Matrix Subtraction
37 
38   Subtract two matrices.
39   @par Subraction of two 3 x 3 matrices
40 
41   \f[
42   \begin{pmatrix}
43    a_{1,1} & a_{1,2} & a_{1,3} \\
44    a_{2,1} & a_{2,2} & a_{2,3} \\
45    a_{3,1} & a_{3,2} & a_{3,3} \\
46   \end{pmatrix}
47   -
48   \begin{pmatrix}
49    b_{1,1} & b_{1,2} & b_{1,3} \\
50    b_{2,1} & b_{2,2} & b_{2,3} \\
51    b_{3,1} & b_{3,2} & b_{3,3} \\
52   \end{pmatrix}
53   =
54   \begin{pmatrix}
55    a_{1,1}-b_{1,1} & a_{1,2}-b_{1,2} & a_{1,3}-b_{1,3} \\
56    a_{2,1}-b_{2,1} & a_{2,2}-b_{2,2} & a_{2,3}-b_{2,3} \\
57    a_{3,1}-b_{3,1} & a_{3,2}-b_{3,2} & a_{3,3}-b_{3,3} \\
58   \end{pmatrix}
59   \f]
60   The functions check to make sure that
61   <code>pSrcA</code>, <code>pSrcB</code>, and <code>pDst</code> have the same
62   number of rows and columns.
63  */
64 
65 /**
66   @addtogroup MatrixSub
67   @{
68  */
69 
70 /**
71   @brief         Floating-point matrix subtraction.
72   @param[in]     pSrcA      points to the first input matrix structure
73   @param[in]     pSrcB      points to the second input matrix structure
74   @param[out]    pDst       points to output matrix structure
75   @return        execution status
76                    - \ref ARM_MATH_SUCCESS       : Operation successful
77                    - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
78  */
79 #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_mat_sub_f32(const arm_matrix_instance_f32 * pSrcA,const arm_matrix_instance_f32 * pSrcB,arm_matrix_instance_f32 * pDst)80 arm_status arm_mat_sub_f32(
81   const arm_matrix_instance_f32 * pSrcA,
82   const arm_matrix_instance_f32 * pSrcB,
83   arm_matrix_instance_f32 * pDst)
84 {
85     arm_status status;                             /* status of matrix subtraction */
86     uint32_t  numSamples;       /* total number of elements in the matrix  */
87     float32_t *pDataA, *pDataB, *pDataDst;
88     f32x4_t vecA, vecB, vecDst = { 0 };
89     float32_t const *pSrcAVec;
90     float32_t const *pSrcBVec;
91     uint32_t  blkCnt;           /* loop counters */
92 
93     pDataA = pSrcA->pData;
94     pDataB = pSrcB->pData;
95     pDataDst = pDst->pData;
96     pSrcAVec = (float32_t const *) pDataA;
97     pSrcBVec = (float32_t const *) pDataB;
98 
99 #ifdef ARM_MATH_MATRIX_CHECK
100   /* Check for matrix mismatch condition */
101   if ((pSrcA->numRows != pSrcB->numRows) ||
102      (pSrcA->numCols != pSrcB->numCols) ||
103      (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
104   {
105     /* Set status as ARM_MATH_SIZE_MISMATCH */
106     status = ARM_MATH_SIZE_MISMATCH;
107   }
108   else
109 #endif /*    #ifdef ARM_MATH_MATRIX_CHECK    */
110   {
111     /*
112      * Total number of samples in the input matrix
113      */
114     numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
115     blkCnt = numSamples >> 2;
116     while (blkCnt > 0U)
117     {
118         /* C(m,n) = A(m,n) + B(m,n) */
119         /* sub and then store the results in the destination buffer. */
120         vecA = vld1q(pSrcAVec);
121         pSrcAVec += 4;
122         vecB = vld1q(pSrcBVec);
123         pSrcBVec += 4;
124         vecDst = vsubq(vecA, vecB);
125         vst1q(pDataDst, vecDst);
126         pDataDst += 4;
127         /*
128          * Decrement the blockSize loop counter
129          */
130         blkCnt--;
131     }
132     /*
133      * tail
134      * (will be merged thru tail predication)
135      */
136     blkCnt = numSamples & 3;
137     if (blkCnt > 0U)
138     {
139         mve_pred16_t p0 = vctp32q(blkCnt);
140         vecA = vld1q(pSrcAVec);
141         vecB = vld1q(pSrcBVec);
142         vecDst = vsubq_m(vecDst, vecA, vecB, p0);
143         vstrwq_p(pDataDst, vecDst, p0);
144     }
145     status = ARM_MATH_SUCCESS;
146   }
147 
148   /* Return to application */
149   return (status);
150 }
151 
152 #else
153 #if defined(ARM_MATH_NEON)
arm_mat_sub_f32(const arm_matrix_instance_f32 * pSrcA,const arm_matrix_instance_f32 * pSrcB,arm_matrix_instance_f32 * pDst)154 arm_status arm_mat_sub_f32(
155   const arm_matrix_instance_f32 * pSrcA,
156   const arm_matrix_instance_f32 * pSrcB,
157   arm_matrix_instance_f32 * pDst)
158 {
159   float32_t *pIn1 = pSrcA->pData;                /* input data matrix pointer A */
160   float32_t *pIn2 = pSrcB->pData;                /* input data matrix pointer B */
161   float32_t *pOut = pDst->pData;                 /* output data matrix pointer  */
162 
163 
164   uint32_t numSamples;                           /* total number of elements in the matrix  */
165   uint32_t blkCnt;                               /* loop counters */
166   arm_status status;                             /* status of matrix subtraction */
167 
168 #ifdef ARM_MATH_MATRIX_CHECK
169   /* Check for matrix mismatch condition */
170   if ((pSrcA->numRows != pSrcB->numRows) ||
171      (pSrcA->numCols != pSrcB->numCols) ||
172      (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
173   {
174     /* Set status as ARM_MATH_SIZE_MISMATCH */
175     status = ARM_MATH_SIZE_MISMATCH;
176   }
177   else
178 #endif /*    #ifdef ARM_MATH_MATRIX_CHECK    */
179   {
180     float32x4_t vec1;
181     float32x4_t vec2;
182     float32x4_t res;
183 
184     /* Total number of samples in the input matrix */
185     numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
186 
187     blkCnt = numSamples >> 2U;
188 
189     /* Compute 4 outputs at a time.
190      ** a second loop below computes the remaining 1 to 3 samples. */
191     while (blkCnt > 0U)
192     {
193       /* C(m,n) = A(m,n) - B(m,n) */
194       /* Subtract and then store the results in the destination buffer. */
195       /* Read values from source A */
196       vec1 = vld1q_f32(pIn1);
197       vec2 = vld1q_f32(pIn2);
198       res = vsubq_f32(vec1, vec2);
199       vst1q_f32(pOut, res);
200 
201       /* Update pointers to process next samples */
202       pIn1 += 4U;
203       pIn2 += 4U;
204       pOut += 4U;
205 
206       /* Decrement the loop counter */
207       blkCnt--;
208     }
209 
210     /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
211      ** No loop unrolling is used. */
212     blkCnt = numSamples % 0x4U;
213 
214 
215     while (blkCnt > 0U)
216     {
217       /* C(m,n) = A(m,n) - B(m,n) */
218       /* Subtract and then store the results in the destination buffer. */
219       *pOut++ = (*pIn1++) - (*pIn2++);
220 
221       /* Decrement the loop counter */
222       blkCnt--;
223     }
224 
225     /* Set status as ARM_MATH_SUCCESS */
226     status = ARM_MATH_SUCCESS;
227   }
228 
229   /* Return to application */
230   return (status);
231 }
232 #else
arm_mat_sub_f32(const arm_matrix_instance_f32 * pSrcA,const arm_matrix_instance_f32 * pSrcB,arm_matrix_instance_f32 * pDst)233 arm_status arm_mat_sub_f32(
234   const arm_matrix_instance_f32 * pSrcA,
235   const arm_matrix_instance_f32 * pSrcB,
236         arm_matrix_instance_f32 * pDst)
237 {
238   float32_t *pInA = pSrcA->pData;                /* input data matrix pointer A */
239   float32_t *pInB = pSrcB->pData;                /* input data matrix pointer B */
240   float32_t *pOut = pDst->pData;                 /* output data matrix pointer */
241 
242   uint32_t numSamples;                           /* total number of elements in the matrix */
243   uint32_t blkCnt;                               /* loop counters */
244   arm_status status;                             /* status of matrix subtraction */
245 
246 #ifdef ARM_MATH_MATRIX_CHECK
247 
248   /* Check for matrix mismatch condition */
249   if ((pSrcA->numRows != pSrcB->numRows) ||
250       (pSrcA->numCols != pSrcB->numCols) ||
251       (pSrcA->numRows != pDst->numRows)  ||
252       (pSrcA->numCols != pDst->numCols)    )
253   {
254     /* Set status as ARM_MATH_SIZE_MISMATCH */
255     status = ARM_MATH_SIZE_MISMATCH;
256   }
257   else
258 
259 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
260 
261   {
262     /* Total number of samples in input matrix */
263     numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
264 
265 #if defined (ARM_MATH_LOOPUNROLL)
266 
267     /* Loop unrolling: Compute 4 outputs at a time */
268     blkCnt = numSamples >> 2U;
269 
270     while (blkCnt > 0U)
271     {
272       /* C(m,n) = A(m,n) - B(m,n) */
273 
274       /* Subtract and store result in destination buffer. */
275       *pOut++ = (*pInA++) - (*pInB++);
276       *pOut++ = (*pInA++) - (*pInB++);
277       *pOut++ = (*pInA++) - (*pInB++);
278       *pOut++ = (*pInA++) - (*pInB++);
279 
280       /* Decrement loop counter */
281       blkCnt--;
282     }
283 
284     /* Loop unrolling: Compute remaining outputs */
285     blkCnt = numSamples % 0x4U;
286 
287 #else
288 
289     /* Initialize blkCnt with number of samples */
290     blkCnt = numSamples;
291 
292 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
293 
294     while (blkCnt > 0U)
295     {
296       /* C(m,n) = A(m,n) - B(m,n) */
297 
298       /* Subtract and store result in destination buffer. */
299       *pOut++ = (*pInA++) - (*pInB++);
300 
301       /* Decrement loop counter */
302       blkCnt--;
303     }
304 
305     /* Set status as ARM_MATH_SUCCESS */
306     status = ARM_MATH_SUCCESS;
307   }
308 
309   /* Return to application */
310   return (status);
311 }
312 #endif /* #if defined(ARM_MATH_NEON) */
313 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
314 
315 /**
316   @} end of MatrixSub group
317  */
318