arm_mse_q7.c 4.4 KB

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