arm_absmax_f16.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_absmax_f16.c
  4. * Description: Maximum value of a absolute values of a floating-point vector
  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. #if (defined(ARM_MATH_NEON) || defined(ARM_MATH_MVEF)) && !defined(ARM_MATH_AUTOVECTORIZE)
  31. #include <limits.h>
  32. #endif
  33. /**
  34. @ingroup groupStats
  35. */
  36. /**
  37. @addtogroup AbsMax
  38. @{
  39. */
  40. /**
  41. @brief Maximum value of absolute values of a floating-point vector.
  42. @param[in] pSrc points to the input vector
  43. @param[in] blockSize number of samples in input vector
  44. @param[out] pResult maximum value returned here
  45. @param[out] pIndex index of maximum value returned here
  46. @return none
  47. */
  48. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  49. #include "arm_helium_utils.h"
  50. void arm_absmax_f16(
  51. const float16_t * pSrc,
  52. uint32_t blockSize,
  53. float16_t * pResult,
  54. uint32_t * pIndex)
  55. {
  56. uint16_t blkCnt; /* loop counters */
  57. f16x8_t vecSrc;
  58. float16_t const *pSrcVec;
  59. f16x8_t curExtremValVec = vdupq_n_f16(F16_ABSMIN);
  60. float16_t maxValue = F16_ABSMIN;
  61. uint16_t idx = blockSize;
  62. uint16x8_t indexVec;
  63. uint16x8_t curExtremIdxVec;
  64. mve_pred16_t p0;
  65. indexVec = vidupq_u16((uint32_t)0, 1);
  66. curExtremIdxVec = vdupq_n_u16(0);
  67. pSrcVec = (float16_t const *) pSrc;
  68. blkCnt = blockSize >> 3;
  69. while (blkCnt > 0U)
  70. {
  71. vecSrc = vldrhq_f16(pSrcVec);
  72. pSrcVec += 8;
  73. vecSrc = vabsq(vecSrc);
  74. /*
  75. * Get current max per lane and current index per lane
  76. * when a max is selected
  77. */
  78. p0 = vcmpgeq(vecSrc, curExtremValVec);
  79. curExtremValVec = vpselq(vecSrc, curExtremValVec, p0);
  80. curExtremIdxVec = vpselq(indexVec, curExtremIdxVec, p0);
  81. indexVec = indexVec + 8;
  82. /*
  83. * Decrement the blockSize loop counter
  84. */
  85. blkCnt--;
  86. }
  87. /*
  88. * tail
  89. * (will be merged thru tail predication)
  90. */
  91. blkCnt = blockSize & 7;
  92. if (blkCnt > 0U)
  93. {
  94. vecSrc = vldrhq_f16(pSrcVec);
  95. pSrcVec += 8;
  96. vecSrc = vabsq(vecSrc);
  97. p0 = vctp16q(blkCnt);
  98. /*
  99. * Get current max per lane and current index per lane
  100. * when a max is selected
  101. */
  102. p0 = vcmpgeq_m(vecSrc, curExtremValVec, p0);
  103. curExtremValVec = vpselq(vecSrc, curExtremValVec, p0);
  104. curExtremIdxVec = vpselq(indexVec, curExtremIdxVec, p0);
  105. }
  106. /*
  107. * Get max value across the vector
  108. */
  109. maxValue = vmaxnmvq(maxValue, curExtremValVec);
  110. /*
  111. * set index for lower values to max possible index
  112. */
  113. p0 = vcmpgeq(curExtremValVec, maxValue);
  114. indexVec = vpselq(curExtremIdxVec, vdupq_n_u16(blockSize), p0);
  115. /*
  116. * Get min index which is thus for a max value
  117. */
  118. idx = vminvq(idx, indexVec);
  119. /*
  120. * Save result
  121. */
  122. *pIndex = idx;
  123. *pResult = maxValue;
  124. }
  125. #else
  126. #if defined(ARM_MATH_LOOPUNROLL)
  127. void arm_absmax_f16(
  128. const float16_t * pSrc,
  129. uint32_t blockSize,
  130. float16_t * pResult,
  131. uint32_t * pIndex)
  132. {
  133. float16_t cur_absmax, out; /* Temporary variables to store the output value. */\
  134. uint32_t blkCnt, outIndex; /* Loop counter */ \
  135. uint32_t index; /* index of maximum value */ \
  136. \
  137. /* Initialize index value to zero. */ \
  138. outIndex = 0U; \
  139. /* Load first input value that act as reference value for comparision */ \
  140. out = *pSrc++; \
  141. out = ((_Float16)out > 0.0f16) ? out : -(_Float16)out; \
  142. /* Initialize index of extrema value. */ \
  143. index = 0U; \
  144. \
  145. /* Loop unrolling: Compute 4 outputs at a time */ \
  146. blkCnt = (blockSize - 1U) >> 2U; \
  147. \
  148. while (blkCnt > 0U) \
  149. { \
  150. /* Initialize cur_absmax to next consecutive values one by one */ \
  151. cur_absmax = *pSrc++; \
  152. cur_absmax = ((_Float16)cur_absmax > 0.0f16) ? cur_absmax : -(_Float16)cur_absmax; \
  153. /* compare for the extrema value */ \
  154. if ((_Float16)cur_absmax > (_Float16)out) \
  155. { \
  156. /* Update the extrema value and it's index */ \
  157. out = cur_absmax; \
  158. outIndex = index + 1U; \
  159. } \
  160. \
  161. cur_absmax = *pSrc++; \
  162. cur_absmax = ((_Float16)cur_absmax > 0.0f16) ? cur_absmax : -(_Float16)cur_absmax; \
  163. if ((_Float16)cur_absmax > (_Float16)out) \
  164. { \
  165. out = cur_absmax; \
  166. outIndex = index + 2U; \
  167. } \
  168. \
  169. cur_absmax = *pSrc++; \
  170. cur_absmax = ((_Float16)cur_absmax > 0.0f16) ? cur_absmax : -(_Float16)cur_absmax; \
  171. if ((_Float16)cur_absmax > (_Float16)out) \
  172. { \
  173. out = cur_absmax; \
  174. outIndex = index + 3U; \
  175. } \
  176. \
  177. cur_absmax = *pSrc++; \
  178. cur_absmax = ((_Float16)cur_absmax > 0.0f16) ? cur_absmax : -(_Float16)cur_absmax; \
  179. if ((_Float16)cur_absmax > (_Float16)out) \
  180. { \
  181. out = cur_absmax; \
  182. outIndex = index + 4U; \
  183. } \
  184. \
  185. index += 4U; \
  186. \
  187. /* Decrement loop counter */ \
  188. blkCnt--; \
  189. } \
  190. \
  191. /* Loop unrolling: Compute remaining outputs */ \
  192. blkCnt = (blockSize - 1U) % 4U; \
  193. \
  194. \
  195. while (blkCnt > 0U) \
  196. { \
  197. cur_absmax = *pSrc++; \
  198. cur_absmax = ((_Float16)cur_absmax > 0.0f16) ? cur_absmax : -(_Float16)cur_absmax; \
  199. if ((_Float16)cur_absmax > (_Float16)out) \
  200. { \
  201. out = cur_absmax; \
  202. outIndex = blockSize - blkCnt; \
  203. } \
  204. \
  205. /* Decrement loop counter */ \
  206. blkCnt--; \
  207. } \
  208. \
  209. /* Store the extrema value and it's index into destination pointers */ \
  210. *pResult = out; \
  211. *pIndex = outIndex;
  212. }
  213. #else
  214. void arm_absmax_f16(
  215. const float16_t * pSrc,
  216. uint32_t blockSize,
  217. float16_t * pResult,
  218. uint32_t * pIndex)
  219. {
  220. float16_t maxVal, out; /* Temporary variables to store the output value. */
  221. uint32_t blkCnt, outIndex; /* Loop counter */
  222. /* Initialise index value to zero. */
  223. outIndex = 0U;
  224. /* Load first input value that act as reference value for comparision */
  225. out = (_Float16)fabsf((float32_t)*pSrc++);
  226. /* Initialize blkCnt with number of samples */
  227. blkCnt = (blockSize - 1U);
  228. while (blkCnt > 0U)
  229. {
  230. /* Initialize maxVal to the next consecutive values one by one */
  231. maxVal = (_Float16)fabsf((float32_t)*pSrc++);
  232. /* compare for the maximum value */
  233. if ((_Float16)out < (_Float16)maxVal)
  234. {
  235. /* Update the maximum value and it's index */
  236. out = maxVal;
  237. outIndex = blockSize - blkCnt;
  238. }
  239. /* Decrement loop counter */
  240. blkCnt--;
  241. }
  242. /* Store the maximum value and it's index into destination pointers */
  243. *pResult = out;
  244. *pIndex = outIndex;
  245. }
  246. #endif /* defined(ARM_MATH_LOOPUNROLL) */
  247. #endif /* defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE) */
  248. /**
  249. @} end of AbsMax group
  250. */
  251. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */