arm_max_q7.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_max_q7.c
  4. * Description: Maximum value of a Q7 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 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. @return none
  43. */
  44. void arm_max_q7(
  45. const q7_t * pSrc,
  46. uint32_t blockSize,
  47. q7_t * pResult,
  48. uint32_t * pIndex)
  49. {
  50. q7_t maxVal, out; /* Temporary variables to store the output value. */
  51. uint32_t blkCnt, outIndex; /* Loop counter */
  52. #if defined (ARM_MATH_LOOPUNROLL)
  53. uint32_t index; /* index of maximum value */
  54. #endif
  55. /* Initialise index value to zero. */
  56. outIndex = 0U;
  57. /* Load first input value that act as reference value for comparision */
  58. out = *pSrc++;
  59. #if defined (ARM_MATH_LOOPUNROLL)
  60. /* Initialise index of maximum value. */
  61. index = 0U;
  62. /* Loop unrolling: Compute 4 outputs at a time */
  63. blkCnt = (blockSize - 1U) >> 2U;
  64. while (blkCnt > 0U)
  65. {
  66. /* Initialize maxVal to next consecutive values one by one */
  67. maxVal = *pSrc++;
  68. /* compare for the maximum value */
  69. if (out < maxVal)
  70. {
  71. /* Update the maximum value and it's index */
  72. out = maxVal;
  73. outIndex = index + 1U;
  74. }
  75. maxVal = *pSrc++;
  76. if (out < maxVal)
  77. {
  78. out = maxVal;
  79. outIndex = index + 2U;
  80. }
  81. maxVal = *pSrc++;
  82. if (out < maxVal)
  83. {
  84. out = maxVal;
  85. outIndex = index + 3U;
  86. }
  87. maxVal = *pSrc++;
  88. if (out < maxVal)
  89. {
  90. out = maxVal;
  91. outIndex = index + 4U;
  92. }
  93. index += 4U;
  94. /* Decrement loop counter */
  95. blkCnt--;
  96. }
  97. /* Loop unrolling: Compute remaining outputs */
  98. blkCnt = (blockSize - 1U) % 4U;
  99. #else
  100. /* Initialize blkCnt with number of samples */
  101. blkCnt = (blockSize - 1U);
  102. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  103. while (blkCnt > 0U)
  104. {
  105. /* Initialize maxVal to the next consecutive values one by one */
  106. maxVal = *pSrc++;
  107. /* compare for the maximum value */
  108. if (out < maxVal)
  109. {
  110. /* Update the maximum value and it's index */
  111. out = maxVal;
  112. outIndex = blockSize - blkCnt;
  113. }
  114. /* Decrement loop counter */
  115. blkCnt--;
  116. }
  117. /* Store the maximum value and it's index into destination pointers */
  118. *pResult = out;
  119. *pIndex = outIndex;
  120. }
  121. /**
  122. @} end of Max group
  123. */