25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

189 lines
4.1 KiB

  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_minkowski_distance_f32.c
  4. * Description: Minkowski 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 Minkowski
  33. @{
  34. */
  35. /* 6.14 bug */
  36. #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6100100) && (__ARMCC_VERSION < 6150001)
  37. __attribute__((weak)) float __powisf2(float a, int b)
  38. {
  39. const int recip = b < 0;
  40. float r = 1;
  41. while (1)
  42. {
  43. if (b & 1)
  44. r *= a;
  45. b /= 2;
  46. if (b == 0)
  47. break;
  48. a *= a;
  49. }
  50. return recip ? 1/r : r;
  51. }
  52. #endif
  53. /**
  54. * @brief Minkowski distance between two vectors
  55. *
  56. * @param[in] pA First vector
  57. * @param[in] pB Second vector
  58. * @param[in] order Distance order
  59. * @param[in] blockSize Number of samples
  60. * @return distance
  61. *
  62. */
  63. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  64. #include "arm_helium_utils.h"
  65. #include "arm_vec_math.h"
  66. float32_t arm_minkowski_distance_f32(const float32_t *pA,const float32_t *pB, int32_t order, uint32_t blockSize)
  67. {
  68. uint32_t blkCnt;
  69. f32x4_t a, b, tmpV, sumV;
  70. sumV = vdupq_n_f32(0.0f);
  71. blkCnt = blockSize >> 2;
  72. while (blkCnt > 0U) {
  73. a = vld1q(pA);
  74. b = vld1q(pB);
  75. tmpV = vabdq(a, b);
  76. tmpV = vpowq_f32(tmpV, vdupq_n_f32(order));
  77. sumV = vaddq(sumV, tmpV);
  78. pA += 4;
  79. pB += 4;
  80. blkCnt--;
  81. }
  82. /*
  83. * tail
  84. * (will be merged thru tail predication)
  85. */
  86. blkCnt = blockSize & 3;
  87. if (blkCnt > 0U) {
  88. mve_pred16_t p0 = vctp32q(blkCnt);
  89. a = vldrwq_z_f32(pA, p0);
  90. b = vldrwq_z_f32(pB, p0);
  91. tmpV = vabdq(a, b);
  92. tmpV = vpowq_f32(tmpV, vdupq_n_f32(order));
  93. sumV = vaddq_m(sumV, sumV, tmpV, p0);
  94. }
  95. return (powf(vecAddAcrossF32Mve(sumV), (1.0f / (float32_t) order)));
  96. }
  97. #else
  98. #if defined(ARM_MATH_NEON)
  99. #include "NEMath.h"
  100. float32_t arm_minkowski_distance_f32(const float32_t *pA,const float32_t *pB, int32_t order, uint32_t blockSize)
  101. {
  102. float32_t sum;
  103. uint32_t blkCnt;
  104. float32x4_t sumV,aV,bV, tmpV, n;
  105. float32x2_t sumV2;
  106. sum = 0.0f;
  107. sumV = vdupq_n_f32(0.0f);
  108. n = vdupq_n_f32(order);
  109. blkCnt = blockSize >> 2;
  110. while(blkCnt > 0)
  111. {
  112. aV = vld1q_f32(pA);
  113. bV = vld1q_f32(pB);
  114. pA += 4;
  115. pB += 4;
  116. tmpV = vabdq_f32(aV,bV);
  117. tmpV = vpowq_f32(tmpV,n);
  118. sumV = vaddq_f32(sumV, tmpV);
  119. blkCnt --;
  120. }
  121. sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV));
  122. sum = vget_lane_f32(sumV2, 0) + vget_lane_f32(sumV2, 1);
  123. blkCnt = blockSize & 3;
  124. while(blkCnt > 0)
  125. {
  126. sum += powf(fabsf(*pA++ - *pB++),order);
  127. blkCnt --;
  128. }
  129. return(powf(sum,(1.0f/order)));
  130. }
  131. #else
  132. float32_t arm_minkowski_distance_f32(const float32_t *pA,const float32_t *pB, int32_t order, uint32_t blockSize)
  133. {
  134. float32_t sum;
  135. uint32_t i;
  136. sum = 0.0f;
  137. for(i=0; i < blockSize; i++)
  138. {
  139. sum += powf(fabsf(pA[i] - pB[i]),order);
  140. }
  141. return(powf(sum,(1.0f/order)));
  142. }
  143. #endif
  144. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  145. /**
  146. * @} end of Minkowski group
  147. */