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