Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

153 рядки
3.5 KiB

  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_euclidean_distance_f32.c
  4. * Description: Euclidean distance between two vectors
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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/distance_functions.h"
  29. #include <limits.h>
  30. #include <math.h>
  31. /**
  32. @addtogroup Euclidean
  33. @{
  34. */
  35. /**
  36. * @brief Euclidean distance between two vectors
  37. * @param[in] pA First vector
  38. * @param[in] pB Second vector
  39. * @param[in] blockSize vector length
  40. * @return distance
  41. *
  42. */
  43. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. #include "arm_vec_math.h"
  46. float32_t arm_euclidean_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  47. {
  48. uint32_t blkCnt;
  49. float32_t tmp;
  50. f32x4_t a, b, accumV, tempV;
  51. accumV = vdupq_n_f32(0.0f);
  52. blkCnt = blockSize >> 2;
  53. while (blkCnt > 0U) {
  54. a = vld1q(pA);
  55. b = vld1q(pB);
  56. tempV = vsubq(a, b);
  57. accumV = vfmaq(accumV, tempV, tempV);
  58. pA += 4;
  59. pB += 4;
  60. blkCnt--;
  61. }
  62. /*
  63. * tail
  64. * (will be merged thru tail predication)
  65. */
  66. blkCnt = blockSize & 3;
  67. if (blkCnt > 0U) {
  68. mve_pred16_t p0 = vctp32q(blkCnt);
  69. a = vldrwq_z_f32(pA, p0);
  70. b = vldrwq_z_f32(pB, p0);
  71. tempV = vsubq(a, b);
  72. accumV = vfmaq_m(accumV, tempV, tempV, p0);
  73. }
  74. arm_sqrt_f32(vecAddAcrossF32Mve(accumV), &tmp);
  75. return (tmp);
  76. }
  77. #else
  78. #if defined(ARM_MATH_NEON)
  79. #include "NEMath.h"
  80. float32_t arm_euclidean_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  81. {
  82. float32_t accum=0.0f,tmp;
  83. uint32_t blkCnt;
  84. float32x4_t a,b,accumV;
  85. float32x2_t accumV2;
  86. accumV = vdupq_n_f32(0.0f);
  87. blkCnt = blockSize >> 2;
  88. while(blkCnt > 0)
  89. {
  90. a = vld1q_f32(pA);
  91. b = vld1q_f32(pB);
  92. a = vsubq_f32(a,b);
  93. accumV = vmlaq_f32(accumV,a,a);
  94. pA += 4;
  95. pB += 4;
  96. blkCnt --;
  97. }
  98. accumV2 = vpadd_f32(vget_low_f32(accumV),vget_high_f32(accumV));
  99. accum = vget_lane_f32(accumV2, 0) + vget_lane_f32(accumV2, 1);
  100. blkCnt = blockSize & 3;
  101. while(blkCnt > 0)
  102. {
  103. tmp = *pA++ - *pB++;
  104. accum += SQ(tmp);
  105. blkCnt --;
  106. }
  107. arm_sqrt_f32(accum,&tmp);
  108. return(tmp);
  109. }
  110. #else
  111. float32_t arm_euclidean_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  112. {
  113. float32_t accum=0.0f,tmp;
  114. while(blockSize > 0)
  115. {
  116. tmp = *pA++ - *pB++;
  117. accum += SQ(tmp);
  118. blockSize --;
  119. }
  120. arm_sqrt_f32(accum,&tmp);
  121. return(tmp);
  122. }
  123. #endif
  124. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  125. /**
  126. * @} end of Euclidean group
  127. */