1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_power_q15.c
10 * Description: Sum of the squares of the elements of a Q15 vector.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
28 * -------------------------------------------------------------------- */
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
49 * <b>Scaling and Overflow Behavior:</b>
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.
67 q63_t sum = 0; /* Temporary result storage */
71 /* Run the below code for Cortex-M4 and Cortex-M3 */
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 */
79 blkCnt = blockSize >> 2u;
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. */
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);
92 /* Decrement the loop counter */
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;
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. */
105 sum = __SMLALD(in16, in16, sum);
107 /* Decrement the loop counter */
113 /* Run the below code for Cortex-M0 */
115 q15_t in; /* Temporary variable to store input value */
116 uint32_t blkCnt; /* loop counter */
119 /* Loop over blockSize number of values */
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. */
127 sum += ((q31_t) in * in);
129 /* Decrement the loop counter */
133 #endif /* #ifndef ARM_MATH_CM0 */
135 /* Store the results in 34.30 format */
140 * @} end of power group