Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / MatrixFunctions / arm_mat_sub_q15.c
1 /* ----------------------------------------------------------------------   
2 * Copyright (C) 2010 ARM Limited. All rights reserved.   
3 *   
4 * $Date:        15. July 2011  
5 * $Revision:    V1.0.10  
6 *   
7 * Project:          CMSIS DSP Library   
8 * Title:            arm_mat_sub_q15.c   
9 *   
10 * Description:  Q15 Matrix subtraction   
11 *   
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13 *  
14 * Version 1.0.10 2011/7/15 
15 *    Big Endian support added and Merged M0 and M3/M4 Source code.  
16 *   
17 * Version 1.0.3 2010/11/29  
18 *    Re-organized the CMSIS folders and updated documentation.   
19 *    
20 * Version 1.0.2 2010/11/11   
21 *    Documentation updated.    
22 *   
23 * Version 1.0.1 2010/10/05    
24 *    Production release and review comments incorporated.   
25 *   
26 * Version 1.0.0 2010/09/20    
27 *    Production release and review comments incorporated.   
28 *   
29 * Version 0.0.5  2010/04/26    
30 *    incorporated review comments and updated with latest CMSIS layer   
31 *   
32 * Version 0.0.3  2010/03/10    
33 *    Initial version   
34 * -------------------------------------------------------------------- */
35
36 #include "arm_math.h"
37
38 /**   
39  * @ingroup groupMatrix   
40  */
41
42 /**   
43  * @addtogroup MatrixSub   
44  * @{   
45  */
46
47 /**   
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.   
54  *   
55  * <b>Scaling and Overflow Behavior:</b>   
56  * \par   
57  * The function uses saturating arithmetic.   
58  * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.   
59  */
60
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)
65 {
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  */
72
73
74 #ifdef ARM_MATH_MATRIX_CHECK
75
76
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))
81   {
82     /* Set status as ARM_MATH_SIZE_MISMATCH */
83     status = ARM_MATH_SIZE_MISMATCH;
84   }
85   else
86 #endif /*    #ifdef ARM_MATH_MATRIX_CHECK    */
87
88   {
89     /* Total number of samples in the input matrix */
90     numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
91
92 #ifndef ARM_MATH_CM0
93
94     /* Run the below code for Cortex-M4 and Cortex-M3 */
95
96     /* Apply loop unrolling */
97     blkCnt = numSamples >> 2u;
98
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. */
101     while(blkCnt > 0u)
102     {
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)++);
107
108       /* Decrement the loop counter */
109       blkCnt--;
110     }
111
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;
115
116     while(blkCnt > 0u)
117     {
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++);
121
122       /* Decrement the loop counter */
123       blkCnt--;
124     }
125
126 #else
127
128     /* Run the below code for Cortex-M0 */
129
130     /* Initialize blkCnt with number of samples */
131     blkCnt = numSamples;
132
133     while(blkCnt > 0u)
134     {
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);
138
139       /* Decrement the loop counter */
140       blkCnt--;
141     }
142
143 #endif /* #ifndef ARM_MATH_CM0 */
144
145     /* Set status as ARM_MATH_SUCCESS */
146     status = ARM_MATH_SUCCESS;
147   }
148
149   /* Return to application */
150   return (status);
151 }
152
153 /**   
154  * @} end of MatrixSub group   
155  */