arm_max_f16.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_max_f16.c
  4. * Description: Maximum 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 Max
  38. @{
  39. */
  40. /**
  41. @brief Maximum 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 maximum value returned here
  45. @param[out] pIndex index of maximum value returned here
  46. @return none
  47. */
  48. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  49. void arm_max_f16(
  50. const float16_t * pSrc,
  51. uint32_t blockSize,
  52. float16_t * pResult,
  53. uint32_t * pIndex)
  54. {
  55. int32_t blkCnt;
  56. f16x8_t vecSrc;
  57. f16x8_t curExtremValVec = vdupq_n_f16(F16_MIN);
  58. float16_t maxValue = F16_MIN;
  59. uint32_t idx = blockSize;
  60. uint16x8_t indexVec;
  61. uint16x8_t curExtremIdxVec;
  62. uint32_t curIdx = 0;
  63. mve_pred16_t p0;
  64. float16_t tmp;
  65. indexVec = vidupq_wb_u16(&curIdx, 1);
  66. curExtremIdxVec = vdupq_n_u16(0);
  67. /* Compute 4 outputs at a time */
  68. blkCnt = blockSize >> 3;
  69. while (blkCnt > 0)
  70. {
  71. vecSrc = vldrhq_f16(pSrc);
  72. /*
  73. * Get current max per lane and current index per lane
  74. * when a max is selected
  75. */
  76. p0 = vcmpgeq(vecSrc, curExtremValVec);
  77. curExtremValVec = vpselq(vecSrc, curExtremValVec, p0);
  78. curExtremIdxVec = vpselq(indexVec, curExtremIdxVec, p0);
  79. indexVec = vidupq_wb_u16(&curIdx, 1);
  80. pSrc += 8;
  81. /* Decrement the loop counter */
  82. blkCnt--;
  83. }
  84. /*
  85. * Get max value across the vector
  86. */
  87. maxValue = vmaxnmvq(maxValue, curExtremValVec);
  88. /*
  89. * set index for lower values to max possible index
  90. */
  91. p0 = vcmpgeq(curExtremValVec, maxValue);
  92. indexVec = vpselq(curExtremIdxVec, vdupq_n_u16(blockSize), p0);
  93. /*
  94. * Get min index which is thus for a max value
  95. */
  96. idx = vminvq(idx, indexVec);
  97. /* Tail */
  98. blkCnt = blockSize & 7;
  99. while (blkCnt > 0)
  100. {
  101. /* Initialize tmp to the next consecutive values one by one */
  102. tmp = *pSrc++;
  103. /* compare for the maximum value */
  104. if ((_Float16)maxValue < (_Float16)tmp)
  105. {
  106. /* Update the maximum value and it's index */
  107. maxValue = tmp;
  108. idx = blockSize - blkCnt;
  109. }
  110. /* Decrement loop counter */
  111. blkCnt--;
  112. }
  113. /*
  114. * Save result
  115. */
  116. *pIndex = idx;
  117. *pResult = maxValue;
  118. }
  119. #else
  120. void arm_max_f16(
  121. const float16_t * pSrc,
  122. uint32_t blockSize,
  123. float16_t * pResult,
  124. uint32_t * pIndex)
  125. {
  126. float16_t maxVal, 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 maxVal to next consecutive values one by one */
  143. maxVal = *pSrc++;
  144. /* compare for the maximum value */
  145. if ((_Float16)out < (_Float16)maxVal)
  146. {
  147. /* Update the maximum value and it's index */
  148. out = maxVal;
  149. outIndex = index + 1U;
  150. }
  151. maxVal = *pSrc++;
  152. if ((_Float16)out < (_Float16)maxVal)
  153. {
  154. out = maxVal;
  155. outIndex = index + 2U;
  156. }
  157. maxVal = *pSrc++;
  158. if ((_Float16)out < (_Float16)maxVal)
  159. {
  160. out = maxVal;
  161. outIndex = index + 3U;
  162. }
  163. maxVal = *pSrc++;
  164. if ((_Float16)out < (_Float16)maxVal)
  165. {
  166. out = maxVal;
  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 maxVal to the next consecutive values one by one */
  182. maxVal = *pSrc++;
  183. /* compare for the maximum value */
  184. if ((_Float16)out < (_Float16)maxVal)
  185. {
  186. /* Update the maximum value and it's index */
  187. out = maxVal;
  188. outIndex = blockSize - blkCnt;
  189. }
  190. /* Decrement loop counter */
  191. blkCnt--;
  192. }
  193. /* Store the maximum 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 Max group
  200. */
  201. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */