arm_canberra_distance_f16.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_canberra_distance_f16.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_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. #include <limits.h>
  31. #include <math.h>
  32. /**
  33. @ingroup FloatDist
  34. */
  35. /**
  36. @defgroup Canberra Canberra distance
  37. Canberra distance
  38. */
  39. /**
  40. @addtogroup Canberra
  41. @{
  42. */
  43. /**
  44. * @brief Canberra distance between two vectors
  45. *
  46. * This function may divide by zero when samples pA[i] and pB[i] are both zero.
  47. * The result of the computation will be correct. So the division per zero may be
  48. * ignored.
  49. *
  50. * @param[in] pA First vector
  51. * @param[in] pB Second vector
  52. * @param[in] blockSize vector length
  53. * @return distance
  54. *
  55. */
  56. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  57. #include "arm_helium_utils.h"
  58. #include "arm_vec_math_f16.h"
  59. float16_t arm_canberra_distance_f16(const float16_t *pA,const float16_t *pB, uint32_t blockSize)
  60. {
  61. _Float16 accum = 0.0f16;
  62. uint32_t blkCnt;
  63. f16x8_t a, b, c, accumV;
  64. accumV = vdupq_n_f16(0.0f);
  65. blkCnt = blockSize >> 3;
  66. while (blkCnt > 0) {
  67. a = vld1q(pA);
  68. b = vld1q(pB);
  69. c = vabdq(a, b);
  70. a = vabsq(a);
  71. b = vabsq(b);
  72. a = vaddq(a, b);
  73. /*
  74. * May divide by zero when a and b have both the same lane at zero.
  75. */
  76. a = vrecip_hiprec_f16(a);
  77. /*
  78. * Force result of a division by 0 to 0. It the behavior of the
  79. * sklearn canberra function.
  80. */
  81. a = vdupq_m_n_f16(a, 0.0f, vcmpeqq(a, 0.0f));
  82. c = vmulq(c, a);
  83. accumV = vaddq(accumV, c);
  84. pA += 8;
  85. pB += 8;
  86. blkCnt--;
  87. }
  88. blkCnt = blockSize & 7;
  89. if (blkCnt > 0U) {
  90. mve_pred16_t p0 = vctp16q(blkCnt);
  91. a = vldrhq_z_f16(pA, p0);
  92. b = vldrhq_z_f16(pB, p0);
  93. c = vabdq(a, b);
  94. a = vabsq(a);
  95. b = vabsq(b);
  96. a = vaddq(a, b);
  97. /*
  98. * May divide by zero when a and b have both the same lane at zero.
  99. */
  100. a = vrecip_hiprec_f16(a);
  101. /*
  102. * Force result of a division by 0 to 0. It the behavior of the
  103. * sklearn canberra function.
  104. */
  105. a = vdupq_m_n_f16(a, 0.0f, vcmpeqq(a, 0.0f));
  106. c = vmulq(c, a);
  107. accumV = vaddq_m(accumV, accumV, c, p0);
  108. }
  109. accum = vecAddAcrossF16Mve(accumV);
  110. return (accum);
  111. }
  112. #else
  113. float16_t arm_canberra_distance_f16(const float16_t *pA,const float16_t *pB, uint32_t blockSize)
  114. {
  115. _Float16 accum=0.0f, tmpA, tmpB,diff,sum;
  116. while(blockSize > 0)
  117. {
  118. tmpA = *pA++;
  119. tmpB = *pB++;
  120. diff = fabsf(tmpA - tmpB);
  121. sum = fabsf(tmpA) + fabsf(tmpB);
  122. if ((tmpA != 0.0f16) || (tmpB != 0.0f16))
  123. {
  124. accum += (diff / sum);
  125. }
  126. blockSize --;
  127. }
  128. return(accum);
  129. }
  130. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  131. /**
  132. * @} end of Canberra group
  133. */
  134. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */