arm_logsumexp_f32.c 6.1 KB

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