arm_cmplx_mult_real_q15.c 7.6 KB

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