arm_mse_f32.c 5.5 KB

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