arm_canberra_distance_f32.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_canberra_distance_f32.c
  4. * Description: Canberra distance between two vectors
  5. *
  6. *
  7. * Target Processor: Cortex-M cores
  8. * -------------------------------------------------------------------- */
  9. /*
  10. * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved.
  11. *
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the License); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #include "arm_math.h"
  27. #include <limits.h>
  28. #include <math.h>
  29. /**
  30. @addtogroup FloatDist
  31. @{
  32. */
  33. /**
  34. * @brief Canberra distance between two vectors
  35. *
  36. * This function may divide by zero when samples pA[i] and pB[i] are both zero.
  37. * The result of the computation will be correct. So the division per zero may be
  38. * ignored.
  39. *
  40. * @param[in] pA First vector
  41. * @param[in] pB Second vector
  42. * @param[in] blockSize vector length
  43. * @return distance
  44. *
  45. */
  46. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  47. #include "arm_helium_utils.h"
  48. #include "arm_vec_math.h"
  49. float32_t arm_canberra_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  50. {
  51. float32_t accum = 0.0f;
  52. uint32_t blkCnt;
  53. f32x4_t a, b, c, accumV;
  54. accumV = vdupq_n_f32(0.0f);
  55. blkCnt = blockSize >> 2;
  56. while (blkCnt > 0) {
  57. a = vld1q(pA);
  58. b = vld1q(pB);
  59. c = vabdq(a, b);
  60. a = vabsq(a);
  61. b = vabsq(b);
  62. a = vaddq(a, b);
  63. /*
  64. * May divide by zero when a and b have both the same lane at zero.
  65. */
  66. a = vrecip_medprec_f32(a);
  67. /*
  68. * Force result of a division by 0 to 0. It the behavior of the
  69. * sklearn canberra function.
  70. */
  71. a = vdupq_m_n_f32(a, 0.0f, vcmpeqq(a, 0.0f));
  72. c = vmulq(c, a);
  73. accumV = vaddq(accumV, c);
  74. pA += 4;
  75. pB += 4;
  76. blkCnt--;
  77. }
  78. blkCnt = blockSize & 3;
  79. if (blkCnt > 0U) {
  80. mve_pred16_t p0 = vctp32q(blkCnt);
  81. a = vldrwq_z_f32(pA, p0);
  82. b = vldrwq_z_f32(pB, p0);
  83. c = vabdq(a, b);
  84. a = vabsq(a);
  85. b = vabsq(b);
  86. a = vaddq(a, b);
  87. /*
  88. * May divide by zero when a and b have both the same lane at zero.
  89. */
  90. a = vrecip_medprec_f32(a);
  91. /*
  92. * Force result of a division by 0 to 0. It the behavior of the
  93. * sklearn canberra function.
  94. */
  95. a = vdupq_m_n_f32(a, 0.0f, vcmpeqq(a, 0.0f));
  96. c = vmulq(c, a);
  97. accumV = vaddq_m(accumV, accumV, c, p0);
  98. }
  99. accum = vecAddAcrossF32Mve(accumV);
  100. return (accum);
  101. }
  102. #else
  103. #if defined(ARM_MATH_NEON)
  104. #include "NEMath.h"
  105. float32_t arm_canberra_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  106. {
  107. float32_t accum=0.0f, tmpA, tmpB,diff,sum;
  108. uint32_t blkCnt;
  109. float32x4_t a,b,c,accumV;
  110. float32x2_t accumV2;
  111. uint32x4_t isZeroV;
  112. float32x4_t zeroV = vdupq_n_f32(0.0f);
  113. accumV = vdupq_n_f32(0.0f);
  114. blkCnt = blockSize >> 2;
  115. while(blkCnt > 0)
  116. {
  117. a = vld1q_f32(pA);
  118. b = vld1q_f32(pB);
  119. c = vabdq_f32(a,b);
  120. a = vabsq_f32(a);
  121. b = vabsq_f32(b);
  122. a = vaddq_f32(a,b);
  123. isZeroV = vceqq_f32(a,zeroV);
  124. /*
  125. * May divide by zero when a and b have both the same lane at zero.
  126. */
  127. a = vinvq_f32(a);
  128. /*
  129. * Force result of a division by 0 to 0. It the behavior of the
  130. * sklearn canberra function.
  131. */
  132. a = vreinterpretq_f32_s32(vbicq_s32(vreinterpretq_s32_f32(a),vreinterpretq_s32_u32(isZeroV)));
  133. c = vmulq_f32(c,a);
  134. accumV = vaddq_f32(accumV,c);
  135. pA += 4;
  136. pB += 4;
  137. blkCnt --;
  138. }
  139. accumV2 = vpadd_f32(vget_low_f32(accumV),vget_high_f32(accumV));
  140. accum = vget_lane_f32(accumV2, 0) + vget_lane_f32(accumV2, 1);
  141. blkCnt = blockSize & 3;
  142. while(blkCnt > 0)
  143. {
  144. tmpA = *pA++;
  145. tmpB = *pB++;
  146. diff = fabsf(tmpA - tmpB);
  147. sum = fabsf(tmpA) + fabsf(tmpB);
  148. if ((tmpA != 0.0f) || (tmpB != 0.0f))
  149. {
  150. accum += (diff / sum);
  151. }
  152. blkCnt --;
  153. }
  154. return(accum);
  155. }
  156. #else
  157. float32_t arm_canberra_distance_f32(const float32_t *pA,const float32_t *pB, uint32_t blockSize)
  158. {
  159. float32_t accum=0.0f, tmpA, tmpB,diff,sum;
  160. while(blockSize > 0)
  161. {
  162. tmpA = *pA++;
  163. tmpB = *pB++;
  164. diff = fabsf(tmpA - tmpB);
  165. sum = fabsf(tmpA) + fabsf(tmpB);
  166. if ((tmpA != 0.0f) || (tmpB != 0.0f))
  167. {
  168. accum += (diff / sum);
  169. }
  170. blockSize --;
  171. }
  172. return(accum);
  173. }
  174. #endif
  175. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  176. /**
  177. * @} end of FloatDist group
  178. */