arm_mean_f32.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mean_f32.c
  4. * Description: Mean value of a floating-point vector
  5. *
  6. * $Date: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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 mean
  34. @{
  35. */
  36. /**
  37. @brief Mean 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 mean value returned here.
  41. @return none
  42. */
  43. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. void arm_mean_f32(
  46. const float32_t * pSrc,
  47. uint32_t blockSize,
  48. float32_t * pResult)
  49. {
  50. uint32_t blkCnt; /* loop counters */
  51. f32x4_t vecSrc;
  52. f32x4_t sumVec = vdupq_n_f32(0.0f);
  53. float32_t sum = 0.0f;
  54. /* Compute 4 outputs at a time */
  55. blkCnt = blockSize >> 2U;
  56. while (blkCnt > 0U)
  57. {
  58. vecSrc = vldrwq_f32(pSrc);
  59. sumVec = vaddq_f32(sumVec, vecSrc);
  60. blkCnt --;
  61. pSrc += 4;
  62. }
  63. sum = vecAddAcrossF32Mve(sumVec);
  64. /* Tail */
  65. blkCnt = blockSize & 0x3;
  66. while (blkCnt > 0U)
  67. {
  68. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  69. sum += *pSrc++;
  70. /* Decrement loop counter */
  71. blkCnt--;
  72. }
  73. *pResult = sum / (float32_t) blockSize;
  74. }
  75. #else
  76. #if defined(ARM_MATH_NEON_EXPERIMENTAL) && !defined(ARM_MATH_AUTOVECTORIZE)
  77. void arm_mean_f32(
  78. const float32_t * pSrc,
  79. uint32_t blockSize,
  80. float32_t * pResult)
  81. {
  82. float32_t sum = 0.0f; /* Temporary result storage */
  83. float32x4_t sumV = vdupq_n_f32(0.0f); /* Temporary result storage */
  84. float32x2_t sumV2;
  85. uint32_t blkCnt; /* Loop counter */
  86. float32x4_t inV;
  87. blkCnt = blockSize >> 2U;
  88. /* Compute 4 outputs at a time.
  89. ** a second loop below computes the remaining 1 to 3 samples. */
  90. while (blkCnt > 0U)
  91. {
  92. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  93. inV = vld1q_f32(pSrc);
  94. sumV = vaddq_f32(sumV, inV);
  95. pSrc += 4;
  96. /* Decrement the loop counter */
  97. blkCnt--;
  98. }
  99. sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV));
  100. sum = vget_lane_f32(sumV2, 0) + vget_lane_f32(sumV2, 1);
  101. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  102. ** No loop unrolling is used. */
  103. blkCnt = blockSize & 3;
  104. while (blkCnt > 0U)
  105. {
  106. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  107. sum += *pSrc++;
  108. /* Decrement the loop counter */
  109. blkCnt--;
  110. }
  111. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
  112. /* Store the result to the destination */
  113. *pResult = sum / (float32_t) blockSize;
  114. }
  115. #else
  116. void arm_mean_f32(
  117. const float32_t * pSrc,
  118. uint32_t blockSize,
  119. float32_t * pResult)
  120. {
  121. uint32_t blkCnt; /* Loop counter */
  122. float32_t sum = 0.0f; /* Temporary result storage */
  123. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  124. /* Loop unrolling: Compute 4 outputs at a time */
  125. blkCnt = blockSize >> 2U;
  126. while (blkCnt > 0U)
  127. {
  128. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  129. sum += *pSrc++;
  130. sum += *pSrc++;
  131. sum += *pSrc++;
  132. sum += *pSrc++;
  133. /* Decrement the loop counter */
  134. blkCnt--;
  135. }
  136. /* Loop unrolling: Compute remaining outputs */
  137. blkCnt = blockSize % 0x4U;
  138. #else
  139. /* Initialize blkCnt with number of samples */
  140. blkCnt = blockSize;
  141. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  142. while (blkCnt > 0U)
  143. {
  144. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  145. sum += *pSrc++;
  146. /* Decrement loop counter */
  147. blkCnt--;
  148. }
  149. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
  150. /* Store result to destination */
  151. *pResult = (sum / blockSize);
  152. }
  153. #endif /* #if defined(ARM_MATH_NEON) */
  154. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  155. /**
  156. @} end of mean group
  157. */