Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_mult_q15.c Source File

arm_mult_q15.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_mult_q15.c
00004  * Description:  Q15 vector multiplication
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 BasicMult
00037  * @{
00038  */
00039 
00040 
00041 /**
00042  * @brief           Q15 vector multiplication
00043  * @param[in]       *pSrcA points to the first input vector
00044  * @param[in]       *pSrcB points to the second input vector
00045  * @param[out]      *pDst points to the output vector
00046  * @param[in]       blockSize number of samples in each vector
00047  * @return none.
00048  *
00049  * <b>Scaling and Overflow Behavior:</b>
00050  * \par
00051  * The function uses saturating arithmetic.
00052  * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.
00053  */
00054 
00055 void arm_mult_q15(
00056   q15_t * pSrcA,
00057   q15_t * pSrcB,
00058   q15_t * pDst,
00059   uint32_t blockSize)
00060 {
00061   uint32_t blkCnt;                               /* loop counters */
00062 
00063 #if defined (ARM_MATH_DSP)
00064 
00065 /* Run the below code for Cortex-M4 and Cortex-M3 */
00066   q31_t inA1, inA2, inB1, inB2;                  /* temporary input variables */
00067   q15_t out1, out2, out3, out4;                  /* temporary output variables */
00068   q31_t mul1, mul2, mul3, mul4;                  /* temporary variables */
00069 
00070   /* loop Unrolling */
00071   blkCnt = blockSize >> 2U;
00072 
00073   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00074    ** a second loop below computes the remaining 1 to 3 samples. */
00075   while (blkCnt > 0U)
00076   {
00077     /* read two samples at a time from sourceA */
00078     inA1 = *__SIMD32(pSrcA)++;
00079     /* read two samples at a time from sourceB */
00080     inB1 = *__SIMD32(pSrcB)++;
00081     /* read two samples at a time from sourceA */
00082     inA2 = *__SIMD32(pSrcA)++;
00083     /* read two samples at a time from sourceB */
00084     inB2 = *__SIMD32(pSrcB)++;
00085 
00086     /* multiply mul = sourceA * sourceB */
00087     mul1 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
00088     mul2 = (q31_t) ((q15_t) inA1 * (q15_t) inB1);
00089     mul3 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB2 >> 16));
00090     mul4 = (q31_t) ((q15_t) inA2 * (q15_t) inB2);
00091 
00092     /* saturate result to 16 bit */
00093     out1 = (q15_t) __SSAT(mul1 >> 15, 16);
00094     out2 = (q15_t) __SSAT(mul2 >> 15, 16);
00095     out3 = (q15_t) __SSAT(mul3 >> 15, 16);
00096     out4 = (q15_t) __SSAT(mul4 >> 15, 16);
00097 
00098     /* store the result */
00099 #ifndef ARM_MATH_BIG_ENDIAN
00100 
00101     *__SIMD32(pDst)++ = __PKHBT(out2, out1, 16);
00102     *__SIMD32(pDst)++ = __PKHBT(out4, out3, 16);
00103 
00104 #else
00105 
00106     *__SIMD32(pDst)++ = __PKHBT(out2, out1, 16);
00107     *__SIMD32(pDst)++ = __PKHBT(out4, out3, 16);
00108 
00109 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
00110 
00111     /* Decrement the blockSize loop counter */
00112     blkCnt--;
00113   }
00114 
00115   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00116    ** No loop unrolling is used. */
00117   blkCnt = blockSize % 0x4U;
00118 
00119 #else
00120 
00121   /* Run the below code for Cortex-M0 */
00122 
00123   /* Initialize blkCnt with number of samples */
00124   blkCnt = blockSize;
00125 
00126 #endif /* #if defined (ARM_MATH_DSP) */
00127 
00128 
00129   while (blkCnt > 0U)
00130   {
00131     /* C = A * B */
00132     /* Multiply the inputs and store the result in the destination buffer */
00133     *pDst++ = (q15_t) __SSAT((((q31_t) (*pSrcA++) * (*pSrcB++)) >> 15), 16);
00134 
00135     /* Decrement the blockSize loop counter */
00136     blkCnt--;
00137   }
00138 }
00139 
00140 /**
00141  * @} end of BasicMult group
00142  */
00143