arm_mean_q31.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mean_q31.c
  4. * Description: Mean value of a Q31 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 "arm_math.h"
  29. /**
  30. @ingroup groupStats
  31. */
  32. /**
  33. @addtogroup mean
  34. @{
  35. */
  36. /**
  37. @brief Mean value of a Q31 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. @par Scaling and Overflow Behavior
  43. The function is implemented using a 64-bit internal accumulator.
  44. The input is represented in 1.31 format and is accumulated in a 64-bit
  45. accumulator in 33.31 format.
  46. There is no risk of internal overflow with this approach, and the
  47. full precision of intermediate result is preserved.
  48. Finally, the accumulator is truncated to yield a result of 1.31 format.
  49. */
  50. #if defined(ARM_MATH_MVEI)
  51. void arm_mean_q31(
  52. const q31_t * pSrc,
  53. uint32_t blockSize,
  54. q31_t * pResult)
  55. {
  56. uint32_t blkCnt; /* loop counters */
  57. q31x4_t vecSrc;
  58. q63_t sum = 0LL;
  59. /* Compute 4 outputs at a time */
  60. blkCnt = blockSize >> 2U;
  61. while (blkCnt > 0U)
  62. {
  63. vecSrc = vldrwq_s32(pSrc);
  64. /*
  65. * sum lanes
  66. */
  67. sum = vaddlvaq(sum, vecSrc);
  68. blkCnt --;
  69. pSrc += 4;
  70. }
  71. /* Tail */
  72. blkCnt = blockSize & 0x3;
  73. while (blkCnt > 0U)
  74. {
  75. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  76. sum += *pSrc++;
  77. blkCnt --;
  78. }
  79. *pResult = arm_div_q63_to_q31(sum, blockSize);
  80. }
  81. #else
  82. void arm_mean_q31(
  83. const q31_t * pSrc,
  84. uint32_t blockSize,
  85. q31_t * pResult)
  86. {
  87. uint32_t blkCnt; /* Loop counter */
  88. q63_t sum = 0; /* Temporary result storage */
  89. #if defined (ARM_MATH_LOOPUNROLL)
  90. /* Loop unrolling: Compute 4 outputs at a time */
  91. blkCnt = blockSize >> 2U;
  92. while (blkCnt > 0U)
  93. {
  94. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  95. sum += *pSrc++;
  96. sum += *pSrc++;
  97. sum += *pSrc++;
  98. sum += *pSrc++;
  99. /* Decrement the loop counter */
  100. blkCnt--;
  101. }
  102. /* Loop unrolling: Compute remaining outputs */
  103. blkCnt = blockSize % 0x4U;
  104. #else
  105. /* Initialize blkCnt with number of samples */
  106. blkCnt = blockSize;
  107. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  108. while (blkCnt > 0U)
  109. {
  110. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  111. sum += *pSrc++;
  112. /* Decrement loop counter */
  113. blkCnt--;
  114. }
  115. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
  116. /* Store result to destination */
  117. *pResult = (q31_t) (sum / blockSize);
  118. }
  119. #endif /* defined(ARM_MATH_MVEI) */
  120. /**
  121. @} end of mean group
  122. */