arm_cmplx_dot_prod_q15.c 7.1 KB

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