Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / MatrixFunctions / arm_mat_add_q31.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_add_q31.c   
9 *   
10 * Description:  Q31 matrix addition   
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 MatrixAdd   
44  * @{   
45  */
46
47 /**   
48  * @brief Q31 matrix addition.   
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 Q31 range [0x80000000 0x7FFFFFFF] will be saturated.   
59  */
60
61 arm_status arm_mat_add_q31(
62   const arm_matrix_instance_q31 * pSrcA,
63   const arm_matrix_instance_q31 * pSrcB,
64   arm_matrix_instance_q31 * pDst)
65 {
66   q31_t *pIn1 = pSrcA->pData;                    /* input data matrix pointer A */
67   q31_t *pIn2 = pSrcB->pData;                    /* input data matrix pointer B */
68   q31_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 addition */
72
73 #ifdef ARM_MATH_MATRIX_CHECK
74
75
76   /* Check for matrix mismatch condition */
77   if((pSrcA->numRows != pSrcB->numRows) ||
78      (pSrcA->numCols != pSrcB->numCols) ||
79      (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
80   {
81     /* Set status as ARM_MATH_SIZE_MISMATCH */
82     status = ARM_MATH_SIZE_MISMATCH;
83   }
84   else
85 #endif /*    #ifdef ARM_MATH_MATRIX_CHECK    */
86
87   {
88     /* Total number of samples in the input matrix */
89     numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
90
91 #ifndef ARM_MATH_CM0
92
93     /* Run the below code for Cortex-M4 and Cortex-M3 */
94
95     /* Loop Unrolling */
96     blkCnt = numSamples >> 2u;
97
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       /* Add, saturate and then store the results in the destination buffer. */
105       *pOut++ = __QADD(*pIn1++, *pIn2++);
106       *pOut++ = __QADD(*pIn1++, *pIn2++);
107       *pOut++ = __QADD(*pIn1++, *pIn2++);
108       *pOut++ = __QADD(*pIn1++, *pIn2++);
109
110       /* Decrement the loop counter */
111       blkCnt--;
112     }
113
114     /* If the numSamples is not a multiple of 4, compute any remaining output samples here.   
115      ** No loop unrolling is used. */
116     blkCnt = numSamples % 0x4u;
117
118     while(blkCnt > 0u)
119     {
120       /* C(m,n) = A(m,n) + B(m,n) */
121       /* Add, saturate and then store the results in the destination buffer. */
122       *pOut++ = __QADD(*pIn1++, *pIn2++);
123
124       /* Decrement the loop counter */
125       blkCnt--;
126     }
127
128 #else
129
130     /* Run the below code for Cortex-M0 */
131
132     /* Initialize blkCnt with number of samples */
133     blkCnt = numSamples;
134
135     while(blkCnt > 0u)
136     {
137       /* C(m,n) = A(m,n) + B(m,n) */
138       /* Add, saturate and then store the results in the destination buffer. */
139       *pOut++ = clip_q63_to_q31(((q63_t) (*pIn1++)) + (*pIn2++));
140
141       /* Decrement the loop counter */
142       blkCnt--;
143     }
144
145 #endif /* #ifndef ARM_MATH_CM0 */
146
147     /* set status as ARM_MATH_SUCCESS */
148     status = ARM_MATH_SUCCESS;
149   }
150
151   /* Return to application */
152   return (status);
153 }
154
155 /**   
156  * @} end of MatrixAdd group   
157  */