arm_accumulate_f32.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_accumulate_f32.c
  4. * Description: Sum value of a floating-point vector
  5. *
  6. * $Date: 14 July 2022
  7. * $Revision: V1.0.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/statistics_functions.h"
  29. /**
  30. @ingroup groupStats
  31. */
  32. /**
  33. @addtogroup Accumulation
  34. @{
  35. */
  36. /**
  37. @brief Accumulation value of a floating-point vector.
  38. @param[in] pSrc points to the input vector.
  39. @param[in] blockSize number of samples in input vector.
  40. @param[out] pResult sum of values in input vector.
  41. */
  42. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  43. #include "arm_helium_utils.h"
  44. void arm_accumulate_f32(
  45. const float32_t * pSrc,
  46. uint32_t blockSize,
  47. float32_t * pResult)
  48. {
  49. f32x4_t vecA;
  50. f32x4_t vecSum;
  51. uint32_t blkCnt;
  52. float32_t sum = 0.0f;
  53. vecSum = vdupq_n_f32(0.0f);
  54. /* Compute 4 outputs at a time */
  55. blkCnt = blockSize >> 2U;
  56. while (blkCnt > 0U)
  57. {
  58. /*
  59. * C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1]
  60. * Calculate dot product and then store the result in a temporary buffer.
  61. * and advance vector source and destination pointers
  62. */
  63. vecA = vld1q_f32(pSrc);
  64. pSrc += 4;
  65. vecSum = vaddq_f32(vecSum, vecA);
  66. /*
  67. * Decrement the blockSize loop counter
  68. */
  69. blkCnt --;
  70. }
  71. blkCnt = blockSize & 3;
  72. if (blkCnt > 0U)
  73. {
  74. /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
  75. mve_pred16_t p0 = vctp32q(blkCnt);
  76. vecA = vld1q(pSrc);
  77. vecSum = vaddq_m(vecSum,vecSum, vecA, p0);
  78. }
  79. sum = vecAddAcrossF32Mve(vecSum);
  80. /* Store result in destination buffer */
  81. *pResult = sum;
  82. }
  83. #else
  84. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  85. void arm_accumulate_f32(
  86. const float32_t * pSrc,
  87. uint32_t blockSize,
  88. float32_t * pResult)
  89. {
  90. float32_t sum = 0.0f; /* Temporary result storage */
  91. float32x4_t sumV = vdupq_n_f32(0.0f); /* Temporary result storage */
  92. float32x2_t sumV2;
  93. uint32_t blkCnt; /* Loop counter */
  94. float32x4_t inV;
  95. blkCnt = blockSize >> 2U;
  96. /* Compute 4 outputs at a time.
  97. ** a second loop below computes the remaining 1 to 3 samples. */
  98. while (blkCnt > 0U)
  99. {
  100. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  101. inV = vld1q_f32(pSrc);
  102. sumV = vaddq_f32(sumV, inV);
  103. pSrc += 4;
  104. /* Decrement the loop counter */
  105. blkCnt--;
  106. }
  107. sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV));
  108. sum = vget_lane_f32(sumV2, 0) + vget_lane_f32(sumV2, 1);
  109. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  110. ** No loop unrolling is used. */
  111. blkCnt = blockSize & 3;
  112. while (blkCnt > 0U)
  113. {
  114. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  115. sum += *pSrc++;
  116. /* Decrement the loop counter */
  117. blkCnt--;
  118. }
  119. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  120. /* Store the result to the destination */
  121. *pResult = sum;
  122. }
  123. #else
  124. void arm_accumulate_f32(
  125. const float32_t * pSrc,
  126. uint32_t blockSize,
  127. float32_t * pResult)
  128. {
  129. uint32_t blkCnt; /* Loop counter */
  130. float32_t sum = 0.0f; /* Temporary result storage */
  131. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  132. /* Loop unrolling: Compute 4 outputs at a time */
  133. blkCnt = blockSize >> 2U;
  134. while (blkCnt > 0U)
  135. {
  136. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  137. sum += *pSrc++;
  138. sum += *pSrc++;
  139. sum += *pSrc++;
  140. sum += *pSrc++;
  141. /* Decrement the loop counter */
  142. blkCnt--;
  143. }
  144. /* Loop unrolling: Compute remaining outputs */
  145. blkCnt = blockSize % 0x4U;
  146. #else
  147. /* Initialize blkCnt with number of samples */
  148. blkCnt = blockSize;
  149. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  150. while (blkCnt > 0U)
  151. {
  152. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  153. sum += *pSrc++;
  154. /* Decrement loop counter */
  155. blkCnt--;
  156. }
  157. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  158. /* Store result to destination */
  159. *pResult = sum ;
  160. }
  161. #endif /* #if defined(ARM_MATH_NEON) */
  162. #endif /* #if defined(ARM_MATH_MVEF) */
  163. /**
  164. @} end of Accumulation group
  165. */