arm_logsumexp_f32.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 LogSumExp
  33. * @{
  34. */
  35. /**
  36. * @brief Computation of the LogSumExp
  37. *
  38. * In probabilistic computations, the dynamic of the probability values can be very
  39. * wide because they come from gaussian functions.
  40. * To avoid underflow and overflow issues, the values are represented by their log.
  41. * In this representation, multiplying the original exp values is easy : their logs are added.
  42. * But adding the original exp values is requiring some special handling and it is the
  43. * goal of the LogSumExp function.
  44. *
  45. * If the values are x1...xn, the function is computing:
  46. *
  47. * ln(exp(x1) + ... + exp(xn)) and the computation is done in such a way that
  48. * rounding issues are minimised.
  49. *
  50. * The max xm of the values is extracted and the function is computing:
  51. * xm + ln(exp(x1 - xm) + ... + exp(xn - xm))
  52. *
  53. * @param[in] *in Pointer to an array of input values.
  54. * @param[in] blockSize Number of samples in the input array.
  55. * @return LogSumExp
  56. *
  57. */
  58. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  59. #include "arm_helium_utils.h"
  60. #include "arm_vec_math.h"
  61. float32_t arm_logsumexp_f32(const float32_t *in, uint32_t blockSize)
  62. {
  63. float32_t maxVal;
  64. const float32_t *pIn;
  65. int32_t blkCnt;
  66. float32_t accum=0.0f;
  67. float32_t tmp;
  68. arm_max_no_idx_f32((float32_t *) in, blockSize, &maxVal);
  69. blkCnt = blockSize;
  70. pIn = in;
  71. f32x4_t vSum = vdupq_n_f32(0.0f);
  72. blkCnt = blockSize >> 2;
  73. while(blkCnt > 0)
  74. {
  75. f32x4_t vecIn = vld1q(pIn);
  76. f32x4_t vecExp;
  77. vecExp = vexpq_f32(vsubq_n_f32(vecIn, maxVal));
  78. vSum = vaddq_f32(vSum, vecExp);
  79. /*
  80. * Decrement the blockSize loop counter
  81. * Advance vector source and destination pointers
  82. */
  83. pIn += 4;
  84. blkCnt --;
  85. }
  86. /* sum + log */
  87. accum = vecAddAcrossF32Mve(vSum);
  88. blkCnt = blockSize & 0x3;
  89. while(blkCnt > 0)
  90. {
  91. tmp = *pIn++;
  92. accum += expf(tmp - maxVal);
  93. blkCnt--;
  94. }
  95. accum = maxVal + logf(accum);
  96. return (accum);
  97. }
  98. #else
  99. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  100. #include "NEMath.h"
  101. float32_t arm_logsumexp_f32(const float32_t *in, uint32_t blockSize)
  102. {
  103. float32_t maxVal;
  104. float32_t tmp;
  105. float32x4_t tmpV, tmpVb;
  106. float32x4_t maxValV;
  107. uint32x4_t idxV;
  108. float32x4_t accumV;
  109. float32x2_t accumV2;
  110. const float32_t *pIn;
  111. uint32_t blkCnt;
  112. float32_t accum;
  113. pIn = in;
  114. blkCnt = blockSize;
  115. if (blockSize <= 3)
  116. {
  117. maxVal = *pIn++;
  118. blkCnt--;
  119. while(blkCnt > 0)
  120. {
  121. tmp = *pIn++;
  122. if (tmp > maxVal)
  123. {
  124. maxVal = tmp;
  125. }
  126. blkCnt--;
  127. }
  128. }
  129. else
  130. {
  131. maxValV = vld1q_f32(pIn);
  132. pIn += 4;
  133. blkCnt = (blockSize - 4) >> 2;
  134. while(blkCnt > 0)
  135. {
  136. tmpVb = vld1q_f32(pIn);
  137. pIn += 4;
  138. idxV = vcgtq_f32(tmpVb, maxValV);
  139. maxValV = vbslq_f32(idxV, tmpVb, maxValV );
  140. blkCnt--;
  141. }
  142. accumV2 = vpmax_f32(vget_low_f32(maxValV),vget_high_f32(maxValV));
  143. accumV2 = vpmax_f32(accumV2,accumV2);
  144. maxVal = vget_lane_f32(accumV2, 0) ;
  145. blkCnt = (blockSize - 4) & 3;
  146. while(blkCnt > 0)
  147. {
  148. tmp = *pIn++;
  149. if (tmp > maxVal)
  150. {
  151. maxVal = tmp;
  152. }
  153. blkCnt--;
  154. }
  155. }
  156. maxValV = vdupq_n_f32(maxVal);
  157. pIn = in;
  158. accum = 0;
  159. accumV = vdupq_n_f32(0.0f);
  160. blkCnt = blockSize >> 2;
  161. while(blkCnt > 0)
  162. {
  163. tmpV = vld1q_f32(pIn);
  164. pIn += 4;
  165. tmpV = vsubq_f32(tmpV, maxValV);
  166. tmpV = vexpq_f32(tmpV);
  167. accumV = vaddq_f32(accumV, tmpV);
  168. blkCnt--;
  169. }
  170. accumV2 = vpadd_f32(vget_low_f32(accumV),vget_high_f32(accumV));
  171. accum = vget_lane_f32(accumV2, 0) + vget_lane_f32(accumV2, 1);
  172. blkCnt = blockSize & 0x3;
  173. while(blkCnt > 0)
  174. {
  175. tmp = *pIn++;
  176. accum += expf(tmp - maxVal);
  177. blkCnt--;
  178. }
  179. accum = maxVal + logf(accum);
  180. return(accum);
  181. }
  182. #else
  183. float32_t arm_logsumexp_f32(const float32_t *in, uint32_t blockSize)
  184. {
  185. float32_t maxVal;
  186. float32_t tmp;
  187. const float32_t *pIn;
  188. uint32_t blkCnt;
  189. float32_t accum;
  190. pIn = in;
  191. blkCnt = blockSize;
  192. maxVal = *pIn++;
  193. blkCnt--;
  194. while(blkCnt > 0)
  195. {
  196. tmp = *pIn++;
  197. if (tmp > maxVal)
  198. {
  199. maxVal = tmp;
  200. }
  201. blkCnt--;
  202. }
  203. blkCnt = blockSize;
  204. pIn = in;
  205. accum = 0;
  206. while(blkCnt > 0)
  207. {
  208. tmp = *pIn++;
  209. accum += expf(tmp - maxVal);
  210. blkCnt--;
  211. }
  212. accum = maxVal + logf(accum);
  213. return(accum);
  214. }
  215. #endif
  216. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  217. /**
  218. * @} end of LogSumExp group
  219. */