arm_max_f32.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_max_f32.c
  4. * Description: Maximum value of a floating-point vector
  5. *
  6. * $Date: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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 "arm_math.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 Max Maximum
  37. Computes the maximum value of an array of data.
  38. The function returns both the maximum 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 Max
  43. @{
  44. */
  45. /**
  46. @brief Maximum value 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 maximum value returned here
  50. @param[out] pIndex index of maximum value returned here
  51. @return none
  52. */
  53. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  54. void arm_max_f32(
  55. const float32_t * pSrc,
  56. uint32_t blockSize,
  57. float32_t * pResult,
  58. uint32_t * pIndex)
  59. {
  60. uint32_t blkCnt;
  61. f32x4_t vecSrc;
  62. f32x4_t curExtremValVec = vdupq_n_f32(F32_MIN);
  63. float32_t maxValue = F32_MIN;
  64. uint32_t idx = blockSize;
  65. uint32x4_t indexVec;
  66. uint32x4_t curExtremIdxVec;
  67. uint32_t curIdx = 0;
  68. mve_pred16_t p0;
  69. float32_t tmp;
  70. indexVec = vidupq_wb_u32(&curIdx, 1);
  71. curExtremIdxVec = vdupq_n_u32(0);
  72. /* Compute 4 outputs at a time */
  73. blkCnt = blockSize >> 2U;
  74. while (blkCnt > 0U)
  75. {
  76. vecSrc = vldrwq_f32(pSrc);
  77. /*
  78. * Get current max per lane and current index per lane
  79. * when a max is selected
  80. */
  81. p0 = vcmpgeq(vecSrc, curExtremValVec);
  82. curExtremValVec = vpselq(vecSrc, curExtremValVec, p0);
  83. curExtremIdxVec = vpselq(indexVec, curExtremIdxVec, p0);
  84. indexVec = vidupq_wb_u32(&curIdx, 1);
  85. pSrc += 4;
  86. /* Decrement the loop counter */
  87. blkCnt--;
  88. }
  89. /*
  90. * Get max value across the vector
  91. */
  92. maxValue = vmaxnmvq(maxValue, curExtremValVec);
  93. /*
  94. * set index for lower values to max possible index
  95. */
  96. p0 = vcmpgeq(curExtremValVec, maxValue);
  97. indexVec = vpselq(curExtremIdxVec, vdupq_n_u32(blockSize), p0);
  98. /*
  99. * Get min index which is thus for a max value
  100. */
  101. idx = vminvq(idx, indexVec);
  102. /* Tail */
  103. blkCnt = blockSize & 0x3;
  104. while (blkCnt > 0U)
  105. {
  106. /* Initialize tmp to the next consecutive values one by one */
  107. tmp = *pSrc++;
  108. /* compare for the maximum value */
  109. if (maxValue < tmp)
  110. {
  111. /* Update the maximum value and it's index */
  112. maxValue = tmp;
  113. idx = blockSize - blkCnt;
  114. }
  115. /* Decrement loop counter */
  116. blkCnt--;
  117. }
  118. /*
  119. * Save result
  120. */
  121. *pIndex = idx;
  122. *pResult = maxValue;
  123. }
  124. #else
  125. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  126. void arm_max_f32(
  127. const float32_t * pSrc,
  128. uint32_t blockSize,
  129. float32_t * pResult,
  130. uint32_t * pIndex)
  131. {
  132. float32_t maxVal1, out; /* Temporary variables to store the output value. */
  133. uint32_t blkCnt, outIndex; /* loop counter */
  134. float32x4_t outV, srcV;
  135. float32x2_t outV2;
  136. uint32x4_t idxV;
  137. uint32x4_t maxIdx;
  138. static const uint32_t indexInit[4]={4,5,6,7};
  139. static const uint32_t countVInit[4]={0,1,2,3};
  140. uint32x4_t index;
  141. uint32x4_t delta;
  142. uint32x4_t countV;
  143. uint32x2_t countV2;
  144. maxIdx = vdupq_n_u32(ULONG_MAX);
  145. delta = vdupq_n_u32(4);
  146. index = vld1q_u32(indexInit);
  147. countV = vld1q_u32(countVInit);
  148. /* Initialise the index value to zero. */
  149. outIndex = 0U;
  150. /* Load first input value that act as reference value for comparison */
  151. if (blockSize <= 3)
  152. {
  153. out = *pSrc++;
  154. blkCnt = blockSize - 1;
  155. while (blkCnt > 0U)
  156. {
  157. /* Initialize maxVal to the next consecutive values one by one */
  158. maxVal1 = *pSrc++;
  159. /* compare for the maximum value */
  160. if (out < maxVal1)
  161. {
  162. /* Update the maximum value and it's index */
  163. out = maxVal1;
  164. outIndex = blockSize - blkCnt;
  165. }
  166. /* Decrement the loop counter */
  167. blkCnt--;
  168. }
  169. }
  170. else
  171. {
  172. outV = vld1q_f32(pSrc);
  173. pSrc += 4;
  174. /* Compute 4 outputs at a time */
  175. blkCnt = (blockSize - 4 ) >> 2U;
  176. while (blkCnt > 0U)
  177. {
  178. srcV = vld1q_f32(pSrc);
  179. pSrc += 4;
  180. idxV = vcgtq_f32(srcV, outV);
  181. outV = vbslq_f32(idxV, srcV, outV );
  182. countV = vbslq_u32(idxV, index,countV );
  183. index = vaddq_u32(index,delta);
  184. /* Decrement the loop counter */
  185. blkCnt--;
  186. }
  187. outV2 = vpmax_f32(vget_low_f32(outV),vget_high_f32(outV));
  188. outV2 = vpmax_f32(outV2,outV2);
  189. out = vget_lane_f32(outV2, 0);
  190. idxV = vceqq_f32(outV, vdupq_n_f32(out));
  191. countV = vbslq_u32(idxV, countV,maxIdx);
  192. countV2 = vpmin_u32(vget_low_u32(countV),vget_high_u32(countV));
  193. countV2 = vpmin_u32(countV2,countV2);
  194. outIndex = vget_lane_u32(countV2,0);
  195. /* if (blockSize - 1U) is not multiple of 4 */
  196. blkCnt = (blockSize - 4 ) % 4U;
  197. while (blkCnt > 0U)
  198. {
  199. /* Initialize maxVal to the next consecutive values one by one */
  200. maxVal1 = *pSrc++;
  201. /* compare for the maximum value */
  202. if (out < maxVal1)
  203. {
  204. /* Update the maximum value and it's index */
  205. out = maxVal1;
  206. outIndex = blockSize - blkCnt ;
  207. }
  208. /* Decrement the loop counter */
  209. blkCnt--;
  210. }
  211. }
  212. /* Store the maximum value and it's index into destination pointers */
  213. *pResult = out;
  214. *pIndex = outIndex;
  215. }
  216. #else
  217. void arm_max_f32(
  218. const float32_t * pSrc,
  219. uint32_t blockSize,
  220. float32_t * pResult,
  221. uint32_t * pIndex)
  222. {
  223. float32_t maxVal, out; /* Temporary variables to store the output value. */
  224. uint32_t blkCnt, outIndex; /* Loop counter */
  225. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  226. uint32_t index; /* index of maximum value */
  227. #endif
  228. /* Initialise index value to zero. */
  229. outIndex = 0U;
  230. /* Load first input value that act as reference value for comparision */
  231. out = *pSrc++;
  232. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  233. /* Initialise index of maximum value. */
  234. index = 0U;
  235. /* Loop unrolling: Compute 4 outputs at a time */
  236. blkCnt = (blockSize - 1U) >> 2U;
  237. while (blkCnt > 0U)
  238. {
  239. /* Initialize maxVal to next consecutive values one by one */
  240. maxVal = *pSrc++;
  241. /* compare for the maximum value */
  242. if (out < maxVal)
  243. {
  244. /* Update the maximum value and it's index */
  245. out = maxVal;
  246. outIndex = index + 1U;
  247. }
  248. maxVal = *pSrc++;
  249. if (out < maxVal)
  250. {
  251. out = maxVal;
  252. outIndex = index + 2U;
  253. }
  254. maxVal = *pSrc++;
  255. if (out < maxVal)
  256. {
  257. out = maxVal;
  258. outIndex = index + 3U;
  259. }
  260. maxVal = *pSrc++;
  261. if (out < maxVal)
  262. {
  263. out = maxVal;
  264. outIndex = index + 4U;
  265. }
  266. index += 4U;
  267. /* Decrement loop counter */
  268. blkCnt--;
  269. }
  270. /* Loop unrolling: Compute remaining outputs */
  271. blkCnt = (blockSize - 1U) % 4U;
  272. #else
  273. /* Initialize blkCnt with number of samples */
  274. blkCnt = (blockSize - 1U);
  275. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  276. while (blkCnt > 0U)
  277. {
  278. /* Initialize maxVal to the next consecutive values one by one */
  279. maxVal = *pSrc++;
  280. /* compare for the maximum value */
  281. if (out < maxVal)
  282. {
  283. /* Update the maximum value and it's index */
  284. out = maxVal;
  285. outIndex = blockSize - blkCnt;
  286. }
  287. /* Decrement loop counter */
  288. blkCnt--;
  289. }
  290. /* Store the maximum value and it's index into destination pointers */
  291. *pResult = out;
  292. *pIndex = outIndex;
  293. }
  294. #endif /* #if defined(ARM_MATH_NEON) */
  295. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  296. /**
  297. @} end of Max group
  298. */