arm_entropy_f32.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_logsumexp_f32.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.h"
  29. #include <limits.h>
  30. #include <math.h>
  31. /**
  32. * @addtogroup Entropy
  33. * @{
  34. */
  35. /**
  36. * @brief Entropy
  37. *
  38. * @param[in] pSrcA Array of input values.
  39. * @param[in] blockSize Number of samples in the input array.
  40. * @return Entropy -Sum(p ln p)
  41. *
  42. */
  43. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. #include "arm_vec_math.h"
  46. float32_t arm_entropy_f32(const float32_t * pSrcA,uint32_t blockSize)
  47. {
  48. uint32_t blkCnt;
  49. float32_t accum=0.0f,p;
  50. blkCnt = blockSize;
  51. f32x4_t vSum = vdupq_n_f32(0.0f);
  52. /* Compute 4 outputs at a time */
  53. blkCnt = blockSize >> 2U;
  54. while (blkCnt > 0U)
  55. {
  56. f32x4_t vecIn = vld1q(pSrcA);
  57. vSum = vaddq_f32(vSum, vmulq(vecIn, vlogq_f32(vecIn)));
  58. /*
  59. * Decrement the blockSize loop counter
  60. * Advance vector source and destination pointers
  61. */
  62. pSrcA += 4;
  63. blkCnt --;
  64. }
  65. accum = vecAddAcrossF32Mve(vSum);
  66. /* Tail */
  67. blkCnt = blockSize & 0x3;
  68. while(blkCnt > 0)
  69. {
  70. p = *pSrcA++;
  71. accum += p * logf(p);
  72. blkCnt--;
  73. }
  74. return (-accum);
  75. }
  76. #else
  77. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  78. #include "NEMath.h"
  79. float32_t arm_entropy_f32(const float32_t * pSrcA,uint32_t blockSize)
  80. {
  81. const float32_t *pIn;
  82. uint32_t blkCnt;
  83. float32_t accum, p;
  84. float32x4_t accumV;
  85. float32x2_t accumV2;
  86. float32x4_t tmpV, tmpV2;
  87. pIn = pSrcA;
  88. accum = 0.0f;
  89. accumV = vdupq_n_f32(0.0f);
  90. blkCnt = blockSize >> 2;
  91. while(blkCnt > 0)
  92. {
  93. tmpV = vld1q_f32(pIn);
  94. pIn += 4;
  95. tmpV2 = vlogq_f32(tmpV);
  96. accumV = vmlaq_f32(accumV, tmpV, tmpV2);
  97. blkCnt--;
  98. }
  99. accumV2 = vpadd_f32(vget_low_f32(accumV),vget_high_f32(accumV));
  100. accum = vget_lane_f32(accumV2, 0) + vget_lane_f32(accumV2, 1);
  101. blkCnt = blockSize & 3;
  102. while(blkCnt > 0)
  103. {
  104. p = *pIn++;
  105. accum += p * logf(p);
  106. blkCnt--;
  107. }
  108. return(-accum);
  109. }
  110. #else
  111. float32_t arm_entropy_f32(const float32_t * pSrcA,uint32_t blockSize)
  112. {
  113. const float32_t *pIn;
  114. uint32_t blkCnt;
  115. float32_t accum, p;
  116. pIn = pSrcA;
  117. blkCnt = blockSize;
  118. accum = 0.0f;
  119. while(blkCnt > 0)
  120. {
  121. p = *pIn++;
  122. accum += p * logf(p);
  123. blkCnt--;
  124. }
  125. return(-accum);
  126. }
  127. #endif
  128. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  129. /**
  130. * @} end of Entropy group
  131. */