arm_min_q31.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_min_q31.c
  4. * Description: Minimum value of a Q31 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 Min
  34. @{
  35. */
  36. /**
  37. @brief Minimum value of a Q31 vector.
  38. @param[in] pSrc points to the input vector
  39. @param[in] blockSize number of samples in input vector
  40. @param[out] pResult minimum value returned here
  41. @param[out] pIndex index of minimum value returned here
  42. */
  43. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. void arm_min_q31(
  46. const q31_t * pSrc,
  47. uint32_t blockSize,
  48. q31_t * pResult,
  49. uint32_t * pIndex)
  50. {
  51. int32_t blkCnt; /* loop counters */
  52. q31x4_t extremValVec = vdupq_n_s32(Q31_MAX);
  53. q31_t minValue = Q31_MAX;
  54. uint32x4_t indexVec;
  55. uint32x4_t extremIdxVec;
  56. mve_pred16_t p0;
  57. uint32_t extremIdxArr[4];
  58. indexVec = vidupq_u32(0U, 1);
  59. blkCnt = blockSize;
  60. do {
  61. mve_pred16_t p = vctp32q(blkCnt);
  62. q31x4_t extremIdxVal = vld1q_z_s32(pSrc, p);
  63. /*
  64. * Get current min per lane and current index per lane
  65. * when a min is selected
  66. */
  67. p0 = vcmpleq_m(extremIdxVal, extremValVec, p);
  68. extremValVec = vorrq_m(extremValVec, extremIdxVal, extremIdxVal, p0);
  69. /* store per-lane extrema indexes */
  70. vst1q_p_u32(extremIdxArr, indexVec, p0);
  71. indexVec += 4;
  72. pSrc += 4;
  73. blkCnt -= 4;
  74. }
  75. while (blkCnt > 0);
  76. /* Get min value across the vector */
  77. minValue = vminvq(minValue, extremValVec);
  78. /* set index for lower values to min possible index */
  79. p0 = vcmpleq(extremValVec, minValue);
  80. extremIdxVec = vld1q_u32(extremIdxArr);
  81. indexVec = vpselq(extremIdxVec, vdupq_n_u32(blockSize - 1), p0);
  82. *pIndex = vminvq(blockSize - 1, indexVec);
  83. *pResult = minValue;
  84. }
  85. #else
  86. void arm_min_q31(
  87. const q31_t * pSrc,
  88. uint32_t blockSize,
  89. q31_t * pResult,
  90. uint32_t * pIndex)
  91. {
  92. q31_t minVal, 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 minVal to next consecutive values one by one */
  109. minVal = *pSrc++;
  110. /* compare for the minimum value */
  111. if (out > minVal)
  112. {
  113. /* Update the minimum value and it's index */
  114. out = minVal;
  115. outIndex = index + 1U;
  116. }
  117. minVal = *pSrc++;
  118. if (out > minVal)
  119. {
  120. out = minVal;
  121. outIndex = index + 2U;
  122. }
  123. minVal = *pSrc++;
  124. if (out > minVal)
  125. {
  126. out = minVal;
  127. outIndex = index + 3U;
  128. }
  129. minVal = *pSrc++;
  130. if (out > minVal)
  131. {
  132. out = minVal;
  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 minVal to the next consecutive values one by one */
  148. minVal = *pSrc++;
  149. /* compare for the minimum value */
  150. if (out > minVal)
  151. {
  152. /* Update the minimum value and it's index */
  153. out = minVal;
  154. outIndex = blockSize - blkCnt;
  155. }
  156. /* Decrement loop counter */
  157. blkCnt--;
  158. }
  159. /* Store the minimum 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 Min group
  166. */