arm_kullback_leibler_f32.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 Kullback-Leibler
  33. * @{
  34. */
  35. /**
  36. * @brief Kullback-Leibler
  37. *
  38. * Distribution A may contain 0 with Neon version.
  39. * Result will be right but some exception flags will be set.
  40. *
  41. * Distribution B must not contain 0 probability.
  42. *
  43. * @param[in] *pSrcA points to an array of input values for probaility distribution A.
  44. * @param[in] *pSrcB points to an array of input values for probaility distribution B.
  45. * @param[in] blockSize number of samples in the input array.
  46. * @return Kullback-Leibler divergence D(A || B)
  47. *
  48. */
  49. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  50. #include "arm_helium_utils.h"
  51. #include "arm_vec_math.h"
  52. float32_t arm_kullback_leibler_f32(const float32_t * pSrcA,const float32_t * pSrcB,uint32_t blockSize)
  53. {
  54. uint32_t blkCnt;
  55. float32_t accum, pA,pB;
  56. blkCnt = blockSize;
  57. accum = 0.0f;
  58. f32x4_t vSum = vdupq_n_f32(0.0f);
  59. blkCnt = blockSize >> 2;
  60. while(blkCnt > 0)
  61. {
  62. f32x4_t vecA = vld1q(pSrcA);
  63. f32x4_t vecB = vld1q(pSrcB);
  64. f32x4_t vRatio;
  65. vRatio = vdiv_f32(vecB, vecA);
  66. vSum = vaddq_f32(vSum, vmulq(vecA, vlogq_f32(vRatio)));
  67. /*
  68. * Decrement the blockSize loop counter
  69. * Advance vector source and destination pointers
  70. */
  71. pSrcA += 4;
  72. pSrcB += 4;
  73. blkCnt --;
  74. }
  75. accum = vecAddAcrossF32Mve(vSum);
  76. blkCnt = blockSize & 3;
  77. while(blkCnt > 0)
  78. {
  79. pA = *pSrcA++;
  80. pB = *pSrcB++;
  81. accum += pA * logf(pB / pA);
  82. blkCnt--;
  83. }
  84. return(-accum);
  85. }
  86. #else
  87. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  88. #include "NEMath.h"
  89. float32_t arm_kullback_leibler_f32(const float32_t * pSrcA,const float32_t * pSrcB,uint32_t blockSize)
  90. {
  91. const float32_t *pInA, *pInB;
  92. uint32_t blkCnt;
  93. float32_t accum, pA,pB;
  94. float32x4_t accumV;
  95. float32x2_t accumV2;
  96. float32x4_t tmpVA, tmpVB,tmpV;
  97. pInA = pSrcA;
  98. pInB = pSrcB;
  99. accum = 0.0f;
  100. accumV = vdupq_n_f32(0.0f);
  101. blkCnt = blockSize >> 2;
  102. while(blkCnt > 0)
  103. {
  104. tmpVA = vld1q_f32(pInA);
  105. pInA += 4;
  106. tmpVB = vld1q_f32(pInB);
  107. pInB += 4;
  108. tmpV = vinvq_f32(tmpVA);
  109. tmpVB = vmulq_f32(tmpVB, tmpV);
  110. tmpVB = vlogq_f32(tmpVB);
  111. accumV = vmlaq_f32(accumV, tmpVA, tmpVB);
  112. blkCnt--;
  113. }
  114. accumV2 = vpadd_f32(vget_low_f32(accumV),vget_high_f32(accumV));
  115. accum = vget_lane_f32(accumV2, 0) + vget_lane_f32(accumV2, 1);
  116. blkCnt = blockSize & 3;
  117. while(blkCnt > 0)
  118. {
  119. pA = *pInA++;
  120. pB = *pInB++;
  121. accum += pA * logf(pB/pA);
  122. blkCnt--;
  123. }
  124. return(-accum);
  125. }
  126. #else
  127. float32_t arm_kullback_leibler_f32(const float32_t * pSrcA,const float32_t * pSrcB,uint32_t blockSize)
  128. {
  129. const float32_t *pInA, *pInB;
  130. uint32_t blkCnt;
  131. float32_t accum, pA,pB;
  132. pInA = pSrcA;
  133. pInB = pSrcB;
  134. blkCnt = blockSize;
  135. accum = 0.0f;
  136. while(blkCnt > 0)
  137. {
  138. pA = *pInA++;
  139. pB = *pInB++;
  140. accum += pA * logf(pB / pA);
  141. blkCnt--;
  142. }
  143. return(-accum);
  144. }
  145. #endif
  146. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  147. /**
  148. * @} end of Kullback-Leibler group
  149. */