arm_max_f32.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 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)
  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_NEON)
  54. void arm_max_f32(
  55. const float32_t * pSrc,
  56. uint32_t blockSize,
  57. float32_t * pResult,
  58. uint32_t * pIndex)
  59. {
  60. float32_t maxVal1, maxVal2, out; /* Temporary variables to store the output value. */
  61. uint32_t blkCnt, outIndex, count; /* loop counter */
  62. float32x4_t outV, srcV;
  63. float32x2_t outV2;
  64. uint32x4_t idxV;
  65. uint32x4_t maxIdx={ULONG_MAX,ULONG_MAX,ULONG_MAX,ULONG_MAX};
  66. uint32x4_t index={4,5,6,7};
  67. uint32x4_t delta={4,4,4,4};
  68. uint32x4_t countV={0,1,2,3};
  69. uint32x2_t countV2;
  70. /* Initialise the count value. */
  71. count = 0U;
  72. /* Initialise the index value to zero. */
  73. outIndex = 0U;
  74. /* Load first input value that act as reference value for comparison */
  75. if (blockSize <= 3)
  76. {
  77. out = *pSrc++;
  78. blkCnt = blockSize - 1;
  79. while (blkCnt > 0U)
  80. {
  81. /* Initialize maxVal to the next consecutive values one by one */
  82. maxVal1 = *pSrc++;
  83. /* compare for the maximum value */
  84. if (out < maxVal1)
  85. {
  86. /* Update the maximum value and it's index */
  87. out = maxVal1;
  88. outIndex = blockSize - blkCnt;
  89. }
  90. /* Decrement the loop counter */
  91. blkCnt--;
  92. }
  93. }
  94. else
  95. {
  96. outV = vld1q_f32(pSrc);
  97. pSrc += 4;
  98. /* Compute 4 outputs at a time */
  99. blkCnt = (blockSize - 4 ) >> 2U;
  100. while (blkCnt > 0U)
  101. {
  102. srcV = vld1q_f32(pSrc);
  103. pSrc += 4;
  104. idxV = vcgtq_f32(srcV, outV);
  105. outV = vbslq_f32(idxV, srcV, outV );
  106. countV = vbslq_u32(idxV, index,countV );
  107. index = vaddq_u32(index,delta);
  108. /* Decrement the loop counter */
  109. blkCnt--;
  110. }
  111. outV2 = vpmax_f32(vget_low_f32(outV),vget_high_f32(outV));
  112. outV2 = vpmax_f32(outV2,outV2);
  113. out = outV2[0];
  114. idxV = vceqq_f32(outV, vdupq_n_f32(out));
  115. countV = vbslq_u32(idxV, countV,maxIdx);
  116. countV2 = vpmin_u32(vget_low_u32(countV),vget_high_u32(countV));
  117. countV2 = vpmin_u32(countV2,countV2);
  118. outIndex = countV2[0];
  119. /* if (blockSize - 1U) is not multiple of 4 */
  120. blkCnt = (blockSize - 4 ) % 4U;
  121. while (blkCnt > 0U)
  122. {
  123. /* Initialize maxVal to the next consecutive values one by one */
  124. maxVal1 = *pSrc++;
  125. /* compare for the maximum value */
  126. if (out < maxVal1)
  127. {
  128. /* Update the maximum value and it's index */
  129. out = maxVal1;
  130. outIndex = blockSize - blkCnt ;
  131. }
  132. /* Decrement the loop counter */
  133. blkCnt--;
  134. }
  135. }
  136. /* Store the maximum value and it's index into destination pointers */
  137. *pResult = out;
  138. *pIndex = outIndex;
  139. }
  140. #else
  141. void arm_max_f32(
  142. const float32_t * pSrc,
  143. uint32_t blockSize,
  144. float32_t * pResult,
  145. uint32_t * pIndex)
  146. {
  147. float32_t maxVal, out; /* Temporary variables to store the output value. */
  148. uint32_t blkCnt, outIndex; /* Loop counter */
  149. #if defined (ARM_MATH_LOOPUNROLL)
  150. uint32_t index; /* index of maximum value */
  151. #endif
  152. /* Initialise index value to zero. */
  153. outIndex = 0U;
  154. /* Load first input value that act as reference value for comparision */
  155. out = *pSrc++;
  156. #if defined (ARM_MATH_LOOPUNROLL)
  157. /* Initialise index of maximum value. */
  158. index = 0U;
  159. /* Loop unrolling: Compute 4 outputs at a time */
  160. blkCnt = (blockSize - 1U) >> 2U;
  161. while (blkCnt > 0U)
  162. {
  163. /* Initialize maxVal to next consecutive values one by one */
  164. maxVal = *pSrc++;
  165. /* compare for the maximum value */
  166. if (out < maxVal)
  167. {
  168. /* Update the maximum value and it's index */
  169. out = maxVal;
  170. outIndex = index + 1U;
  171. }
  172. maxVal = *pSrc++;
  173. if (out < maxVal)
  174. {
  175. out = maxVal;
  176. outIndex = index + 2U;
  177. }
  178. maxVal = *pSrc++;
  179. if (out < maxVal)
  180. {
  181. out = maxVal;
  182. outIndex = index + 3U;
  183. }
  184. maxVal = *pSrc++;
  185. if (out < maxVal)
  186. {
  187. out = maxVal;
  188. outIndex = index + 4U;
  189. }
  190. index += 4U;
  191. /* Decrement loop counter */
  192. blkCnt--;
  193. }
  194. /* Loop unrolling: Compute remaining outputs */
  195. blkCnt = (blockSize - 1U) % 4U;
  196. #else
  197. /* Initialize blkCnt with number of samples */
  198. blkCnt = (blockSize - 1U);
  199. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  200. while (blkCnt > 0U)
  201. {
  202. /* Initialize maxVal to the next consecutive values one by one */
  203. maxVal = *pSrc++;
  204. /* compare for the maximum value */
  205. if (out < maxVal)
  206. {
  207. /* Update the maximum value and it's index */
  208. out = maxVal;
  209. outIndex = blockSize - blkCnt;
  210. }
  211. /* Decrement loop counter */
  212. blkCnt--;
  213. }
  214. /* Store the maximum value and it's index into destination pointers */
  215. *pResult = out;
  216. *pIndex = outIndex;
  217. }
  218. #endif /* #if defined(ARM_MATH_NEON) */
  219. /**
  220. @} end of Max group
  221. */