arm_kullback_leibler_f32.c 4.3 KB

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