Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / StatisticsFunctions / arm_power_q7.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_power_q7.c   
9 *   
10 * Description:  Sum of the squares of the elements of a Q7 vector.   
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
30 #include "arm_math.h"
31
32 /**   
33  * @ingroup groupStats   
34  */
35
36 /**   
37  * @addtogroup power   
38  * @{   
39  */
40
41 /**   
42  * @brief Sum of the squares of the elements of a Q7 vector.   
43  * @param[in]       *pSrc points to the input vector   
44  * @param[in]       blockSize length of the input vector   
45  * @param[out]      *pResult sum of the squares value returned here   
46  * @return none.   
47  *   
48  * @details   
49  * <b>Scaling and Overflow Behavior:</b>   
50  *   
51  * \par   
52  * The function is implemented using a 32-bit internal accumulator.    
53  * The input is represented in 1.7 format.  
54  * Intermediate multiplication yields a 2.14 format, and this   
55  * result is added without saturation to an accumulator in 18.14 format.   
56  * With 17 guard bits in the accumulator, there is no risk of overflow, and the   
57  * full precision of the intermediate multiplication is preserved.   
58  * Finally, the return result is in 18.14 format.    
59  *   
60  */
61
62 void arm_power_q7(
63   q7_t * pSrc,
64   uint32_t blockSize,
65   q31_t * pResult)
66 {
67   q31_t sum = 0;                                 /* Temporary result storage */
68   q7_t in;                                       /* Temporary variable to store input */
69   uint32_t blkCnt;                               /* loop counter */
70
71 #ifndef ARM_MATH_CM0
72
73   /* Run the below code for Cortex-M4 and Cortex-M3 */
74
75   q31_t input1;                                  /* Temporary variable to store packed input */
76   q15_t in1, in2;                                /* Temporary variables to store input */
77
78   /*loop Unrolling */
79   blkCnt = blockSize >> 2u;
80
81   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.   
82    ** a second loop below computes the remaining 1 to 3 samples. */
83   while(blkCnt > 0u)
84   {
85     /* Reading two inputs of pSrc vector and packing */
86     in1 = (q15_t) * pSrc++;
87     in2 = (q15_t) * pSrc++;
88     input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
89
90     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
91     /* Compute Power and then store the result in a temporary variable, sum. */
92     sum = __SMLAD(input1, input1, sum);
93
94     /* Reading two inputs of pSrc vector and packing */
95     in1 = (q15_t) * pSrc++;
96     in2 = (q15_t) * pSrc++;
97     input1 = ((q31_t) in1 & 0x0000FFFF) | ((q31_t) in2 << 16);
98
99     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
100     /* Compute Power and then store the result in a temporary variable, sum. */
101     sum = __SMLAD(input1, input1, sum);
102
103     /* Decrement the loop counter */
104     blkCnt--;
105   }
106
107   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.   
108    ** No loop unrolling is used. */
109   blkCnt = blockSize % 0x4u;
110
111 #else
112
113   /* Run the below code for Cortex-M0 */
114
115   /* Loop over blockSize number of values */
116   blkCnt = blockSize;
117
118 #endif /* #ifndef ARM_MATH_CM0 */
119
120   while(blkCnt > 0u)
121   {
122     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
123     /* Compute Power and then store the result in a temporary variable, sum. */
124     in = *pSrc++;
125     sum += ((q15_t) in * in);
126
127     /* Decrement the loop counter */
128     blkCnt--;
129   }
130
131   /* Store the result in 18.14 format  */
132   *pResult = sum;
133 }
134
135 /**   
136  * @} end of power group   
137  */