arm_cmplx_dot_prod_q31.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_dot_prod_q31.c
  4. * Description: Q31 complex dot product
  5. *
  6. * $Date: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 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 cmplx_dot_prod
  34. * @{
  35. */
  36. /**
  37. * @brief Q31 complex dot product
  38. * @param *pSrcA points to the first input vector
  39. * @param *pSrcB points to the second input vector
  40. * @param numSamples number of complex samples in each vector
  41. * @param *realResult real part of the result returned here
  42. * @param *imagResult imaginary part of the result returned here
  43. * @return none.
  44. *
  45. * <b>Scaling and Overflow Behavior:</b>
  46. * \par
  47. * The function is implemented using an internal 64-bit accumulator.
  48. * The intermediate 1.31 by 1.31 multiplications are performed with 64-bit precision and then shifted to 16.48 format.
  49. * The internal real and imaginary accumulators are in 16.48 format and provide 15 guard bits.
  50. * Additions are nonsaturating and no overflow will occur as long as <code>numSamples</code> is less than 32768.
  51. * The return results <code>realResult</code> and <code>imagResult</code> are in 16.48 format.
  52. * Input down scaling is not required.
  53. */
  54. void arm_cmplx_dot_prod_q31(
  55. q31_t * pSrcA,
  56. q31_t * pSrcB,
  57. uint32_t numSamples,
  58. q63_t * realResult,
  59. q63_t * imagResult)
  60. {
  61. q63_t real_sum = 0, imag_sum = 0; /* Temporary result storage */
  62. q31_t a0,b0,c0,d0;
  63. #if defined (ARM_MATH_DSP)
  64. /* Run the below code for Cortex-M4 and Cortex-M3 */
  65. uint32_t blkCnt; /* loop counter */
  66. /*loop Unrolling */
  67. blkCnt = numSamples >> 2U;
  68. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  69. ** a second loop below computes the remaining 1 to 3 samples. */
  70. while (blkCnt > 0U)
  71. {
  72. a0 = *pSrcA++;
  73. b0 = *pSrcA++;
  74. c0 = *pSrcB++;
  75. d0 = *pSrcB++;
  76. real_sum += ((q63_t)a0 * c0) >> 14;
  77. imag_sum += ((q63_t)a0 * d0) >> 14;
  78. real_sum -= ((q63_t)b0 * d0) >> 14;
  79. imag_sum += ((q63_t)b0 * c0) >> 14;
  80. a0 = *pSrcA++;
  81. b0 = *pSrcA++;
  82. c0 = *pSrcB++;
  83. d0 = *pSrcB++;
  84. real_sum += ((q63_t)a0 * c0) >> 14;
  85. imag_sum += ((q63_t)a0 * d0) >> 14;
  86. real_sum -= ((q63_t)b0 * d0) >> 14;
  87. imag_sum += ((q63_t)b0 * c0) >> 14;
  88. a0 = *pSrcA++;
  89. b0 = *pSrcA++;
  90. c0 = *pSrcB++;
  91. d0 = *pSrcB++;
  92. real_sum += ((q63_t)a0 * c0) >> 14;
  93. imag_sum += ((q63_t)a0 * d0) >> 14;
  94. real_sum -= ((q63_t)b0 * d0) >> 14;
  95. imag_sum += ((q63_t)b0 * c0) >> 14;
  96. a0 = *pSrcA++;
  97. b0 = *pSrcA++;
  98. c0 = *pSrcB++;
  99. d0 = *pSrcB++;
  100. real_sum += ((q63_t)a0 * c0) >> 14;
  101. imag_sum += ((q63_t)a0 * d0) >> 14;
  102. real_sum -= ((q63_t)b0 * d0) >> 14;
  103. imag_sum += ((q63_t)b0 * c0) >> 14;
  104. /* Decrement the loop counter */
  105. blkCnt--;
  106. }
  107. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  108. ** No loop unrolling is used. */
  109. blkCnt = numSamples % 0x4U;
  110. while (blkCnt > 0U)
  111. {
  112. a0 = *pSrcA++;
  113. b0 = *pSrcA++;
  114. c0 = *pSrcB++;
  115. d0 = *pSrcB++;
  116. real_sum += ((q63_t)a0 * c0) >> 14;
  117. imag_sum += ((q63_t)a0 * d0) >> 14;
  118. real_sum -= ((q63_t)b0 * d0) >> 14;
  119. imag_sum += ((q63_t)b0 * c0) >> 14;
  120. /* Decrement the loop counter */
  121. blkCnt--;
  122. }
  123. #else
  124. /* Run the below code for Cortex-M0 */
  125. while (numSamples > 0U)
  126. {
  127. a0 = *pSrcA++;
  128. b0 = *pSrcA++;
  129. c0 = *pSrcB++;
  130. d0 = *pSrcB++;
  131. real_sum += ((q63_t)a0 * c0) >> 14;
  132. imag_sum += ((q63_t)a0 * d0) >> 14;
  133. real_sum -= ((q63_t)b0 * d0) >> 14;
  134. imag_sum += ((q63_t)b0 * c0) >> 14;
  135. /* Decrement the loop counter */
  136. numSamples--;
  137. }
  138. #endif /* #if defined (ARM_MATH_DSP) */
  139. /* Store the real and imaginary results in 16.48 format */
  140. *realResult = real_sum;
  141. *imagResult = imag_sum;
  142. }
  143. /**
  144. * @} end of cmplx_dot_prod group
  145. */