Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / MatrixFunctions / arm_mat_scale_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_scale_q31.c   
9 *   
10 * Description:  Multiplies a Q31 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 Q31 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.31 format.   
60  * These are multiplied to yield a 2.62 intermediate result and this is shifted with saturation to 1.31 format.   
61  */
62
63 arm_status arm_mat_scale_q31(
64   const arm_matrix_instance_q31 * pSrc,
65   q31_t scaleFract,
66   int32_t shift,
67   arm_matrix_instance_q31 * pDst)
68 {
69   q31_t *pIn = pSrc->pData;                      /* input data matrix pointer */
70   q31_t *pOut = pDst->pData;                     /* output data matrix pointer */
71   q63_t out;                                     /* temporary variable to hold output value */
72   uint32_t numSamples;                           /* total number of elements in the matrix */
73   int32_t totShift = 31 - shift;                 /* shift to apply after scaling */
74   uint32_t blkCnt;                               /* loop counters  */
75   arm_status status;                             /* status of matrix scaling      */
76
77 #ifdef ARM_MATH_MATRIX_CHECK
78
79
80   /* Check for matrix mismatch  */
81   if((pSrc->numRows != pDst->numRows) || (pSrc->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     /* Total number of samples in the input matrix */
91     numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
92
93 #ifndef ARM_MATH_CM0
94
95     /* Run the below code for Cortex-M4 and Cortex-M3 */
96
97     /* Loop Unrolling */
98     blkCnt = numSamples >> 2u;
99
100     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.   
101      ** a second loop below computes the remaining 1 to 3 samples. */
102     while(blkCnt > 0u)
103     {
104       /* C(m,n) = A(m,n) * k */
105       /* Scale, saturate and then store the results in the destination buffer. */
106       out = ((q63_t) * pIn++ * scaleFract) >> totShift;
107       *pOut++ = clip_q63_to_q31(out);
108       out = ((q63_t) * pIn++ * scaleFract) >> totShift;
109       *pOut++ = clip_q63_to_q31(out);
110       out = ((q63_t) * pIn++ * scaleFract) >> totShift;
111       *pOut++ = clip_q63_to_q31(out);
112       out = ((q63_t) * pIn++ * scaleFract) >> totShift;
113       *pOut++ = clip_q63_to_q31(out);
114
115       /* Decrement the numSamples loop counter */
116       blkCnt--;
117     }
118
119     /* If the numSamples is not a multiple of 4, compute any remaining output samples here.   
120      ** No loop unrolling is used. */
121     blkCnt = numSamples % 0x4u;
122
123 #else
124
125     /* Run the below code for Cortex-M0 */
126
127     /* Initialize blkCnt with number of samples */
128     blkCnt = numSamples;
129
130 #endif /* #ifndef ARM_MATH_CM0 */
131
132     while(blkCnt > 0u)
133     {
134       /* C(m,n) = A(m,n) * k */
135       /* Scale, saturate and then store the results in the destination buffer. */
136       out = ((q63_t) * pIn++ * scaleFract) >> totShift;
137       *pOut++ = clip_q63_to_q31(out);
138
139       /* Decrement the numSamples loop counter */
140       blkCnt--;
141     }
142     /* Set status as ARM_MATH_SUCCESS */
143     status = ARM_MATH_SUCCESS;
144   }
145
146   /* Return to application */
147   return (status);
148 }
149
150 /**   
151  * @} end of MatrixScale group   
152  */