arm_mse_q31.c 4.2 KB

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