Robert Lopez / CMSIS5
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  * Project:      CMSIS DSP Library
00003  * Title:        arm_mat_scale_q31.c
00004  * Description:  Multiplies a Q31 matrix by a scalar
00005  *
00006  * $Date:        27. January 2017
00007  * $Revision:    V.1.5.1
00008  *
00009  * Target Processor: Cortex-M cores
00010  * -------------------------------------------------------------------- */
00011 /*
00012  * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
00013  *
00014  * SPDX-License-Identifier: Apache-2.0
00015  *
00016  * Licensed under the Apache License, Version 2.0 (the License); you may
00017  * not use this file except in compliance with the License.
00018  * You may obtain a copy of the License at
00019  *
00020  * www.apache.org/licenses/LICENSE-2.0
00021  *
00022  * Unless required by applicable law or agreed to in writing, software
00023  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00024  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00025  * See the License for the specific language governing permissions and
00026  * limitations under the License.
00027  */
00028 
00029 #include "arm_math.h"
00030 
00031 /**
00032  * @ingroup groupMatrix
00033  */
00034 
00035 /**
00036  * @addtogroup MatrixScale
00037  * @{
00038  */
00039 
00040 /**
00041  * @brief Q31 matrix scaling.
00042  * @param[in]       *pSrc points to input matrix
00043  * @param[in]       scaleFract fractional portion of the scale factor
00044  * @param[in]       shift number of bits to shift the result by
00045  * @param[out]      *pDst points to output matrix structure
00046  * @return          The function returns either
00047  * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
00048  *
00049  * @details
00050  * <b>Scaling and Overflow Behavior:</b>
00051  * \par
00052  * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.31 format.
00053  * These are multiplied to yield a 2.62 intermediate result and this is shifted with saturation to 1.31 format.
00054  */
00055 
00056 arm_status arm_mat_scale_q31(
00057   const arm_matrix_instance_q31 * pSrc,
00058   q31_t scaleFract,
00059   int32_t shift,
00060   arm_matrix_instance_q31 * pDst)
00061 {
00062   q31_t *pIn = pSrc->pData;                      /* input data matrix pointer */
00063   q31_t *pOut = pDst->pData;                     /* output data matrix pointer */
00064   uint32_t numSamples;                           /* total number of elements in the matrix */
00065   int32_t totShift = shift + 1;                  /* shift to apply after scaling */
00066   uint32_t blkCnt;                               /* loop counters  */
00067   arm_status status;                             /* status of matrix scaling      */
00068   q31_t in1, in2, out1;                          /* temporary variabels */
00069 
00070 #if defined (ARM_MATH_DSP)
00071 
00072   q31_t in3, in4, out2, out3, out4;              /* temporary variables */
00073 
00074 #endif //      #ifndef ARM_MAT_CM0
00075 
00076 #ifdef ARM_MATH_MATRIX_CHECK
00077   /* Check for matrix mismatch  */
00078   if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
00079   {
00080     /* Set status as ARM_MATH_SIZE_MISMATCH */
00081     status = ARM_MATH_SIZE_MISMATCH;
00082   }
00083   else
00084 #endif //    #ifdef ARM_MATH_MATRIX_CHECK
00085   {
00086     /* Total number of samples in the input matrix */
00087     numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
00088 
00089 #if defined (ARM_MATH_DSP)
00090 
00091     /* Run the below code for Cortex-M4 and Cortex-M3 */
00092 
00093     /* Loop Unrolling */
00094     blkCnt = numSamples >> 2U;
00095 
00096     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00097      ** a second loop below computes the remaining 1 to 3 samples. */
00098     while (blkCnt > 0U)
00099     {
00100       /* C(m,n) = A(m,n) * k */
00101       /* Read values from input */
00102       in1 = *pIn;
00103       in2 = *(pIn + 1);
00104       in3 = *(pIn + 2);
00105       in4 = *(pIn + 3);
00106 
00107       /* multiply input with scaler value */
00108       in1 = ((q63_t) in1 * scaleFract) >> 32;
00109       in2 = ((q63_t) in2 * scaleFract) >> 32;
00110       in3 = ((q63_t) in3 * scaleFract) >> 32;
00111       in4 = ((q63_t) in4 * scaleFract) >> 32;
00112 
00113       /* apply shifting */
00114       out1 = in1 << totShift;
00115       out2 = in2 << totShift;
00116 
00117       /* saturate the results. */
00118       if (in1 != (out1 >> totShift))
00119         out1 = 0x7FFFFFFF ^ (in1 >> 31);
00120 
00121       if (in2 != (out2 >> totShift))
00122         out2 = 0x7FFFFFFF ^ (in2 >> 31);
00123 
00124       out3 = in3 << totShift;
00125       out4 = in4 << totShift;
00126 
00127       *pOut = out1;
00128       *(pOut + 1) = out2;
00129 
00130       if (in3 != (out3 >> totShift))
00131         out3 = 0x7FFFFFFF ^ (in3 >> 31);
00132 
00133       if (in4 != (out4 >> totShift))
00134         out4 = 0x7FFFFFFF ^ (in4 >> 31);
00135 
00136 
00137       *(pOut + 2) = out3;
00138       *(pOut + 3) = out4;
00139 
00140       /* update pointers to process next sampels */
00141       pIn += 4U;
00142       pOut += 4U;
00143 
00144 
00145       /* Decrement the numSamples loop counter */
00146       blkCnt--;
00147     }
00148 
00149     /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
00150      ** No loop unrolling is used. */
00151     blkCnt = numSamples % 0x4U;
00152 
00153 #else
00154 
00155     /* Run the below code for Cortex-M0 */
00156 
00157     /* Initialize blkCnt with number of samples */
00158     blkCnt = numSamples;
00159 
00160 #endif /* #if defined (ARM_MATH_DSP) */
00161 
00162     while (blkCnt > 0U)
00163     {
00164       /* C(m,n) = A(m,n) * k */
00165       /* Scale, saturate and then store the results in the destination buffer. */
00166       in1 = *pIn++;
00167 
00168       in2 = ((q63_t) in1 * scaleFract) >> 32;
00169 
00170       out1 = in2 << totShift;
00171 
00172       if (in2 != (out1 >> totShift))
00173         out1 = 0x7FFFFFFF ^ (in2 >> 31);
00174 
00175       *pOut++ = out1;
00176 
00177       /* Decrement the numSamples loop counter */
00178       blkCnt--;
00179     }
00180 
00181     /* Set status as ARM_MATH_SUCCESS */
00182     status = ARM_MATH_SUCCESS;
00183   }
00184 
00185   /* Return to application */
00186   return (status);
00187 }
00188 
00189 /**
00190  * @} end of MatrixScale group
00191  */
00192