1 
2 /* ----------------------------------------------------------------------
3  * Project:      CMSIS DSP Library
4  * Title:        arm_minkowski_distance_f16.c
5  * Description:  Minkowski 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 Minkowski Minkowski distance
43 
44   Minkowski distance
45  */
46 
47 /**
48   @addtogroup Minkowski
49   @{
50  */
51 
52 
53 /**
54  * @brief        Minkowski distance between two vectors
55  *
56  * @param[in]    pA         First vector
57  * @param[in]    pB         Second vector
58  * @param[in]    order      Distance order
59  * @param[in]    blockSize  Number of samples
60  * @return distance
61  *
62  */
63 
64 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
65 
66 #include "arm_helium_utils.h"
67 #include "arm_vec_math_f16.h"
68 
arm_minkowski_distance_f16(const float16_t * pA,const float16_t * pB,int32_t order,uint32_t blockSize)69 float16_t arm_minkowski_distance_f16(const float16_t *pA,const float16_t *pB, int32_t order, uint32_t blockSize)
70 {
71     uint32_t        blkCnt;
72     f16x8_t         a, b, tmpV, sumV;
73 
74     sumV = vdupq_n_f16(0.0f);
75 
76     blkCnt = blockSize >> 3;
77     while (blkCnt > 0U) {
78         a = vld1q(pA);
79         b = vld1q(pB);
80 
81         tmpV = vabdq(a, b);
82         tmpV = vpowq_f16(tmpV, vdupq_n_f16(order));
83         sumV = vaddq(sumV, tmpV);
84 
85         pA += 8;
86         pB += 8;
87         blkCnt--;
88     }
89 
90     /*
91      * tail
92      * (will be merged thru tail predication)
93      */
94     blkCnt = blockSize & 7;
95     if (blkCnt > 0U) {
96         mve_pred16_t    p0 = vctp16q(blkCnt);
97 
98         a = vldrhq_z_f16(pA, p0);
99         b = vldrhq_z_f16(pB, p0);
100 
101         tmpV = vabdq(a, b);
102         tmpV = vpowq_f16(tmpV, vdupq_n_f16(order));
103         sumV = vaddq_m(sumV, sumV, tmpV, p0);
104     }
105 
106     return (powf(vecAddAcrossF16Mve(sumV), (1.0f / (float16_t) order)));
107 }
108 
109 
110 #else
111 
112 
arm_minkowski_distance_f16(const float16_t * pA,const float16_t * pB,int32_t order,uint32_t blockSize)113 float16_t arm_minkowski_distance_f16(const float16_t *pA,const float16_t *pB, int32_t order, uint32_t blockSize)
114 {
115     _Float16 sum;
116     uint32_t i;
117 
118     sum = 0.0f;
119     for(i=0; i < blockSize; i++)
120     {
121        sum += (_Float16)powf(fabsf(pA[i] - pB[i]),order);
122     }
123 
124 
125     return(powf(sum,(1.0f/order)));
126 
127 }
128 
129 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
130 
131 
132 /**
133  * @} end of Minkowski group
134  */
135 
136 #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
137 
138