1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_logsumexp_f16.c
4  * Description:  LogSumExp
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 "dsp/statistics_functions_f16.h"
30 
31 #if defined(ARM_FLOAT16_SUPPORTED)
32 
33 #include <limits.h>
34 #include <math.h>
35 
36 /**
37   @ingroup groupStats
38  */
39 
40 /**
41   @defgroup Entropy Entropy
42 
43   Computes the entropy of a distribution
44 
45  */
46 
47 /**
48  * @addtogroup Entropy
49  * @{
50  */
51 
52 
53 /**
54  * @brief Entropy
55  *
56  * @param[in]  pSrcA        Array of input values.
57  * @param[in]  blockSize    Number of samples in the input array.
58  * @return     Entropy      -Sum(p ln p)
59  *
60  */
61 
62 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
63 
64 #include "arm_helium_utils.h"
65 #include "arm_vec_math_f16.h"
66 
arm_entropy_f16(const float16_t * pSrcA,uint32_t blockSize)67 float16_t arm_entropy_f16(const float16_t * pSrcA,uint32_t blockSize)
68 {
69     uint32_t        blkCnt;
70     _Float16       accum=0.0f16,p;
71 
72 
73     blkCnt = blockSize;
74 
75     f16x8_t         vSum = vdupq_n_f16(0.0f);
76     /* Compute 4 outputs at a time */
77     blkCnt = blockSize >> 3U;
78 
79     while (blkCnt > 0U)
80     {
81         f16x8_t         vecIn = vld1q(pSrcA);
82 
83         vSum = vaddq_f16(vSum, vmulq(vecIn, vlogq_f16(vecIn)));
84 
85         /*
86          * Decrement the blockSize loop counter
87          * Advance vector source and destination pointers
88          */
89         pSrcA += 8;
90         blkCnt --;
91     }
92 
93     accum = vecAddAcrossF16Mve(vSum);
94 
95     /* Tail */
96     blkCnt = blockSize & 0x7;
97     while(blkCnt > 0)
98     {
99        p = *pSrcA++;
100        accum += p * logf(p);
101 
102        blkCnt--;
103 
104     }
105 
106     return (-accum);
107 }
108 
109 #else
110 
arm_entropy_f16(const float16_t * pSrcA,uint32_t blockSize)111 float16_t arm_entropy_f16(const float16_t * pSrcA,uint32_t blockSize)
112 {
113     const float16_t *pIn;
114     uint32_t blkCnt;
115     _Float16 accum, p;
116 
117     pIn = pSrcA;
118     blkCnt = blockSize;
119 
120     accum = 0.0f;
121 
122     while(blkCnt > 0)
123     {
124        p = *pIn++;
125        accum += p * logf(p);
126 
127        blkCnt--;
128 
129     }
130 
131     return(-accum);
132 }
133 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
134 
135 /**
136  * @} end of Entropy group
137  */
138 
139 #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
140 
141