arm_mse_q15.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mse_q15.c
  4. * Description: Mean square error between two Q15 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 Q15 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_q15(
  46. const q15_t * pSrcA,
  47. const q15_t * pSrcB,
  48. uint32_t blockSize,
  49. q15_t * pResult)
  50. {
  51. uint32_t blkCnt; /* loop counters */
  52. q15x8_t vecSrcA,vecSrcB;
  53. q63_t sum = 0LL;
  54. blkCnt = blockSize >> 3U;
  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 = vmlaldavaq(sum, vecSrcA, vecSrcA);
  66. blkCnt--;
  67. pSrcA += 8;
  68. pSrcB += 8;
  69. }
  70. /*
  71. * tail
  72. */
  73. blkCnt = blockSize & 7;
  74. if (blkCnt > 0U)
  75. {
  76. mve_pred16_t p0 = vctp16q(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 = vmlaldavaq_p(sum, vecSrcA, vecSrcA, p0);
  83. }
  84. *pResult = (q15_t) __SSAT((q31_t) (sum / blockSize)>>13, 16);
  85. }
  86. #else
  87. void arm_mse_q15(
  88. const q15_t * pSrcA,
  89. const q15_t * pSrcB,
  90. uint32_t blockSize,
  91. q15_t * pResult)
  92. {
  93. uint32_t blkCnt; /* Loop counter */
  94. q63_t sum = 0; /* Temporary result storage */
  95. q15_t inA,inB; /* Temporary variable to store 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. inA = *pSrcA++ >> 1;
  102. inB = *pSrcB++ >> 1;
  103. inA = (q15_t) __SSAT(((q31_t) inA - (q31_t)inB), 16);
  104. sum += (q63_t)((q31_t) inA * inA);
  105. inA = *pSrcA++ >> 1;
  106. inB = *pSrcB++ >> 1;
  107. inA = (q15_t) __SSAT(((q31_t) inA - (q31_t)inB), 16);
  108. sum += (q63_t)((q31_t) inA * inA);
  109. inA = *pSrcA++ >> 1;
  110. inB = *pSrcB++ >> 1;
  111. inA = (q15_t) __SSAT(((q31_t) inA - (q31_t)inB), 16);
  112. sum += (q63_t)((q31_t) inA * inA);
  113. inA = *pSrcA++ >> 1;
  114. inB = *pSrcB++ >> 1;
  115. inA = (q15_t) __SSAT(((q31_t) inA - (q31_t)inB), 16);
  116. sum += (q63_t)((q31_t) inA * inA);
  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. inA = *pSrcA++ >> 1;
  129. inB = *pSrcB++ >> 1;
  130. inA = (q15_t) __SSAT(((q31_t) inA - (q31_t)inB), 16);
  131. sum += (q63_t)((q31_t) inA * inA);
  132. /* Decrement loop counter */
  133. blkCnt--;
  134. }
  135. /* Store result in q15 format */
  136. *pResult = (q15_t) __SSAT((q31_t) (sum / blockSize)>>13, 16);
  137. }
  138. #endif /* defined(ARM_MATH_MVEI) */
  139. /**
  140. @} end of MSE group
  141. */