1 
2 /* ----------------------------------------------------------------------
3  * Project:      CMSIS DSP Library
4  * Title:        arm_euclidean_distance_f16.c
5  * Description:  Euclidean 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 /**
39   @ingroup FloatDist
40  */
41 
42 /**
43   @defgroup Euclidean Euclidean distance
44 
45   Euclidean distance
46  */
47 
48 
49 /**
50   @addtogroup Euclidean
51   @{
52  */
53 
54 
55 /**
56  * @brief        Euclidean distance between two vectors
57  * @param[in]    pA         First vector
58  * @param[in]    pB         Second vector
59  * @param[in]    blockSize  vector length
60  * @return distance
61  *
62  */
63 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
64 
65 #include "arm_helium_utils.h"
66 #include "arm_vec_math.h"
arm_euclidean_distance_f16(const float16_t * pA,const float16_t * pB,uint32_t blockSize)67 float16_t arm_euclidean_distance_f16(const float16_t *pA,const float16_t *pB, uint32_t blockSize)
68 {
69     uint32_t        blkCnt;
70     float16_t       tmp;
71     f16x8_t         a, b, accumV, tempV;
72 
73     accumV = vdupq_n_f16(0.0f);
74 
75     blkCnt = blockSize >> 3;
76     while (blkCnt > 0U) {
77         a = vld1q(pA);
78         b = vld1q(pB);
79 
80         tempV = vsubq(a, b);
81         accumV = vfmaq(accumV, tempV, tempV);
82 
83         pA += 8;
84         pB += 8;
85         blkCnt--;
86     }
87 
88     /*
89      * tail
90      * (will be merged thru tail predication)
91      */
92     blkCnt = blockSize & 7;
93     if (blkCnt > 0U) {
94         mve_pred16_t    p0 = vctp16q(blkCnt);
95 
96         a = vldrhq_z_f16(pA, p0);
97         b = vldrhq_z_f16(pB, p0);
98 
99         tempV = vsubq(a, b);
100         accumV = vfmaq_m(accumV, tempV, tempV, p0);
101     }
102 
103     arm_sqrt_f16(vecAddAcrossF16Mve(accumV), &tmp);
104     return (tmp);
105 }
106 
107 #else
arm_euclidean_distance_f16(const float16_t * pA,const float16_t * pB,uint32_t blockSize)108 float16_t arm_euclidean_distance_f16(const float16_t *pA,const float16_t *pB, uint32_t blockSize)
109 {
110    _Float16 accum=0.0f,tmp;
111    float16_t result;
112 
113    while(blockSize > 0)
114    {
115       tmp = (_Float16)*pA++ - (_Float16)*pB++;
116       accum += SQ(tmp);
117       blockSize --;
118    }
119    arm_sqrt_f16(accum,&result);
120    return(result);
121 }
122 
123 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
124 
125 
126 /**
127  * @} end of Euclidean group
128  */
129 
130 #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
131 
132