Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_mat_scale_q15.c Source File

arm_mat_scale_q15.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_mat_scale_q15.c
00004  * Description:  Multiplies a Q15 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 Q15 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.15 format.
00053  * These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format.
00054  */
00055 
00056 arm_status arm_mat_scale_q15(
00057   const arm_matrix_instance_q15 * pSrc,
00058   q15_t scaleFract,
00059   int32_t shift,
00060   arm_matrix_instance_q15 * pDst)
00061 {
00062   q15_t *pIn = pSrc->pData;                      /* input data matrix pointer */
00063   q15_t *pOut = pDst->pData;                     /* output data matrix pointer */
00064   uint32_t numSamples;                           /* total number of elements in the matrix */
00065   int32_t totShift = 15 - shift;                 /* total shift to apply after scaling */
00066   uint32_t blkCnt;                               /* loop counters */
00067   arm_status status;                             /* status of matrix scaling     */
00068 
00069 #if defined (ARM_MATH_DSP)
00070 
00071   q15_t in1, in2, in3, in4;
00072   q31_t out1, out2, out3, out4;
00073   q31_t inA1, inA2;
00074 
00075 #endif //     #if defined (ARM_MATH_DSP)
00076 
00077 #ifdef ARM_MATH_MATRIX_CHECK
00078   /* Check for matrix mismatch */
00079   if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
00080   {
00081     /* Set status as ARM_MATH_SIZE_MISMATCH */
00082     status = ARM_MATH_SIZE_MISMATCH;
00083   }
00084   else
00085 #endif //    #ifdef ARM_MATH_MATRIX_CHECK
00086   {
00087     /* Total number of samples in the input matrix */
00088     numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
00089 
00090 #if defined (ARM_MATH_DSP)
00091 
00092     /* Run the below code for Cortex-M4 and Cortex-M3 */
00093     /* Loop Unrolling */
00094     blkCnt = numSamples >> 2;
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       /* Scale, saturate and then store the results in the destination buffer. */
00102       /* Reading 2 inputs from memory */
00103       inA1 = _SIMD32_OFFSET(pIn);
00104       inA2 = _SIMD32_OFFSET(pIn + 2);
00105 
00106       /* C = A * scale */
00107       /* Scale the inputs and then store the 2 results in the destination buffer
00108        * in single cycle by packing the outputs */
00109       out1 = (q31_t) ((q15_t) (inA1 >> 16) * scaleFract);
00110       out2 = (q31_t) ((q15_t) inA1 * scaleFract);
00111       out3 = (q31_t) ((q15_t) (inA2 >> 16) * scaleFract);
00112       out4 = (q31_t) ((q15_t) inA2 * scaleFract);
00113 
00114       out1 = out1 >> totShift;
00115       inA1 = _SIMD32_OFFSET(pIn + 4);
00116       out2 = out2 >> totShift;
00117       inA2 = _SIMD32_OFFSET(pIn + 6);
00118       out3 = out3 >> totShift;
00119       out4 = out4 >> totShift;
00120 
00121       in1 = (q15_t) (__SSAT(out1, 16));
00122       in2 = (q15_t) (__SSAT(out2, 16));
00123       in3 = (q15_t) (__SSAT(out3, 16));
00124       in4 = (q15_t) (__SSAT(out4, 16));
00125 
00126       _SIMD32_OFFSET(pOut) = __PKHBT(in2, in1, 16);
00127       _SIMD32_OFFSET(pOut + 2) = __PKHBT(in4, in3, 16);
00128 
00129       /* update pointers to process next sampels */
00130       pIn += 4U;
00131       pOut += 4U;
00132 
00133 
00134       /* Decrement the numSamples loop counter */
00135       blkCnt--;
00136     }
00137 
00138     /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
00139      ** No loop unrolling is used. */
00140     blkCnt = numSamples % 0x4U;
00141 
00142 #else
00143 
00144     /* Run the below code for Cortex-M0 */
00145 
00146     /* Initialize blkCnt with number of samples */
00147     blkCnt = numSamples;
00148 
00149 #endif /* #if defined (ARM_MATH_DSP) */
00150 
00151     while (blkCnt > 0U)
00152     {
00153       /* C(m,n) = A(m,n) * k */
00154       /* Scale, saturate and then store the results in the destination buffer. */
00155       *pOut++ =
00156         (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
00157 
00158       /* Decrement the numSamples loop counter */
00159       blkCnt--;
00160     }
00161     /* Set status as ARM_MATH_SUCCESS */
00162     status = ARM_MATH_SUCCESS;
00163   }
00164 
00165   /* Return to application */
00166   return (status);
00167 }
00168 
00169 /**
00170  * @} end of MatrixScale group
00171  */
00172