Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_dot_prod_q15.c Source File

arm_dot_prod_q15.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_dot_prod_q15.c
00004  * Description:  Q15 dot product
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 dot_prod
00037  * @{
00038  */
00039 
00040 /**
00041  * @brief Dot product of Q15 vectors.
00042  * @param[in]       *pSrcA points to the first input vector
00043  * @param[in]       *pSrcB points to the second input vector
00044  * @param[in]       blockSize number of samples in each vector
00045  * @param[out]      *result output result returned here
00046  * @return none.
00047  *
00048  * <b>Scaling and Overflow Behavior:</b>
00049  * \par
00050  * The intermediate multiplications are in 1.15 x 1.15 = 2.30 format and these
00051  * results are added to a 64-bit accumulator in 34.30 format.
00052  * Nonsaturating additions are used and given that there are 33 guard bits in the accumulator
00053  * there is no risk of overflow.
00054  * The return result is in 34.30 format.
00055  */
00056 
00057 void arm_dot_prod_q15(
00058   q15_t * pSrcA,
00059   q15_t * pSrcB,
00060   uint32_t blockSize,
00061   q63_t * result)
00062 {
00063   q63_t sum = 0;                                 /* Temporary result storage */
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 
00070 
00071   /*loop Unrolling */
00072   blkCnt = blockSize >> 2U;
00073 
00074   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00075    ** a second loop below computes the remaining 1 to 3 samples. */
00076   while (blkCnt > 0U)
00077   {
00078     /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
00079     /* Calculate dot product and then store the result in a temporary buffer. */
00080     sum = __SMLALD(*__SIMD32(pSrcA)++, *__SIMD32(pSrcB)++, sum);
00081     sum = __SMLALD(*__SIMD32(pSrcA)++, *__SIMD32(pSrcB)++, sum);
00082 
00083     /* Decrement the loop counter */
00084     blkCnt--;
00085   }
00086 
00087   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00088    ** No loop unrolling is used. */
00089   blkCnt = blockSize % 0x4U;
00090 
00091   while (blkCnt > 0U)
00092   {
00093     /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
00094     /* Calculate dot product and then store the results in a temporary buffer. */
00095     sum = __SMLALD(*pSrcA++, *pSrcB++, sum);
00096 
00097     /* Decrement the loop counter */
00098     blkCnt--;
00099   }
00100 
00101 
00102 #else
00103 
00104   /* Run the below code for Cortex-M0 */
00105 
00106   /* Initialize blkCnt with number of samples */
00107   blkCnt = blockSize;
00108 
00109   while (blkCnt > 0U)
00110   {
00111     /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
00112     /* Calculate dot product and then store the results in a temporary buffer. */
00113     sum += (q63_t) ((q31_t) * pSrcA++ * *pSrcB++);
00114 
00115     /* Decrement the loop counter */
00116     blkCnt--;
00117   }
00118 
00119 #endif /* #if defined (ARM_MATH_DSP) */
00120 
00121   /* Store the result in the destination buffer in 34.30 format */
00122   *result = sum;
00123 
00124 }
00125 
00126 /**
00127  * @} end of dot_prod group
00128  */
00129