arm_absmin_f16.c 14 KB

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