arm_cmplx_mult_real_q31.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mult_real_q31.c
  4. * Description: Q31 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 Q31 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 Q31 range[0x80000000 0x7FFFFFFF] are saturated.
  45. */
  46. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  47. void arm_cmplx_mult_real_q31(
  48. const q31_t * pSrcCmplx,
  49. const q31_t * pSrcReal,
  50. q31_t * pCmplxDst,
  51. uint32_t numSamples)
  52. {
  53. static const uint32_t stride_cmplx_x_real_32[4] = {
  54. 0, 0, 1, 1
  55. };
  56. q31x4_t rVec;
  57. q31x4_t cmplxVec;
  58. q31x4_t dstVec;
  59. uint32x4_t strideVec;
  60. uint32_t blockSizeC = numSamples * CMPLX_DIM; /* loop counters */
  61. uint32_t blkCnt;
  62. q31_t in;
  63. /*
  64. * stride vector for pairs of real generation
  65. */
  66. strideVec = vld1q(stride_cmplx_x_real_32);
  67. /* Compute 4 complex outputs at a time */
  68. blkCnt = blockSizeC >> 2;
  69. while (blkCnt > 0U)
  70. {
  71. cmplxVec = vld1q(pSrcCmplx);
  72. rVec = vldrwq_gather_shifted_offset_s32(pSrcReal, strideVec);
  73. dstVec = vqdmulhq(cmplxVec, rVec);
  74. vst1q(pCmplxDst, dstVec);
  75. pSrcReal += 2;
  76. pSrcCmplx += 4;
  77. pCmplxDst += 4;
  78. blkCnt --;
  79. }
  80. blkCnt = (blockSizeC & 3) >> 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 saturated result in 1.31 format to destination buffer */
  87. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  88. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  89. /* Decrement loop counter */
  90. blkCnt--;
  91. }
  92. }
  93. #else
  94. void arm_cmplx_mult_real_q31(
  95. const q31_t * pSrcCmplx,
  96. const q31_t * pSrcReal,
  97. q31_t * pCmplxDst,
  98. uint32_t numSamples)
  99. {
  100. uint32_t blkCnt; /* Loop counter */
  101. q31_t in; /* Temporary variable */
  102. #if defined (ARM_MATH_LOOPUNROLL)
  103. /* Loop unrolling: Compute 4 outputs at a time */
  104. blkCnt = numSamples >> 2U;
  105. while (blkCnt > 0U)
  106. {
  107. /* C[2 * i ] = A[2 * i ] * B[i]. */
  108. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  109. in = *pSrcReal++;
  110. #if defined (ARM_MATH_DSP)
  111. /* store saturated result in 1.31 format to destination buffer */
  112. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  113. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  114. #else
  115. /* store result in destination buffer. */
  116. *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
  117. *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
  118. #endif
  119. in = *pSrcReal++;
  120. #if defined (ARM_MATH_DSP)
  121. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  122. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  123. #else
  124. *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
  125. *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
  126. #endif
  127. in = *pSrcReal++;
  128. #if defined (ARM_MATH_DSP)
  129. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  130. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  131. #else
  132. *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
  133. *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
  134. #endif
  135. in = *pSrcReal++;
  136. #if defined (ARM_MATH_DSP)
  137. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  138. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  139. #else
  140. *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
  141. *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
  142. #endif
  143. /* Decrement loop counter */
  144. blkCnt--;
  145. }
  146. /* Loop unrolling: Compute remaining outputs */
  147. blkCnt = numSamples % 0x4U;
  148. #else
  149. /* Initialize blkCnt with number of samples */
  150. blkCnt = numSamples;
  151. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  152. while (blkCnt > 0U)
  153. {
  154. /* C[2 * i ] = A[2 * i ] * B[i]. */
  155. /* C[2 * i + 1] = A[2 * i + 1] * B[i]. */
  156. in = *pSrcReal++;
  157. #if defined (ARM_MATH_DSP)
  158. /* store saturated result in 1.31 format to destination buffer */
  159. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  160. *pCmplxDst++ = (__SSAT((q31_t) (((q63_t) *pSrcCmplx++ * in) >> 32), 31) << 1);
  161. #else
  162. /* store result in destination buffer. */
  163. *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
  164. *pCmplxDst++ = (q31_t) clip_q63_to_q31(((q63_t) *pSrcCmplx++ * in) >> 31);
  165. #endif
  166. /* Decrement loop counter */
  167. blkCnt--;
  168. }
  169. }
  170. #endif /* defined(ARM_MATH_MVEI) */
  171. /**
  172. @} end of CmplxByRealMult group
  173. */