arm_absmax_q7.c 15 KB

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