arm_chebyshev_distance_f16.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_chebyshev_distance_f16.c
  4. * Description: Chebyshev 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 Chebyshev Chebyshev distance
  37. Chebyshev distance
  38. */
  39. /**
  40. @addtogroup Chebyshev
  41. @{
  42. */
  43. /**
  44. * @brief Chebyshev 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_chebyshev_distance_f16(const float16_t *pA,const float16_t *pB, uint32_t blockSize)
  55. {
  56. uint32_t blkCnt; /* loop counters */
  57. f16x8_t vecA, vecB;
  58. f16x8_t vecDiff = vdupq_n_f16(0.0);
  59. float16_t maxValue = 0.0f16;
  60. blkCnt = blockSize >> 3;
  61. while (blkCnt > 0U) {
  62. vecA = vld1q(pA);
  63. pA += 8;
  64. vecB = vld1q(pB);
  65. pB += 8;
  66. /*
  67. * update per-lane max.
  68. */
  69. vecDiff = vmaxnmaq(vsubq(vecA, vecB), vecDiff);
  70. /*
  71. * Decrement the blockSize loop counter
  72. */
  73. blkCnt--;
  74. }
  75. /*
  76. * tail
  77. * (will be merged thru tail predication)
  78. */
  79. blkCnt = blockSize & 7;
  80. if (blkCnt > 0U) {
  81. mve_pred16_t p0 = vctp16q(blkCnt);
  82. vecA = vldrhq_z_f16(pA, p0);
  83. vecB = vldrhq_z_f16(pB, p0);
  84. /*
  85. * Get current max per lane and current index per lane
  86. * when a max is selected
  87. */
  88. vecDiff = vmaxnmaq_m(vecDiff, vsubq(vecA, vecB), p0);
  89. }
  90. /*
  91. * Get max value across the vector
  92. */
  93. return vmaxnmavq(maxValue, vecDiff);
  94. }
  95. #else
  96. float16_t arm_chebyshev_distance_f16(const float16_t *pA,const float16_t *pB, uint32_t blockSize)
  97. {
  98. _Float16 diff=0.0f, maxVal,tmpA, tmpB;
  99. tmpA = *pA++;
  100. tmpB = *pB++;
  101. diff = (_Float16)fabsf((float32_t)((_Float16)tmpA - (_Float16)tmpB));
  102. maxVal = diff;
  103. blockSize--;
  104. while(blockSize > 0)
  105. {
  106. tmpA = *pA++;
  107. tmpB = *pB++;
  108. diff = (_Float16)fabsf((float32_t)((_Float16)tmpA - (_Float16)tmpB));
  109. if ((_Float16)diff > (_Float16)maxVal)
  110. {
  111. maxVal = diff;
  112. }
  113. blockSize --;
  114. }
  115. return(maxVal);
  116. }
  117. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  118. /**
  119. * @} end of Chebyshev group
  120. */
  121. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */