arm_entropy_f16.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include "dsp/statistics_functions_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. #include <limits.h>
  31. #include <math.h>
  32. /**
  33. @ingroup groupStats
  34. */
  35. /**
  36. @defgroup Entropy Entropy
  37. Computes the entropy of a distribution
  38. */
  39. /**
  40. * @addtogroup Entropy
  41. * @{
  42. */
  43. /**
  44. * @brief Entropy
  45. *
  46. * @param[in] pSrcA Array of input values.
  47. * @param[in] blockSize Number of samples in the input array.
  48. * @return Entropy -Sum(p ln p)
  49. *
  50. */
  51. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  52. #include "arm_helium_utils.h"
  53. #include "arm_vec_math_f16.h"
  54. float16_t arm_entropy_f16(const float16_t * pSrcA,uint32_t blockSize)
  55. {
  56. uint32_t blkCnt;
  57. _Float16 accum=0.0f16,p;
  58. blkCnt = blockSize;
  59. f16x8_t vSum = vdupq_n_f16(0.0f);
  60. /* Compute 4 outputs at a time */
  61. blkCnt = blockSize >> 3U;
  62. while (blkCnt > 0U)
  63. {
  64. f16x8_t vecIn = vld1q(pSrcA);
  65. vSum = vaddq_f16(vSum, vmulq(vecIn, vlogq_f16(vecIn)));
  66. /*
  67. * Decrement the blockSize loop counter
  68. * Advance vector source and destination pointers
  69. */
  70. pSrcA += 8;
  71. blkCnt --;
  72. }
  73. accum = vecAddAcrossF16Mve(vSum);
  74. /* Tail */
  75. blkCnt = blockSize & 0x7;
  76. while(blkCnt > 0)
  77. {
  78. p = *pSrcA++;
  79. accum += p * (_Float16)logf((float32_t)p);
  80. blkCnt--;
  81. }
  82. return (-accum);
  83. }
  84. #else
  85. float16_t arm_entropy_f16(const float16_t * pSrcA,uint32_t blockSize)
  86. {
  87. const float16_t *pIn;
  88. uint32_t blkCnt;
  89. _Float16 accum, p;
  90. pIn = pSrcA;
  91. blkCnt = blockSize;
  92. accum = 0.0f;
  93. while(blkCnt > 0)
  94. {
  95. p = *pIn++;
  96. accum += p * (_Float16)logf((float32_t)p);
  97. blkCnt--;
  98. }
  99. return(-accum);
  100. }
  101. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  102. /**
  103. * @} end of Entropy group
  104. */
  105. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */