arm_entropy_f16.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_logsumexp_f16.c
  4. * Description: LogSumExp
  5. *
  6. *
  7. * Target Processor: Cortex-M and Cortex-A cores
  8. * -------------------------------------------------------------------- */
  9. /*
  10. * Copyright (C) 2010-2020 ARM Limited or its affiliates. All rights reserved.
  11. *
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the License); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #include "dsp/statistics_functions_f16.h"
  27. #if defined(ARM_FLOAT16_SUPPORTED)
  28. #include <limits.h>
  29. #include <math.h>
  30. /**
  31. @ingroup groupStats
  32. */
  33. /**
  34. @defgroup Entropy Entropy
  35. Computes the entropy of a distribution
  36. */
  37. /**
  38. * @addtogroup Entropy
  39. * @{
  40. */
  41. /**
  42. * @brief Entropy
  43. *
  44. * @param[in] pSrcA Array of input values.
  45. * @param[in] blockSize Number of samples in the input array.
  46. * @return Entropy -Sum(p ln p)
  47. *
  48. */
  49. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  50. #include "arm_helium_utils.h"
  51. #include "arm_vec_math_f16.h"
  52. float16_t arm_entropy_f16(const float16_t * pSrcA,uint32_t blockSize)
  53. {
  54. uint32_t blkCnt;
  55. _Float16 accum=0.0f16,p;
  56. blkCnt = blockSize;
  57. f16x8_t vSum = vdupq_n_f16(0.0f);
  58. /* Compute 4 outputs at a time */
  59. blkCnt = blockSize >> 3U;
  60. while (blkCnt > 0U)
  61. {
  62. f16x8_t vecIn = vld1q(pSrcA);
  63. vSum = vaddq_f16(vSum, vmulq(vecIn, vlogq_f16(vecIn)));
  64. /*
  65. * Decrement the blockSize loop counter
  66. * Advance vector source and destination pointers
  67. */
  68. pSrcA += 8;
  69. blkCnt --;
  70. }
  71. accum = vecAddAcrossF16Mve(vSum);
  72. /* Tail */
  73. blkCnt = blockSize & 0x7;
  74. while(blkCnt > 0)
  75. {
  76. p = *pSrcA++;
  77. accum += p * logf(p);
  78. blkCnt--;
  79. }
  80. return (-accum);
  81. }
  82. #else
  83. float16_t arm_entropy_f16(const float16_t * pSrcA,uint32_t blockSize)
  84. {
  85. const float16_t *pIn;
  86. uint32_t blkCnt;
  87. _Float16 accum, p;
  88. pIn = pSrcA;
  89. blkCnt = blockSize;
  90. accum = 0.0f;
  91. while(blkCnt > 0)
  92. {
  93. p = *pIn++;
  94. accum += p * logf(p);
  95. blkCnt--;
  96. }
  97. return(-accum);
  98. }
  99. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  100. /**
  101. * @} end of Entropy group
  102. */
  103. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */