arm_min_f32.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_min_f32.c
  4. * Description: Minimum 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. #include <limits.h>
  30. /**
  31. @ingroup groupStats
  32. */
  33. /**
  34. @defgroup Min Minimum
  35. Computes the minimum value of an array of data.
  36. The function returns both the minimum value and its position within the array.
  37. There are separate functions for floating-point, Q31, Q15, and Q7 data types.
  38. */
  39. /**
  40. @addtogroup Min
  41. @{
  42. */
  43. /**
  44. @brief Minimum value of a floating-point vector.
  45. @param[in] pSrc points to the input vector
  46. @param[in] blockSize number of samples in input vector
  47. @param[out] pResult minimum value returned here
  48. @param[out] pIndex index of minimum value returned here
  49. @return none
  50. */
  51. #if defined(ARM_MATH_NEON)
  52. void arm_min_f32(
  53. const float32_t * pSrc,
  54. uint32_t blockSize,
  55. float32_t * pResult,
  56. uint32_t * pIndex)
  57. {
  58. float32_t maxVal1, maxVal2, out; /* Temporary variables to store the output value. */
  59. uint32_t blkCnt, outIndex, count; /* loop counter */
  60. float32x4_t outV, srcV;
  61. float32x2_t outV2;
  62. uint32x4_t idxV;
  63. uint32x4_t maxIdx={ULONG_MAX,ULONG_MAX,ULONG_MAX,ULONG_MAX};
  64. uint32x4_t index={4,5,6,7};
  65. uint32x4_t delta={4,4,4,4};
  66. uint32x4_t countV={0,1,2,3};
  67. uint32x2_t countV2;
  68. /* Initialise the count value. */
  69. count = 0U;
  70. /* Initialise the index value to zero. */
  71. outIndex = 0U;
  72. /* Load first input value that act as reference value for comparison */
  73. if (blockSize <= 3)
  74. {
  75. out = *pSrc++;
  76. blkCnt = blockSize - 1;
  77. while (blkCnt > 0U)
  78. {
  79. /* Initialize maxVal to the next consecutive values one by one */
  80. maxVal1 = *pSrc++;
  81. /* compare for the maximum value */
  82. if (out > maxVal1)
  83. {
  84. /* Update the maximum value and it's index */
  85. out = maxVal1;
  86. outIndex = blockSize - blkCnt;
  87. }
  88. /* Decrement the loop counter */
  89. blkCnt--;
  90. }
  91. }
  92. else
  93. {
  94. outV = vld1q_f32(pSrc);
  95. pSrc += 4;
  96. /* Compute 4 outputs at a time */
  97. blkCnt = (blockSize - 4 ) >> 2U;
  98. while (blkCnt > 0U)
  99. {
  100. srcV = vld1q_f32(pSrc);
  101. pSrc += 4;
  102. idxV = vcltq_f32(srcV, outV);
  103. outV = vbslq_f32(idxV, srcV, outV );
  104. countV = vbslq_u32(idxV, index,countV );
  105. index = vaddq_u32(index,delta);
  106. /* Decrement the loop counter */
  107. blkCnt--;
  108. }
  109. outV2 = vpmin_f32(vget_low_f32(outV),vget_high_f32(outV));
  110. outV2 = vpmin_f32(outV2,outV2);
  111. out = outV2[0];
  112. idxV = vceqq_f32(outV, vdupq_n_f32(out));
  113. countV = vbslq_u32(idxV, countV,maxIdx);
  114. countV2 = vpmin_u32(vget_low_u32(countV),vget_high_u32(countV));
  115. countV2 = vpmin_u32(countV2,countV2);
  116. outIndex = countV2[0];
  117. /* if (blockSize - 1U) is not multiple of 4 */
  118. blkCnt = (blockSize - 4 ) % 4U;
  119. while (blkCnt > 0U)
  120. {
  121. /* Initialize maxVal to the next consecutive values one by one */
  122. maxVal1 = *pSrc++;
  123. /* compare for the maximum value */
  124. if (out > maxVal1)
  125. {
  126. /* Update the maximum value and it's index */
  127. out = maxVal1;
  128. outIndex = blockSize - blkCnt ;
  129. }
  130. /* Decrement the loop counter */
  131. blkCnt--;
  132. }
  133. }
  134. /* Store the maximum value and it's index into destination pointers */
  135. *pResult = out;
  136. *pIndex = outIndex;
  137. }
  138. #else
  139. void arm_min_f32(
  140. const float32_t * pSrc,
  141. uint32_t blockSize,
  142. float32_t * pResult,
  143. uint32_t * pIndex)
  144. {
  145. float32_t minVal, out; /* Temporary variables to store the output value. */
  146. uint32_t blkCnt, outIndex; /* Loop counter */
  147. #if defined (ARM_MATH_LOOPUNROLL)
  148. uint32_t index; /* index of maximum value */
  149. #endif
  150. /* Initialise index value to zero. */
  151. outIndex = 0U;
  152. /* Load first input value that act as reference value for comparision */
  153. out = *pSrc++;
  154. #if defined (ARM_MATH_LOOPUNROLL)
  155. /* Initialise index of maximum value. */
  156. index = 0U;
  157. /* Loop unrolling: Compute 4 outputs at a time */
  158. blkCnt = (blockSize - 1U) >> 2U;
  159. while (blkCnt > 0U)
  160. {
  161. /* Initialize minVal to next consecutive values one by one */
  162. minVal = *pSrc++;
  163. /* compare for the minimum value */
  164. if (out > minVal)
  165. {
  166. /* Update the minimum value and it's index */
  167. out = minVal;
  168. outIndex = index + 1U;
  169. }
  170. minVal = *pSrc++;
  171. if (out > minVal)
  172. {
  173. out = minVal;
  174. outIndex = index + 2U;
  175. }
  176. minVal = *pSrc++;
  177. if (out > minVal)
  178. {
  179. out = minVal;
  180. outIndex = index + 3U;
  181. }
  182. minVal = *pSrc++;
  183. if (out > minVal)
  184. {
  185. out = minVal;
  186. outIndex = index + 4U;
  187. }
  188. index += 4U;
  189. /* Decrement loop counter */
  190. blkCnt--;
  191. }
  192. /* Loop unrolling: Compute remaining outputs */
  193. blkCnt = (blockSize - 1U) % 4U;
  194. #else
  195. /* Initialize blkCnt with number of samples */
  196. blkCnt = (blockSize - 1U);
  197. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  198. while (blkCnt > 0U)
  199. {
  200. /* Initialize minVal to the next consecutive values one by one */
  201. minVal = *pSrc++;
  202. /* compare for the minimum value */
  203. if (out > minVal)
  204. {
  205. /* Update the minimum value and it's index */
  206. out = minVal;
  207. outIndex = blockSize - blkCnt;
  208. }
  209. /* Decrement loop counter */
  210. blkCnt--;
  211. }
  212. /* Store the minimum value and it's index into destination pointers */
  213. *pResult = out;
  214. *pIndex = outIndex;
  215. }
  216. #endif /* #if defined(ARM_MATH_NEON) */
  217. /**
  218. @} end of Min group
  219. */