arm_cmplx_mult_real_q15.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mult_real_q15.c
  4. * Description: Q15 complex by real multiplication
  5. *
  6. * $Date: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  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
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  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.
  27. */
  28. #include "arm_math.h"
  29. /**
  30. * @ingroup groupCmplxMath
  31. */
  32. /**
  33. * @addtogroup CmplxByRealMult
  34. * @{
  35. */
  36. /**
  37. * @brief Q15 complex-by-real multiplication
  38. * @param[in] *pSrcCmplx points to the complex input vector
  39. * @param[in] *pSrcReal points to the real input vector
  40. * @param[out] *pCmplxDst points to the complex output vector
  41. * @param[in] numSamples number of samples in each vector
  42. * @return none.
  43. *
  44. * <b>Scaling and Overflow Behavior:</b>
  45. * \par
  46. * The function uses saturating arithmetic.
  47. * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.
  48. */
  49. void arm_cmplx_mult_real_q15(
  50. q15_t * pSrcCmplx,
  51. q15_t * pSrcReal,
  52. q15_t * pCmplxDst,
  53. uint32_t numSamples)
  54. {
  55. q15_t in; /* Temporary variable to store input value */
  56. #if defined (ARM_MATH_DSP)
  57. /* Run the below code for Cortex-M4 and Cortex-M3 */
  58. uint32_t blkCnt; /* loop counters */
  59. q31_t inA1, inA2; /* Temporary variables to hold input data */
  60. q31_t inB1; /* Temporary variables to hold input data */
  61. q15_t out1, out2, out3, out4; /* Temporary variables to hold output data */
  62. q31_t mul1, mul2, mul3, mul4; /* Temporary variables to hold intermediate data */
  63. /* loop Unrolling */
  64. blkCnt = numSamples >> 2U;
  65. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  66. ** a second loop below computes the remaining 1 to 3 samples. */
  67. while (blkCnt > 0U)
  68. {
  69. /* C[2 * i] = A[2 * i] * B[i]. */
  70. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  71. /* read complex number both real and imaginary from complex input buffer */
  72. inA1 = *__SIMD32(pSrcCmplx)++;
  73. /* read two real values at a time from real input buffer */
  74. inB1 = *__SIMD32(pSrcReal)++;
  75. /* read complex number both real and imaginary from complex input buffer */
  76. inA2 = *__SIMD32(pSrcCmplx)++;
  77. /* multiply complex number with real numbers */
  78. #ifndef ARM_MATH_BIG_ENDIAN
  79. mul1 = (q31_t) ((q15_t) (inA1) * (q15_t) (inB1));
  80. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1));
  81. mul3 = (q31_t) ((q15_t) (inA2) * (q15_t) (inB1 >> 16));
  82. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB1 >> 16));
  83. #else
  84. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
  85. mul1 = (q31_t) ((q15_t) inA1 * (q15_t) (inB1 >> 16));
  86. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) inB1);
  87. mul3 = (q31_t) ((q15_t) inA2 * (q15_t) inB1);
  88. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  89. /* saturate the result */
  90. out1 = (q15_t) __SSAT(mul1 >> 15U, 16);
  91. out2 = (q15_t) __SSAT(mul2 >> 15U, 16);
  92. out3 = (q15_t) __SSAT(mul3 >> 15U, 16);
  93. out4 = (q15_t) __SSAT(mul4 >> 15U, 16);
  94. /* pack real and imaginary outputs and store them to destination */
  95. *__SIMD32(pCmplxDst)++ = __PKHBT(out1, out2, 16);
  96. *__SIMD32(pCmplxDst)++ = __PKHBT(out3, out4, 16);
  97. inA1 = *__SIMD32(pSrcCmplx)++;
  98. inB1 = *__SIMD32(pSrcReal)++;
  99. inA2 = *__SIMD32(pSrcCmplx)++;
  100. #ifndef ARM_MATH_BIG_ENDIAN
  101. mul1 = (q31_t) ((q15_t) (inA1) * (q15_t) (inB1));
  102. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1));
  103. mul3 = (q31_t) ((q15_t) (inA2) * (q15_t) (inB1 >> 16));
  104. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB1 >> 16));
  105. #else
  106. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
  107. mul1 = (q31_t) ((q15_t) inA1 * (q15_t) (inB1 >> 16));
  108. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) inB1);
  109. mul3 = (q31_t) ((q15_t) inA2 * (q15_t) inB1);
  110. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  111. out1 = (q15_t) __SSAT(mul1 >> 15U, 16);
  112. out2 = (q15_t) __SSAT(mul2 >> 15U, 16);
  113. out3 = (q15_t) __SSAT(mul3 >> 15U, 16);
  114. out4 = (q15_t) __SSAT(mul4 >> 15U, 16);
  115. *__SIMD32(pCmplxDst)++ = __PKHBT(out1, out2, 16);
  116. *__SIMD32(pCmplxDst)++ = __PKHBT(out3, out4, 16);
  117. /* Decrement the numSamples loop counter */
  118. blkCnt--;
  119. }
  120. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  121. ** No loop unrolling is used. */
  122. blkCnt = numSamples % 0x4U;
  123. while (blkCnt > 0U)
  124. {
  125. /* C[2 * i] = A[2 * i] * B[i]. */
  126. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  127. in = *pSrcReal++;
  128. /* store the result in the destination buffer. */
  129. *pCmplxDst++ =
  130. (q15_t) __SSAT((((q31_t) (*pSrcCmplx++) * (in)) >> 15), 16);
  131. *pCmplxDst++ =
  132. (q15_t) __SSAT((((q31_t) (*pSrcCmplx++) * (in)) >> 15), 16);
  133. /* Decrement the numSamples loop counter */
  134. blkCnt--;
  135. }
  136. #else
  137. /* Run the below code for Cortex-M0 */
  138. while (numSamples > 0U)
  139. {
  140. /* realOut = realA * realB. */
  141. /* imagOut = imagA * realB. */
  142. in = *pSrcReal++;
  143. /* store the result in the destination buffer. */
  144. *pCmplxDst++ =
  145. (q15_t) __SSAT((((q31_t) (*pSrcCmplx++) * (in)) >> 15), 16);
  146. *pCmplxDst++ =
  147. (q15_t) __SSAT((((q31_t) (*pSrcCmplx++) * (in)) >> 15), 16);
  148. /* Decrement the numSamples loop counter */
  149. numSamples--;
  150. }
  151. #endif /* #if defined (ARM_MATH_DSP) */
  152. }
  153. /**
  154. * @} end of CmplxByRealMult group
  155. */