arm_logsumexp_f16.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * @addtogroup LogSumExp
  34. * @{
  35. */
  36. /**
  37. * @brief Computation of the LogSumExp
  38. *
  39. * In probabilistic computations, the dynamic of the probability values can be very
  40. * wide because they come from gaussian functions.
  41. * To avoid underflow and overflow issues, the values are represented by their log.
  42. * In this representation, multiplying the original exp values is easy : their logs are added.
  43. * But adding the original exp values is requiring some special handling and it is the
  44. * goal of the LogSumExp function.
  45. *
  46. * If the values are x1...xn, the function is computing:
  47. *
  48. * ln(exp(x1) + ... + exp(xn)) and the computation is done in such a way that
  49. * rounding issues are minimised.
  50. *
  51. * The max xm of the values is extracted and the function is computing:
  52. * xm + ln(exp(x1 - xm) + ... + exp(xn - xm))
  53. *
  54. * @param[in] *in Pointer to an array of input values.
  55. * @param[in] blockSize Number of samples in the input array.
  56. * @return LogSumExp
  57. *
  58. */
  59. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  60. #include "arm_helium_utils.h"
  61. #include "arm_vec_math_f16.h"
  62. float16_t arm_logsumexp_f16(const float16_t *in, uint32_t blockSize)
  63. {
  64. float16_t maxVal;
  65. const float16_t *pIn;
  66. int32_t blkCnt;
  67. _Float16 accum=0.0f16;
  68. _Float16 tmp;
  69. arm_max_no_idx_f16((float16_t *) in, blockSize, &maxVal);
  70. blkCnt = blockSize;
  71. pIn = in;
  72. f16x8_t vSum = vdupq_n_f16(0.0f16);
  73. blkCnt = blockSize >> 3;
  74. while(blkCnt > 0)
  75. {
  76. f16x8_t vecIn = vld1q(pIn);
  77. f16x8_t vecExp;
  78. vecExp = vexpq_f16(vsubq_n_f16(vecIn, maxVal));
  79. vSum = vaddq_f16(vSum, vecExp);
  80. /*
  81. * Decrement the blockSize loop counter
  82. * Advance vector source and destination pointers
  83. */
  84. pIn += 8;
  85. blkCnt --;
  86. }
  87. /* sum + log */
  88. accum = vecAddAcrossF16Mve(vSum);
  89. blkCnt = blockSize & 0x7;
  90. while(blkCnt > 0)
  91. {
  92. tmp = *pIn++;
  93. accum += (_Float16)expf((float32_t)((_Float16)tmp - (_Float16)maxVal));
  94. blkCnt--;
  95. }
  96. accum = (_Float16)maxVal + (_Float16)logf((float32_t)accum);
  97. return (accum);
  98. }
  99. #else
  100. float16_t arm_logsumexp_f16(const float16_t *in, uint32_t blockSize)
  101. {
  102. _Float16 maxVal;
  103. _Float16 tmp;
  104. const float16_t *pIn;
  105. uint32_t blkCnt;
  106. _Float16 accum;
  107. pIn = in;
  108. blkCnt = blockSize;
  109. maxVal = *pIn++;
  110. blkCnt--;
  111. while(blkCnt > 0)
  112. {
  113. tmp = *pIn++;
  114. if (tmp > maxVal)
  115. {
  116. maxVal = tmp;
  117. }
  118. blkCnt--;
  119. }
  120. blkCnt = blockSize;
  121. pIn = in;
  122. accum = 0;
  123. while(blkCnt > 0)
  124. {
  125. tmp = *pIn++;
  126. accum += (_Float16)expf((float32_t)((_Float16)tmp - (_Float16)maxVal));
  127. blkCnt--;
  128. }
  129. accum = (_Float16)maxVal + (_Float16)logf((float32_t)accum);
  130. return(accum);
  131. }
  132. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  133. /**
  134. * @} end of LogSumExp group
  135. */
  136. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */