arm_canberra_distance_f32.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_canberra_distance_f32.c
  4. * Description: Canberra 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 Canberra
  33. @{
  34. */
  35. /**
  36. * @brief Canberra distance between two vectors
  37. *
  38. * This function may divide by zero when samples pA[i] and pB[i] are both zero.
  39. * The result of the computation will be correct. So the division per zero may be
  40. * ignored.
  41. *
  42. * @param[in] pA First vector
  43. * @param[in] pB Second vector
  44. * @param[in] blockSize vector length
  45. * @return distance
  46. *
  47. */
  48. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  49. #include "arm_helium_utils.h"
  50. #include "arm_vec_math.h"
  51. float32_t arm_canberra_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  52. {
  53. float32_t accum = 0.0f;
  54. uint32_t blkCnt;
  55. f32x4_t a, b, c, accumV;
  56. accumV = vdupq_n_f32(0.0f);
  57. blkCnt = blockSize >> 2;
  58. while (blkCnt > 0) {
  59. a = vld1q(pA);
  60. b = vld1q(pB);
  61. c = vabdq(a, b);
  62. a = vabsq(a);
  63. b = vabsq(b);
  64. a = vaddq(a, b);
  65. /*
  66. * May divide by zero when a and b have both the same lane at zero.
  67. */
  68. a = vrecip_medprec_f32(a);
  69. /*
  70. * Force result of a division by 0 to 0. It the behavior of the
  71. * sklearn canberra function.
  72. */
  73. a = vdupq_m_n_f32(a, 0.0f, vcmpeqq(a, 0.0f));
  74. c = vmulq(c, a);
  75. accumV = vaddq(accumV, c);
  76. pA += 4;
  77. pB += 4;
  78. blkCnt--;
  79. }
  80. blkCnt = blockSize & 3;
  81. if (blkCnt > 0U) {
  82. mve_pred16_t p0 = vctp32q(blkCnt);
  83. a = vldrwq_z_f32(pA, p0);
  84. b = vldrwq_z_f32(pB, p0);
  85. c = vabdq(a, b);
  86. a = vabsq(a);
  87. b = vabsq(b);
  88. a = vaddq(a, b);
  89. /*
  90. * May divide by zero when a and b have both the same lane at zero.
  91. */
  92. a = vrecip_medprec_f32(a);
  93. /*
  94. * Force result of a division by 0 to 0. It the behavior of the
  95. * sklearn canberra function.
  96. */
  97. a = vdupq_m_n_f32(a, 0.0f, vcmpeqq(a, 0.0f));
  98. c = vmulq(c, a);
  99. accumV = vaddq_m(accumV, accumV, c, p0);
  100. }
  101. accum = vecAddAcrossF32Mve(accumV);
  102. return (accum);
  103. }
  104. #else
  105. #if defined(ARM_MATH_NEON)
  106. #include "NEMath.h"
  107. float32_t arm_canberra_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  108. {
  109. float32_t accum=0.0f, tmpA, tmpB,diff,sum;
  110. uint32_t blkCnt;
  111. float32x4_t a,b,c,accumV;
  112. float32x2_t accumV2;
  113. uint32x4_t isZeroV;
  114. float32x4_t zeroV = vdupq_n_f32(0.0f);
  115. accumV = vdupq_n_f32(0.0f);
  116. blkCnt = blockSize >> 2;
  117. while(blkCnt > 0)
  118. {
  119. a = vld1q_f32(pA);
  120. b = vld1q_f32(pB);
  121. c = vabdq_f32(a,b);
  122. a = vabsq_f32(a);
  123. b = vabsq_f32(b);
  124. a = vaddq_f32(a,b);
  125. isZeroV = vceqq_f32(a,zeroV);
  126. /*
  127. * May divide by zero when a and b have both the same lane at zero.
  128. */
  129. a = vinvq_f32(a);
  130. /*
  131. * Force result of a division by 0 to 0. It the behavior of the
  132. * sklearn canberra function.
  133. */
  134. a = vreinterpretq_f32_s32(vbicq_s32(vreinterpretq_s32_f32(a),vreinterpretq_s32_u32(isZeroV)));
  135. c = vmulq_f32(c,a);
  136. accumV = vaddq_f32(accumV,c);
  137. pA += 4;
  138. pB += 4;
  139. blkCnt --;
  140. }
  141. accumV2 = vpadd_f32(vget_low_f32(accumV),vget_high_f32(accumV));
  142. accum = vget_lane_f32(accumV2, 0) + vget_lane_f32(accumV2, 1);
  143. blkCnt = blockSize & 3;
  144. while(blkCnt > 0)
  145. {
  146. tmpA = *pA++;
  147. tmpB = *pB++;
  148. diff = fabsf(tmpA - tmpB);
  149. sum = fabsf(tmpA) + fabsf(tmpB);
  150. if ((tmpA != 0.0f) || (tmpB != 0.0f))
  151. {
  152. accum += (diff / sum);
  153. }
  154. blkCnt --;
  155. }
  156. return(accum);
  157. }
  158. #else
  159. float32_t arm_canberra_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  160. {
  161. float32_t accum=0.0f, tmpA, tmpB,diff,sum;
  162. while(blockSize > 0)
  163. {
  164. tmpA = *pA++;
  165. tmpB = *pB++;
  166. diff = fabsf(tmpA - tmpB);
  167. sum = fabsf(tmpA) + fabsf(tmpB);
  168. if ((tmpA != 0.0f) || (tmpB != 0.0f))
  169. {
  170. accum += (diff / sum);
  171. }
  172. blockSize --;
  173. }
  174. return(accum);
  175. }
  176. #endif
  177. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  178. /**
  179. * @} end of Canberra group
  180. */