Subversion Repositories AFRtranscoder

Rev

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

  1. /* ----------------------------------------------------------------------
  2.  * Project:      CMSIS DSP Library
  3.  * Title:        arm_scale_q31.c
  4.  * Description:  Multiplies a Q31 vector by a scalar
  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. /**
  36.  * @addtogroup scale
  37.  * @{
  38.  */
  39.  
  40. /**
  41.  * @brief Multiplies a Q31 vector by a scalar.
  42.  * @param[in]       *pSrc points to the input vector
  43.  * @param[in]       scaleFract fractional portion of the scale value
  44.  * @param[in]       shift number of bits to shift the result by
  45.  * @param[out]      *pDst points to the output vector
  46.  * @param[in]       blockSize number of samples in the vector
  47.  * @return none.
  48.  *
  49.  * <b>Scaling and Overflow Behavior:</b>
  50.  * \par
  51.  * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.31 format.
  52.  * These are multiplied to yield a 2.62 intermediate result and this is shifted with saturation to 1.31 format.
  53.  */
  54.  
  55. void arm_scale_q31(
  56.   q31_t * pSrc,
  57.   q31_t scaleFract,
  58.   int8_t shift,
  59.   q31_t * pDst,
  60.   uint32_t blockSize)
  61. {
  62.   int8_t kShift = shift + 1;                     /* Shift to apply after scaling */
  63.   int8_t sign = (kShift & 0x80);
  64.   uint32_t blkCnt;                               /* loop counter */
  65.   q31_t in, out;
  66.  
  67. #if defined (ARM_MATH_DSP)
  68.  
  69. /* Run the below code for Cortex-M4 and Cortex-M3 */
  70.  
  71.   q31_t in1, in2, in3, in4;                      /* temporary input variables */
  72.   q31_t out1, out2, out3, out4;                  /* temporary output variabels */
  73.  
  74.  
  75.   /*loop Unrolling */
  76.   blkCnt = blockSize >> 2U;
  77.  
  78.   if (sign == 0U)
  79.   {
  80.     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
  81.      ** a second loop below computes the remaining 1 to 3 samples. */
  82.     while (blkCnt > 0U)
  83.     {
  84.       /* read four inputs from source */
  85.       in1 = *pSrc;
  86.       in2 = *(pSrc + 1);
  87.       in3 = *(pSrc + 2);
  88.       in4 = *(pSrc + 3);
  89.  
  90.       /* multiply input with scaler value */
  91.       in1 = ((q63_t) in1 * scaleFract) >> 32;
  92.       in2 = ((q63_t) in2 * scaleFract) >> 32;
  93.       in3 = ((q63_t) in3 * scaleFract) >> 32;
  94.       in4 = ((q63_t) in4 * scaleFract) >> 32;
  95.  
  96.       /* apply shifting */
  97.       out1 = in1 << kShift;
  98.       out2 = in2 << kShift;
  99.  
  100.       /* saturate the results. */
  101.       if (in1 != (out1 >> kShift))
  102.         out1 = 0x7FFFFFFF ^ (in1 >> 31);
  103.  
  104.       if (in2 != (out2 >> kShift))
  105.         out2 = 0x7FFFFFFF ^ (in2 >> 31);
  106.  
  107.       out3 = in3 << kShift;
  108.       out4 = in4 << kShift;
  109.  
  110.       *pDst = out1;
  111.       *(pDst + 1) = out2;
  112.  
  113.       if (in3 != (out3 >> kShift))
  114.         out3 = 0x7FFFFFFF ^ (in3 >> 31);
  115.  
  116.       if (in4 != (out4 >> kShift))
  117.         out4 = 0x7FFFFFFF ^ (in4 >> 31);
  118.  
  119.       /* Store result destination */
  120.       *(pDst + 2) = out3;
  121.       *(pDst + 3) = out4;
  122.  
  123.       /* Update pointers to process next sampels */
  124.       pSrc += 4U;
  125.       pDst += 4U;
  126.  
  127.       /* Decrement the loop counter */
  128.       blkCnt--;
  129.     }
  130.  
  131.   }
  132.   else
  133.   {
  134.     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
  135.      ** a second loop below computes the remaining 1 to 3 samples. */
  136.     while (blkCnt > 0U)
  137.     {
  138.       /* read four inputs from source */
  139.       in1 = *pSrc;
  140.       in2 = *(pSrc + 1);
  141.       in3 = *(pSrc + 2);
  142.       in4 = *(pSrc + 3);
  143.  
  144.       /* multiply input with scaler value */
  145.       in1 = ((q63_t) in1 * scaleFract) >> 32;
  146.       in2 = ((q63_t) in2 * scaleFract) >> 32;
  147.       in3 = ((q63_t) in3 * scaleFract) >> 32;
  148.       in4 = ((q63_t) in4 * scaleFract) >> 32;
  149.  
  150.       /* apply shifting */
  151.       out1 = in1 >> -kShift;
  152.       out2 = in2 >> -kShift;
  153.  
  154.       out3 = in3 >> -kShift;
  155.       out4 = in4 >> -kShift;
  156.  
  157.       /* Store result destination */
  158.       *pDst = out1;
  159.       *(pDst + 1) = out2;
  160.  
  161.       *(pDst + 2) = out3;
  162.       *(pDst + 3) = out4;
  163.  
  164.       /* Update pointers to process next sampels */
  165.       pSrc += 4U;
  166.       pDst += 4U;
  167.  
  168.       /* Decrement the loop counter */
  169.       blkCnt--;
  170.     }
  171.   }
  172.   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  173.    ** No loop unrolling is used. */
  174.   blkCnt = blockSize % 0x4U;
  175.  
  176. #else
  177.  
  178.   /* Run the below code for Cortex-M0 */
  179.  
  180.   /* Initialize blkCnt with number of samples */
  181.   blkCnt = blockSize;
  182.  
  183. #endif /* #if defined (ARM_MATH_DSP) */
  184.  
  185.   if (sign == 0)
  186.   {
  187.           while (blkCnt > 0U)
  188.           {
  189.                 /* C = A * scale */
  190.                 /* Scale the input and then store the result in the destination buffer. */
  191.                 in = *pSrc++;
  192.                 in = ((q63_t) in * scaleFract) >> 32;
  193.  
  194.                 out = in << kShift;
  195.  
  196.                 if (in != (out >> kShift))
  197.                         out = 0x7FFFFFFF ^ (in >> 31);
  198.  
  199.                 *pDst++ = out;
  200.  
  201.                 /* Decrement the loop counter */
  202.                 blkCnt--;
  203.           }
  204.   }
  205.   else
  206.   {
  207.           while (blkCnt > 0U)
  208.           {
  209.                 /* C = A * scale */
  210.                 /* Scale the input and then store the result in the destination buffer. */
  211.                 in = *pSrc++;
  212.                 in = ((q63_t) in * scaleFract) >> 32;
  213.  
  214.                 out = in >> -kShift;
  215.  
  216.                 *pDst++ = out;
  217.  
  218.                 /* Decrement the loop counter */
  219.                 blkCnt--;
  220.           }
  221.  
  222.   }
  223. }
  224.  
  225. /**
  226.  * @} end of scale group
  227.  */
  228.