arm_cmplx_mult_real_q15.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mult_real_q15.c
  4. * Description: Q15 complex by real multiplication
  5. *
  6. * $Date: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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 complex input vector
  39. @param[in] pSrcReal points to real input vector
  40. @param[out] pCmplxDst points to complex output vector
  41. @param[in] numSamples number of samples in each vector
  42. @return none
  43. @par Scaling and Overflow Behavior
  44. The function uses saturating arithmetic.
  45. Results outside of the allowable Q15 range [0x8000 0x7FFF] are saturated.
  46. */
  47. void arm_cmplx_mult_real_q15(
  48. const q15_t * pSrcCmplx,
  49. const q15_t * pSrcReal,
  50. q15_t * pCmplxDst,
  51. uint32_t numSamples)
  52. {
  53. uint32_t blkCnt; /* Loop counter */
  54. q15_t in; /* Temporary variable */
  55. #if defined (ARM_MATH_LOOPUNROLL)
  56. #if defined (ARM_MATH_DSP)
  57. q31_t inA1, inA2; /* Temporary variables to hold input data */
  58. q31_t inB1; /* Temporary variables to hold input data */
  59. q15_t out1, out2, out3, out4; /* Temporary variables to hold output data */
  60. q31_t mul1, mul2, mul3, mul4; /* Temporary variables to hold intermediate data */
  61. #endif
  62. /* Loop unrolling: Compute 4 outputs at a time */
  63. blkCnt = numSamples >> 2U;
  64. while (blkCnt > 0U)
  65. {
  66. /* C[2 * i ] = A[2 * i ] * B[i]. */
  67. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  68. #if defined (ARM_MATH_DSP)
  69. /* read 2 complex numbers both real and imaginary from complex input buffer */
  70. inA1 = read_q15x2_ia ((q15_t **) &pSrcCmplx);
  71. inA2 = read_q15x2_ia ((q15_t **) &pSrcCmplx);
  72. /* read 2 real values at a time from real input buffer */
  73. inB1 = read_q15x2_ia ((q15_t **) &pSrcReal);
  74. /* multiply complex number with real numbers */
  75. #ifndef ARM_MATH_BIG_ENDIAN
  76. mul1 = (q31_t) ((q15_t) (inA1) * (q15_t) (inB1));
  77. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1));
  78. mul3 = (q31_t) ((q15_t) (inA2) * (q15_t) (inB1 >> 16));
  79. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB1 >> 16));
  80. #else
  81. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
  82. mul1 = (q31_t) ((q15_t) inA1 * (q15_t) (inB1 >> 16));
  83. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) inB1);
  84. mul3 = (q31_t) ((q15_t) inA2 * (q15_t) inB1);
  85. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  86. /* saturate the result */
  87. out1 = (q15_t) __SSAT(mul1 >> 15U, 16);
  88. out2 = (q15_t) __SSAT(mul2 >> 15U, 16);
  89. out3 = (q15_t) __SSAT(mul3 >> 15U, 16);
  90. out4 = (q15_t) __SSAT(mul4 >> 15U, 16);
  91. /* pack real and imaginary outputs and store them to destination */
  92. write_q15x2_ia (&pCmplxDst, __PKHBT(out1, out2, 16));
  93. write_q15x2_ia (&pCmplxDst, __PKHBT(out3, out4, 16));
  94. inA1 = read_q15x2_ia ((q15_t **) &pSrcCmplx);
  95. inA2 = read_q15x2_ia ((q15_t **) &pSrcCmplx);
  96. inB1 = read_q15x2_ia ((q15_t **) &pSrcReal);
  97. #ifndef ARM_MATH_BIG_ENDIAN
  98. mul1 = (q31_t) ((q15_t) (inA1) * (q15_t) (inB1));
  99. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1));
  100. mul3 = (q31_t) ((q15_t) (inA2) * (q15_t) (inB1 >> 16));
  101. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB1 >> 16));
  102. #else
  103. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
  104. mul1 = (q31_t) ((q15_t) inA1 * (q15_t) (inB1 >> 16));
  105. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) inB1);
  106. mul3 = (q31_t) ((q15_t) inA2 * (q15_t) inB1);
  107. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  108. out1 = (q15_t) __SSAT(mul1 >> 15U, 16);
  109. out2 = (q15_t) __SSAT(mul2 >> 15U, 16);
  110. out3 = (q15_t) __SSAT(mul3 >> 15U, 16);
  111. out4 = (q15_t) __SSAT(mul4 >> 15U, 16);
  112. write_q15x2_ia (&pCmplxDst, __PKHBT(out1, out2, 16));
  113. write_q15x2_ia (&pCmplxDst, __PKHBT(out3, out4, 16));
  114. #else
  115. in = *pSrcReal++;
  116. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  117. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  118. in = *pSrcReal++;
  119. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  120. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  121. in = *pSrcReal++;
  122. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  123. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  124. in = *pSrcReal++;
  125. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  126. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  127. #endif
  128. /* Decrement loop counter */
  129. blkCnt--;
  130. }
  131. /* Loop unrolling: Compute remaining outputs */
  132. blkCnt = numSamples % 0x4U;
  133. #else
  134. /* Initialize blkCnt with number of samples */
  135. blkCnt = numSamples;
  136. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  137. while (blkCnt > 0U)
  138. {
  139. /* C[2 * i ] = A[2 * i ] * B[i]. */
  140. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  141. in = *pSrcReal++;
  142. /* store the result in the destination buffer. */
  143. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  144. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  145. /* Decrement loop counter */
  146. blkCnt--;
  147. }
  148. }
  149. /**
  150. @} end of CmplxByRealMult group
  151. */