arm_power_q15.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_power_q15.c
  4. * Description: Sum of the squares of the elements 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 power
  34. @{
  35. */
  36. /**
  37. @brief Sum of the squares of the elements 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 sum of the squares 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.15 format.
  45. Intermediate multiplication yields a 2.30 format, and this
  46. result is added without saturation to a 64-bit accumulator in 34.30 format.
  47. With 33 guard bits in the accumulator, there is no risk of overflow, and the
  48. full precision of the intermediate multiplication is preserved.
  49. Finally, the return result is in 34.30 format.
  50. */
  51. #if defined(ARM_MATH_MVEI)
  52. void arm_power_q15(
  53. const q15_t * pSrc,
  54. uint32_t blockSize,
  55. q63_t * pResult)
  56. {
  57. uint32_t blkCnt; /* loop counters */
  58. q15x8_t vecSrc;
  59. q63_t sum = 0LL;
  60. q15_t in;
  61. /* Compute 8 outputs at a time */
  62. blkCnt = blockSize >> 3U;
  63. while (blkCnt > 0U)
  64. {
  65. vecSrc = vldrhq_s16(pSrc);
  66. /*
  67. * sum lanes
  68. */
  69. sum = vmlaldavaq(sum, vecSrc, vecSrc);
  70. blkCnt --;
  71. pSrc += 8;
  72. }
  73. /*
  74. * tail
  75. */
  76. blkCnt = blockSize & 0x7;
  77. while (blkCnt > 0U)
  78. {
  79. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  80. /* Compute Power and store result in a temporary variable, sum. */
  81. in = *pSrc++;
  82. sum += ((q31_t) in * in);
  83. /* Decrement loop counter */
  84. blkCnt--;
  85. }
  86. *pResult = sum;
  87. }
  88. #else
  89. void arm_power_q15(
  90. const q15_t * pSrc,
  91. uint32_t blockSize,
  92. q63_t * pResult)
  93. {
  94. uint32_t blkCnt; /* Loop counter */
  95. q63_t sum = 0; /* Temporary result storage */
  96. q15_t in; /* Temporary variable to store input value */
  97. #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
  98. q31_t in32; /* Temporary variable to store packed input value */
  99. #endif
  100. #if defined (ARM_MATH_LOOPUNROLL)
  101. /* Loop unrolling: Compute 4 outputs at a time */
  102. blkCnt = blockSize >> 2U;
  103. while (blkCnt > 0U)
  104. {
  105. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  106. /* Compute Power and store result in a temporary variable, sum. */
  107. #if defined (ARM_MATH_DSP)
  108. in32 = read_q15x2_ia ((q15_t **) &pSrc);
  109. sum = __SMLALD(in32, in32, sum);
  110. in32 = read_q15x2_ia ((q15_t **) &pSrc);
  111. sum = __SMLALD(in32, in32, sum);
  112. #else
  113. in = *pSrc++;
  114. sum += ((q31_t) in * in);
  115. in = *pSrc++;
  116. sum += ((q31_t) in * in);
  117. in = *pSrc++;
  118. sum += ((q31_t) in * in);
  119. in = *pSrc++;
  120. sum += ((q31_t) in * in);
  121. #endif /* #if defined (ARM_MATH_DSP) */
  122. /* Decrement loop counter */
  123. blkCnt--;
  124. }
  125. /* Loop unrolling: Compute remaining outputs */
  126. blkCnt = blockSize % 0x4U;
  127. #else
  128. /* Initialize blkCnt with number of samples */
  129. blkCnt = blockSize;
  130. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  131. while (blkCnt > 0U)
  132. {
  133. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  134. /* Compute Power and store result in a temporary variable, sum. */
  135. in = *pSrc++;
  136. sum += ((q31_t) in * in);
  137. /* Decrement loop counter */
  138. blkCnt--;
  139. }
  140. /* Store result in 34.30 format */
  141. *pResult = sum;
  142. }
  143. #endif /* defined(ARM_MATH_MVEI) */
  144. /**
  145. @} end of power group
  146. */