arm_cmplx_mult_real_q15.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mult_real_q15.c
  4. * Description: Q15 complex by real multiplication
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 "dsp/complex_math_functions.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. @par Scaling and Overflow Behavior
  43. The function uses saturating arithmetic.
  44. Results outside of the allowable Q15 range [0x8000 0x7FFF] are saturated.
  45. */
  46. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  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. static const uint16_t stride_cmplx_x_real_16[8] = {
  54. 0, 0, 1, 1, 2, 2, 3, 3
  55. };
  56. q15x8_t rVec;
  57. q15x8_t cmplxVec;
  58. q15x8_t dstVec;
  59. uint16x8_t strideVec;
  60. uint32_t blockSizeC = numSamples * CMPLX_DIM; /* loop counters */
  61. uint32_t blkCnt;
  62. q15_t in;
  63. /*
  64. * stride vector for pairs of real generation
  65. */
  66. strideVec = vld1q(stride_cmplx_x_real_16);
  67. blkCnt = blockSizeC >> 3;
  68. while (blkCnt > 0U)
  69. {
  70. cmplxVec = vld1q(pSrcCmplx);
  71. rVec = vldrhq_gather_shifted_offset_s16(pSrcReal, strideVec);
  72. dstVec = vqdmulhq(cmplxVec, rVec);
  73. vst1q(pCmplxDst, dstVec);
  74. pSrcReal += 4;
  75. pSrcCmplx += 8;
  76. pCmplxDst += 8;
  77. blkCnt --;
  78. }
  79. /* Tail */
  80. blkCnt = (blockSizeC & 7) >> 1;
  81. while (blkCnt > 0U)
  82. {
  83. /* C[2 * i ] = A[2 * i ] * B[i]. */
  84. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  85. in = *pSrcReal++;
  86. /* store the result in the destination buffer. */
  87. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  88. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  89. /* Decrement loop counter */
  90. blkCnt--;
  91. }
  92. }
  93. #else
  94. void arm_cmplx_mult_real_q15(
  95. const q15_t * pSrcCmplx,
  96. const q15_t * pSrcReal,
  97. q15_t * pCmplxDst,
  98. uint32_t numSamples)
  99. {
  100. uint32_t blkCnt; /* Loop counter */
  101. q15_t in; /* Temporary variable */
  102. #if defined (ARM_MATH_LOOPUNROLL)
  103. #if defined (ARM_MATH_DSP)
  104. q31_t inA1, inA2; /* Temporary variables to hold input data */
  105. q31_t inB1; /* Temporary variables to hold input data */
  106. q15_t out1, out2, out3, out4; /* Temporary variables to hold output data */
  107. q31_t mul1, mul2, mul3, mul4; /* Temporary variables to hold intermediate data */
  108. #endif
  109. /* Loop unrolling: Compute 4 outputs at a time */
  110. blkCnt = numSamples >> 2U;
  111. while (blkCnt > 0U)
  112. {
  113. /* C[2 * i ] = A[2 * i ] * B[i]. */
  114. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  115. #if defined (ARM_MATH_DSP)
  116. /* read 2 complex numbers both real and imaginary from complex input buffer */
  117. inA1 = read_q15x2_ia (&pSrcCmplx);
  118. inA2 = read_q15x2_ia (&pSrcCmplx);
  119. /* read 2 real values at a time from real input buffer */
  120. inB1 = read_q15x2_ia (&pSrcReal);
  121. /* multiply complex number with real numbers */
  122. #ifndef ARM_MATH_BIG_ENDIAN
  123. mul1 = (q31_t) ((q15_t) (inA1) * (q15_t) (inB1));
  124. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1));
  125. mul3 = (q31_t) ((q15_t) (inA2) * (q15_t) (inB1 >> 16));
  126. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB1 >> 16));
  127. #else
  128. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
  129. mul1 = (q31_t) ((q15_t) inA1 * (q15_t) (inB1 >> 16));
  130. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) inB1);
  131. mul3 = (q31_t) ((q15_t) inA2 * (q15_t) inB1);
  132. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  133. /* saturate the result */
  134. out1 = (q15_t) __SSAT(mul1 >> 15U, 16);
  135. out2 = (q15_t) __SSAT(mul2 >> 15U, 16);
  136. out3 = (q15_t) __SSAT(mul3 >> 15U, 16);
  137. out4 = (q15_t) __SSAT(mul4 >> 15U, 16);
  138. /* pack real and imaginary outputs and store them to destination */
  139. write_q15x2_ia (&pCmplxDst, __PKHBT(out1, out2, 16));
  140. write_q15x2_ia (&pCmplxDst, __PKHBT(out3, out4, 16));
  141. inA1 = read_q15x2_ia (&pSrcCmplx);
  142. inA2 = read_q15x2_ia (&pSrcCmplx);
  143. inB1 = read_q15x2_ia (&pSrcReal);
  144. #ifndef ARM_MATH_BIG_ENDIAN
  145. mul1 = (q31_t) ((q15_t) (inA1) * (q15_t) (inB1));
  146. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1));
  147. mul3 = (q31_t) ((q15_t) (inA2) * (q15_t) (inB1 >> 16));
  148. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB1 >> 16));
  149. #else
  150. mul2 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
  151. mul1 = (q31_t) ((q15_t) inA1 * (q15_t) (inB1 >> 16));
  152. mul4 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) inB1);
  153. mul3 = (q31_t) ((q15_t) inA2 * (q15_t) inB1);
  154. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  155. out1 = (q15_t) __SSAT(mul1 >> 15U, 16);
  156. out2 = (q15_t) __SSAT(mul2 >> 15U, 16);
  157. out3 = (q15_t) __SSAT(mul3 >> 15U, 16);
  158. out4 = (q15_t) __SSAT(mul4 >> 15U, 16);
  159. write_q15x2_ia (&pCmplxDst, __PKHBT(out1, out2, 16));
  160. write_q15x2_ia (&pCmplxDst, __PKHBT(out3, out4, 16));
  161. #else
  162. in = *pSrcReal++;
  163. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  164. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  165. in = *pSrcReal++;
  166. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  167. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  168. in = *pSrcReal++;
  169. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  170. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  171. in = *pSrcReal++;
  172. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  173. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  174. #endif
  175. /* Decrement loop counter */
  176. blkCnt--;
  177. }
  178. /* Loop unrolling: Compute remaining outputs */
  179. blkCnt = numSamples % 0x4U;
  180. #else
  181. /* Initialize blkCnt with number of samples */
  182. blkCnt = numSamples;
  183. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  184. while (blkCnt > 0U)
  185. {
  186. /* C[2 * i ] = A[2 * i ] * B[i]. */
  187. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  188. in = *pSrcReal++;
  189. /* store the result in the destination buffer. */
  190. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  191. *pCmplxDst++ = (q15_t) __SSAT((((q31_t) *pSrcCmplx++ * in) >> 15), 16);
  192. /* Decrement loop counter */
  193. blkCnt--;
  194. }
  195. }
  196. #endif /* defined(ARM_MATH_MVEI) */
  197. /**
  198. @} end of CmplxByRealMult group
  199. */