1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_weighted_average_f16.c
4  * Description:  Weighted average
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 
29 #include <limits.h>
30 #include <math.h>
31 
32 #include "dsp/support_functions_f16.h"
33 
34 #if defined(ARM_FLOAT16_SUPPORTED)
35 
36 /**
37   @ingroup groupSupport
38  */
39 
40 /**
41   @defgroup weightedaverage Weighted Average
42 
43   Weighted average of values
44  */
45 
46 
47 /**
48  * @addtogroup weightedaverage
49  * @{
50  */
51 
52 
53 /**
54  * @brief Weighted average
55  *
56  *
57  * @param[in]    *in           Array of input values.
58  * @param[in]    *weigths      Weights
59  * @param[in]    blockSize     Number of samples in the input array.
60  *
61  * @return       Weighted average
62  *
63  */
64 
65 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
66 
67 #include "arm_helium_utils.h"
68 
arm_weighted_average_f16(const float16_t * in,const float16_t * weigths,uint32_t blockSize)69 ARM_DSP_ATTRIBUTE float16_t arm_weighted_average_f16(const float16_t *in,const float16_t *weigths, uint32_t blockSize)
70 {
71     _Float16       accum1, accum2;
72     float16x8_t    accum1V, accum2V;
73     float16x8_t    inV, wV;
74     const float16_t *pIn, *pW;
75     uint32_t        blkCnt;
76 
77 
78     pIn = in;
79     pW = weigths;
80 
81 
82     accum1V = vdupq_n_f16(0.0f16);
83     accum2V = vdupq_n_f16(0.0f16);
84 
85     blkCnt = blockSize >> 3;
86     while (blkCnt > 0)
87     {
88         inV = vld1q(pIn);
89         wV = vld1q(pW);
90 
91         pIn += 4;
92         pW += 4;
93 
94         accum1V = vfmaq(accum1V, inV, wV);
95         accum2V = vaddq(accum2V, wV);
96         blkCnt--;
97     }
98 
99     accum1 = vecAddAcrossF16Mve(accum1V);
100     accum2 = vecAddAcrossF16Mve(accum2V);
101 
102     blkCnt = blockSize & 7;
103     while(blkCnt > 0)
104     {
105         accum1 += (_Float16)*pIn++ * (_Float16)*pW;
106         accum2 += (_Float16)*pW++;
107         blkCnt--;
108     }
109 
110 
111     return (accum1 / accum2);
112 }
113 
114 #else
115 
arm_weighted_average_f16(const float16_t * in,const float16_t * weigths,uint32_t blockSize)116 ARM_DSP_ATTRIBUTE float16_t arm_weighted_average_f16(const float16_t *in, const float16_t *weigths, uint32_t blockSize)
117 {
118 
119     _Float16 accum1, accum2;
120     const float16_t *pIn, *pW;
121     uint32_t blkCnt;
122 
123 
124     pIn = in;
125     pW = weigths;
126 
127     accum1=0.0f16;
128     accum2=0.0f16;
129 
130     blkCnt = blockSize;
131     while(blkCnt > 0)
132     {
133         accum1 += (_Float16)*pIn++ * (_Float16)*pW;
134         accum2 += (_Float16)*pW++;
135         blkCnt--;
136     }
137 
138     return(accum1 / accum2);
139 }
140 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
141 
142 /**
143  * @} end of weightedaverage group
144  */
145 
146 #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
147 
148