Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / StatisticsFunctions / arm_power_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_power_q15.c   
9 *   
10 * Description:  Sum of the squares of the elements of a Q15 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 Q15 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 64-bit internal accumulator.    
53  * The input is represented in 1.15 format.  
54  * Intermediate multiplication yields a 2.30 format, and this   
55  * result is added without saturation to a 64-bit accumulator in 34.30 format.   
56  * With 33 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 34.30 format.    
59  *   
60  */
61
62 void arm_power_q15(
63   q15_t * pSrc,
64   uint32_t blockSize,
65   q63_t * pResult)
66 {
67   q63_t sum = 0;                                 /* Temporary result storage */
68
69 #ifndef ARM_MATH_CM0
70
71   /* Run the below code for Cortex-M4 and Cortex-M3 */
72
73   q31_t in32;                                    /* Temporary variable to store input value */
74   q15_t in16;                                    /* Temporary variable to store input value */
75   uint32_t blkCnt;                               /* loop counter */
76
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     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
86     /* Compute Power and then store the result in a temporary variable, sum. */
87     in32 = *__SIMD32(pSrc)++;
88     sum = __SMLALD(in32, in32, sum);
89     in32 = *__SIMD32(pSrc)++;
90     sum = __SMLALD(in32, in32, sum);
91
92     /* Decrement the loop counter */
93     blkCnt--;
94   }
95
96   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.   
97    ** No loop unrolling is used. */
98   blkCnt = blockSize % 0x4u;
99
100   while(blkCnt > 0u)
101   {
102     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
103     /* Compute Power and then store the result in a temporary variable, sum. */
104     in16 = *pSrc++;
105     sum = __SMLALD(in16, in16, sum);
106
107     /* Decrement the loop counter */
108     blkCnt--;
109   }
110
111 #else
112
113   /* Run the below code for Cortex-M0 */
114
115   q15_t in;                                      /* Temporary variable to store input value */
116   uint32_t blkCnt;                               /* loop counter */
117
118
119   /* Loop over blockSize number of values */
120   blkCnt = blockSize;
121
122   while(blkCnt > 0u)
123   {
124     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
125     /* Compute Power and then store the result in a temporary variable, sum. */
126     in = *pSrc++;
127     sum += ((q31_t) in * in);
128
129     /* Decrement the loop counter */
130     blkCnt--;
131   }
132
133 #endif /* #ifndef ARM_MATH_CM0 */
134
135   /* Store the results in 34.30 format  */
136   *pResult = sum;
137 }
138
139 /**   
140  * @} end of power group   
141  */