arm_min_f16.c 6.0 KB

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