arm_mse_q15.c 4.3 KB

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