arm_max_q15.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_max_q15.c
  4. * Description: Maximum value of a Q15 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 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. @return none
  43. */
  44. #if defined(ARM_MATH_MVEI)
  45. #include "arm_helium_utils.h"
  46. void arm_max_q15(
  47. const q15_t * pSrc,
  48. uint32_t blockSize,
  49. q15_t * pResult,
  50. uint32_t * pIndex)
  51. {
  52. uint32_t blkCnt; /* loop counters */
  53. q15x8_t vecSrc;
  54. q15x8_t curExtremValVec = vdupq_n_s16(Q15_MIN);
  55. q15_t maxValue = Q15_MIN, temp;
  56. uint32_t idx = blockSize;
  57. uint16x8_t indexVec;
  58. uint16x8_t curExtremIdxVec;
  59. mve_pred16_t p0;
  60. indexVec = vidupq_u16((uint32_t)0, 1);
  61. curExtremIdxVec = vdupq_n_u16(0);
  62. blkCnt = blockSize >> 3;
  63. while (blkCnt > 0U)
  64. {
  65. vecSrc = vldrhq_s16(pSrc);
  66. pSrc += 8;
  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 + 8;
  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_u16(blockSize), p0);
  89. /*
  90. * Get min index which is thus for a max value
  91. */
  92. idx = vminvq(idx, indexVec);
  93. /* Tail */
  94. blkCnt = blockSize & 0x7;
  95. while (blkCnt > 0U)
  96. {
  97. /* Initialize temp to the next consecutive values one by one */
  98. temp = *pSrc++;
  99. /* compare for the maximum value */
  100. if (maxValue < temp)
  101. {
  102. /* Update the maximum value and it's index */
  103. maxValue = temp;
  104. idx = blockSize - blkCnt;
  105. }
  106. /* Decrement loop counter */
  107. blkCnt--;
  108. }
  109. /*
  110. * Save result
  111. */
  112. *pIndex = idx;
  113. *pResult = maxValue;
  114. }
  115. #else
  116. void arm_max_q15(
  117. const q15_t * pSrc,
  118. uint32_t blockSize,
  119. q15_t * pResult,
  120. uint32_t * pIndex)
  121. {
  122. q15_t maxVal, out; /* Temporary variables to store the output value. */
  123. uint32_t blkCnt, outIndex; /* Loop counter */
  124. #if defined (ARM_MATH_LOOPUNROLL)
  125. uint32_t index; /* index of maximum value */
  126. #endif
  127. /* Initialise index value to zero. */
  128. outIndex = 0U;
  129. /* Load first input value that act as reference value for comparision */
  130. out = *pSrc++;
  131. #if defined (ARM_MATH_LOOPUNROLL)
  132. /* Initialise index of maximum value. */
  133. index = 0U;
  134. /* Loop unrolling: Compute 4 outputs at a time */
  135. blkCnt = (blockSize - 1U) >> 2U;
  136. while (blkCnt > 0U)
  137. {
  138. /* Initialize maxVal to next consecutive values one by one */
  139. maxVal = *pSrc++;
  140. /* compare for the maximum value */
  141. if (out < maxVal)
  142. {
  143. /* Update the maximum value and it's index */
  144. out = maxVal;
  145. outIndex = index + 1U;
  146. }
  147. maxVal = *pSrc++;
  148. if (out < maxVal)
  149. {
  150. out = maxVal;
  151. outIndex = index + 2U;
  152. }
  153. maxVal = *pSrc++;
  154. if (out < maxVal)
  155. {
  156. out = maxVal;
  157. outIndex = index + 3U;
  158. }
  159. maxVal = *pSrc++;
  160. if (out < maxVal)
  161. {
  162. out = maxVal;
  163. outIndex = index + 4U;
  164. }
  165. index += 4U;
  166. /* Decrement loop counter */
  167. blkCnt--;
  168. }
  169. /* Loop unrolling: Compute remaining outputs */
  170. blkCnt = (blockSize - 1U) % 4U;
  171. #else
  172. /* Initialize blkCnt with number of samples */
  173. blkCnt = (blockSize - 1U);
  174. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  175. while (blkCnt > 0U)
  176. {
  177. /* Initialize maxVal to the next consecutive values one by one */
  178. maxVal = *pSrc++;
  179. /* compare for the maximum value */
  180. if (out < maxVal)
  181. {
  182. /* Update the maximum value and it's index */
  183. out = maxVal;
  184. outIndex = blockSize - blkCnt;
  185. }
  186. /* Decrement loop counter */
  187. blkCnt--;
  188. }
  189. /* Store the maximum value and it's index into destination pointers */
  190. *pResult = out;
  191. *pIndex = outIndex;
  192. }
  193. #endif /* defined(ARM_MATH_MVEI) */
  194. /**
  195. @} end of Max group
  196. */