arm_mean_q7.c 4.1 KB

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