1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_cmplx_mag_q15.c
4 * Description: Q15 complex magnitude
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.
32 * @ingroup groupCmplxMath
36 * @addtogroup cmplx_mag
42 * @brief Q15 complex magnitude
43 * @param *pSrc points to the complex input vector
44 * @param *pDst points to the real output vector
45 * @param numSamples number of complex samples in the input vector
48 * <b>Scaling and Overflow Behavior:</b>
50 * The function implements 1.15 by 1.15 multiplications and finally output is converted into 2.14 format.
53 void arm_cmplx_mag_q15(
58 q31_t acc0
, acc1
; /* Accumulators */
60 #if defined (ARM_MATH_DSP)
62 /* Run the below code for Cortex-M4 and Cortex-M3 */
63 uint32_t blkCnt
; /* loop counter */
64 q31_t in1
, in2
, in3
, in4
;
69 blkCnt
= numSamples
>> 2U;
71 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
72 ** a second loop below computes the remaining 1 to 3 samples. */
76 /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
77 in1
= *__SIMD32(pSrc
)++;
78 in2
= *__SIMD32(pSrc
)++;
79 in3
= *__SIMD32(pSrc
)++;
80 in4
= *__SIMD32(pSrc
)++;
82 acc0
= __SMUAD(in1
, in1
);
83 acc1
= __SMUAD(in2
, in2
);
84 acc2
= __SMUAD(in3
, in3
);
85 acc3
= __SMUAD(in4
, in4
);
87 /* store the result in 2.14 format in the destination buffer. */
88 arm_sqrt_q15((q15_t
) ((acc0
) >> 17), pDst
++);
89 arm_sqrt_q15((q15_t
) ((acc1
) >> 17), pDst
++);
90 arm_sqrt_q15((q15_t
) ((acc2
) >> 17), pDst
++);
91 arm_sqrt_q15((q15_t
) ((acc3
) >> 17), pDst
++);
93 /* Decrement the loop counter */
97 /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
98 ** No loop unrolling is used. */
99 blkCnt
= numSamples
% 0x4U
;
103 /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
104 in1
= *__SIMD32(pSrc
)++;
105 acc0
= __SMUAD(in1
, in1
);
107 /* store the result in 2.14 format in the destination buffer. */
108 arm_sqrt_q15((q15_t
) (acc0
>> 17), pDst
++);
110 /* Decrement the loop counter */
116 /* Run the below code for Cortex-M0 */
117 q15_t real
, imag
; /* Temporary variables to hold input values */
119 while (numSamples
> 0U)
121 /* out = sqrt(real * real + imag * imag) */
125 acc0
= (real
* real
);
126 acc1
= (imag
* imag
);
128 /* store the result in 2.14 format in the destination buffer. */
129 arm_sqrt_q15((q15_t
) (((q63_t
) acc0
+ acc1
) >> 17), pDst
++);
131 /* Decrement the loop counter */
135 #endif /* #if defined (ARM_MATH_DSP) */
140 * @} end of cmplx_mag group