arm_entropy_f32.c 3.6 KB

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