CMSIS DSP library

Dependents:   KL25Z_FFT_Demo Hat_Board_v5_1 KL25Z_FFT_Demo_tony KL25Z_FFT_Demo_tony ... more

Fork of mbed-dsp by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_scale_q31.c Source File

arm_scale_q31.c

00001 /* ----------------------------------------------------------------------    
00002 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.    
00003 *    
00004 * $Date:        17. January 2013
00005 * $Revision:    V1.4.1
00006 *    
00007 * Project:      CMSIS DSP Library    
00008 * Title:        arm_scale_q31.c    
00009 *    
00010 * Description:  Multiplies a Q31 vector by a scalar.    
00011 *    
00012 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
00013 *  
00014 * Redistribution and use in source and binary forms, with or without 
00015 * modification, are permitted provided that the following conditions
00016 * are met:
00017 *   - Redistributions of source code must retain the above copyright
00018 *     notice, this list of conditions and the following disclaimer.
00019 *   - Redistributions in binary form must reproduce the above copyright
00020 *     notice, this list of conditions and the following disclaimer in
00021 *     the documentation and/or other materials provided with the 
00022 *     distribution.
00023 *   - Neither the name of ARM LIMITED nor the names of its contributors
00024 *     may be used to endorse or promote products derived from this
00025 *     software without specific prior written permission.
00026 *
00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00028 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00029 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00030 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
00031 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00032 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00033 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00034 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00035 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00036 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00037 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00038 * POSSIBILITY OF SUCH DAMAGE.   
00039 * -------------------------------------------------------------------- */
00040 
00041 #include "arm_math.h"
00042 
00043 /**       
00044  * @ingroup groupMath       
00045  */
00046 
00047 /**       
00048  * @addtogroup scale       
00049  * @{       
00050  */
00051 
00052 /**       
00053  * @brief Multiplies a Q31 vector by a scalar.       
00054  * @param[in]       *pSrc points to the input vector       
00055  * @param[in]       scaleFract fractional portion of the scale value       
00056  * @param[in]       shift number of bits to shift the result by       
00057  * @param[out]      *pDst points to the output vector       
00058  * @param[in]       blockSize number of samples in the vector       
00059  * @return none.       
00060  *       
00061  * <b>Scaling and Overflow Behavior:</b>       
00062  * \par       
00063  * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.31 format.       
00064  * These are multiplied to yield a 2.62 intermediate result and this is shifted with saturation to 1.31 format.       
00065  */
00066 
00067 void arm_scale_q31(
00068   q31_t * pSrc,
00069   q31_t scaleFract,
00070   int8_t shift,
00071   q31_t * pDst,
00072   uint32_t blockSize)
00073 {
00074   int8_t kShift = shift + 1;                     /* Shift to apply after scaling */
00075   int8_t sign = (kShift & 0x80);
00076   uint32_t blkCnt;                               /* loop counter */
00077   q31_t in, out;
00078 
00079 #ifndef ARM_MATH_CM0_FAMILY
00080 
00081 /* Run the below code for Cortex-M4 and Cortex-M3 */
00082 
00083   q31_t in1, in2, in3, in4;                      /* temporary input variables */
00084   q31_t out1, out2, out3, out4;                  /* temporary output variabels */
00085 
00086 
00087   /*loop Unrolling */
00088   blkCnt = blockSize >> 2u;
00089 
00090   if(sign == 0u)
00091   {
00092     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.       
00093      ** a second loop below computes the remaining 1 to 3 samples. */
00094     while(blkCnt > 0u)
00095     {
00096       /* read four inputs from source */
00097       in1 = *pSrc;
00098       in2 = *(pSrc + 1);
00099       in3 = *(pSrc + 2);
00100       in4 = *(pSrc + 3);
00101 
00102       /* multiply input with scaler value */
00103       in1 = ((q63_t) in1 * scaleFract) >> 32;
00104       in2 = ((q63_t) in2 * scaleFract) >> 32;
00105       in3 = ((q63_t) in3 * scaleFract) >> 32;
00106       in4 = ((q63_t) in4 * scaleFract) >> 32;
00107 
00108       /* apply shifting */
00109       out1 = in1 << kShift;
00110       out2 = in2 << kShift;
00111 
00112       /* saturate the results. */
00113       if(in1 != (out1 >> kShift))
00114         out1 = 0x7FFFFFFF ^ (in1 >> 31);
00115 
00116       if(in2 != (out2 >> kShift))
00117         out2 = 0x7FFFFFFF ^ (in2 >> 31);
00118 
00119       out3 = in3 << kShift;
00120       out4 = in4 << kShift;
00121 
00122       *pDst = out1;
00123       *(pDst + 1) = out2;
00124 
00125       if(in3 != (out3 >> kShift))
00126         out3 = 0x7FFFFFFF ^ (in3 >> 31);
00127 
00128       if(in4 != (out4 >> kShift))
00129         out4 = 0x7FFFFFFF ^ (in4 >> 31);
00130 
00131       /* Store result destination */
00132       *(pDst + 2) = out3;
00133       *(pDst + 3) = out4;
00134 
00135       /* Update pointers to process next sampels */
00136       pSrc += 4u;
00137       pDst += 4u;
00138 
00139       /* Decrement the loop counter */
00140       blkCnt--;
00141     }
00142 
00143   }
00144   else
00145   {
00146     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.       
00147      ** a second loop below computes the remaining 1 to 3 samples. */
00148     while(blkCnt > 0u)
00149     {
00150       /* read four inputs from source */
00151       in1 = *pSrc;
00152       in2 = *(pSrc + 1);
00153       in3 = *(pSrc + 2);
00154       in4 = *(pSrc + 3);
00155 
00156       /* multiply input with scaler value */
00157       in1 = ((q63_t) in1 * scaleFract) >> 32;
00158       in2 = ((q63_t) in2 * scaleFract) >> 32;
00159       in3 = ((q63_t) in3 * scaleFract) >> 32;
00160       in4 = ((q63_t) in4 * scaleFract) >> 32;
00161 
00162       /* apply shifting */
00163       out1 = in1 >> -kShift;
00164       out2 = in2 >> -kShift;
00165 
00166       out3 = in3 >> -kShift;
00167       out4 = in4 >> -kShift;
00168 
00169       /* Store result destination */
00170       *pDst = out1;
00171       *(pDst + 1) = out2;
00172 
00173       *(pDst + 2) = out3;
00174       *(pDst + 3) = out4;
00175 
00176       /* Update pointers to process next sampels */
00177       pSrc += 4u;
00178       pDst += 4u;
00179 
00180       /* Decrement the loop counter */
00181       blkCnt--;
00182     }
00183   }
00184   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.       
00185    ** No loop unrolling is used. */
00186   blkCnt = blockSize % 0x4u;
00187 
00188 #else
00189 
00190   /* Run the below code for Cortex-M0 */
00191 
00192   /* Initialize blkCnt with number of samples */
00193   blkCnt = blockSize;
00194 
00195 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
00196 
00197   if(sign == 0)
00198   {
00199       while(blkCnt > 0u)
00200       {
00201         /* C = A * scale */
00202         /* Scale the input and then store the result in the destination buffer. */
00203         in = *pSrc++;
00204         in = ((q63_t) in * scaleFract) >> 32;
00205 
00206         out = in << kShift;
00207         
00208         if(in != (out >> kShift))
00209             out = 0x7FFFFFFF ^ (in >> 31);
00210 
00211         *pDst++ = out;
00212 
00213         /* Decrement the loop counter */
00214         blkCnt--;
00215       }
00216   }
00217   else
00218   {
00219       while(blkCnt > 0u)
00220       {
00221         /* C = A * scale */
00222         /* Scale the input and then store the result in the destination buffer. */
00223         in = *pSrc++;
00224         in = ((q63_t) in * scaleFract) >> 32;
00225 
00226         out = in >> -kShift;
00227 
00228         *pDst++ = out;
00229 
00230         /* Decrement the loop counter */
00231         blkCnt--;
00232       }
00233   
00234   }
00235 }
00236 
00237 /**       
00238  * @} end of scale group       
00239  */