arm_mse_q31.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mse_q31.c
  4. * Description: Mean square error between two Q31 vectors
  5. *
  6. * $Date: 04 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 Q31 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_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. void arm_mse_q31(
  45. const q31_t * pSrcA,
  46. const q31_t * pSrcB,
  47. uint32_t blockSize,
  48. q31_t * pResult)
  49. {
  50. uint32_t blkCnt; /* loop counters */
  51. q31x4_t vecSrcA,vecSrcB;
  52. q63_t sum = 0LL;
  53. /* Compute 4 outputs at a time */
  54. blkCnt = blockSize >> 2U;
  55. while (blkCnt > 0U)
  56. {
  57. vecSrcA = vld1q(pSrcA);
  58. vecSrcB = vld1q(pSrcB);
  59. vecSrcA = vshrq(vecSrcA,1);
  60. vecSrcB = vshrq(vecSrcB,1);
  61. vecSrcA = vqsubq(vecSrcA,vecSrcB);
  62. /*
  63. * sum lanes
  64. */
  65. sum = vrmlaldavhaq(sum, vecSrcA, vecSrcA);
  66. blkCnt--;
  67. pSrcA += 4;
  68. pSrcB += 4;
  69. }
  70. /*
  71. * tail
  72. */
  73. blkCnt = blockSize & 3;
  74. if (blkCnt > 0U)
  75. {
  76. mve_pred16_t p0 = vctp32q(blkCnt);
  77. vecSrcA = vld1q(pSrcA);
  78. vecSrcB = vld1q(pSrcB);
  79. vecSrcA = vshrq(vecSrcA,1);
  80. vecSrcB = vshrq(vecSrcB,1);
  81. vecSrcA = vqsubq(vecSrcA,vecSrcB);
  82. sum = vrmlaldavhaq_p(sum, vecSrcA, vecSrcA, p0);
  83. }
  84. *pResult = (q31_t) ((sum / blockSize)>>21);
  85. }
  86. #else
  87. void arm_mse_q31(
  88. const q31_t * pSrcA,
  89. const q31_t * pSrcB,
  90. uint32_t blockSize,
  91. q31_t * pResult)
  92. {
  93. uint32_t blkCnt; /* Loop counter */
  94. q63_t sum = 0; /* Temporary result storage */
  95. q31_t inA32,inB32; /* Temporary variable to store packed input value */
  96. #if defined (ARM_MATH_LOOPUNROLL)
  97. /* Loop unrolling: Compute 4 outputs at a time */
  98. blkCnt = blockSize >> 2U;
  99. while (blkCnt > 0U)
  100. {
  101. inA32 = *pSrcA++ >> 1;
  102. inB32 = *pSrcB++ >> 1;
  103. inA32 = __QSUB(inA32, inB32);
  104. sum += ((q63_t) inA32 * inA32) >> 14U;
  105. inA32 = *pSrcA++ >> 1;
  106. inB32 = *pSrcB++ >> 1;
  107. inA32 = __QSUB(inA32, inB32);
  108. sum += ((q63_t) inA32 * inA32) >> 14U;
  109. inA32 = *pSrcA++ >> 1;
  110. inB32 = *pSrcB++ >> 1;
  111. inA32 = __QSUB(inA32, inB32);
  112. sum += ((q63_t) inA32 * inA32) >> 14U;
  113. inA32 = *pSrcA++ >> 1;
  114. inB32 = *pSrcB++ >> 1;
  115. inA32 = __QSUB(inA32, inB32);
  116. sum += ((q63_t) inA32 * inA32) >> 14U;
  117. /* Decrement loop counter */
  118. blkCnt--;
  119. }
  120. /* Loop unrolling: Compute remaining outputs */
  121. blkCnt = blockSize % 0x4U;
  122. #else
  123. /* Initialize blkCnt with number of samples */
  124. blkCnt = blockSize;
  125. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  126. while (blkCnt > 0U)
  127. {
  128. inA32 = *pSrcA++ >> 1;
  129. inB32 = *pSrcB++ >> 1;
  130. inA32 = __QSUB(inA32, inB32);
  131. sum += ((q63_t) inA32 * inA32) >> 14U;
  132. /* Decrement loop counter */
  133. blkCnt--;
  134. }
  135. /* Store result in q31 format */
  136. *pResult = (q31_t) ((sum / blockSize)>>15);
  137. }
  138. #endif /* defined(ARM_MATH_MVEI) */
  139. /**
  140. @} end of MSE group
  141. */