arm_mse_q7.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. q7_t inA,inB;
  59. /* Compute 16 outputs at a time */
  60. blkCnt = blockSize >> 4U;
  61. while (blkCnt > 0U)
  62. {
  63. vecSrcA = vldrbq_s8(pSrcA);
  64. vecSrcB = vldrbq_s8(pSrcB);
  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. while (blkCnt > 0U)
  79. {
  80. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  81. /* Compute Power and store result in a temporary variable, sum. */
  82. inA = *pSrcA++;
  83. inB = *pSrcB++;
  84. inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
  85. sum += ((q15_t) inA * inA);
  86. /* Decrement loop counter */
  87. blkCnt--;
  88. }
  89. *pResult = (q7_t) __SSAT((q15_t) (sum / blockSize)>>7, 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) && defined (ARM_MATH_DSP)
  102. q31_t inA32,inB32; /* Temporary variable to store packed input value */
  103. q31_t in1, in2; /* Temporary variables to store input value */
  104. #endif
  105. #if defined (ARM_MATH_LOOPUNROLL)
  106. /* Loop unrolling: Compute 4 outputs at a time */
  107. blkCnt = blockSize >> 2U;
  108. while (blkCnt > 0U)
  109. {
  110. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  111. /* Compute Power and store result in a temporary variable, sum. */
  112. #if defined (ARM_MATH_DSP)
  113. inA32 = read_q7x4_ia ((q7_t **) &pSrcA);
  114. inB32 = read_q7x4_ia ((q7_t **) &pSrcB);
  115. inA32 = __QSUB8(inA32, inB32);
  116. in1 = __SXTB16(__ROR(inA32, 8));
  117. in2 = __SXTB16(inA32);
  118. /* calculate power and accumulate to accumulator */
  119. sum = __SMLAD(in1, in1, sum);
  120. sum = __SMLAD(in2, in2, sum);
  121. #else
  122. inA = *pSrcA++;
  123. inB = *pSrcB++;
  124. inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
  125. sum += ((q15_t) inA * inA);
  126. inA = *pSrcA++;
  127. inB = *pSrcB++;
  128. inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
  129. sum += ((q15_t) inA * inA);
  130. inA = *pSrcA++;
  131. inB = *pSrcB++;
  132. inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
  133. sum += ((q15_t) inA * inA);
  134. inA = *pSrcA++;
  135. inB = *pSrcB++;
  136. inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
  137. sum += ((q15_t) inA * inA);
  138. #endif /* #if defined (ARM_MATH_DSP) */
  139. /* Decrement loop counter */
  140. blkCnt--;
  141. }
  142. /* Loop unrolling: Compute remaining outputs */
  143. blkCnt = blockSize % 0x4U;
  144. #else
  145. /* Initialize blkCnt with number of samples */
  146. blkCnt = blockSize;
  147. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  148. while (blkCnt > 0U)
  149. {
  150. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  151. /* Compute Power and store result in a temporary variable, sum. */
  152. inA = *pSrcA++;
  153. inB = *pSrcB++;
  154. inA = (q7_t) __SSAT((q15_t) inA - (q15_t)inB, 8);
  155. sum += ((q15_t) inA * inA);
  156. /* Decrement loop counter */
  157. blkCnt--;
  158. }
  159. /* Store result in q7 format */
  160. *pResult = (q7_t) __SSAT((q15_t) (sum / blockSize)>>7, 8);;
  161. }
  162. #endif /* defined(ARM_MATH_MVEI) */
  163. /**
  164. @} end of power group
  165. */