1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_power_q7.c
10 * Description: Sum of the squares of the elements of a Q7 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 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
49 * <b>Scaling and Overflow Behavior:</b>
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.
67 q31_t sum = 0; /* Temporary result storage */
68 q7_t in; /* Temporary variable to store input */
69 uint32_t blkCnt; /* loop counter */
73 /* Run the below code for Cortex-M4 and Cortex-M3 */
75 q31_t input1; /* Temporary variable to store packed input */
76 q15_t in1, in2; /* Temporary variables to store input */
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 /* 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);
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);
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);
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);
103 /* Decrement the loop counter */
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;
113 /* Run the below code for Cortex-M0 */
115 /* Loop over blockSize number of values */
118 #endif /* #ifndef ARM_MATH_CM0 */
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. */
125 sum += ((q15_t) in * in);
127 /* Decrement the loop counter */
131 /* Store the result in 18.14 format */
136 * @} end of power group