arm_mean_f16.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mean_f16.c
  4. * Description: Mean value of a floating-point vector
  5. *
  6. * $Date: 18. March 2020
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2020 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_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. /**
  31. @ingroup groupStats
  32. */
  33. /**
  34. @defgroup mean Mean
  35. Calculates the mean of the input vector. Mean is defined as the average of the elements in the vector.
  36. The underlying algorithm is used:
  37. <pre>
  38. Result = (pSrc[0] + pSrc[1] + pSrc[2] + ... + pSrc[blockSize-1]) / blockSize;
  39. </pre>
  40. There are separate functions for floating-point, Q31, Q15, and Q7 data types.
  41. */
  42. /**
  43. @addtogroup mean
  44. @{
  45. */
  46. /**
  47. @brief Mean value of a floating-point vector.
  48. @param[in] pSrc points to the input vector.
  49. @param[in] blockSize number of samples in input vector.
  50. @param[out] pResult mean value returned here.
  51. @return none
  52. */
  53. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  54. #include "arm_helium_utils.h"
  55. void arm_mean_f16(
  56. const float16_t * pSrc,
  57. uint32_t blockSize,
  58. float16_t * pResult)
  59. {
  60. int32_t blkCnt; /* loop counters */
  61. f16x8_t vecSrc;
  62. f16x8_t sumVec = vdupq_n_f16(0.0f16);
  63. blkCnt = blockSize;
  64. do {
  65. mve_pred16_t p = vctp16q(blkCnt);
  66. vecSrc = vldrhq_z_f16((float16_t const *) pSrc, p);
  67. sumVec = vaddq_m_f16(sumVec, sumVec, vecSrc, p);
  68. blkCnt -= 8;
  69. pSrc += 8;
  70. }
  71. while (blkCnt > 0);
  72. *pResult = vecAddAcrossF16Mve(sumVec) / (float16_t) blockSize;
  73. }
  74. #else
  75. void arm_mean_f16(
  76. const float16_t * pSrc,
  77. uint32_t blockSize,
  78. float16_t * pResult)
  79. {
  80. uint32_t blkCnt; /* Loop counter */
  81. float16_t sum = 0.0f; /* Temporary result storage */
  82. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  83. /* Loop unrolling: Compute 4 outputs at a time */
  84. blkCnt = blockSize >> 2U;
  85. while (blkCnt > 0U)
  86. {
  87. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  88. sum += *pSrc++;
  89. sum += *pSrc++;
  90. sum += *pSrc++;
  91. sum += *pSrc++;
  92. /* Decrement the loop counter */
  93. blkCnt--;
  94. }
  95. /* Loop unrolling: Compute remaining outputs */
  96. blkCnt = blockSize % 0x4U;
  97. #else
  98. /* Initialize blkCnt with number of samples */
  99. blkCnt = blockSize;
  100. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  101. while (blkCnt > 0U)
  102. {
  103. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  104. sum += *pSrc++;
  105. /* Decrement loop counter */
  106. blkCnt--;
  107. }
  108. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
  109. /* Store result to destination */
  110. *pResult = (sum / (float16_t)blockSize);
  111. }
  112. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  113. /**
  114. @} end of mean group
  115. */
  116. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */