arm_max_q15.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_max_q15.c
  4. * Description: Maximum value of a Q15 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 Q15 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. void arm_max_q15(
  46. const q15_t * pSrc,
  47. uint32_t blockSize,
  48. q15_t * pResult,
  49. uint32_t * pIndex)
  50. {
  51. int32_t blkCnt; /* loop counters */
  52. q15x8_t extremValVec = vdupq_n_s16(Q15_MIN);
  53. q15_t maxValue = Q15_MIN;
  54. uint16x8_t indexVec;
  55. uint16x8_t extremIdxVec;
  56. mve_pred16_t p0;
  57. uint16_t extremIdxArr[8];
  58. indexVec = vidupq_u16(0U, 1);
  59. blkCnt = blockSize;
  60. do {
  61. mve_pred16_t p = vctp16q(blkCnt);
  62. q15x8_t extremIdxVal = vld1q_z_s16(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_u16(extremIdxArr, indexVec, p0);
  71. indexVec += 8;
  72. pSrc += 8;
  73. blkCnt -= 8;
  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_u16(extremIdxArr);
  81. indexVec = vpselq(extremIdxVec, vdupq_n_u16(blockSize - 1), p0);
  82. *pIndex = vminvq(blockSize - 1, indexVec);
  83. *pResult = maxValue;
  84. }
  85. #else
  86. void arm_max_q15(
  87. const q15_t * pSrc,
  88. uint32_t blockSize,
  89. q15_t * pResult,
  90. uint32_t * pIndex)
  91. {
  92. q15_t maxVal, out; /* Temporary variables to store the output value. */
  93. uint32_t blkCnt, outIndex; /* Loop counter */
  94. #if defined (ARM_MATH_LOOPUNROLL)
  95. uint32_t index; /* index of maximum value */
  96. #endif
  97. /* Initialise index value to zero. */
  98. outIndex = 0U;
  99. /* Load first input value that act as reference value for comparision */
  100. out = *pSrc++;
  101. #if defined (ARM_MATH_LOOPUNROLL)
  102. /* Initialise index of maximum value. */
  103. index = 0U;
  104. /* Loop unrolling: Compute 4 outputs at a time */
  105. blkCnt = (blockSize - 1U) >> 2U;
  106. while (blkCnt > 0U)
  107. {
  108. /* Initialize maxVal to next consecutive values one by one */
  109. maxVal = *pSrc++;
  110. /* compare for the maximum value */
  111. if (out < maxVal)
  112. {
  113. /* Update the maximum value and it's index */
  114. out = maxVal;
  115. outIndex = index + 1U;
  116. }
  117. maxVal = *pSrc++;
  118. if (out < maxVal)
  119. {
  120. out = maxVal;
  121. outIndex = index + 2U;
  122. }
  123. maxVal = *pSrc++;
  124. if (out < maxVal)
  125. {
  126. out = maxVal;
  127. outIndex = index + 3U;
  128. }
  129. maxVal = *pSrc++;
  130. if (out < maxVal)
  131. {
  132. out = maxVal;
  133. outIndex = index + 4U;
  134. }
  135. index += 4U;
  136. /* Decrement loop counter */
  137. blkCnt--;
  138. }
  139. /* Loop unrolling: Compute remaining outputs */
  140. blkCnt = (blockSize - 1U) % 4U;
  141. #else
  142. /* Initialize blkCnt with number of samples */
  143. blkCnt = (blockSize - 1U);
  144. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  145. while (blkCnt > 0U)
  146. {
  147. /* Initialize maxVal to the next consecutive values one by one */
  148. maxVal = *pSrc++;
  149. /* compare for the maximum value */
  150. if (out < maxVal)
  151. {
  152. /* Update the maximum value and it's index */
  153. out = maxVal;
  154. outIndex = blockSize - blkCnt;
  155. }
  156. /* Decrement loop counter */
  157. blkCnt--;
  158. }
  159. /* Store the maximum value and it's index into destination pointers */
  160. *pResult = out;
  161. *pIndex = outIndex;
  162. }
  163. #endif /* defined(ARM_MATH_MVEI) */
  164. /**
  165. @} end of Max group
  166. */