arm_mean_q15.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mean_q15.c
  4. * Description: Mean value of a Q15 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 Q15 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 32-bit internal accumulator.
  44. The input is represented in 1.15 format and is accumulated in a 32-bit
  45. accumulator in 17.15 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.15 format.
  49. */
  50. #if defined(ARM_MATH_MVEI)
  51. void arm_mean_q15(
  52. const q15_t * pSrc,
  53. uint32_t blockSize,
  54. q15_t * pResult)
  55. {
  56. uint32_t blkCnt; /* loop counters */
  57. q15x8_t vecSrc;
  58. q31_t sum = 0L;
  59. /* Compute 8 outputs at a time */
  60. blkCnt = blockSize >> 3U;
  61. while (blkCnt > 0U)
  62. {
  63. vecSrc = vldrhq_s16(pSrc);
  64. /*
  65. * sum lanes
  66. */
  67. sum = vaddvaq(sum, vecSrc);
  68. blkCnt--;
  69. pSrc += 8;
  70. }
  71. /* Tail */
  72. blkCnt = blockSize & 0x7;
  73. while (blkCnt > 0U)
  74. {
  75. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  76. sum += *pSrc++;
  77. /* Decrement loop counter */
  78. blkCnt--;
  79. }
  80. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
  81. /* Store the result to the destination */
  82. *pResult = (q15_t) (sum / (int32_t) blockSize);
  83. }
  84. #else
  85. void arm_mean_q15(
  86. const q15_t * pSrc,
  87. uint32_t blockSize,
  88. q15_t * pResult)
  89. {
  90. uint32_t blkCnt; /* Loop counter */
  91. q31_t sum = 0; /* Temporary result storage */
  92. #if defined (ARM_MATH_LOOPUNROLL)
  93. q31_t in;
  94. #endif
  95. #if defined (ARM_MATH_LOOPUNROLL)
  96. /* Loop unrolling: Compute 4 outputs at a time */
  97. blkCnt = blockSize >> 2U;
  98. while (blkCnt > 0U)
  99. {
  100. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  101. in = read_q15x2_ia ((q15_t **) &pSrc);
  102. sum += ((in << 16U) >> 16U);
  103. sum += (in >> 16U);
  104. in = read_q15x2_ia ((q15_t **) &pSrc);
  105. sum += ((in << 16U) >> 16U);
  106. sum += (in >> 16U);
  107. /* Decrement the loop counter */
  108. blkCnt--;
  109. }
  110. /* Loop unrolling: Compute remaining outputs */
  111. blkCnt = blockSize % 0x4U;
  112. #else
  113. /* Initialize blkCnt with number of samples */
  114. blkCnt = blockSize;
  115. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  116. while (blkCnt > 0U)
  117. {
  118. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  119. sum += *pSrc++;
  120. /* Decrement loop counter */
  121. blkCnt--;
  122. }
  123. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
  124. /* Store result to destination */
  125. *pResult = (q15_t) (sum / (int32_t) blockSize);
  126. }
  127. #endif /* defined(ARM_MATH_MVEI) */
  128. /**
  129. @} end of mean group
  130. */