arm_absmin_f32.c 14 KB

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