arm_cmplx_mult_cmplx_f16.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mult_cmplx_f16.c
  4. * Description: Floating-point complex-by-complex 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_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. /**
  31. @ingroup groupCmplxMath
  32. */
  33. /**
  34. @defgroup CmplxByCmplxMult Complex-by-Complex Multiplication
  35. Multiplies a complex vector by another complex vector and generates a complex result.
  36. The data in the complex arrays is stored in an interleaved fashion
  37. (real, imag, real, imag, ...).
  38. The parameter <code>numSamples</code> represents the number of complex
  39. samples processed. The complex arrays have a total of <code>2*numSamples</code>
  40. real values.
  41. The underlying algorithm is used:
  42. <pre>
  43. for (n = 0; n < numSamples; n++) {
  44. pDst[(2*n)+0] = pSrcA[(2*n)+0] * pSrcB[(2*n)+0] - pSrcA[(2*n)+1] * pSrcB[(2*n)+1];
  45. pDst[(2*n)+1] = pSrcA[(2*n)+0] * pSrcB[(2*n)+1] + pSrcA[(2*n)+1] * pSrcB[(2*n)+0];
  46. }
  47. </pre>
  48. There are separate functions for floating-point, Q15, and Q31 data types.
  49. */
  50. /**
  51. @addtogroup CmplxByCmplxMult
  52. @{
  53. */
  54. /**
  55. @brief Floating-point complex-by-complex multiplication.
  56. @param[in] pSrcA points to first input vector
  57. @param[in] pSrcB points to second input vector
  58. @param[out] pDst points to output vector
  59. @param[in] numSamples number of samples in each vector
  60. @return none
  61. */
  62. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  63. void arm_cmplx_mult_cmplx_f16(
  64. const float16_t * pSrcA,
  65. const float16_t * pSrcB,
  66. float16_t * pDst,
  67. uint32_t numSamples)
  68. {
  69. int32_t blkCnt;
  70. f16x8_t vecSrcA, vecSrcB;
  71. f16x8_t vecSrcC, vecSrcD;
  72. f16x8_t vec_acc;
  73. blkCnt = (numSamples >> 3);
  74. blkCnt -= 1;
  75. if (blkCnt > 0) {
  76. /* should give more freedom to generate stall free code */
  77. vecSrcA = vld1q(pSrcA);
  78. vecSrcB = vld1q(pSrcB);
  79. pSrcA += 8;
  80. pSrcB += 8;
  81. while (blkCnt > 0) {
  82. vec_acc = vcmulq(vecSrcA, vecSrcB);
  83. vecSrcC = vld1q(pSrcA);
  84. pSrcA += 8;
  85. vec_acc = vcmlaq_rot90(vec_acc, vecSrcA, vecSrcB);
  86. vecSrcD = vld1q(pSrcB);
  87. pSrcB += 8;
  88. vst1q(pDst, vec_acc);
  89. pDst += 8;
  90. vec_acc = vcmulq(vecSrcC, vecSrcD);
  91. vecSrcA = vld1q(pSrcA);
  92. pSrcA += 8;
  93. vec_acc = vcmlaq_rot90(vec_acc, vecSrcC, vecSrcD);
  94. vecSrcB = vld1q(pSrcB);
  95. pSrcB += 8;
  96. vst1q(pDst, vec_acc);
  97. pDst += 8;
  98. /*
  99. * Decrement the blockSize loop counter
  100. */
  101. blkCnt--;
  102. }
  103. /* process last elements out of the loop avoid the armclang breaking the SW pipeline */
  104. vec_acc = vcmulq(vecSrcA, vecSrcB);
  105. vecSrcC = vld1q(pSrcA);
  106. vec_acc = vcmlaq_rot90(vec_acc, vecSrcA, vecSrcB);
  107. vecSrcD = vld1q(pSrcB);
  108. vst1q(pDst, vec_acc);
  109. pDst += 8;
  110. vec_acc = vcmulq(vecSrcC, vecSrcD);
  111. vec_acc = vcmlaq_rot90(vec_acc, vecSrcC, vecSrcD);
  112. vst1q(pDst, vec_acc);
  113. pDst += 8;
  114. /*
  115. * tail
  116. */
  117. blkCnt = CMPLX_DIM * (numSamples & 7);
  118. while (blkCnt > 0) {
  119. mve_pred16_t p = vctp16q(blkCnt);
  120. pSrcA += 8;
  121. pSrcB += 8;
  122. vecSrcA = vldrhq_z_f16(pSrcA, p);
  123. vecSrcB = vldrhq_z_f16(pSrcB, p);
  124. vec_acc = vcmulq_m(vuninitializedq_f16(),vecSrcA, vecSrcB, p);
  125. vec_acc = vcmlaq_rot90_m(vec_acc, vecSrcA, vecSrcB, p);
  126. vstrhq_p_f16(pDst, vec_acc, p);
  127. pDst += 8;
  128. blkCnt -= 8;
  129. }
  130. } else {
  131. /* small vector */
  132. blkCnt = numSamples * CMPLX_DIM;
  133. do {
  134. mve_pred16_t p = vctp16q(blkCnt);
  135. vecSrcA = vldrhq_z_f16(pSrcA, p);
  136. vecSrcB = vldrhq_z_f16(pSrcB, p);
  137. vec_acc = vcmulq_m(vuninitializedq_f16(),vecSrcA, vecSrcB, p);
  138. vec_acc = vcmlaq_rot90_m(vec_acc, vecSrcA, vecSrcB, p);
  139. vstrhq_p_f16(pDst, vec_acc, p);
  140. pDst += 8;
  141. /*
  142. * Decrement the blkCnt loop counter
  143. * Advance vector source and destination pointers
  144. */
  145. pSrcA += 8;
  146. pSrcB += 8;
  147. blkCnt -= 8;
  148. }
  149. while (blkCnt > 0);
  150. }
  151. }
  152. #else
  153. void arm_cmplx_mult_cmplx_f16(
  154. const float16_t * pSrcA,
  155. const float16_t * pSrcB,
  156. float16_t * pDst,
  157. uint32_t numSamples)
  158. {
  159. uint32_t blkCnt; /* Loop counter */
  160. _Float16 a, b, c, d; /* Temporary variables to store real and imaginary values */
  161. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  162. /* Loop unrolling: Compute 4 outputs at a time */
  163. blkCnt = numSamples >> 2U;
  164. while (blkCnt > 0U)
  165. {
  166. /* C[2 * i ] = A[2 * i] * B[2 * i ] - A[2 * i + 1] * B[2 * i + 1]. */
  167. /* C[2 * i + 1] = A[2 * i] * B[2 * i + 1] + A[2 * i + 1] * B[2 * i ]. */
  168. a = *pSrcA++;
  169. b = *pSrcA++;
  170. c = *pSrcB++;
  171. d = *pSrcB++;
  172. /* store result in destination buffer. */
  173. *pDst++ = (a * c) - (b * d);
  174. *pDst++ = (a * d) + (b * c);
  175. a = *pSrcA++;
  176. b = *pSrcA++;
  177. c = *pSrcB++;
  178. d = *pSrcB++;
  179. *pDst++ = (a * c) - (b * d);
  180. *pDst++ = (a * d) + (b * c);
  181. a = *pSrcA++;
  182. b = *pSrcA++;
  183. c = *pSrcB++;
  184. d = *pSrcB++;
  185. *pDst++ = (a * c) - (b * d);
  186. *pDst++ = (a * d) + (b * c);
  187. a = *pSrcA++;
  188. b = *pSrcA++;
  189. c = *pSrcB++;
  190. d = *pSrcB++;
  191. *pDst++ = (a * c) - (b * d);
  192. *pDst++ = (a * d) + (b * c);
  193. /* Decrement loop counter */
  194. blkCnt--;
  195. }
  196. /* Loop unrolling: Compute remaining outputs */
  197. blkCnt = numSamples % 0x4U;
  198. #else
  199. /* Initialize blkCnt with number of samples */
  200. blkCnt = numSamples;
  201. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  202. while (blkCnt > 0U)
  203. {
  204. /* C[2 * i ] = A[2 * i] * B[2 * i ] - A[2 * i + 1] * B[2 * i + 1]. */
  205. /* C[2 * i + 1] = A[2 * i] * B[2 * i + 1] + A[2 * i + 1] * B[2 * i ]. */
  206. a = *pSrcA++;
  207. b = *pSrcA++;
  208. c = *pSrcB++;
  209. d = *pSrcB++;
  210. /* store result in destination buffer. */
  211. *pDst++ = (a * c) - (b * d);
  212. *pDst++ = (a * d) + (b * c);
  213. /* Decrement loop counter */
  214. blkCnt--;
  215. }
  216. }
  217. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  218. /**
  219. @} end of CmplxByCmplxMult group
  220. */
  221. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */