Subversion Repositories ScreenTimer

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* ----------------------------------------------------------------------
  2.  * Project:      CMSIS DSP Library
  3.  * Title:        arm_shift_q31.c
  4.  * Description:  Shifts the elements of a Q31 vector by a specified number of bits
  5.  *
  6.  * $Date:        27. January 2017
  7.  * $Revision:    V.1.5.1
  8.  *
  9.  * Target Processor: Cortex-M cores
  10.  * -------------------------------------------------------------------- */
  11. /*
  12.  * Copyright (C) 2010-2017 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 "arm_math.h"
  30.  
  31. /**
  32.  * @ingroup groupMath
  33.  */
  34. /**
  35.  * @defgroup shift Vector Shift
  36.  *
  37.  * Shifts the elements of a fixed-point vector by a specified number of bits.
  38.  * There are separate functions for Q7, Q15, and Q31 data types.
  39.  * The underlying algorithm used is:
  40.  *
  41.  * <pre>
  42.  *     pDst[n] = pSrc[n] << shift,   0 <= n < blockSize.
  43.  * </pre>
  44.  *
  45.  * If <code>shift</code> is positive then the elements of the vector are shifted to the left.
  46.  * If <code>shift</code> is negative then the elements of the vector are shifted to the right.
  47.  *
  48.  * The functions support in-place computation allowing the source and destination
  49.  * pointers to reference the same memory buffer.
  50.  */
  51.  
  52. /**
  53.  * @addtogroup shift
  54.  * @{
  55.  */
  56.  
  57. /**
  58.  * @brief  Shifts the elements of a Q31 vector a specified number of bits.
  59.  * @param[in]  *pSrc points to the input vector
  60.  * @param[in]  shiftBits number of bits to shift.  A positive value shifts left; a negative value shifts right.
  61.  * @param[out]  *pDst points to the output vector
  62.  * @param[in]  blockSize number of samples in the vector
  63.  * @return none.
  64.  *
  65.  *
  66.  * <b>Scaling and Overflow Behavior:</b>
  67.  * \par
  68.  * The function uses saturating arithmetic.
  69.  * Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] will be saturated.
  70.  */
  71.  
  72. void arm_shift_q31(
  73.   q31_t * pSrc,
  74.   int8_t shiftBits,
  75.   q31_t * pDst,
  76.   uint32_t blockSize)
  77. {
  78.   uint32_t blkCnt;                               /* loop counter */
  79.   uint8_t sign = (shiftBits & 0x80);             /* Sign of shiftBits */
  80.  
  81. #if defined (ARM_MATH_DSP)
  82.  
  83.   q31_t in1, in2, in3, in4;                      /* Temporary input variables */
  84.   q31_t out1, out2, out3, out4;                  /* Temporary output variables */
  85.  
  86.   /*loop Unrolling */
  87.   blkCnt = blockSize >> 2U;
  88.  
  89.  
  90.   if (sign == 0U)
  91.   {
  92.     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
  93.      ** a second loop below computes the remaining 1 to 3 samples. */
  94.     while (blkCnt > 0U)
  95.     {
  96.       /* C = A  << shiftBits */
  97.       /* Shift the input and then store the results in the destination buffer. */
  98.       in1 = *pSrc;
  99.       in2 = *(pSrc + 1);
  100.       out1 = in1 << shiftBits;
  101.       in3 = *(pSrc + 2);
  102.       out2 = in2 << shiftBits;
  103.       in4 = *(pSrc + 3);
  104.       if (in1 != (out1 >> shiftBits))
  105.         out1 = 0x7FFFFFFF ^ (in1 >> 31);
  106.  
  107.       if (in2 != (out2 >> shiftBits))
  108.         out2 = 0x7FFFFFFF ^ (in2 >> 31);
  109.  
  110.       *pDst = out1;
  111.       out3 = in3 << shiftBits;
  112.       *(pDst + 1) = out2;
  113.       out4 = in4 << shiftBits;
  114.  
  115.       if (in3 != (out3 >> shiftBits))
  116.         out3 = 0x7FFFFFFF ^ (in3 >> 31);
  117.  
  118.       if (in4 != (out4 >> shiftBits))
  119.         out4 = 0x7FFFFFFF ^ (in4 >> 31);
  120.  
  121.       *(pDst + 2) = out3;
  122.       *(pDst + 3) = out4;
  123.  
  124.       /* Update destination pointer to process next sampels */
  125.       pSrc += 4U;
  126.       pDst += 4U;
  127.  
  128.       /* Decrement the loop counter */
  129.       blkCnt--;
  130.     }
  131.   }
  132.   else
  133.   {
  134.  
  135.     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
  136.      ** a second loop below computes the remaining 1 to 3 samples. */
  137.     while (blkCnt > 0U)
  138.     {
  139.       /* C = A >>  shiftBits */
  140.       /* Shift the input and then store the results in the destination buffer. */
  141.       in1 = *pSrc;
  142.       in2 = *(pSrc + 1);
  143.       in3 = *(pSrc + 2);
  144.       in4 = *(pSrc + 3);
  145.  
  146.       *pDst = (in1 >> -shiftBits);
  147.       *(pDst + 1) = (in2 >> -shiftBits);
  148.       *(pDst + 2) = (in3 >> -shiftBits);
  149.       *(pDst + 3) = (in4 >> -shiftBits);
  150.  
  151.  
  152.       pSrc += 4U;
  153.       pDst += 4U;
  154.  
  155.       blkCnt--;
  156.     }
  157.  
  158.   }
  159.  
  160.   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  161.    ** No loop unrolling is used. */
  162.   blkCnt = blockSize % 0x4U;
  163.  
  164. #else
  165.  
  166.   /* Run the below code for Cortex-M0 */
  167.  
  168.  
  169.   /* Initialize blkCnt with number of samples */
  170.   blkCnt = blockSize;
  171.  
  172. #endif /* #if defined (ARM_MATH_DSP) */
  173.  
  174.  
  175.   while (blkCnt > 0U)
  176.   {
  177.     /* C = A (>> or <<) shiftBits */
  178.     /* Shift the input and then store the result in the destination buffer. */
  179.     *pDst++ = (sign == 0U) ? clip_q63_to_q31((q63_t) * pSrc++ << shiftBits) :
  180.       (*pSrc++ >> -shiftBits);
  181.  
  182.     /* Decrement the loop counter */
  183.     blkCnt--;
  184.   }
  185.  
  186.  
  187. }
  188.  
  189. /**
  190.  * @} end of shift group
  191.  */
  192.