Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / MatrixFunctions / arm_mat_scale_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_scale_q15.c   
9 *   
10 * Description:  Multiplies a Q15 matrix by a scalar.   
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 MatrixScale   
44  * @{   
45  */
46
47 /**   
48  * @brief Q15 matrix scaling.   
49  * @param[in]       *pSrc points to input matrix   
50  * @param[in]       scaleFract fractional portion of the scale factor   
51  * @param[in]       shift number of bits to shift the result by   
52  * @param[out]      *pDst points to output matrix structure   
53  * @return              The function returns either   
54  * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.   
55  *   
56  * @details   
57  * <b>Scaling and Overflow Behavior:</b>   
58  * \par   
59  * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.15 format.   
60  * These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format.   
61  */
62
63 arm_status arm_mat_scale_q15(
64   const arm_matrix_instance_q15 * pSrc,
65   q15_t scaleFract,
66   int32_t shift,
67   arm_matrix_instance_q15 * pDst)
68 {
69   q15_t *pIn = pSrc->pData;                      /* input data matrix pointer */
70   q15_t *pOut = pDst->pData;                     /* output data matrix pointer */
71   uint32_t numSamples;                           /* total number of elements in the matrix */
72   int32_t totShift = 15 - shift;                 /* total shift to apply after scaling */
73   uint32_t blkCnt;                               /* loop counters */
74   arm_status status;                             /* status of matrix scaling     */
75
76 #ifdef ARM_MATH_MATRIX_CHECK
77
78
79   /* Check for matrix mismatch */
80   if((pSrc->numRows != pDst->numRows) || (pSrc->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) pSrc->numRows * pSrc->numCols;
91
92 #ifndef ARM_MATH_CM0
93
94     /* Run the below code for Cortex-M4 and Cortex-M3 */
95     /* Loop Unrolling */
96     blkCnt = numSamples >> 2;
97
98     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.   
99      ** a second loop below computes the remaining 1 to 3 samples. */
100     while(blkCnt > 0u)
101     {
102       /* C(m,n) = A(m,n) * k */
103       /* Scale, saturate and then store the results in the destination buffer. */
104       *pOut++ =
105         (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
106       *pOut++ =
107         (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
108       *pOut++ =
109         (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
110       *pOut++ =
111         (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
112
113       /* Decrement the numSamples loop counter */
114       blkCnt--;
115     }
116
117     /* If the numSamples is not a multiple of 4, compute any remaining output samples here.   
118      ** No loop unrolling is used. */
119     blkCnt = numSamples % 0x4u;
120
121 #else
122
123     /* Run the below code for Cortex-M0 */
124
125     /* Initialize blkCnt with number of samples */
126     blkCnt = numSamples;
127
128 #endif /* #ifndef ARM_MATH_CM0 */
129
130     while(blkCnt > 0u)
131     {
132       /* C(m,n) = A(m,n) * k */
133       /* Scale, saturate and then store the results in the destination buffer. */
134       *pOut++ =
135         (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
136
137       /* Decrement the numSamples loop counter */
138       blkCnt--;
139     }
140     /* Set status as ARM_MATH_SUCCESS */
141     status = ARM_MATH_SUCCESS;
142   }
143
144   /* Return to application */
145   return (status);
146 }
147
148 /**   
149  * @} end of MatrixScale group   
150  */