arm_weighted_sum_f32.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_weighted_sum_f32.c
  4. * Description: Weighted Sum
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 <limits.h>
  29. #include <math.h>
  30. #include "dsp/support_functions.h"
  31. /**
  32. * @addtogroup weightedsum
  33. * @{
  34. */
  35. /**
  36. * @brief Weighted sum
  37. *
  38. *
  39. * @param[in] *in Array of input values.
  40. * @param[in] *weigths Weights
  41. * @param[in] blockSize Number of samples in the input array.
  42. * @return Weighted sum
  43. *
  44. */
  45. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  46. #include "arm_helium_utils.h"
  47. float32_t arm_weighted_sum_f32(const float32_t *in,const float32_t *weigths, uint32_t blockSize)
  48. {
  49. float32_t accum1, accum2;
  50. f32x4_t accum1V, accum2V;
  51. f32x4_t inV, wV;
  52. const float32_t *pIn, *pW;
  53. uint32_t blkCnt;
  54. pIn = in;
  55. pW = weigths;
  56. accum1V = vdupq_n_f32(0.0);
  57. accum2V = vdupq_n_f32(0.0);
  58. blkCnt = blockSize >> 2;
  59. while (blkCnt > 0)
  60. {
  61. inV = vld1q(pIn);
  62. wV = vld1q(pW);
  63. pIn += 4;
  64. pW += 4;
  65. accum1V = vfmaq(accum1V, inV, wV);
  66. accum2V = vaddq(accum2V, wV);
  67. blkCnt--;
  68. }
  69. accum1 = vecAddAcrossF32Mve(accum1V);
  70. accum2 = vecAddAcrossF32Mve(accum2V);
  71. blkCnt = blockSize & 3;
  72. while(blkCnt > 0)
  73. {
  74. accum1 += *pIn++ * *pW;
  75. accum2 += *pW++;
  76. blkCnt--;
  77. }
  78. return (accum1 / accum2);
  79. }
  80. #else
  81. #if defined(ARM_MATH_NEON)
  82. #include "NEMath.h"
  83. float32_t arm_weighted_sum_f32(const float32_t *in,const float32_t *weigths, uint32_t blockSize)
  84. {
  85. float32_t accum1, accum2;
  86. float32x4_t accum1V, accum2V;
  87. float32x2_t tempV;
  88. float32x4_t inV,wV;
  89. const float32_t *pIn, *pW;
  90. uint32_t blkCnt;
  91. pIn = in;
  92. pW = weigths;
  93. accum1=0.0f;
  94. accum2=0.0f;
  95. accum1V = vdupq_n_f32(0.0f);
  96. accum2V = vdupq_n_f32(0.0f);
  97. blkCnt = blockSize >> 2;
  98. while(blkCnt > 0)
  99. {
  100. inV = vld1q_f32(pIn);
  101. wV = vld1q_f32(pW);
  102. pIn += 4;
  103. pW += 4;
  104. accum1V = vmlaq_f32(accum1V,inV,wV);
  105. accum2V = vaddq_f32(accum2V,wV);
  106. blkCnt--;
  107. }
  108. tempV = vpadd_f32(vget_low_f32(accum1V),vget_high_f32(accum1V));
  109. accum1 = vget_lane_f32(tempV, 0) + vget_lane_f32(tempV, 1);
  110. tempV = vpadd_f32(vget_low_f32(accum2V),vget_high_f32(accum2V));
  111. accum2 = vget_lane_f32(tempV, 0) + vget_lane_f32(tempV, 1);
  112. blkCnt = blockSize & 3;
  113. while(blkCnt > 0)
  114. {
  115. accum1 += *pIn++ * *pW;
  116. accum2 += *pW++;
  117. blkCnt--;
  118. }
  119. return(accum1 / accum2);
  120. }
  121. #else
  122. float32_t arm_weighted_sum_f32(const float32_t *in, const float32_t *weigths, uint32_t blockSize)
  123. {
  124. float32_t accum1, accum2;
  125. const float32_t *pIn, *pW;
  126. uint32_t blkCnt;
  127. pIn = in;
  128. pW = weigths;
  129. accum1=0.0f;
  130. accum2=0.0f;
  131. blkCnt = blockSize;
  132. while(blkCnt > 0)
  133. {
  134. accum1 += *pIn++ * *pW;
  135. accum2 += *pW++;
  136. blkCnt--;
  137. }
  138. return(accum1 / accum2);
  139. }
  140. #endif
  141. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  142. /**
  143. * @} end of weightedsum group
  144. */