arm_cmplx_mult_real_q31.c 6.3 KB

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