arm_max_q7.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_max_q7.c
  4. * Description: Maximum value of a Q7 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. /**
  30. @ingroup groupStats
  31. */
  32. /**
  33. @addtogroup Max
  34. @{
  35. */
  36. /**
  37. @brief Maximum value of a Q7 vector.
  38. @param[in] pSrc points to the input vector
  39. @param[in] blockSize number of samples in input vector
  40. @param[out] pResult maximum value returned here
  41. @param[out] pIndex index of maximum value returned here
  42. @return none
  43. */
  44. #if defined(ARM_MATH_MVEI)
  45. #include "arm_helium_utils.h"
  46. static void arm_small_blk_max_q7(
  47. const q7_t * pSrc,
  48. uint8_t blockSize,
  49. q7_t * pResult,
  50. uint32_t * pIndex)
  51. {
  52. uint32_t blkCnt; /* loop counters */
  53. q7x16_t vecSrc;
  54. q7x16_t curExtremValVec = vdupq_n_s8( Q7_MIN);
  55. q7_t maxValue = Q7_MIN, temp;
  56. uint32_t idx = blockSize;
  57. uint8x16_t indexVec;
  58. uint8x16_t curExtremIdxVec;
  59. mve_pred16_t p0;
  60. indexVec = vidupq_u8((uint32_t)0, 1);
  61. curExtremIdxVec = vdupq_n_u8(0);
  62. blkCnt = blockSize >> 4;
  63. while (blkCnt > 0U)
  64. {
  65. vecSrc = vldrbq_s8(pSrc);
  66. pSrc += 16;
  67. /*
  68. * Get current max per lane and current index per lane
  69. * when a max is selected
  70. */
  71. p0 = vcmpgeq(vecSrc, curExtremValVec);
  72. curExtremValVec = vpselq(vecSrc, curExtremValVec, p0);
  73. curExtremIdxVec = vpselq(indexVec, curExtremIdxVec, p0);
  74. indexVec = indexVec + 16;
  75. /*
  76. * Decrement the blockSize loop counter
  77. */
  78. blkCnt--;
  79. }
  80. /*
  81. * Get max value across the vector
  82. */
  83. maxValue = vmaxvq(maxValue, curExtremValVec);
  84. /*
  85. * set index for lower values to max possible index
  86. */
  87. p0 = vcmpgeq(curExtremValVec, maxValue);
  88. indexVec = vpselq(curExtremIdxVec, vdupq_n_u8(blockSize), p0);
  89. /*
  90. * Get min index which is thus for a max value
  91. */
  92. idx = vminvq(idx, indexVec);
  93. /*
  94. * tail
  95. */
  96. blkCnt = blockSize & 0xF;
  97. while (blkCnt > 0U)
  98. {
  99. /* Initialize temp to the next consecutive values one by one */
  100. temp = *pSrc++;
  101. /* compare for the maximum value */
  102. if (maxValue < temp)
  103. {
  104. /* Update the maximum value and it's index */
  105. maxValue = temp;
  106. idx = blockSize - blkCnt;
  107. }
  108. /* Decrement loop counter */
  109. blkCnt--;
  110. }
  111. /*
  112. * Save result
  113. */
  114. *pIndex = idx;
  115. *pResult = maxValue;
  116. }
  117. void arm_max_q7(
  118. const q7_t * pSrc,
  119. uint32_t blockSize,
  120. q7_t * pResult,
  121. uint32_t * pIndex)
  122. {
  123. int32_t totalSize = blockSize;
  124. if (totalSize <= UINT8_MAX)
  125. {
  126. arm_small_blk_max_q7(pSrc, blockSize, pResult, pIndex);
  127. }
  128. else
  129. {
  130. uint32_t curIdx = 0;
  131. q7_t curBlkExtr = Q7_MIN;
  132. uint32_t curBlkPos = 0;
  133. uint32_t curBlkIdx = 0;
  134. /*
  135. * process blocks of 255 elts
  136. */
  137. while (totalSize >= UINT8_MAX)
  138. {
  139. const q7_t *curSrc = pSrc;
  140. arm_small_blk_max_q7(curSrc, UINT8_MAX, pResult, pIndex);
  141. if (*pResult > curBlkExtr)
  142. {
  143. /*
  144. * update partial extrema
  145. */
  146. curBlkExtr = *pResult;
  147. curBlkPos = *pIndex;
  148. curBlkIdx = curIdx;
  149. }
  150. curIdx++;
  151. pSrc += UINT8_MAX;
  152. totalSize -= UINT8_MAX;
  153. }
  154. /*
  155. * remainder
  156. */
  157. arm_small_blk_max_q7(pSrc, totalSize, pResult, pIndex);
  158. if (*pResult > curBlkExtr)
  159. {
  160. curBlkExtr = *pResult;
  161. curBlkPos = *pIndex;
  162. curBlkIdx = curIdx;
  163. }
  164. *pIndex = curBlkIdx * UINT8_MAX + curBlkPos;
  165. *pResult = curBlkExtr;
  166. }
  167. }
  168. #else
  169. void arm_max_q7(
  170. const q7_t * pSrc,
  171. uint32_t blockSize,
  172. q7_t * pResult,
  173. uint32_t * pIndex)
  174. {
  175. q7_t maxVal, out; /* Temporary variables to store the output value. */
  176. uint32_t blkCnt, outIndex; /* Loop counter */
  177. #if defined (ARM_MATH_LOOPUNROLL)
  178. uint32_t index; /* index of maximum value */
  179. #endif
  180. /* Initialise index value to zero. */
  181. outIndex = 0U;
  182. /* Load first input value that act as reference value for comparision */
  183. out = *pSrc++;
  184. #if defined (ARM_MATH_LOOPUNROLL)
  185. /* Initialise index of maximum value. */
  186. index = 0U;
  187. /* Loop unrolling: Compute 4 outputs at a time */
  188. blkCnt = (blockSize - 1U) >> 2U;
  189. while (blkCnt > 0U)
  190. {
  191. /* Initialize maxVal to next consecutive values one by one */
  192. maxVal = *pSrc++;
  193. /* compare for the maximum value */
  194. if (out < maxVal)
  195. {
  196. /* Update the maximum value and it's index */
  197. out = maxVal;
  198. outIndex = index + 1U;
  199. }
  200. maxVal = *pSrc++;
  201. if (out < maxVal)
  202. {
  203. out = maxVal;
  204. outIndex = index + 2U;
  205. }
  206. maxVal = *pSrc++;
  207. if (out < maxVal)
  208. {
  209. out = maxVal;
  210. outIndex = index + 3U;
  211. }
  212. maxVal = *pSrc++;
  213. if (out < maxVal)
  214. {
  215. out = maxVal;
  216. outIndex = index + 4U;
  217. }
  218. index += 4U;
  219. /* Decrement loop counter */
  220. blkCnt--;
  221. }
  222. /* Loop unrolling: Compute remaining outputs */
  223. blkCnt = (blockSize - 1U) % 4U;
  224. #else
  225. /* Initialize blkCnt with number of samples */
  226. blkCnt = (blockSize - 1U);
  227. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  228. while (blkCnt > 0U)
  229. {
  230. /* Initialize maxVal to the next consecutive values one by one */
  231. maxVal = *pSrc++;
  232. /* compare for the maximum value */
  233. if (out < maxVal)
  234. {
  235. /* Update the maximum value and it's index */
  236. out = maxVal;
  237. outIndex = blockSize - blkCnt;
  238. }
  239. /* Decrement loop counter */
  240. blkCnt--;
  241. }
  242. /* Store the maximum value and it's index into destination pointers */
  243. *pResult = out;
  244. *pIndex = outIndex;
  245. }
  246. #endif /* defined(ARM_MATH_MVEI) */
  247. /**
  248. @} end of Max group
  249. */