arm_absmax_q31.c 13 KB

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