arm_max_f32.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_max_f32.c
  4. * Description: Maximum value of a floating-point vector
  5. *
  6. * $Date: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 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. * @defgroup Max Maximum
  34. *
  35. * Computes the maximum value of an array of data.
  36. * The function returns both the maximum value and its position within the array.
  37. * There are separate functions for floating-point, Q31, Q15, and Q7 data types.
  38. */
  39. /**
  40. * @addtogroup Max
  41. * @{
  42. */
  43. /**
  44. * @brief Maximum value of a floating-point vector.
  45. * @param[in] *pSrc points to the input vector
  46. * @param[in] blockSize length of the input vector
  47. * @param[out] *pResult maximum value returned here
  48. * @param[out] *pIndex index of maximum value returned here
  49. * @return none.
  50. */
  51. void arm_max_f32(
  52. float32_t * pSrc,
  53. uint32_t blockSize,
  54. float32_t * pResult,
  55. uint32_t * pIndex)
  56. {
  57. #if defined (ARM_MATH_DSP)
  58. /* Run the below code for Cortex-M4 and Cortex-M3 */
  59. float32_t maxVal1, maxVal2, out; /* Temporary variables to store the output value. */
  60. uint32_t blkCnt, outIndex, count; /* loop counter */
  61. /* Initialise the count value. */
  62. count = 0U;
  63. /* Initialise the index value to zero. */
  64. outIndex = 0U;
  65. /* Load first input value that act as reference value for comparision */
  66. out = *pSrc++;
  67. /* Loop unrolling */
  68. blkCnt = (blockSize - 1U) >> 2U;
  69. while (blkCnt > 0U)
  70. {
  71. /* Initialize maxVal to the next consecutive values one by one */
  72. maxVal1 = *pSrc++;
  73. maxVal2 = *pSrc++;
  74. /* compare for the maximum value */
  75. if (out < maxVal1)
  76. {
  77. /* Update the maximum value and its index */
  78. out = maxVal1;
  79. outIndex = count + 1U;
  80. }
  81. /* compare for the maximum value */
  82. if (out < maxVal2)
  83. {
  84. /* Update the maximum value and its index */
  85. out = maxVal2;
  86. outIndex = count + 2U;
  87. }
  88. /* Initialize maxVal to the next consecutive values one by one */
  89. maxVal1 = *pSrc++;
  90. maxVal2 = *pSrc++;
  91. /* compare for the maximum value */
  92. if (out < maxVal1)
  93. {
  94. /* Update the maximum value and its index */
  95. out = maxVal1;
  96. outIndex = count + 3U;
  97. }
  98. /* compare for the maximum value */
  99. if (out < maxVal2)
  100. {
  101. /* Update the maximum value and its index */
  102. out = maxVal2;
  103. outIndex = count + 4U;
  104. }
  105. count += 4U;
  106. /* Decrement the loop counter */
  107. blkCnt--;
  108. }
  109. /* if (blockSize - 1U) is not multiple of 4 */
  110. blkCnt = (blockSize - 1U) % 4U;
  111. #else
  112. /* Run the below code for Cortex-M0 */
  113. float32_t maxVal1, out; /* Temporary variables to store the output value. */
  114. uint32_t blkCnt, outIndex; /* loop counter */
  115. /* Initialise the index value to zero. */
  116. outIndex = 0U;
  117. /* Load first input value that act as reference value for comparision */
  118. out = *pSrc++;
  119. blkCnt = (blockSize - 1U);
  120. #endif /* #if defined (ARM_MATH_DSP) */
  121. while (blkCnt > 0U)
  122. {
  123. /* Initialize maxVal to the next consecutive values one by one */
  124. maxVal1 = *pSrc++;
  125. /* compare for the maximum value */
  126. if (out < maxVal1)
  127. {
  128. /* Update the maximum value and it's index */
  129. out = maxVal1;
  130. outIndex = blockSize - blkCnt;
  131. }
  132. /* Decrement the loop counter */
  133. blkCnt--;
  134. }
  135. /* Store the maximum value and it's index into destination pointers */
  136. *pResult = out;
  137. *pIndex = outIndex;
  138. }
  139. /**
  140. * @} end of Max group
  141. */