arm_max_f16.c 6.0 KB

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