arm_kullback_leibler_f16.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_logsumexp_f16.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_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. #include <limits.h>
  31. #include <math.h>
  32. /**
  33. @ingroup groupStats
  34. */
  35. /**
  36. @defgroup Kullback-Leibler Kullback-Leibler divergence
  37. Computes the Kullback-Leibler divergence between two distributions
  38. */
  39. /**
  40. * @addtogroup Kullback-Leibler
  41. * @{
  42. */
  43. /**
  44. * @brief Kullback-Leibler
  45. *
  46. * Distribution A may contain 0 with Neon version.
  47. * Result will be right but some exception flags will be set.
  48. *
  49. * Distribution B must not contain 0 probability.
  50. *
  51. * @param[in] *pSrcA points to an array of input values for probaility distribution A.
  52. * @param[in] *pSrcB points to an array of input values for probaility distribution B.
  53. * @param[in] blockSize number of samples in the input array.
  54. * @return Kullback-Leibler divergence D(A || B)
  55. *
  56. */
  57. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  58. #include "arm_helium_utils.h"
  59. #include "arm_vec_math_f16.h"
  60. float16_t arm_kullback_leibler_f16(const float16_t * pSrcA,const float16_t * pSrcB,uint32_t blockSize)
  61. {
  62. uint32_t blkCnt;
  63. _Float16 accum, pA,pB;
  64. blkCnt = blockSize;
  65. accum = 0.0f16;
  66. f16x8_t vSum = vdupq_n_f16(0.0f16);
  67. blkCnt = blockSize >> 3;
  68. while(blkCnt > 0)
  69. {
  70. f16x8_t vecA = vld1q(pSrcA);
  71. f16x8_t vecB = vld1q(pSrcB);
  72. f16x8_t vRatio;
  73. vRatio = vdiv_f16(vecB, vecA);
  74. vSum = vaddq_f16(vSum, vmulq(vecA, vlogq_f16(vRatio)));
  75. /*
  76. * Decrement the blockSize loop counter
  77. * Advance vector source and destination pointers
  78. */
  79. pSrcA += 8;
  80. pSrcB += 8;
  81. blkCnt --;
  82. }
  83. accum = vecAddAcrossF16Mve(vSum);
  84. blkCnt = blockSize & 7;
  85. while(blkCnt > 0)
  86. {
  87. pA = *pSrcA++;
  88. pB = *pSrcB++;
  89. accum += pA * (_Float16)logf((float32_t)pB / (float32_t)pA);
  90. blkCnt--;
  91. }
  92. return(-accum);
  93. }
  94. #else
  95. float16_t arm_kullback_leibler_f16(const float16_t * pSrcA,const float16_t * pSrcB,uint32_t blockSize)
  96. {
  97. const float16_t *pInA, *pInB;
  98. uint32_t blkCnt;
  99. _Float16 accum, pA,pB;
  100. pInA = pSrcA;
  101. pInB = pSrcB;
  102. blkCnt = blockSize;
  103. accum = 0.0f;
  104. while(blkCnt > 0)
  105. {
  106. pA = *pInA++;
  107. pB = *pInB++;
  108. accum += pA * (_Float16)logf((float32_t)pB / (float32_t)pA);
  109. blkCnt--;
  110. }
  111. return(-accum);
  112. }
  113. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  114. /**
  115. * @} end of Kullback-Leibler group
  116. */
  117. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */