arm_max_q7.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_max_q7.c
  4. * Description: Maximum value of a Q7 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.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. */
  43. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. static void arm_small_blk_max_q7(
  46. const q7_t * pSrc,
  47. uint16_t blockSize,
  48. q7_t * pResult,
  49. uint32_t * pIndex)
  50. {
  51. int32_t blkCnt; /* loop counters */
  52. q7x16_t extremValVec = vdupq_n_s8(Q7_MIN);
  53. q7_t maxValue = Q7_MIN;
  54. uint8x16_t indexVec;
  55. uint8x16_t extremIdxVec;
  56. mve_pred16_t p0;
  57. uint8_t extremIdxArr[16];
  58. indexVec = vidupq_u8(0U, 1);
  59. blkCnt = blockSize;
  60. do {
  61. mve_pred16_t p = vctp8q(blkCnt);
  62. q7x16_t extremIdxVal = vld1q_z_s8(pSrc, p);
  63. /*
  64. * Get current max per lane and current index per lane
  65. * when a max is selected
  66. */
  67. p0 = vcmpgeq_m(extremIdxVal, extremValVec, p);
  68. extremValVec = vorrq_m(extremValVec, extremIdxVal, extremIdxVal, p0);
  69. /* store per-lane extrema indexes */
  70. vst1q_p_u8(extremIdxArr, indexVec, p0);
  71. indexVec += 16;
  72. pSrc += 16;
  73. blkCnt -= 16;
  74. }
  75. while (blkCnt > 0);
  76. /* Get max value across the vector */
  77. maxValue = vmaxvq(maxValue, extremValVec);
  78. /* set index for lower values to max possible index */
  79. p0 = vcmpgeq(extremValVec, maxValue);
  80. extremIdxVec = vld1q_u8(extremIdxArr);
  81. indexVec = vpselq(extremIdxVec, vdupq_n_u8(blockSize - 1), p0);
  82. *pIndex = vminvq_u8(blockSize - 1, indexVec);
  83. *pResult = maxValue;
  84. }
  85. void arm_max_q7(
  86. const q7_t * pSrc,
  87. uint32_t blockSize,
  88. q7_t * pResult,
  89. uint32_t * pIndex)
  90. {
  91. int32_t totalSize = blockSize;
  92. const uint16_t sub_blk_sz = UINT8_MAX + 1;
  93. if (totalSize <= sub_blk_sz)
  94. {
  95. arm_small_blk_max_q7(pSrc, blockSize, pResult, pIndex);
  96. }
  97. else
  98. {
  99. uint32_t curIdx = 0;
  100. q7_t curBlkExtr = Q7_MIN;
  101. uint32_t curBlkPos = 0;
  102. uint32_t curBlkIdx = 0;
  103. /*
  104. * process blocks of 255 elts
  105. */
  106. while (totalSize >= sub_blk_sz)
  107. {
  108. const q7_t *curSrc = pSrc;
  109. arm_small_blk_max_q7(curSrc, sub_blk_sz, pResult, pIndex);
  110. if (*pResult > curBlkExtr)
  111. {
  112. /*
  113. * update partial extrema
  114. */
  115. curBlkExtr = *pResult;
  116. curBlkPos = *pIndex;
  117. curBlkIdx = curIdx;
  118. }
  119. curIdx++;
  120. pSrc += sub_blk_sz;
  121. totalSize -= sub_blk_sz;
  122. }
  123. /*
  124. * remainder
  125. */
  126. arm_small_blk_max_q7(pSrc, totalSize, pResult, pIndex);
  127. if (*pResult > curBlkExtr)
  128. {
  129. curBlkExtr = *pResult;
  130. curBlkPos = *pIndex;
  131. curBlkIdx = curIdx;
  132. }
  133. *pIndex = curBlkIdx * sub_blk_sz + curBlkPos;
  134. *pResult = curBlkExtr;
  135. }
  136. }
  137. #else
  138. void arm_max_q7(
  139. const q7_t * pSrc,
  140. uint32_t blockSize,
  141. q7_t * pResult,
  142. uint32_t * pIndex)
  143. {
  144. q7_t maxVal, out; /* Temporary variables to store the output value. */
  145. uint32_t blkCnt, outIndex; /* Loop counter */
  146. #if defined (ARM_MATH_LOOPUNROLL)
  147. uint32_t index; /* index of maximum value */
  148. #endif
  149. /* Initialise index value to zero. */
  150. outIndex = 0U;
  151. /* Load first input value that act as reference value for comparision */
  152. out = *pSrc++;
  153. #if defined (ARM_MATH_LOOPUNROLL)
  154. /* Initialise index of maximum value. */
  155. index = 0U;
  156. /* Loop unrolling: Compute 4 outputs at a time */
  157. blkCnt = (blockSize - 1U) >> 2U;
  158. while (blkCnt > 0U)
  159. {
  160. /* Initialize maxVal to next consecutive values one by one */
  161. maxVal = *pSrc++;
  162. /* compare for the maximum value */
  163. if (out < maxVal)
  164. {
  165. /* Update the maximum value and it's index */
  166. out = maxVal;
  167. outIndex = index + 1U;
  168. }
  169. maxVal = *pSrc++;
  170. if (out < maxVal)
  171. {
  172. out = maxVal;
  173. outIndex = index + 2U;
  174. }
  175. maxVal = *pSrc++;
  176. if (out < maxVal)
  177. {
  178. out = maxVal;
  179. outIndex = index + 3U;
  180. }
  181. maxVal = *pSrc++;
  182. if (out < maxVal)
  183. {
  184. out = maxVal;
  185. outIndex = index + 4U;
  186. }
  187. index += 4U;
  188. /* Decrement loop counter */
  189. blkCnt--;
  190. }
  191. /* Loop unrolling: Compute remaining outputs */
  192. blkCnt = (blockSize - 1U) % 4U;
  193. #else
  194. /* Initialize blkCnt with number of samples */
  195. blkCnt = (blockSize - 1U);
  196. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  197. while (blkCnt > 0U)
  198. {
  199. /* Initialize maxVal to the next consecutive values one by one */
  200. maxVal = *pSrc++;
  201. /* compare for the maximum value */
  202. if (out < maxVal)
  203. {
  204. /* Update the maximum value and it's index */
  205. out = maxVal;
  206. outIndex = blockSize - blkCnt;
  207. }
  208. /* Decrement loop counter */
  209. blkCnt--;
  210. }
  211. /* Store the maximum value and it's index into destination pointers */
  212. *pResult = out;
  213. *pIndex = outIndex;
  214. }
  215. #endif /* defined(ARM_MATH_MVEI) */
  216. /**
  217. @} end of Max group
  218. */