arm_min_f16.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_min_f16.c
  4. * Description: Minimum value 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 Min
  38. @{
  39. */
  40. /**
  41. @brief Minimum value 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. void arm_min_f16(
  49. const float16_t * pSrc,
  50. uint32_t blockSize,
  51. float16_t * pResult,
  52. uint32_t * pIndex)
  53. {
  54. int32_t blkCnt; /* loop counters */
  55. f16x8_t vecSrc;
  56. float16_t const *pSrcVec;
  57. f16x8_t curExtremValVec = vdupq_n_f16(F16_MAX);
  58. float16_t minValue = F16_MAX;
  59. uint32_t idx = blockSize;
  60. uint16x8_t indexVec;
  61. uint16x8_t curExtremIdxVec;
  62. mve_pred16_t p0;
  63. indexVec = vidupq_u16((uint32_t)0, 1);
  64. curExtremIdxVec = vdupq_n_u16(0);
  65. pSrcVec = (float16_t const *) pSrc;
  66. blkCnt = blockSize >> 3;
  67. while (blkCnt > 0)
  68. {
  69. vecSrc = vldrhq_f16(pSrcVec); pSrcVec += 8;
  70. /*
  71. * Get current min per lane and current index per lane
  72. * when a min is selected
  73. */
  74. p0 = vcmpleq(vecSrc, curExtremValVec);
  75. curExtremValVec = vpselq(vecSrc, curExtremValVec, p0);
  76. curExtremIdxVec = vpselq(indexVec, curExtremIdxVec, p0);
  77. indexVec = indexVec + 8;
  78. /*
  79. * Decrement the blockSize loop counter
  80. */
  81. blkCnt--;
  82. }
  83. /*
  84. * tail
  85. * (will be merged thru tail predication)
  86. */
  87. blkCnt = blockSize & 7;
  88. if (blkCnt > 0)
  89. {
  90. vecSrc = vldrhq_f16(pSrcVec); pSrcVec += 8;
  91. p0 = vctp16q(blkCnt);
  92. /*
  93. * Get current min per lane and current index per lane
  94. * when a min is selected
  95. */
  96. p0 = vcmpleq_m(vecSrc, curExtremValVec, p0);
  97. curExtremValVec = vpselq(vecSrc, curExtremValVec, p0);
  98. curExtremIdxVec = vpselq(indexVec, curExtremIdxVec, p0);
  99. }
  100. /*
  101. * Get min value across the vector
  102. */
  103. minValue = vminnmvq(minValue, curExtremValVec);
  104. /*
  105. * set index for lower values to min possible index
  106. */
  107. p0 = vcmpleq(curExtremValVec, minValue);
  108. indexVec = vpselq(curExtremIdxVec, vdupq_n_u16(blockSize), p0);
  109. /*
  110. * Get min index which is thus for a min value
  111. */
  112. idx = vminvq(idx, indexVec);
  113. /*
  114. * Save result
  115. */
  116. *pIndex = idx;
  117. *pResult = minValue;
  118. }
  119. #else
  120. void arm_min_f16(
  121. const float16_t * pSrc,
  122. uint32_t blockSize,
  123. float16_t * pResult,
  124. uint32_t * pIndex)
  125. {
  126. float16_t minVal, out; /* Temporary variables to store the output value. */
  127. uint32_t blkCnt, outIndex; /* Loop counter */
  128. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  129. uint32_t index; /* index of maximum value */
  130. #endif
  131. /* Initialise index value to zero. */
  132. outIndex = 0U;
  133. /* Load first input value that act as reference value for comparision */
  134. out = *pSrc++;
  135. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  136. /* Initialise index of maximum value. */
  137. index = 0U;
  138. /* Loop unrolling: Compute 4 outputs at a time */
  139. blkCnt = (blockSize - 1U) >> 2U;
  140. while (blkCnt > 0U)
  141. {
  142. /* Initialize minVal to next consecutive values one by one */
  143. minVal = *pSrc++;
  144. /* compare for the minimum value */
  145. if ((_Float16)out > (_Float16)minVal)
  146. {
  147. /* Update the minimum value and it's index */
  148. out = minVal;
  149. outIndex = index + 1U;
  150. }
  151. minVal = *pSrc++;
  152. if ((_Float16)out > (_Float16)minVal)
  153. {
  154. out = minVal;
  155. outIndex = index + 2U;
  156. }
  157. minVal = *pSrc++;
  158. if ((_Float16)out > (_Float16)minVal)
  159. {
  160. out = minVal;
  161. outIndex = index + 3U;
  162. }
  163. minVal = *pSrc++;
  164. if ((_Float16)out > (_Float16)minVal)
  165. {
  166. out = minVal;
  167. outIndex = index + 4U;
  168. }
  169. index += 4U;
  170. /* Decrement loop counter */
  171. blkCnt--;
  172. }
  173. /* Loop unrolling: Compute remaining outputs */
  174. blkCnt = (blockSize - 1U) % 4U;
  175. #else
  176. /* Initialize blkCnt with number of samples */
  177. blkCnt = (blockSize - 1U);
  178. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  179. while (blkCnt > 0U)
  180. {
  181. /* Initialize minVal to the next consecutive values one by one */
  182. minVal = *pSrc++;
  183. /* compare for the minimum value */
  184. if ((_Float16)out > (_Float16)minVal)
  185. {
  186. /* Update the minimum value and it's index */
  187. out = minVal;
  188. outIndex = blockSize - blkCnt;
  189. }
  190. /* Decrement loop counter */
  191. blkCnt--;
  192. }
  193. /* Store the minimum value and it's index into destination pointers */
  194. *pResult = out;
  195. *pIndex = outIndex;
  196. }
  197. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  198. /**
  199. @} end of Min group
  200. */
  201. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */