arm_absmax_q7.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_absmax_q7.c
  4. * Description: Maximum value of absolute values of a Q7 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 AbsMax
  34. @{
  35. */
  36. /**
  37. @brief Maximum value of absolute values 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 maximum value returned here
  41. @param[out] pIndex index of maximum value returned here
  42. @return none
  43. */
  44. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  45. #include <stdint.h>
  46. #include "arm_helium_utils.h"
  47. #define MAX_BLKSZ_S8 (UINT8_MAX+1)
  48. static void arm_small_blk_absmax_q7(
  49. const q7_t * pSrc,
  50. uint16_t blockSize,
  51. q7_t * pResult,
  52. uint32_t * pIndex)
  53. {
  54. int32_t blkCnt; /* loop counters */
  55. q7x16_t extremValVec = vdupq_n_s8(Q7_ABSMIN);
  56. q7_t maxValue = Q7_ABSMIN;
  57. uint8x16_t indexVec;
  58. uint8x16_t extremIdxVec;
  59. mve_pred16_t p0;
  60. uint8_t extremIdxArr[16];
  61. indexVec = vidupq_u8(0U, 1);
  62. blkCnt = blockSize;
  63. do {
  64. mve_pred16_t p = vctp8q(blkCnt);
  65. q7x16_t extremIdxVal = vld1q_z_s8(pSrc, p);
  66. extremIdxVal = vqabsq(extremIdxVal);
  67. /*
  68. * Get current max per lane and current index per lane
  69. * when a max is selected
  70. */
  71. p0 = vcmpgeq_m(extremIdxVal, extremValVec, p);
  72. extremValVec = vorrq_m(extremValVec, extremIdxVal, extremIdxVal, p0);
  73. /* store per-lane extrema indexes */
  74. vst1q_p_u8(extremIdxArr, indexVec, p0);
  75. indexVec += 16;
  76. pSrc += 16;
  77. blkCnt -= 16;
  78. }
  79. while (blkCnt > 0);
  80. /* Get max value across the vector */
  81. maxValue = vmaxvq(maxValue, extremValVec);
  82. /* set index for lower values to max possible index */
  83. p0 = vcmpgeq(extremValVec, maxValue);
  84. extremIdxVec = vld1q_u8(extremIdxArr);
  85. indexVec = vpselq(extremIdxVec, vdupq_n_u8(blockSize - 1), p0);
  86. *pIndex = vminvq_u8(blockSize - 1, indexVec);
  87. *pResult = maxValue;
  88. }
  89. void arm_absmax_q7(
  90. const q7_t * pSrc,
  91. uint32_t blockSize,
  92. q7_t * pResult,
  93. uint32_t * pIndex)
  94. {
  95. int32_t totalSize = blockSize;
  96. if (totalSize <= MAX_BLKSZ_S8)
  97. {
  98. arm_small_blk_absmax_q7(pSrc, blockSize, pResult, pIndex);
  99. }
  100. else
  101. {
  102. uint32_t curIdx = 0;
  103. q7_t curBlkExtr = Q7_MIN;
  104. uint32_t curBlkPos = 0;
  105. uint32_t curBlkIdx = 0;
  106. /*
  107. * process blocks of 255 elts
  108. */
  109. while (totalSize >= MAX_BLKSZ_S8)
  110. {
  111. const q7_t *curSrc = pSrc;
  112. arm_small_blk_absmax_q7(curSrc, MAX_BLKSZ_S8, pResult, pIndex);
  113. if (*pResult > curBlkExtr)
  114. {
  115. /*
  116. * update partial extrema
  117. */
  118. curBlkExtr = *pResult;
  119. curBlkPos = *pIndex;
  120. curBlkIdx = curIdx;
  121. }
  122. curIdx++;
  123. pSrc += MAX_BLKSZ_S8;
  124. totalSize -= MAX_BLKSZ_S8;
  125. }
  126. /*
  127. * remainder
  128. */
  129. arm_small_blk_absmax_q7(pSrc, totalSize, pResult, pIndex);
  130. if (*pResult > curBlkExtr)
  131. {
  132. curBlkExtr = *pResult;
  133. curBlkPos = *pIndex;
  134. curBlkIdx = curIdx;
  135. }
  136. *pIndex = curBlkIdx * MAX_BLKSZ_S8 + curBlkPos;
  137. *pResult = curBlkExtr;
  138. }
  139. }
  140. #else
  141. #if defined(ARM_MATH_DSP)
  142. void arm_absmax_q7(
  143. const q7_t * pSrc,
  144. uint32_t blockSize,
  145. q7_t * pResult,
  146. uint32_t * pIndex)
  147. {
  148. q7_t cur_absmax, out; /* Temporary variables to store the output value. */\
  149. uint32_t blkCnt, outIndex; /* Loop counter */ \
  150. uint32_t index; /* index of maximum value */ \
  151. \
  152. /* Initialize index value to zero. */ \
  153. outIndex = 0U; \
  154. /* Load first input value that act as reference value for comparision */ \
  155. out = *pSrc++; \
  156. out = (out > 0) ? out : (q7_t)__QSUB8(0, out); \
  157. /* Initialize index of extrema value. */ \
  158. index = 0U; \
  159. \
  160. /* Loop unrolling: Compute 4 outputs at a time */ \
  161. blkCnt = (blockSize - 1U) >> 2U; \
  162. \
  163. while (blkCnt > 0U) \
  164. { \
  165. /* Initialize cur_absmax to next consecutive values one by one */ \
  166. cur_absmax = *pSrc++; \
  167. cur_absmax = (cur_absmax > 0) ? cur_absmax : (q7_t)__QSUB8(0, cur_absmax); \
  168. /* compare for the extrema value */ \
  169. if (cur_absmax > out) \
  170. { \
  171. /* Update the extrema value and it's index */ \
  172. out = cur_absmax; \
  173. outIndex = index + 1U; \
  174. } \
  175. \
  176. cur_absmax = *pSrc++; \
  177. cur_absmax = (cur_absmax > 0) ? cur_absmax : (q7_t)__QSUB8(0, cur_absmax); \
  178. if (cur_absmax > out) \
  179. { \
  180. out = cur_absmax; \
  181. outIndex = index + 2U; \
  182. } \
  183. \
  184. cur_absmax = *pSrc++; \
  185. cur_absmax = (cur_absmax > 0) ? cur_absmax : (q7_t)__QSUB8(0, cur_absmax); \
  186. if (cur_absmax > out) \
  187. { \
  188. out = cur_absmax; \
  189. outIndex = index + 3U; \
  190. } \
  191. \
  192. cur_absmax = *pSrc++; \
  193. cur_absmax = (cur_absmax > 0) ? cur_absmax : (q7_t)__QSUB8(0, cur_absmax); \
  194. if (cur_absmax > out) \
  195. { \
  196. out = cur_absmax; \
  197. outIndex = index + 4U; \
  198. } \
  199. \
  200. index += 4U; \
  201. \
  202. /* Decrement loop counter */ \
  203. blkCnt--; \
  204. } \
  205. \
  206. /* Loop unrolling: Compute remaining outputs */ \
  207. blkCnt = (blockSize - 1U) % 4U; \
  208. \
  209. \
  210. while (blkCnt > 0U) \
  211. { \
  212. cur_absmax = *pSrc++; \
  213. cur_absmax = (cur_absmax > 0) ? cur_absmax : (q7_t)__QSUB8(0, cur_absmax); \
  214. if (cur_absmax > out) \
  215. { \
  216. out = cur_absmax; \
  217. outIndex = blockSize - blkCnt; \
  218. } \
  219. \
  220. /* Decrement loop counter */ \
  221. blkCnt--; \
  222. } \
  223. \
  224. /* Store the extrema value and it's index into destination pointers */ \
  225. *pResult = out; \
  226. *pIndex = outIndex;
  227. }
  228. #else
  229. void arm_absmax_q7(
  230. const q7_t * pSrc,
  231. uint32_t blockSize,
  232. q7_t * pResult,
  233. uint32_t * pIndex)
  234. {
  235. q7_t maxVal, out; /* Temporary variables to store the output value. */
  236. uint32_t blkCnt, outIndex; /* Loop counter */
  237. /* Initialise index value to zero. */
  238. outIndex = 0U;
  239. /* Load first input value that act as reference value for comparision */
  240. out = (*pSrc > 0) ? *pSrc : ((*pSrc == (q7_t) 0x80) ? (q7_t) 0x7f : -*pSrc);
  241. pSrc++;
  242. /* Initialize blkCnt with number of samples */
  243. blkCnt = (blockSize - 1U);
  244. while (blkCnt > 0U)
  245. {
  246. /* Initialize maxVal to the next consecutive values one by one */
  247. maxVal = (*pSrc > 0) ? *pSrc : ((*pSrc == (q7_t) 0x80) ? (q7_t) 0x7f : -*pSrc);
  248. pSrc++;
  249. /* compare for the maximum value */
  250. if (out < maxVal)
  251. {
  252. /* Update the maximum value and it's index */
  253. out = maxVal;
  254. outIndex = blockSize - blkCnt;
  255. }
  256. /* Decrement loop counter */
  257. blkCnt--;
  258. }
  259. /* Store the maximum value and it's index into destination pointers */
  260. *pResult = out;
  261. *pIndex = outIndex;
  262. }
  263. #endif /* defined(ARM_MATH_DSP) */
  264. #endif /* defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE) */
  265. /**
  266. @} end of AbsMax group
  267. */