CMSIS DSP library

Dependents:   performance_timer Surfboard_ gps2rtty Capstone ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_mat_scale_q31.c Source File

arm_mat_scale_q31.c

00001 /* ----------------------------------------------------------------------    
00002 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.    
00003 *    
00004 * $Date:        19. March 2015
00005 * $Revision:    V.1.4.5
00006 *    
00007 * Project:      CMSIS DSP Library    
00008 * Title:        arm_mat_scale_q31.c    
00009 *    
00010 * Description:  Multiplies a Q31 matrix 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 #include "arm_math.h"
00041 
00042 /**        
00043  * @ingroup groupMatrix        
00044  */
00045 
00046 /**        
00047  * @addtogroup MatrixScale        
00048  * @{        
00049  */
00050 
00051 /**        
00052  * @brief Q31 matrix scaling.        
00053  * @param[in]       *pSrc points to input matrix        
00054  * @param[in]       scaleFract fractional portion of the scale factor        
00055  * @param[in]       shift number of bits to shift the result by        
00056  * @param[out]      *pDst points to output matrix structure        
00057  * @return          The function returns either        
00058  * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.        
00059  *        
00060  * @details        
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 arm_status arm_mat_scale_q31(
00068   const arm_matrix_instance_q31 * pSrc,
00069   q31_t scaleFract,
00070   int32_t shift,
00071   arm_matrix_instance_q31 * pDst)
00072 {
00073   q31_t *pIn = pSrc->pData;                      /* input data matrix pointer */
00074   q31_t *pOut = pDst->pData;                     /* output data matrix pointer */
00075   uint32_t numSamples;                           /* total number of elements in the matrix */
00076   int32_t totShift = shift + 1;                  /* shift to apply after scaling */
00077   uint32_t blkCnt;                               /* loop counters  */
00078   arm_status status;                             /* status of matrix scaling      */
00079   q31_t in1, in2, out1;                          /* temporary variabels */
00080 
00081 #ifndef ARM_MATH_CM0_FAMILY
00082 
00083   q31_t in3, in4, out2, out3, out4;              /* temporary variables */
00084 
00085 #endif //      #ifndef ARM_MAT_CM0
00086 
00087 #ifdef ARM_MATH_MATRIX_CHECK
00088   /* Check for matrix mismatch  */
00089   if((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
00090   {
00091     /* Set status as ARM_MATH_SIZE_MISMATCH */
00092     status = ARM_MATH_SIZE_MISMATCH;
00093   }
00094   else
00095 #endif //    #ifdef ARM_MATH_MATRIX_CHECK
00096   {
00097     /* Total number of samples in the input matrix */
00098     numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
00099 
00100 #ifndef ARM_MATH_CM0_FAMILY
00101 
00102     /* Run the below code for Cortex-M4 and Cortex-M3 */
00103 
00104     /* Loop Unrolling */
00105     blkCnt = numSamples >> 2u;
00106 
00107     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.    
00108      ** a second loop below computes the remaining 1 to 3 samples. */
00109     while(blkCnt > 0u)
00110     {
00111       /* C(m,n) = A(m,n) * k */
00112       /* Read values from input */
00113       in1 = *pIn;
00114       in2 = *(pIn + 1);
00115       in3 = *(pIn + 2);
00116       in4 = *(pIn + 3);
00117 
00118       /* multiply input with scaler value */
00119       in1 = ((q63_t) in1 * scaleFract) >> 32;
00120       in2 = ((q63_t) in2 * scaleFract) >> 32;
00121       in3 = ((q63_t) in3 * scaleFract) >> 32;
00122       in4 = ((q63_t) in4 * scaleFract) >> 32;
00123 
00124       /* apply shifting */
00125       out1 = in1 << totShift;
00126       out2 = in2 << totShift;
00127 
00128       /* saturate the results. */
00129       if(in1 != (out1 >> totShift))
00130         out1 = 0x7FFFFFFF ^ (in1 >> 31);
00131 
00132       if(in2 != (out2 >> totShift))
00133         out2 = 0x7FFFFFFF ^ (in2 >> 31);
00134 
00135       out3 = in3 << totShift;
00136       out4 = in4 << totShift;
00137 
00138       *pOut = out1;
00139       *(pOut + 1) = out2;
00140 
00141       if(in3 != (out3 >> totShift))
00142         out3 = 0x7FFFFFFF ^ (in3 >> 31);
00143 
00144       if(in4 != (out4 >> totShift))
00145         out4 = 0x7FFFFFFF ^ (in4 >> 31);
00146 
00147 
00148       *(pOut + 2) = out3;
00149       *(pOut + 3) = out4;
00150 
00151       /* update pointers to process next sampels */
00152       pIn += 4u;
00153       pOut += 4u;
00154 
00155 
00156       /* Decrement the numSamples loop counter */
00157       blkCnt--;
00158     }
00159 
00160     /* If the numSamples is not a multiple of 4, compute any remaining output samples here.    
00161      ** No loop unrolling is used. */
00162     blkCnt = numSamples % 0x4u;
00163 
00164 #else
00165 
00166     /* Run the below code for Cortex-M0 */
00167 
00168     /* Initialize blkCnt with number of samples */
00169     blkCnt = numSamples;
00170 
00171 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
00172 
00173     while(blkCnt > 0u)
00174     {
00175       /* C(m,n) = A(m,n) * k */
00176       /* Scale, saturate and then store the results in the destination buffer. */
00177       in1 = *pIn++;
00178 
00179       in2 = ((q63_t) in1 * scaleFract) >> 32;
00180 
00181       out1 = in2 << totShift;
00182 
00183       if(in2 != (out1 >> totShift))
00184         out1 = 0x7FFFFFFF ^ (in2 >> 31);
00185 
00186       *pOut++ = out1;
00187 
00188       /* Decrement the numSamples loop counter */
00189       blkCnt--;
00190     }
00191 
00192     /* Set status as ARM_MATH_SUCCESS */
00193     status = ARM_MATH_SUCCESS;
00194   }
00195 
00196   /* Return to application */
00197   return (status);
00198 }
00199 
00200 /**        
00201  * @} end of MatrixScale group        
00202  */