arm_min_q7.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_min_q7.c
  4. * Description: Minimum value of a Q7 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 Q7 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. static void arm_small_blk_min_q7(
  47. const q7_t * pSrc,
  48. uint8_t blockSize,
  49. q7_t * pResult,
  50. uint32_t * pIndex)
  51. {
  52. uint32_t blkCnt; /* loop counters */
  53. q7x16_t vecSrc;
  54. q7x16_t curExtremValVec = vdupq_n_s8(Q7_MAX);
  55. q7_t minValue = Q7_MAX,temp;
  56. uint32_t idx = blockSize;
  57. uint8x16_t indexVec;
  58. uint8x16_t curExtremIdxVec;
  59. mve_pred16_t p0;
  60. indexVec = vidupq_u8((uint32_t)0, 1);
  61. curExtremIdxVec = vdupq_n_u8(0);
  62. blkCnt = blockSize >> 4;
  63. while (blkCnt > 0U)
  64. {
  65. vecSrc = vldrbq_s8(pSrc);
  66. pSrc += 16;
  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 + 16;
  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_u8(blockSize), p0);
  89. /*
  90. * Get min index which is thus for a min value
  91. */
  92. idx = vminvq(idx, indexVec);
  93. blkCnt = blockSize & 0xF;
  94. while (blkCnt > 0U)
  95. {
  96. /* Initialize minVal to the next consecutive values one by one */
  97. temp = *pSrc++;
  98. /* compare for the minimum value */
  99. if (minValue > temp)
  100. {
  101. /* Update the minimum value and it's index */
  102. minValue = temp;
  103. idx = blockSize - blkCnt;
  104. }
  105. /* Decrement loop counter */
  106. blkCnt--;
  107. }
  108. /*
  109. * Save result
  110. */
  111. *pIndex = idx;
  112. *pResult = minValue;
  113. }
  114. void arm_min_q7(
  115. const q7_t * pSrc,
  116. uint32_t blockSize,
  117. q7_t * pResult,
  118. uint32_t * pIndex)
  119. {
  120. int32_t totalSize = blockSize;
  121. if (totalSize <= UINT8_MAX)
  122. {
  123. arm_small_blk_min_q7(pSrc, blockSize, pResult, pIndex);
  124. }
  125. else
  126. {
  127. uint32_t curIdx = 0;
  128. q7_t curBlkExtr = Q7_MAX;
  129. uint32_t curBlkPos = 0;
  130. uint32_t curBlkIdx = 0;
  131. /*
  132. * process blocks of 255 elts
  133. */
  134. while (totalSize >= UINT8_MAX)
  135. {
  136. const q7_t *curSrc = pSrc;
  137. arm_small_blk_min_q7(curSrc, UINT8_MAX, pResult, pIndex);
  138. if (*pResult < curBlkExtr)
  139. {
  140. /*
  141. * update partial extrema
  142. */
  143. curBlkExtr = *pResult;
  144. curBlkPos = *pIndex;
  145. curBlkIdx = curIdx;
  146. }
  147. curIdx++;
  148. pSrc += UINT8_MAX;
  149. totalSize -= UINT8_MAX;
  150. }
  151. /*
  152. * remainder
  153. */
  154. arm_small_blk_min_q7(pSrc, totalSize, pResult, pIndex);
  155. if (*pResult < curBlkExtr)
  156. {
  157. curBlkExtr = *pResult;
  158. curBlkPos = *pIndex;
  159. curBlkIdx = curIdx;
  160. }
  161. *pIndex = curBlkIdx * UINT8_MAX + curBlkPos;
  162. *pResult = curBlkExtr;
  163. }
  164. }
  165. #else
  166. void arm_min_q7(
  167. const q7_t * pSrc,
  168. uint32_t blockSize,
  169. q7_t * pResult,
  170. uint32_t * pIndex)
  171. {
  172. q7_t minVal, out; /* Temporary variables to store the output value. */
  173. uint32_t blkCnt, outIndex; /* Loop counter */
  174. #if defined (ARM_MATH_LOOPUNROLL)
  175. uint32_t index; /* index of maximum value */
  176. #endif
  177. /* Initialise index value to zero. */
  178. outIndex = 0U;
  179. /* Load first input value that act as reference value for comparision */
  180. out = *pSrc++;
  181. #if defined (ARM_MATH_LOOPUNROLL)
  182. /* Initialise index of maximum value. */
  183. index = 0U;
  184. /* Loop unrolling: Compute 4 outputs at a time */
  185. blkCnt = (blockSize - 1U) >> 2U;
  186. while (blkCnt > 0U)
  187. {
  188. /* Initialize minVal to next consecutive values one by one */
  189. minVal = *pSrc++;
  190. /* compare for the minimum value */
  191. if (out > minVal)
  192. {
  193. /* Update the minimum value and it's index */
  194. out = minVal;
  195. outIndex = index + 1U;
  196. }
  197. minVal = *pSrc++;
  198. if (out > minVal)
  199. {
  200. out = minVal;
  201. outIndex = index + 2U;
  202. }
  203. minVal = *pSrc++;
  204. if (out > minVal)
  205. {
  206. out = minVal;
  207. outIndex = index + 3U;
  208. }
  209. minVal = *pSrc++;
  210. if (out > minVal)
  211. {
  212. out = minVal;
  213. outIndex = index + 4U;
  214. }
  215. index += 4U;
  216. /* Decrement loop counter */
  217. blkCnt--;
  218. }
  219. /* Loop unrolling: Compute remaining outputs */
  220. blkCnt = (blockSize - 1U) % 4U;
  221. #else
  222. /* Initialize blkCnt with number of samples */
  223. blkCnt = (blockSize - 1U);
  224. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  225. while (blkCnt > 0U)
  226. {
  227. /* Initialize minVal to the next consecutive values one by one */
  228. minVal = *pSrc++;
  229. /* compare for the minimum value */
  230. if (out > minVal)
  231. {
  232. /* Update the minimum value and it's index */
  233. out = minVal;
  234. outIndex = blockSize - blkCnt;
  235. }
  236. /* Decrement loop counter */
  237. blkCnt--;
  238. }
  239. /* Store the minimum value and it's index into destination pointers */
  240. *pResult = out;
  241. *pIndex = outIndex;
  242. }
  243. #endif /* defined(ARM_MATH_MVEI) */
  244. /**
  245. @} end of Min group
  246. */