arm_min_q31.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_min_q31.c
  4. * Description: Minimum value of a Q31 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 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. @return none
  43. */
  44. #if defined(ARM_MATH_MVEI)
  45. #include "arm_helium_utils.h"
  46. void arm_min_q31(
  47. const q31_t * pSrc,
  48. uint32_t blockSize,
  49. q31_t * pResult,
  50. uint32_t * pIndex)
  51. {
  52. uint32_t blkCnt; /* loop counters */
  53. q31x4_t vecSrc;
  54. q31x4_t curExtremValVec = vdupq_n_s32(Q31_MAX);
  55. q31_t minValue = Q31_MAX, temp;
  56. uint32_t idx = blockSize;
  57. uint32x4_t indexVec;
  58. uint32x4_t curExtremIdxVec;
  59. mve_pred16_t p0;
  60. indexVec = vidupq_u32((uint32_t)0, 1);
  61. curExtremIdxVec = vdupq_n_u32(0);
  62. /* Compute 4 outputs at a time */
  63. blkCnt = blockSize >> 2U;
  64. while (blkCnt > 0U)
  65. {
  66. vecSrc = vldrwq_s32(pSrc);
  67. pSrc += 4;
  68. /*
  69. * Get current min per lane and current index per lane
  70. * when a min is selected
  71. */
  72. p0 = vcmpleq(vecSrc, curExtremValVec);
  73. curExtremValVec = vpselq(vecSrc, curExtremValVec, p0);
  74. curExtremIdxVec = vpselq(indexVec, curExtremIdxVec, p0);
  75. indexVec = indexVec + 4;
  76. /*
  77. * Decrement the blockSize loop counter
  78. */
  79. blkCnt--;
  80. }
  81. /*
  82. * Get min value across the vector
  83. */
  84. minValue = vminvq(minValue, curExtremValVec);
  85. /*
  86. * set index for lower values to min possible index
  87. */
  88. p0 = vcmpleq(curExtremValVec, minValue);
  89. indexVec = vpselq(curExtremIdxVec, vdupq_n_u32(blockSize), p0);
  90. /*
  91. * Get min index which is thus for a min value
  92. */
  93. idx = vminvq(idx, indexVec);
  94. /* Tail */
  95. blkCnt = blockSize & 0x3;
  96. while (blkCnt > 0U)
  97. {
  98. /* Initialize temp to the next consecutive values one by one */
  99. temp = *pSrc++;
  100. /* compare for the minimum value */
  101. if (minValue > temp)
  102. {
  103. /* Update the minimum value and it's index */
  104. minValue = temp;
  105. idx = blockSize - blkCnt;
  106. }
  107. /* Decrement loop counter */
  108. blkCnt--;
  109. }
  110. /*
  111. * Save result
  112. */
  113. *pIndex = idx;
  114. *pResult = minValue;
  115. }
  116. #else
  117. void arm_min_q31(
  118. const q31_t * pSrc,
  119. uint32_t blockSize,
  120. q31_t * pResult,
  121. uint32_t * pIndex)
  122. {
  123. q31_t minVal, out; /* Temporary variables to store the output value. */
  124. uint32_t blkCnt, outIndex; /* Loop counter */
  125. #if defined (ARM_MATH_LOOPUNROLL)
  126. uint32_t index; /* index of maximum value */
  127. #endif
  128. /* Initialise index value to zero. */
  129. outIndex = 0U;
  130. /* Load first input value that act as reference value for comparision */
  131. out = *pSrc++;
  132. #if defined (ARM_MATH_LOOPUNROLL)
  133. /* Initialise index of maximum value. */
  134. index = 0U;
  135. /* Loop unrolling: Compute 4 outputs at a time */
  136. blkCnt = (blockSize - 1U) >> 2U;
  137. while (blkCnt > 0U)
  138. {
  139. /* Initialize minVal to next consecutive values one by one */
  140. minVal = *pSrc++;
  141. /* compare for the minimum value */
  142. if (out > minVal)
  143. {
  144. /* Update the minimum value and it's index */
  145. out = minVal;
  146. outIndex = index + 1U;
  147. }
  148. minVal = *pSrc++;
  149. if (out > minVal)
  150. {
  151. out = minVal;
  152. outIndex = index + 2U;
  153. }
  154. minVal = *pSrc++;
  155. if (out > minVal)
  156. {
  157. out = minVal;
  158. outIndex = index + 3U;
  159. }
  160. minVal = *pSrc++;
  161. if (out > minVal)
  162. {
  163. out = minVal;
  164. outIndex = index + 4U;
  165. }
  166. index += 4U;
  167. /* Decrement loop counter */
  168. blkCnt--;
  169. }
  170. /* Loop unrolling: Compute remaining outputs */
  171. blkCnt = (blockSize - 1U) % 4U;
  172. #else
  173. /* Initialize blkCnt with number of samples */
  174. blkCnt = (blockSize - 1U);
  175. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  176. while (blkCnt > 0U)
  177. {
  178. /* Initialize minVal to the next consecutive values one by one */
  179. minVal = *pSrc++;
  180. /* compare for the minimum value */
  181. if (out > minVal)
  182. {
  183. /* Update the minimum value and it's index */
  184. out = minVal;
  185. outIndex = blockSize - blkCnt;
  186. }
  187. /* Decrement loop counter */
  188. blkCnt--;
  189. }
  190. /* Store the minimum value and it's index into destination pointers */
  191. *pResult = out;
  192. *pIndex = outIndex;
  193. }
  194. #endif /* defined(ARM_MATH_MVEI) */
  195. /**
  196. @} end of Min group
  197. */