You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

132 lines
3.0 KiB

  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_euclidean_distance_f16.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_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. #include <limits.h>
  31. #include <math.h>
  32. /**
  33. @ingroup FloatDist
  34. */
  35. /**
  36. @defgroup Euclidean Euclidean distance
  37. Euclidean distance
  38. */
  39. /**
  40. @addtogroup Euclidean
  41. @{
  42. */
  43. /**
  44. * @brief Euclidean distance between two vectors
  45. * @param[in] pA First vector
  46. * @param[in] pB Second vector
  47. * @param[in] blockSize vector length
  48. * @return distance
  49. *
  50. */
  51. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  52. #include "arm_helium_utils.h"
  53. #include "arm_vec_math.h"
  54. float16_t arm_euclidean_distance_f16(const float16_t *pA,const float16_t *pB, uint32_t blockSize)
  55. {
  56. uint32_t blkCnt;
  57. float16_t tmp;
  58. f16x8_t a, b, accumV, tempV;
  59. accumV = vdupq_n_f16(0.0f);
  60. blkCnt = blockSize >> 3;
  61. while (blkCnt > 0U) {
  62. a = vld1q(pA);
  63. b = vld1q(pB);
  64. tempV = vsubq(a, b);
  65. accumV = vfmaq(accumV, tempV, tempV);
  66. pA += 8;
  67. pB += 8;
  68. blkCnt--;
  69. }
  70. /*
  71. * tail
  72. * (will be merged thru tail predication)
  73. */
  74. blkCnt = blockSize & 7;
  75. if (blkCnt > 0U) {
  76. mve_pred16_t p0 = vctp16q(blkCnt);
  77. a = vldrhq_z_f16(pA, p0);
  78. b = vldrhq_z_f16(pB, p0);
  79. tempV = vsubq(a, b);
  80. accumV = vfmaq_m(accumV, tempV, tempV, p0);
  81. }
  82. arm_sqrt_f16(vecAddAcrossF16Mve(accumV), &tmp);
  83. return (tmp);
  84. }
  85. #else
  86. float16_t arm_euclidean_distance_f16(const float16_t *pA,const float16_t *pB, uint32_t blockSize)
  87. {
  88. _Float16 accum=0.0f,tmp;
  89. float16_t result;
  90. while(blockSize > 0)
  91. {
  92. tmp = (_Float16)*pA++ - (_Float16)*pB++;
  93. accum += SQ(tmp);
  94. blockSize --;
  95. }
  96. arm_sqrt_f16(accum,&result);
  97. return(result);
  98. }
  99. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  100. /**
  101. * @} end of Euclidean group
  102. */
  103. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */