arm_mse_f16.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mse_f16.c
  4. * Description: Half floating point mean square error
  5. *
  6. * $Date: 05 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_f16.h"
  29. /**
  30. @ingroup groupStats
  31. */
  32. /**
  33. @addtogroup MSE
  34. @{
  35. */
  36. /**
  37. @brief Mean square error between two half floating point 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] result mean square error
  42. */
  43. #if !defined(ARM_MATH_AUTOVECTORIZE)
  44. #if defined(ARM_MATH_MVE_FLOAT16)
  45. #include "arm_helium_utils.h"
  46. void arm_mse_f16(
  47. const float16_t * pSrcA,
  48. const float16_t * pSrcB,
  49. uint32_t blockSize,
  50. float16_t * result)
  51. {
  52. float16x8_t vecA, vecB;
  53. float16x8_t vecSum;
  54. uint32_t blkCnt;
  55. _Float16 sum = 0.0f16;
  56. vecSum = vdupq_n_f16(0.0f16);
  57. blkCnt = (blockSize) >> 3;
  58. while (blkCnt > 0U)
  59. {
  60. vecA = vld1q(pSrcA);
  61. pSrcA += 8;
  62. vecB = vld1q(pSrcB);
  63. pSrcB += 8;
  64. vecA = vsubq(vecA, vecB);
  65. vecSum = vfmaq(vecSum, vecA, vecA);
  66. /*
  67. * Decrement the blockSize loop counter
  68. */
  69. blkCnt --;
  70. }
  71. blkCnt = (blockSize) & 7;
  72. if (blkCnt > 0U)
  73. {
  74. mve_pred16_t p0 = vctp16q(blkCnt);
  75. vecA = vld1q(pSrcA);
  76. vecB = vld1q(pSrcB);
  77. vecA = vsubq(vecA, vecB);
  78. vecSum = vfmaq_m(vecSum, vecA, vecA, p0);
  79. }
  80. sum = vecAddAcrossF16Mve(vecSum);
  81. /* Store result in destination buffer */
  82. *result = (_Float16)sum / (_Float16)blockSize;
  83. }
  84. #endif
  85. #endif /*#if !defined(ARM_MATH_AUTOVECTORIZE)*/
  86. #if defined(ARM_FLOAT16_SUPPORTED)
  87. #if (!defined(ARM_MATH_MVE_FLOAT16)) || defined(ARM_MATH_AUTOVECTORIZE)
  88. void arm_mse_f16(
  89. const float16_t * pSrcA,
  90. const float16_t * pSrcB,
  91. uint32_t blockSize,
  92. float16_t * result)
  93. {
  94. uint32_t blkCnt; /* Loop counter */
  95. _Float16 inA, inB;
  96. _Float16 sum = 0.0f16; /* Temporary return variable */
  97. #if defined (ARM_MATH_LOOPUNROLL)
  98. blkCnt = (blockSize) >> 3;
  99. while (blkCnt > 0U)
  100. {
  101. inA = *pSrcA++;
  102. inB = *pSrcB++;
  103. inA = (_Float16)inA - (_Float16)inB;
  104. sum += (_Float16)inA * (_Float16)inA;
  105. inA = *pSrcA++;
  106. inB = *pSrcB++;
  107. inA = (_Float16)inA - (_Float16)inB;
  108. sum += (_Float16)inA * (_Float16)inA;
  109. inA = *pSrcA++;
  110. inB = *pSrcB++;
  111. inA = (_Float16)inA - (_Float16)inB;
  112. sum += (_Float16)inA * (_Float16)inA;
  113. inA = *pSrcA++;
  114. inB = *pSrcB++;
  115. inA = (_Float16)inA - (_Float16)inB;
  116. sum += (_Float16)inA * (_Float16)inA;
  117. inA = *pSrcA++;
  118. inB = *pSrcB++;
  119. inA = (_Float16)inA - (_Float16)inB;
  120. sum += (_Float16)inA * (_Float16)inA;
  121. inA = *pSrcA++;
  122. inB = *pSrcB++;
  123. inA = (_Float16)inA - (_Float16)inB;
  124. sum += (_Float16)inA * (_Float16)inA;
  125. inA = *pSrcA++;
  126. inB = *pSrcB++;
  127. inA = (_Float16)inA - (_Float16)inB;
  128. sum += (_Float16)inA * (_Float16)inA;
  129. inA = *pSrcA++;
  130. inB = *pSrcB++;
  131. inA = (_Float16)inA - (_Float16)inB;
  132. sum += (_Float16)inA * (_Float16)inA;
  133. /* Decrement loop counter */
  134. blkCnt--;
  135. }
  136. /* Loop unrolling: Compute remaining outputs */
  137. blkCnt = (blockSize) & 7;
  138. #else
  139. /* Initialize blkCnt with number of samples */
  140. blkCnt = blockSize;
  141. #endif
  142. while (blkCnt > 0U)
  143. {
  144. inA = *pSrcA++;
  145. inB = *pSrcB++;
  146. inA = (_Float16)inA - (_Float16)inB;
  147. sum += (_Float16)inA * (_Float16)inA;
  148. /* Decrement loop counter */
  149. blkCnt--;
  150. }
  151. /* Store result in destination buffer */
  152. *result = (_Float16)sum / (_Float16)blockSize;
  153. }
  154. #endif /* end of test for vector instruction availability */
  155. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
  156. /**
  157. @} end of MSE group
  158. */