arm_mse_q7.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mse_q7.c
  4. * Description: Mean square error between two Q7 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. @defgroup MSE Mean Square Error
  34. Calculates the mean square error between two vectors.
  35. */
  36. /**
  37. @addtogroup MSE
  38. @{
  39. */
  40. /**
  41. @brief Mean square error between two Q7 vectors.
  42. @param[in] pSrcA points to the first input vector
  43. @param[in] pSrcB points to the second input vector
  44. @param[in] blockSize number of samples in input vector
  45. @param[out] pResult mean square error
  46. */
  47. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  48. void arm_mse_q7(
  49. const q7_t * pSrcA,
  50. const q7_t * pSrcB,
  51. uint32_t blockSize,
  52. q7_t * pResult)
  53. {
  54. uint32_t blkCnt; /* loop counters */
  55. q7x16_t vecSrcA,vecSrcB;
  56. q31_t sum = 0LL;
  57. /* Compute 16 outputs at a time */
  58. blkCnt = blockSize >> 4U;
  59. while (blkCnt > 0U)
  60. {
  61. vecSrcA = vld1q(pSrcA);
  62. vecSrcB = vld1q(pSrcB);
  63. vecSrcA = vshrq(vecSrcA,1);
  64. vecSrcB = vshrq(vecSrcB,1);
  65. vecSrcA = vqsubq(vecSrcA,vecSrcB);
  66. /*
  67. * sum lanes
  68. */
  69. sum = vmladavaq(sum, vecSrcA, vecSrcA);
  70. blkCnt--;
  71. pSrcA += 16;
  72. pSrcB += 16;
  73. }
  74. /*
  75. * tail
  76. */
  77. blkCnt = blockSize & 0xF;
  78. if (blkCnt > 0U)
  79. {
  80. mve_pred16_t p0 = vctp8q(blkCnt);
  81. vecSrcA = vld1q(pSrcA);
  82. vecSrcB = vld1q(pSrcB);
  83. vecSrcA = vshrq(vecSrcA,1);
  84. vecSrcB = vshrq(vecSrcB,1);
  85. vecSrcA = vqsubq(vecSrcA,vecSrcB);
  86. sum = vmladavaq_p(sum, vecSrcA, vecSrcA, p0);
  87. }
  88. *pResult = (q7_t) __SSAT((q15_t) (sum / blockSize)>>5, 8);
  89. }
  90. #else
  91. void arm_mse_q7(
  92. const q7_t * pSrcA,
  93. const q7_t * pSrcB,
  94. uint32_t blockSize,
  95. q7_t * pResult)
  96. {
  97. uint32_t blkCnt; /* Loop counter */
  98. q31_t sum = 0; /* Temporary result storage */
  99. q7_t inA,inB; /* Temporary variable to store input value */
  100. #if defined (ARM_MATH_LOOPUNROLL)
  101. /* Loop unrolling: Compute 4 outputs at a time */
  102. blkCnt = blockSize >> 2U;
  103. while (blkCnt > 0U)
  104. {
  105. inA = *pSrcA++ >> 1;
  106. inB = *pSrcB++ >> 1;
  107. inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
  108. sum += ((q15_t) inA * inA);
  109. inA = *pSrcA++ >> 1;
  110. inB = *pSrcB++ >> 1;
  111. inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
  112. sum += ((q15_t) inA * inA);
  113. inA = *pSrcA++ >> 1;
  114. inB = *pSrcB++ >> 1;
  115. inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
  116. sum += ((q15_t) inA * inA);
  117. inA = *pSrcA++ >> 1;
  118. inB = *pSrcB++ >> 1;
  119. inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
  120. sum += ((q15_t) inA * inA);
  121. /* Decrement loop counter */
  122. blkCnt--;
  123. }
  124. /* Loop unrolling: Compute remaining outputs */
  125. blkCnt = blockSize % 0x4U;
  126. #else
  127. /* Initialize blkCnt with number of samples */
  128. blkCnt = blockSize;
  129. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  130. while (blkCnt > 0U)
  131. {
  132. inA = *pSrcA++ >> 1;
  133. inB = *pSrcB++ >> 1;
  134. inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
  135. sum += ((q15_t) inA * inA);
  136. /* Decrement loop counter */
  137. blkCnt--;
  138. }
  139. /* Store result in q7 format */
  140. *pResult = (q7_t) __SSAT((q15_t) (sum / blockSize)>>5, 8);;
  141. }
  142. #endif /* defined(ARM_MATH_MVEI) */
  143. /**
  144. @} end of MSE group
  145. */