Aded CMSIS5 DSP and NN folder. Needs some work

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_scale_q15.c Source File

arm_scale_q15.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_scale_q15.c
00004  * Description:  Multiplies a Q15 vector 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 groupMath
00033  */
00034 
00035 /**
00036  * @addtogroup scale
00037  * @{
00038  */
00039 
00040 /**
00041  * @brief Multiplies a Q15 vector by a scalar.
00042  * @param[in]       *pSrc points to the input vector
00043  * @param[in]       scaleFract fractional portion of the scale value
00044  * @param[in]       shift number of bits to shift the result by
00045  * @param[out]      *pDst points to the output vector
00046  * @param[in]       blockSize number of samples in the vector
00047  * @return none.
00048  *
00049  * <b>Scaling and Overflow Behavior:</b>
00050  * \par
00051  * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.15 format.
00052  * These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format.
00053  */
00054 
00055 
00056 void arm_scale_q15(
00057   q15_t * pSrc,
00058   q15_t scaleFract,
00059   int8_t shift,
00060   q15_t * pDst,
00061   uint32_t blockSize)
00062 {
00063   int8_t kShift = 15 - shift;                    /* shift to apply after scaling */
00064   uint32_t blkCnt;                               /* loop counter */
00065 
00066 #if defined (ARM_MATH_DSP)
00067 
00068 /* Run the below code for Cortex-M4 and Cortex-M3 */
00069   q15_t in1, in2, in3, in4;
00070   q31_t inA1, inA2;                              /* Temporary variables */
00071   q31_t out1, out2, out3, out4;
00072 
00073 
00074   /*loop Unrolling */
00075   blkCnt = blockSize >> 2U;
00076 
00077   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00078    ** a second loop below computes the remaining 1 to 3 samples. */
00079   while (blkCnt > 0U)
00080   {
00081     /* Reading 2 inputs from memory */
00082     inA1 = *__SIMD32(pSrc)++;
00083     inA2 = *__SIMD32(pSrc)++;
00084 
00085     /* C = A * scale */
00086     /* Scale the inputs and then store the 2 results in the destination buffer
00087      * in single cycle by packing the outputs */
00088     out1 = (q31_t) ((q15_t) (inA1 >> 16) * scaleFract);
00089     out2 = (q31_t) ((q15_t) inA1 * scaleFract);
00090     out3 = (q31_t) ((q15_t) (inA2 >> 16) * scaleFract);
00091     out4 = (q31_t) ((q15_t) inA2 * scaleFract);
00092 
00093     /* apply shifting */
00094     out1 = out1 >> kShift;
00095     out2 = out2 >> kShift;
00096     out3 = out3 >> kShift;
00097     out4 = out4 >> kShift;
00098 
00099     /* saturate the output */
00100     in1 = (q15_t) (__SSAT(out1, 16));
00101     in2 = (q15_t) (__SSAT(out2, 16));
00102     in3 = (q15_t) (__SSAT(out3, 16));
00103     in4 = (q15_t) (__SSAT(out4, 16));
00104 
00105     /* store the result to destination */
00106     *__SIMD32(pDst)++ = __PKHBT(in2, in1, 16);
00107     *__SIMD32(pDst)++ = __PKHBT(in4, in3, 16);
00108 
00109     /* Decrement the loop counter */
00110     blkCnt--;
00111   }
00112 
00113   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00114    ** No loop unrolling is used. */
00115   blkCnt = blockSize % 0x4U;
00116 
00117   while (blkCnt > 0U)
00118   {
00119     /* C = A * scale */
00120     /* Scale the input and then store the result in the destination buffer. */
00121     *pDst++ = (q15_t) (__SSAT(((*pSrc++) * scaleFract) >> kShift, 16));
00122 
00123     /* Decrement the loop counter */
00124     blkCnt--;
00125   }
00126 
00127 #else
00128 
00129   /* Run the below code for Cortex-M0 */
00130 
00131   /* Initialize blkCnt with number of samples */
00132   blkCnt = blockSize;
00133 
00134   while (blkCnt > 0U)
00135   {
00136     /* C = A * scale */
00137     /* Scale the input and then store the result in the destination buffer. */
00138     *pDst++ = (q15_t) (__SSAT(((q31_t) * pSrc++ * scaleFract) >> kShift, 16));
00139 
00140     /* Decrement the loop counter */
00141     blkCnt--;
00142   }
00143 
00144 #endif /* #if defined (ARM_MATH_DSP) */
00145 
00146 }
00147 
00148 /**
00149  * @} end of scale group
00150  */
00151