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