arm_max_f32.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 12. March 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_max_f32.c
  9. *
  10. * Description: Maximum value of a floating-point vector.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * - Neither the name of ARM LIMITED nor the names of its contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. * ---------------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. /**
  42. * @ingroup groupStats
  43. */
  44. /**
  45. * @defgroup Max Maximum
  46. *
  47. * Computes the maximum value of an array of data.
  48. * The function returns both the maximum value and its position within the array.
  49. * There are separate functions for floating-point, Q31, Q15, and Q7 data types.
  50. */
  51. /**
  52. * @addtogroup Max
  53. * @{
  54. */
  55. /**
  56. * @brief Maximum value of a floating-point vector.
  57. * @param[in] *pSrc points to the input vector
  58. * @param[in] blockSize length of the input vector
  59. * @param[out] *pResult maximum value returned here
  60. * @param[out] *pIndex index of maximum value returned here
  61. * @return none.
  62. */
  63. void arm_max_f32(
  64. float32_t * pSrc,
  65. uint32_t blockSize,
  66. float32_t * pResult,
  67. uint32_t * pIndex)
  68. {
  69. #ifndef ARM_MATH_CM0_FAMILY
  70. /* Run the below code for Cortex-M4 and Cortex-M3 */
  71. float32_t maxVal1, maxVal2, out; /* Temporary variables to store the output value. */
  72. uint32_t blkCnt, outIndex, count; /* loop counter */
  73. /* Initialise the count value. */
  74. count = 0u;
  75. /* Initialise the index value to zero. */
  76. outIndex = 0u;
  77. /* Load first input value that act as reference value for comparision */
  78. out = *pSrc++;
  79. /* Loop unrolling */
  80. blkCnt = (blockSize - 1u) >> 2u;
  81. /* Run the below code for Cortex-M4 and Cortex-M3 */
  82. while(blkCnt > 0u)
  83. {
  84. /* Initialize maxVal to the next consecutive values one by one */
  85. maxVal1 = *pSrc++;
  86. maxVal2 = *pSrc++;
  87. /* compare for the maximum value */
  88. if(out < maxVal1)
  89. {
  90. /* Update the maximum value and its index */
  91. out = maxVal1;
  92. outIndex = count + 1u;
  93. }
  94. maxVal1 = *pSrc++;
  95. /* compare for the maximum value */
  96. if(out < maxVal2)
  97. {
  98. /* Update the maximum value and its index */
  99. out = maxVal2;
  100. outIndex = count + 2u;
  101. }
  102. maxVal2 = *pSrc++;
  103. /* compare for the maximum value */
  104. if(out < maxVal1)
  105. {
  106. /* Update the maximum value and its index */
  107. out = maxVal1;
  108. outIndex = count + 3u;
  109. }
  110. /* compare for the maximum value */
  111. if(out < maxVal2)
  112. {
  113. /* Update the maximum value and its index */
  114. out = maxVal2;
  115. outIndex = count + 4u;
  116. }
  117. count += 4u;
  118. /* Decrement the loop counter */
  119. blkCnt--;
  120. }
  121. /* if (blockSize - 1u) is not multiple of 4 */
  122. blkCnt = (blockSize - 1u) % 4u;
  123. #else
  124. /* Run the below code for Cortex-M0 */
  125. float32_t maxVal1, out; /* Temporary variables to store the output value. */
  126. uint32_t blkCnt, outIndex; /* loop counter */
  127. /* Initialise the index value to zero. */
  128. outIndex = 0u;
  129. /* Load first input value that act as reference value for comparision */
  130. out = *pSrc++;
  131. blkCnt = (blockSize - 1u);
  132. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  133. while(blkCnt > 0u)
  134. {
  135. /* Initialize maxVal to the next consecutive values one by one */
  136. maxVal1 = *pSrc++;
  137. /* compare for the maximum value */
  138. if(out < maxVal1)
  139. {
  140. /* Update the maximum value and it's index */
  141. out = maxVal1;
  142. outIndex = blockSize - blkCnt;
  143. }
  144. /* Decrement the loop counter */
  145. blkCnt--;
  146. }
  147. /* Store the maximum value and it's index into destination pointers */
  148. *pResult = out;
  149. *pIndex = outIndex;
  150. }
  151. /**
  152. * @} end of Max group
  153. */