1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_mat_sub_q15.c
10 * Description: Q15 Matrix subtraction
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
29 * Version 0.0.5 2010/04/26
30 * incorporated review comments and updated with latest CMSIS layer
32 * Version 0.0.3 2010/03/10
34 * -------------------------------------------------------------------- */
39 * @ingroup groupMatrix
43 * @addtogroup MatrixSub
48 * @brief Q15 matrix subtraction.
49 * @param[in] *pSrcA points to the first input matrix structure
50 * @param[in] *pSrcB points to the second input matrix structure
51 * @param[out] *pDst points to output matrix structure
52 * @return The function returns either
53 * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
55 * <b>Scaling and Overflow Behavior:</b>
57 * The function uses saturating arithmetic.
58 * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.
61 arm_status arm_mat_sub_q15(
62 const arm_matrix_instance_q15 * pSrcA,
63 const arm_matrix_instance_q15 * pSrcB,
64 arm_matrix_instance_q15 * pDst)
66 q15_t *pInA = pSrcA->pData; /* input data matrix pointer A */
67 q15_t *pInB = pSrcB->pData; /* input data matrix pointer B */
68 q15_t *pOut = pDst->pData; /* output data matrix pointer */
69 uint32_t numSamples; /* total number of elements in the matrix */
70 uint32_t blkCnt; /* loop counters */
71 arm_status status; /* status of matrix subtraction */
74 #ifdef ARM_MATH_MATRIX_CHECK
77 /* Check for matrix mismatch condition */
78 if((pSrcA->numRows != pSrcB->numRows) ||
79 (pSrcA->numCols != pSrcB->numCols) ||
80 (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
82 /* Set status as ARM_MATH_SIZE_MISMATCH */
83 status = ARM_MATH_SIZE_MISMATCH;
86 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
89 /* Total number of samples in the input matrix */
90 numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
94 /* Run the below code for Cortex-M4 and Cortex-M3 */
96 /* Apply loop unrolling */
97 blkCnt = numSamples >> 2u;
99 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
100 ** a second loop below computes the remaining 1 to 3 samples. */
103 /* C(m,n) = A(m,n) - B(m,n) */
104 /* Subtract, Saturate and then store the results in the destination buffer. */
105 *__SIMD32(pOut)++ = __QSUB16(*__SIMD32(pInA)++, *__SIMD32(pInB)++);
106 *__SIMD32(pOut)++ = __QSUB16(*__SIMD32(pInA)++, *__SIMD32(pInB)++);
108 /* Decrement the loop counter */
112 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
113 ** No loop unrolling is used. */
114 blkCnt = numSamples % 0x4u;
118 /* C(m,n) = A(m,n) - B(m,n) */
119 /* Subtract and then store the results in the destination buffer. */
120 *pOut++ = (q15_t) __QSUB16(*pInA++, *pInB++);
122 /* Decrement the loop counter */
128 /* Run the below code for Cortex-M0 */
130 /* Initialize blkCnt with number of samples */
135 /* C(m,n) = A(m,n) - B(m,n) */
136 /* Subtract and then store the results in the destination buffer. */
137 *pOut++ = (q15_t) __SSAT(((q31_t) * pInA++ - *pInB++), 16);
139 /* Decrement the loop counter */
143 #endif /* #ifndef ARM_MATH_CM0 */
145 /* Set status as ARM_MATH_SUCCESS */
146 status = ARM_MATH_SUCCESS;
149 /* Return to application */
154 * @} end of MatrixSub group