/ Drivers / CMSIS / DSP / Source / BasicMathFunctions / arm_abs_q31.c
arm_abs_q31.c
  1  /* ----------------------------------------------------------------------
  2   * Project:      CMSIS DSP Library
  3   * Title:        arm_abs_q31.c
  4   * Description:  Q31 vector absolute value
  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  
 29  #include "dsp/basic_math_functions.h"
 30  
 31  /**
 32    @ingroup groupMath
 33   */
 34  
 35  /**
 36    @addtogroup BasicAbs
 37    @{
 38   */
 39  
 40  /**
 41    @brief         Q31 vector absolute value.
 42    @param[in]     pSrc       points to the input vector
 43    @param[out]    pDst       points to the output vector
 44    @param[in]     blockSize  number of samples in each vector
 45    @return        none
 46  
 47    @par           Scaling and Overflow Behavior
 48                     The function uses saturating arithmetic.
 49                     The Q31 value -1 (0x80000000) will be saturated to the maximum allowable positive value 0x7FFFFFFF.
 50   */
 51  
 52  #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
 53  
 54  #include "arm_helium_utils.h"
 55  
 56  void arm_abs_q31(
 57      const q31_t * pSrc,
 58      q31_t * pDst,
 59      uint32_t blockSize)
 60  {
 61      uint32_t  blkCnt;           /* Loop counters */
 62      q31x4_t vecSrc;
 63  
 64      /* Compute 4 outputs at a time */
 65      blkCnt = blockSize >> 2;
 66  
 67      while (blkCnt > 0U)
 68      {
 69          /*
 70           * C = |A|
 71           * Calculate absolute and then store the results in the destination buffer.
 72           */
 73          vecSrc = vld1q(pSrc);
 74          vst1q(pDst, vqabsq(vecSrc));
 75          /*
 76           * Decrement the blockSize loop counter
 77           */
 78          blkCnt--;
 79          /*
 80           * Advance vector source and destination pointers
 81           */
 82          pSrc += 4;
 83          pDst += 4;
 84      }
 85      /*
 86       * Tail
 87       */
 88      blkCnt = blockSize & 3;
 89  
 90      if (blkCnt > 0U)
 91      {
 92          mve_pred16_t p0 = vctp32q(blkCnt);
 93          vecSrc = vld1q(pSrc);
 94          vstrwq_p(pDst, vqabsq(vecSrc), p0);
 95      }
 96  }
 97  
 98  #else
 99  void arm_abs_q31(
100    const q31_t * pSrc,
101          q31_t * pDst,
102          uint32_t blockSize)
103  {
104          uint32_t blkCnt;                               /* Loop counter */
105          q31_t in;                                      /* Temporary variable */
106  
107  #if defined(ARM_MATH_NEON)
108      int32x4_t vec1;
109      int32x4_t res;
110  
111      /* Compute 4 outputs at a time */  
112      blkCnt = blockSize >> 2U;
113  
114      while (blkCnt > 0U)
115      {
116          /* C = |A| */
117          /* Calculate absolute and then store the results in the destination buffer. */
118  
119          vec1 = vld1q_s32(pSrc);
120          res = vqabsq_s32(vec1);
121          vst1q_s32(pDst, res);
122  
123          /* Increment pointers */
124          pSrc += 4;
125          pDst += 4;
126          
127          /* Decrement the blockSize loop counter */
128          blkCnt--;
129      }
130  
131      /* Tail */
132      blkCnt = blockSize & 0x3;
133  
134  #else
135  #if defined (ARM_MATH_LOOPUNROLL)
136  
137    /* Loop unrolling: Compute 4 outputs at a time */
138    blkCnt = blockSize >> 2U;
139  
140    while (blkCnt > 0U)
141    {
142      /* C = |A| */
143  
144      /* Calculate absolute of input (if -1 then saturated to 0x7fffffff) and store result in destination buffer. */
145      in = *pSrc++;
146  #if defined (ARM_MATH_DSP)
147      *pDst++ = (in > 0) ? in : (q31_t)__QSUB(0, in);
148  #else
149      *pDst++ = (in > 0) ? in : ((in == INT32_MIN) ? INT32_MAX : -in);
150  #endif
151  
152      in = *pSrc++;
153  #if defined (ARM_MATH_DSP)
154      *pDst++ = (in > 0) ? in : (q31_t)__QSUB(0, in);
155  #else
156      *pDst++ = (in > 0) ? in : ((in == INT32_MIN) ? INT32_MAX : -in);
157  #endif
158  
159      in = *pSrc++;
160  #if defined (ARM_MATH_DSP)
161      *pDst++ = (in > 0) ? in : (q31_t)__QSUB(0, in);
162  #else
163      *pDst++ = (in > 0) ? in : ((in == INT32_MIN) ? INT32_MAX : -in);
164  #endif
165  
166      in = *pSrc++;
167  #if defined (ARM_MATH_DSP)
168      *pDst++ = (in > 0) ? in : (q31_t)__QSUB(0, in);
169  #else
170      *pDst++ = (in > 0) ? in : ((in == INT32_MIN) ? INT32_MAX : -in);
171  #endif
172  
173      /* Decrement loop counter */
174      blkCnt--;
175    }
176  
177    /* Loop unrolling: Compute remaining outputs */
178    blkCnt = blockSize % 0x4U;
179  
180  #else
181  
182    /* Initialize blkCnt with number of samples */
183    blkCnt = blockSize;
184  
185  #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
186  #endif /* #if defined (ARM_MATH_NEON) */
187  
188    while (blkCnt > 0U)
189    {
190      /* C = |A| */
191  
192      /* Calculate absolute of input (if -1 then saturated to 0x7fffffff) and store result in destination buffer. */
193      in = *pSrc++;
194  #if defined (ARM_MATH_DSP)
195      *pDst++ = (in > 0) ? in : (q31_t)__QSUB(0, in);
196  #else
197      *pDst++ = (in > 0) ? in : ((in == INT32_MIN) ? INT32_MAX : -in);
198  #endif
199  
200      /* Decrement loop counter */
201      blkCnt--;
202    }
203  
204  }
205  #endif /* #if defined (ARM_MATH_MVEI) */
206  /**
207    @} end of BasicAbs group
208   */