/ Drivers / CMSIS / DSP / Source / DistanceFunctions / arm_cityblock_distance_f16.c
arm_cityblock_distance_f16.c
  1  
  2  /* ----------------------------------------------------------------------
  3   * Project:      CMSIS DSP Library
  4   * Title:        arm_cityblock_distance_f16.c
  5   * Description:  Cityblock (Manhattan) distance between two vectors
  6   *
  7   * $Date:        23 April 2021
  8   * $Revision:    V1.9.0
  9   *
 10   * Target Processor: Cortex-M and Cortex-A cores
 11   * -------------------------------------------------------------------- */
 12  /*
 13   * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
 14   *
 15   * SPDX-License-Identifier: Apache-2.0
 16   *
 17   * Licensed under the Apache License, Version 2.0 (the License); you may
 18   * not use this file except in compliance with the License.
 19   * You may obtain a copy of the License at
 20   *
 21   * www.apache.org/licenses/LICENSE-2.0
 22   *
 23   * Unless required by applicable law or agreed to in writing, software
 24   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 25   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 26   * See the License for the specific language governing permissions and
 27   * limitations under the License.
 28   */
 29  
 30  #include "dsp/distance_functions_f16.h"
 31  
 32  #if defined(ARM_FLOAT16_SUPPORTED)
 33  
 34  #include <limits.h>
 35  #include <math.h>
 36  
 37  /**
 38    @ingroup FloatDist
 39   */
 40  
 41  /**
 42    @defgroup Manhattan Cityblock (Manhattan) distance
 43  
 44    Cityblock (Manhattan) distance
 45   */
 46  
 47  /**
 48    @addtogroup Manhattan
 49    @{
 50   */
 51  
 52  
 53  /**
 54   * @brief        Cityblock (Manhattan) distance between two vectors
 55   * @param[in]    pA         First vector
 56   * @param[in]    pB         Second vector
 57   * @param[in]    blockSize  vector length
 58   * @return distance
 59   *
 60   */
 61  #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
 62  
 63  #include "arm_helium_utils.h"
 64  #include "arm_vec_math.h"
 65  
 66  float16_t arm_cityblock_distance_f16(const float16_t *pA,const float16_t *pB, uint32_t blockSize)
 67  {
 68      uint32_t        blkCnt;
 69      f16x8_t         a, b, accumV, tempV;
 70  
 71      accumV = vdupq_n_f16(0.0f);
 72  
 73      blkCnt = blockSize >> 3;
 74      while (blkCnt > 0U) {
 75          a = vld1q(pA);
 76          b = vld1q(pB);
 77  
 78          tempV = vabdq(a, b);
 79          accumV = vaddq(accumV, tempV);
 80  
 81          pA += 8;
 82          pB += 8;
 83          blkCnt--;
 84      }
 85  
 86      /*
 87       * tail
 88       * (will be merged thru tail predication)
 89       */
 90      blkCnt = blockSize & 7;
 91      if (blkCnt > 0U) {
 92          mve_pred16_t    p0 = vctp16q(blkCnt);
 93  
 94          a = vldrhq_z_f16(pA, p0);
 95          b = vldrhq_z_f16(pB, p0);
 96  
 97          tempV = vabdq(a, b);
 98          accumV = vaddq_m(accumV, accumV, tempV, p0);
 99      }
100  
101      return vecAddAcrossF16Mve(accumV);
102  }
103  
104  #else
105  float16_t arm_cityblock_distance_f16(const float16_t *pA,const float16_t *pB, uint32_t blockSize)
106  {
107     _Float16 accum,tmpA, tmpB;
108  
109     accum = 0.0f16;
110     while(blockSize > 0)
111     {
112        tmpA = *pA++;
113        tmpB = *pB++;
114        accum  += (_Float16)fabsf((float32_t)((_Float16)tmpA - (_Float16)tmpB));
115        
116        blockSize --;
117     }
118    
119     return(accum);
120  }
121  #endif
122  
123  /**
124   * @} end of Manhattan group
125   */
126  
127  #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */ 
128