arm_max_q31.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_max_q31.c
  4. * Description: Maximum value of a Q31 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 Q31 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. void arm_max_q31(
  47. const q31_t * pSrc,
  48. uint32_t blockSize,
  49. q31_t * pResult,
  50. uint32_t * pIndex)
  51. {
  52. uint32_t blkCnt; /* loop counters */
  53. q31x4_t vecSrc;
  54. q31x4_t curExtremValVec = vdupq_n_s32( Q31_MIN);
  55. q31_t maxValue = Q31_MIN;
  56. q31_t temp;
  57. uint32_t idx = blockSize;
  58. uint32x4_t indexVec;
  59. uint32x4_t curExtremIdxVec;
  60. mve_pred16_t p0;
  61. indexVec = vidupq_u32((uint32_t)0, 1);
  62. curExtremIdxVec = vdupq_n_u32(0);
  63. /* Compute 4 outputs at a time */
  64. blkCnt = blockSize >> 2U;
  65. while (blkCnt > 0U)
  66. {
  67. vecSrc = vldrwq_s32(pSrc);
  68. pSrc += 4;
  69. /*
  70. * Get current max per lane and current index per lane
  71. * when a max is selected
  72. */
  73. p0 = vcmpgeq(vecSrc, curExtremValVec);
  74. curExtremValVec = vpselq(vecSrc, curExtremValVec, p0);
  75. curExtremIdxVec = vpselq(indexVec, curExtremIdxVec, p0);
  76. indexVec = indexVec + 4;
  77. /*
  78. * Decrement the blockSize loop counter
  79. */
  80. blkCnt--;
  81. }
  82. /*
  83. * Get max value across the vector
  84. */
  85. maxValue = vmaxvq(maxValue, curExtremValVec);
  86. /*
  87. * set index for lower values to max possible index
  88. */
  89. p0 = vcmpgeq(curExtremValVec, maxValue);
  90. indexVec = vpselq(curExtremIdxVec, vdupq_n_u32(blockSize), p0);
  91. /*
  92. * Get min index which is thus for a max value
  93. */
  94. idx = vminvq(idx, indexVec);
  95. /* Tail */
  96. blkCnt = blockSize & 0x3;
  97. while (blkCnt > 0U)
  98. {
  99. /* Initialize maxVal 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. #else
  118. void arm_max_q31(
  119. const q31_t * pSrc,
  120. uint32_t blockSize,
  121. q31_t * pResult,
  122. uint32_t * pIndex)
  123. {
  124. q31_t maxVal, out; /* Temporary variables to store the output value. */
  125. uint32_t blkCnt, outIndex; /* Loop counter */
  126. #if defined (ARM_MATH_LOOPUNROLL)
  127. uint32_t index; /* index of maximum value */
  128. #endif
  129. /* Initialise index value to zero. */
  130. outIndex = 0U;
  131. /* Load first input value that act as reference value for comparision */
  132. out = *pSrc++;
  133. #if defined (ARM_MATH_LOOPUNROLL)
  134. /* Initialise index of maximum value. */
  135. index = 0U;
  136. /* Loop unrolling: Compute 4 outputs at a time */
  137. blkCnt = (blockSize - 1U) >> 2U;
  138. while (blkCnt > 0U)
  139. {
  140. /* Initialize maxVal to next consecutive values one by one */
  141. maxVal = *pSrc++;
  142. /* compare for the maximum value */
  143. if (out < maxVal)
  144. {
  145. /* Update the maximum value and it's index */
  146. out = maxVal;
  147. outIndex = index + 1U;
  148. }
  149. maxVal = *pSrc++;
  150. if (out < maxVal)
  151. {
  152. out = maxVal;
  153. outIndex = index + 2U;
  154. }
  155. maxVal = *pSrc++;
  156. if (out < maxVal)
  157. {
  158. out = maxVal;
  159. outIndex = index + 3U;
  160. }
  161. maxVal = *pSrc++;
  162. if (out < maxVal)
  163. {
  164. out = maxVal;
  165. outIndex = index + 4U;
  166. }
  167. index += 4U;
  168. /* Decrement loop counter */
  169. blkCnt--;
  170. }
  171. /* Loop unrolling: Compute remaining outputs */
  172. blkCnt = (blockSize - 1U) % 4U;
  173. #else
  174. /* Initialize blkCnt with number of samples */
  175. blkCnt = (blockSize - 1U);
  176. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  177. while (blkCnt > 0U)
  178. {
  179. /* Initialize maxVal to the next consecutive values one by one */
  180. maxVal = *pSrc++;
  181. /* compare for the maximum value */
  182. if (out < maxVal)
  183. {
  184. /* Update the maximum value and it's index */
  185. out = maxVal;
  186. outIndex = blockSize - blkCnt;
  187. }
  188. /* Decrement loop counter */
  189. blkCnt--;
  190. }
  191. /* Store the maximum value and it's index into destination pointers */
  192. *pResult = out;
  193. *pIndex = outIndex;
  194. }
  195. #endif /* defined(ARM_MATH_MVEI) */
  196. /**
  197. @} end of Max group
  198. */