arm_min_q15.c 5.7 KB

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