arm_absmax_q31.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_absmax_q31.c
  4. * Description: Maximum value of absolute values of a Q31 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 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 maximum value returned here
  41. @param[out] pIndex index of maximum value returned here
  42. */
  43. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  44. #include "arm_helium_utils.h"
  45. void arm_absmax_q31(
  46. const q31_t * pSrc,
  47. uint32_t blockSize,
  48. q31_t * pResult,
  49. uint32_t * pIndex)
  50. {
  51. int32_t blkCnt; /* loop counters */
  52. q31x4_t extremValVec = vdupq_n_s32(Q31_ABSMIN);
  53. q31_t maxValue = Q31_ABSMIN;
  54. uint32x4_t indexVec;
  55. uint32x4_t extremIdxVec;
  56. mve_pred16_t p0;
  57. uint32_t extremIdxArr[4];
  58. indexVec = vidupq_u32(0U, 1);
  59. blkCnt = blockSize;
  60. do {
  61. mve_pred16_t p = vctp32q(blkCnt);
  62. q31x4_t extremIdxVal = vld1q_z_s32(pSrc, p);
  63. extremIdxVal = vqabsq(extremIdxVal);
  64. /*
  65. * Get current max per lane and current index per lane
  66. * when a max is selected
  67. */
  68. p0 = vcmpgtq_m(extremIdxVal, extremValVec, p);
  69. extremValVec = vorrq_m(extremValVec, extremIdxVal, extremIdxVal, p0);
  70. /* store per-lane extrema indexes */
  71. vst1q_p_u32(extremIdxArr, indexVec, p0);
  72. indexVec += 4;
  73. pSrc += 4;
  74. blkCnt -= 4;
  75. }
  76. while (blkCnt > 0);
  77. /* Get max value across the vector */
  78. maxValue = vmaxvq(maxValue, extremValVec);
  79. /* set index for lower values to max possible index */
  80. p0 = vcmpgeq(extremValVec, maxValue);
  81. extremIdxVec = vld1q_u32(extremIdxArr);
  82. indexVec = vpselq(extremIdxVec, vdupq_n_u32(blockSize - 1), p0);
  83. *pIndex = vminvq(blockSize - 1, indexVec);
  84. *pResult = maxValue;
  85. }
  86. #else
  87. #if defined(ARM_MATH_DSP)
  88. void arm_absmax_q31(
  89. const q31_t * pSrc,
  90. uint32_t blockSize,
  91. q31_t * pResult,
  92. uint32_t * pIndex)
  93. {
  94. q31_t cur_absmax, out; /* Temporary variables to store the output value. */\
  95. uint32_t blkCnt, outIndex; /* Loop counter */ \
  96. uint32_t index; /* index of maximum value */ \
  97. \
  98. /* Initialize index value to zero. */ \
  99. outIndex = 0U; \
  100. /* Load first input value that act as reference value for comparision */ \
  101. out = *pSrc++; \
  102. out = (out > 0) ? out : (q31_t)__QSUB(0, out); \
  103. /* Initialize index of extrema value. */ \
  104. index = 0U; \
  105. \
  106. /* Loop unrolling: Compute 4 outputs at a time */ \
  107. blkCnt = (blockSize - 1U) >> 2U; \
  108. \
  109. while (blkCnt > 0U) \
  110. { \
  111. /* Initialize cur_absmax to next consecutive values one by one */ \
  112. cur_absmax = *pSrc++; \
  113. cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax); \
  114. /* compare for the extrema value */ \
  115. if (cur_absmax > out) \
  116. { \
  117. /* Update the extrema value and it's index */ \
  118. out = cur_absmax; \
  119. outIndex = index + 1U; \
  120. } \
  121. \
  122. cur_absmax = *pSrc++; \
  123. cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax); \
  124. if (cur_absmax > out) \
  125. { \
  126. out = cur_absmax; \
  127. outIndex = index + 2U; \
  128. } \
  129. \
  130. cur_absmax = *pSrc++; \
  131. cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax); \
  132. if (cur_absmax > out) \
  133. { \
  134. out = cur_absmax; \
  135. outIndex = index + 3U; \
  136. } \
  137. \
  138. cur_absmax = *pSrc++; \
  139. cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax); \
  140. if (cur_absmax > out) \
  141. { \
  142. out = cur_absmax; \
  143. outIndex = index + 4U; \
  144. } \
  145. \
  146. index += 4U; \
  147. \
  148. /* Decrement loop counter */ \
  149. blkCnt--; \
  150. } \
  151. \
  152. /* Loop unrolling: Compute remaining outputs */ \
  153. blkCnt = (blockSize - 1U) % 4U; \
  154. \
  155. \
  156. while (blkCnt > 0U) \
  157. { \
  158. cur_absmax = *pSrc++; \
  159. cur_absmax = (cur_absmax > 0) ? cur_absmax : (q31_t)__QSUB(0, cur_absmax); \
  160. if (cur_absmax > out) \
  161. { \
  162. out = cur_absmax; \
  163. outIndex = blockSize - blkCnt; \
  164. } \
  165. \
  166. /* Decrement loop counter */ \
  167. blkCnt--; \
  168. } \
  169. \
  170. /* Store the extrema value and it's index into destination pointers */ \
  171. *pResult = out; \
  172. *pIndex = outIndex;
  173. }
  174. #else
  175. void arm_absmax_q31(
  176. const q31_t * pSrc,
  177. uint32_t blockSize,
  178. q31_t * pResult,
  179. uint32_t * pIndex)
  180. {
  181. q31_t maxVal, out; /* Temporary variables to store the output value. */
  182. uint32_t blkCnt, outIndex; /* Loop counter */
  183. /* Initialise index value to zero. */
  184. outIndex = 0U;
  185. /* Load first input value that act as reference value for comparision */
  186. out = (*pSrc > 0) ? *pSrc : ((*pSrc == INT32_MIN) ? INT32_MAX : -*pSrc);
  187. pSrc++;
  188. /* Initialize blkCnt with number of samples */
  189. blkCnt = (blockSize - 1U);
  190. while (blkCnt > 0U)
  191. {
  192. /* Initialize maxVal to the next consecutive values one by one */
  193. maxVal = (*pSrc > 0) ? *pSrc : ((*pSrc == INT32_MIN) ? INT32_MAX : -*pSrc);
  194. pSrc++;
  195. /* compare for the maximum value */
  196. if (out < maxVal)
  197. {
  198. /* Update the maximum value and it's index */
  199. out = maxVal;
  200. outIndex = blockSize - blkCnt;
  201. }
  202. /* Decrement loop counter */
  203. blkCnt--;
  204. }
  205. /* Store the maximum value and it's index into destination pointers */
  206. *pResult = out;
  207. *pIndex = outIndex;
  208. }
  209. #endif /* defined(ARM_MATH_DSP) */
  210. #endif /* defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE) */
  211. /**
  212. @} end of AbsMax group
  213. */