1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
4 * Description: Root Mean Square of the elements of a Q15 vector
6 * $Date: 27. January 2017
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
16 * Licensed under the Apache License, Version 2.0 (the License); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
20 * www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
37 * @brief Root Mean Square of the elements of a Q15 vector.
38 * @param[in] *pSrc points to the input vector
39 * @param[in] blockSize length of the input vector
40 * @param[out] *pResult rms value returned here
44 * <b>Scaling and Overflow Behavior:</b>
47 * The function is implemented using a 64-bit internal accumulator.
48 * The input is represented in 1.15 format.
49 * Intermediate multiplication yields a 2.30 format, and this
50 * result is added without saturation to a 64-bit accumulator in 34.30 format.
51 * With 33 guard bits in the accumulator, there is no risk of overflow, and the
52 * full precision of the intermediate multiplication is preserved.
53 * Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
54 * 15 bits, and then saturated to yield a result in 1.15 format.
63 q63_t sum
= 0; /* accumulator */
65 #if defined (ARM_MATH_DSP)
66 /* Run the below code for Cortex-M4 and Cortex-M3 */
68 q31_t in
; /* temporary variable to store the input value */
69 q15_t in1
; /* temporary variable to store the input value */
70 uint32_t blkCnt
; /* loop counter */
73 blkCnt
= blockSize
>> 2U;
75 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
76 ** a second loop below computes the remaining 1 to 3 samples. */
79 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
80 /* Compute sum of the squares and then store the results in a temporary variable, sum */
81 in
= *__SIMD32(pSrc
)++;
82 sum
= __SMLALD(in
, in
, sum
);
83 in
= *__SIMD32(pSrc
)++;
84 sum
= __SMLALD(in
, in
, sum
);
86 /* Decrement the loop counter */
90 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
91 ** No loop unrolling is used. */
92 blkCnt
= blockSize
% 0x4U
;
96 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
97 /* Compute sum of the squares and then store the results in a temporary variable, sum */
99 sum
= __SMLALD(in1
, in1
, sum
);
101 /* Decrement the loop counter */
105 /* Truncating and saturating the accumulator to 1.15 format */
106 /* Store the result in the destination */
107 arm_sqrt_q15(__SSAT((sum
/ (q63_t
)blockSize
) >> 15, 16), pResult
);
110 /* Run the below code for Cortex-M0 */
112 q15_t in
; /* temporary variable to store the input value */
113 uint32_t blkCnt
; /* loop counter */
115 /* Loop over blockSize number of values */
120 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
121 /* Compute sum of the squares and then store the results in a temporary variable, sum */
123 sum
+= ((q31_t
) in
* in
);
125 /* Decrement the loop counter */
129 /* Truncating and saturating the accumulator to 1.15 format */
130 /* Store the result in the destination */
131 arm_sqrt_q15(__SSAT((sum
/ (q63_t
)blockSize
) >> 15, 16), pResult
);
133 #endif /* #if defined (ARM_MATH_DSP) */
138 * @} end of RMS group