arm_mse_f32.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mse_f32.c
  4. * Description: Floating point mean square error
  5. *
  6. * $Date: 05 April 2022
  7. * $Revision: V1.10.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2022 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 "dsp/statistics_functions.h"
  29. /**
  30. @ingroup groupStats
  31. */
  32. /**
  33. @addtogroup MSE
  34. @{
  35. */
  36. /**
  37. @brief Mean square error between two floating point vectors.
  38. @param[in] pSrcA points to the first input vector
  39. @param[in] pSrcB points to the second input vector
  40. @param[in] blockSize number of samples in input vector
  41. @param[out] pResult mean square error
  42. */
  43. #if !defined(ARM_MATH_AUTOVECTORIZE)
  44. #if defined(ARM_MATH_MVEF)
  45. #include "arm_helium_utils.h"
  46. void arm_mse_f32(
  47. const float32_t * pSrcA,
  48. const float32_t * pSrcB,
  49. uint32_t blockSize,
  50. float32_t * pResult)
  51. {
  52. float32x4_t vecA, vecB;
  53. float32x4_t vecSum;
  54. uint32_t blkCnt;
  55. float32_t sum = 0.0f;
  56. vecSum = vdupq_n_f32(0.0f);
  57. /* Compute 4 outputs at a time */
  58. blkCnt = (blockSize) >> 2;
  59. while (blkCnt > 0U)
  60. {
  61. vecA = vld1q(pSrcA);
  62. pSrcA += 4;
  63. vecB = vld1q(pSrcB);
  64. pSrcB += 4;
  65. vecA = vsubq(vecA, vecB);
  66. vecSum = vfmaq(vecSum, vecA, vecA);
  67. /*
  68. * Decrement the blockSize loop counter
  69. */
  70. blkCnt --;
  71. }
  72. blkCnt = (blockSize) & 3;
  73. if (blkCnt > 0U)
  74. {
  75. mve_pred16_t p0 = vctp32q(blkCnt);
  76. vecA = vld1q(pSrcA);
  77. vecB = vld1q(pSrcB);
  78. vecA = vsubq(vecA, vecB);
  79. vecSum = vfmaq_m(vecSum, vecA, vecA, p0);
  80. }
  81. sum = vecAddAcrossF32Mve(vecSum);
  82. /* Store result in destination buffer */
  83. *pResult = sum / blockSize;
  84. }
  85. #endif
  86. #if defined(ARM_MATH_NEON)
  87. void arm_mse_f32(
  88. const float32_t * pSrcA,
  89. const float32_t * pSrcB,
  90. uint32_t blockSize,
  91. float32_t * pResult)
  92. {
  93. float32x4_t vecA, vecB;
  94. float32x4_t vecSum;
  95. uint32_t blkCnt;
  96. float32_t inA, inB;
  97. float32_t sum = 0.0f;
  98. vecSum = vdupq_n_f32(0.0f);
  99. #if !defined(__aarch64__)
  100. f32x2_t tmp = vdup_n_f32(0.0f);
  101. #endif
  102. /* Compute 4 outputs at a time */
  103. blkCnt = (blockSize) >> 2;
  104. while (blkCnt > 0U)
  105. {
  106. vecA = vld1q_f32(pSrcA);
  107. pSrcA += 4;
  108. vecB = vld1q_f32(pSrcB);
  109. pSrcB += 4;
  110. vecA = vsubq_f32(vecA, vecB);
  111. vecSum = vfmaq_f32(vecSum, vecA, vecA);
  112. /*
  113. * Decrement the blockSize loop counter
  114. */
  115. blkCnt --;
  116. }
  117. #if defined(__aarch64__)
  118. sum = vpadds_f32(vpadd_f32(vget_low_f32(vecSum), vget_high_f32(vecSum)));
  119. #else
  120. tmp = vpadd_f32(vget_low_f32(vecSum), vget_high_f32(vecSum));
  121. sum = vget_lane_f32(tmp, 0) + vget_lane_f32(tmp, 1);
  122. #endif
  123. blkCnt = (blockSize) & 3;
  124. while (blkCnt > 0U)
  125. {
  126. /* Calculate dot product and store result in a temporary buffer. */
  127. inA = *pSrcA++;
  128. inB = *pSrcB++;
  129. inA = inA - inB;
  130. sum += inA * inA;
  131. /* Decrement loop counter */
  132. blkCnt--;
  133. }
  134. /* Store result in destination buffer */
  135. *pResult = sum / blockSize;
  136. }
  137. #endif
  138. #endif /*#if !defined(ARM_MATH_AUTOVECTORIZE)*/
  139. #if (!defined(ARM_MATH_MVEF) && !defined(ARM_MATH_NEON)) || defined(ARM_MATH_AUTOVECTORIZE)
  140. void arm_mse_f32(
  141. const float32_t * pSrcA,
  142. const float32_t * pSrcB,
  143. uint32_t blockSize,
  144. float32_t * pResult)
  145. {
  146. uint32_t blkCnt; /* Loop counter */
  147. float32_t inA, inB;
  148. float32_t sum = 0.0f; /* Temporary return variable */
  149. #if defined (ARM_MATH_LOOPUNROLL)
  150. /* Loop unrolling: Compute 4 outputs at a time */
  151. blkCnt = (blockSize) >> 2;
  152. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  153. ** a second loop below computes the remaining 1 to 3 samples. */
  154. while (blkCnt > 0U)
  155. {
  156. inA = *pSrcA++;
  157. inB = *pSrcB++;
  158. inA = inA - inB;
  159. sum += inA * inA;
  160. inA = *pSrcA++;
  161. inB = *pSrcB++;
  162. inA = inA - inB;
  163. sum += inA * inA;
  164. inA = *pSrcA++;
  165. inB = *pSrcB++;
  166. inA = inA - inB;
  167. sum += inA * inA;
  168. inA = *pSrcA++;
  169. inB = *pSrcB++;
  170. inA = inA - inB;
  171. sum += inA * inA;
  172. /* Decrement loop counter */
  173. blkCnt--;
  174. }
  175. /* Loop unrolling: Compute remaining outputs */
  176. blkCnt = (blockSize) & 3;
  177. #else
  178. /* Initialize blkCnt with number of samples */
  179. blkCnt = blockSize;
  180. #endif
  181. while (blkCnt > 0U)
  182. {
  183. inA = *pSrcA++;
  184. inB = *pSrcB++;
  185. inA = inA - inB;
  186. sum += inA * inA;
  187. /* Decrement loop counter */
  188. blkCnt--;
  189. }
  190. /* Store result in destination buffer */
  191. *pResult = sum / blockSize;
  192. }
  193. #endif /* end of test for vector instruction availability */
  194. /**
  195. @} end of MSE group
  196. */