arm_cmplx_dot_prod_q15.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_dot_prod_q15.c
  4. * Description: Processing function for the Q15 Complex Dot product
  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 cmplx_dot_prod
  34. @{
  35. */
  36. /**
  37. @brief Q15 complex dot product.
  38. @param[in] pSrcA points to the first input vector
  39. @param[in] pSrcB points to the second input vector
  40. @param[in] numSamples number of samples in each vector
  41. @param[out] realResult real part of the result returned here
  42. @param[out] imagResult imaginary part of the result returned her
  43. @par Scaling and Overflow Behavior
  44. The function is implemented using an internal 64-bit accumulator.
  45. The intermediate 1.15 by 1.15 multiplications are performed with full precision and yield a 2.30 result.
  46. These are accumulated in a 64-bit accumulator with 34.30 precision.
  47. As a final step, the accumulators are converted to 8.24 format.
  48. The return results <code>realResult</code> and <code>imagResult</code> are in 8.24 format.
  49. */
  50. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  51. void arm_cmplx_dot_prod_q15(
  52. const q15_t * pSrcA,
  53. const q15_t * pSrcB,
  54. uint32_t numSamples,
  55. q31_t * realResult,
  56. q31_t * imagResult)
  57. {
  58. int32_t blkCnt;
  59. q63_t accReal = 0LL;
  60. q63_t accImag = 0LL;
  61. q15x8_t vecSrcA, vecSrcB;
  62. q15x8_t vecSrcC, vecSrcD;
  63. blkCnt = (numSamples >> 3);
  64. blkCnt -= 1;
  65. if (blkCnt > 0) {
  66. /* should give more freedom to generate stall free code */
  67. vecSrcA = vld1q(pSrcA);
  68. vecSrcB = vld1q(pSrcB);
  69. pSrcA += 8;
  70. pSrcB += 8;
  71. while (blkCnt > 0) {
  72. accReal = vmlsldavaq(accReal, vecSrcA, vecSrcB);
  73. vecSrcC = vld1q(pSrcA);
  74. pSrcA += 8;
  75. accImag = vmlaldavaxq(accImag, vecSrcA, vecSrcB);
  76. vecSrcD = vld1q(pSrcB);
  77. pSrcB += 8;
  78. accReal = vmlsldavaq(accReal, vecSrcC, vecSrcD);
  79. vecSrcA = vld1q(pSrcA);
  80. pSrcA += 8;
  81. accImag = vmlaldavaxq(accImag, vecSrcC, vecSrcD);
  82. vecSrcB = vld1q(pSrcB);
  83. pSrcB += 8;
  84. /*
  85. * Decrement the blockSize loop counter
  86. */
  87. blkCnt--;
  88. }
  89. /* process last elements out of the loop avoid the armclang breaking the SW pipeline */
  90. accReal = vmlsldavaq(accReal, vecSrcA, vecSrcB);
  91. vecSrcC = vld1q(pSrcA);
  92. accImag = vmlaldavaxq(accImag, vecSrcA, vecSrcB);
  93. vecSrcD = vld1q(pSrcB);
  94. accReal = vmlsldavaq(accReal, vecSrcC, vecSrcD);
  95. vecSrcA = vld1q(pSrcA);
  96. accImag = vmlaldavaxq(accImag, vecSrcC, vecSrcD);
  97. vecSrcB = vld1q(pSrcB);
  98. /*
  99. * tail
  100. */
  101. blkCnt = CMPLX_DIM * (numSamples & 7);
  102. do {
  103. mve_pred16_t p = vctp16q(blkCnt);
  104. pSrcA += 8;
  105. pSrcB += 8;
  106. vecSrcA = vldrhq_z_s16(pSrcA, p);
  107. vecSrcB = vldrhq_z_s16(pSrcB, p);
  108. accReal = vmlsldavaq_p(accReal, vecSrcA, vecSrcB, p);
  109. accImag = vmlaldavaxq_p(accImag, vecSrcA, vecSrcB, p);
  110. blkCnt -= 8;
  111. }
  112. while ((int32_t) blkCnt > 0);
  113. } else {
  114. blkCnt = numSamples * CMPLX_DIM;
  115. while (blkCnt > 0) {
  116. mve_pred16_t p = vctp16q(blkCnt);
  117. vecSrcA = vldrhq_z_s16(pSrcA, p);
  118. vecSrcB = vldrhq_z_s16(pSrcB, p);
  119. accReal = vmlsldavaq_p(accReal, vecSrcA, vecSrcB, p);
  120. accImag = vmlaldavaxq_p(accImag, vecSrcA, vecSrcB, p);
  121. /*
  122. * Decrement the blkCnt loop counter
  123. * Advance vector source and destination pointers
  124. */
  125. pSrcA += 8;
  126. pSrcB += 8;
  127. blkCnt -= 8;
  128. }
  129. }
  130. *realResult = asrl(accReal, (14 - 8));
  131. *imagResult = asrl(accImag, (14 - 8));
  132. }
  133. #else
  134. void arm_cmplx_dot_prod_q15(
  135. const q15_t * pSrcA,
  136. const q15_t * pSrcB,
  137. uint32_t numSamples,
  138. q31_t * realResult,
  139. q31_t * imagResult)
  140. {
  141. uint32_t blkCnt; /* Loop counter */
  142. q63_t real_sum = 0, imag_sum = 0; /* Temporary result variables */
  143. q15_t a0,b0,c0,d0;
  144. #if defined (ARM_MATH_LOOPUNROLL)
  145. /* Loop unrolling: Compute 4 outputs at a time */
  146. blkCnt = numSamples >> 2U;
  147. while (blkCnt > 0U)
  148. {
  149. a0 = *pSrcA++;
  150. b0 = *pSrcA++;
  151. c0 = *pSrcB++;
  152. d0 = *pSrcB++;
  153. real_sum += (q31_t)a0 * c0;
  154. imag_sum += (q31_t)a0 * d0;
  155. real_sum -= (q31_t)b0 * d0;
  156. imag_sum += (q31_t)b0 * c0;
  157. a0 = *pSrcA++;
  158. b0 = *pSrcA++;
  159. c0 = *pSrcB++;
  160. d0 = *pSrcB++;
  161. real_sum += (q31_t)a0 * c0;
  162. imag_sum += (q31_t)a0 * d0;
  163. real_sum -= (q31_t)b0 * d0;
  164. imag_sum += (q31_t)b0 * c0;
  165. a0 = *pSrcA++;
  166. b0 = *pSrcA++;
  167. c0 = *pSrcB++;
  168. d0 = *pSrcB++;
  169. real_sum += (q31_t)a0 * c0;
  170. imag_sum += (q31_t)a0 * d0;
  171. real_sum -= (q31_t)b0 * d0;
  172. imag_sum += (q31_t)b0 * c0;
  173. a0 = *pSrcA++;
  174. b0 = *pSrcA++;
  175. c0 = *pSrcB++;
  176. d0 = *pSrcB++;
  177. real_sum += (q31_t)a0 * c0;
  178. imag_sum += (q31_t)a0 * d0;
  179. real_sum -= (q31_t)b0 * d0;
  180. imag_sum += (q31_t)b0 * c0;
  181. /* Decrement loop counter */
  182. blkCnt--;
  183. }
  184. /* Loop unrolling: Compute remaining outputs */
  185. blkCnt = numSamples % 0x4U;
  186. #else
  187. /* Initialize blkCnt with number of samples */
  188. blkCnt = numSamples;
  189. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  190. while (blkCnt > 0U)
  191. {
  192. a0 = *pSrcA++;
  193. b0 = *pSrcA++;
  194. c0 = *pSrcB++;
  195. d0 = *pSrcB++;
  196. real_sum += (q31_t)a0 * c0;
  197. imag_sum += (q31_t)a0 * d0;
  198. real_sum -= (q31_t)b0 * d0;
  199. imag_sum += (q31_t)b0 * c0;
  200. /* Decrement loop counter */
  201. blkCnt--;
  202. }
  203. /* Store real and imaginary result in 8.24 format */
  204. /* Convert real data in 34.30 to 8.24 by 6 right shifts */
  205. *realResult = (q31_t) (real_sum >> 6);
  206. /* Convert imaginary data in 34.30 to 8.24 by 6 right shifts */
  207. *imagResult = (q31_t) (imag_sum >> 6);
  208. }
  209. #endif /* defined(ARM_MATH_MVEI) */
  210. /**
  211. @} end of cmplx_dot_prod group
  212. */