arm_max_q31.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_max_q31.c
  4. * Description: Maximum value of a Q31 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 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) && !defined(ARM_MATH_AUTOVECTORIZE)
  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. int32_t blkCnt; /* loop counters */
  53. q31x4_t extremValVec = vdupq_n_s32(Q31_MIN);
  54. q31_t maxValue = Q31_MIN;
  55. uint32x4_t indexVec;
  56. uint32x4_t extremIdxVec;
  57. mve_pred16_t p0;
  58. uint32_t extremIdxArr[4];
  59. indexVec = vidupq_u32(0U, 1);
  60. blkCnt = blockSize;
  61. do {
  62. mve_pred16_t p = vctp32q(blkCnt);
  63. q31x4_t extremIdxVal = vld1q_z_s32(pSrc, p);
  64. /*
  65. * Get current max per lane and current index per lane
  66. * when a max is selected
  67. */
  68. p0 = vcmpgeq_m(extremIdxVal, extremValVec, p);
  69. extremValVec = vorrq_m(extremValVec, extremIdxVal, extremIdxVal, p0);
  70. /* store per-lane extrema indexes */
  71. vst1q_p_u32(extremIdxArr, indexVec, p0);
  72. indexVec += 4;
  73. pSrc += 4;
  74. blkCnt -= 4;
  75. }
  76. while (blkCnt > 0);
  77. /* Get max value across the vector */
  78. maxValue = vmaxvq(maxValue, extremValVec);
  79. /* set index for lower values to max possible index */
  80. p0 = vcmpgeq(extremValVec, maxValue);
  81. extremIdxVec = vld1q_u32(extremIdxArr);
  82. indexVec = vpselq(extremIdxVec, vdupq_n_u32(blockSize - 1), p0);
  83. *pIndex = vminvq(blockSize - 1, indexVec);
  84. *pResult = maxValue;
  85. }
  86. #else
  87. void arm_max_q31(
  88. const q31_t * pSrc,
  89. uint32_t blockSize,
  90. q31_t * pResult,
  91. uint32_t * pIndex)
  92. {
  93. q31_t maxVal, out; /* Temporary variables to store the output value. */
  94. uint32_t blkCnt, outIndex; /* Loop counter */
  95. #if defined (ARM_MATH_LOOPUNROLL)
  96. uint32_t index; /* index of maximum value */
  97. #endif
  98. /* Initialise index value to zero. */
  99. outIndex = 0U;
  100. /* Load first input value that act as reference value for comparision */
  101. out = *pSrc++;
  102. #if defined (ARM_MATH_LOOPUNROLL)
  103. /* Initialise index of maximum value. */
  104. index = 0U;
  105. /* Loop unrolling: Compute 4 outputs at a time */
  106. blkCnt = (blockSize - 1U) >> 2U;
  107. while (blkCnt > 0U)
  108. {
  109. /* Initialize maxVal to next consecutive values one by one */
  110. maxVal = *pSrc++;
  111. /* compare for the maximum value */
  112. if (out < maxVal)
  113. {
  114. /* Update the maximum value and it's index */
  115. out = maxVal;
  116. outIndex = index + 1U;
  117. }
  118. maxVal = *pSrc++;
  119. if (out < maxVal)
  120. {
  121. out = maxVal;
  122. outIndex = index + 2U;
  123. }
  124. maxVal = *pSrc++;
  125. if (out < maxVal)
  126. {
  127. out = maxVal;
  128. outIndex = index + 3U;
  129. }
  130. maxVal = *pSrc++;
  131. if (out < maxVal)
  132. {
  133. out = maxVal;
  134. outIndex = index + 4U;
  135. }
  136. index += 4U;
  137. /* Decrement loop counter */
  138. blkCnt--;
  139. }
  140. /* Loop unrolling: Compute remaining outputs */
  141. blkCnt = (blockSize - 1U) % 4U;
  142. #else
  143. /* Initialize blkCnt with number of samples */
  144. blkCnt = (blockSize - 1U);
  145. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  146. while (blkCnt > 0U)
  147. {
  148. /* Initialize maxVal to the next consecutive values one by one */
  149. maxVal = *pSrc++;
  150. /* compare for the maximum value */
  151. if (out < maxVal)
  152. {
  153. /* Update the maximum value and it's index */
  154. out = maxVal;
  155. outIndex = blockSize - blkCnt;
  156. }
  157. /* Decrement loop counter */
  158. blkCnt--;
  159. }
  160. /* Store the maximum value and it's index into destination pointers */
  161. *pResult = out;
  162. *pIndex = outIndex;
  163. }
  164. #endif /* defined(ARM_MATH_MVEI) */
  165. /**
  166. @} end of Max group
  167. */