1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_dot_prod_q31.c
10 * Description: Q31 dot product.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
29 * Version 0.0.7 2010/06/10
30 * Misra-C changes done
31 * -------------------------------------------------------------------- */
40 * @addtogroup dot_prod
45 * @brief Dot product of Q31 vectors.
46 * @param[in] *pSrcA points to the first input vector
47 * @param[in] *pSrcB points to the second input vector
48 * @param[in] blockSize number of samples in each vector
49 * @param[out] *result output result returned here
52 * <b>Scaling and Overflow Behavior:</b>
54 * The intermediate multiplications are in 1.31 x 1.31 = 2.62 format and these
55 * are truncated to 2.48 format by discarding the lower 14 bits.
56 * The 2.48 result is then added without saturation to a 64-bit accumulator in 16.48 format.
57 * There are 15 guard bits in the accumulator and there is no risk of overflow as long as
58 * the length of the vectors is less than 2^16 elements.
59 * The return result is in 16.48 format.
62 void arm_dot_prod_q31(
68 q63_t sum = 0; /* Temporary result storage */
69 uint32_t blkCnt; /* loop counter */
74 /* Run the below code for Cortex-M4 and Cortex-M3 */
77 blkCnt = blockSize >> 2u;
79 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
80 ** a second loop below computes the remaining 1 to 3 samples. */
83 /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
84 /* Calculate dot product and then store the result in a temporary buffer. */
85 sum += ((q63_t) * pSrcA++ * *pSrcB++) >> 14u;
86 sum += ((q63_t) * pSrcA++ * *pSrcB++) >> 14u;
87 sum += ((q63_t) * pSrcA++ * *pSrcB++) >> 14u;
88 sum += ((q63_t) * pSrcA++ * *pSrcB++) >> 14u;
90 /* Decrement the loop counter */
94 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
95 ** No loop unrolling is used. */
96 blkCnt = blockSize % 0x4u;
100 /* Run the below code for Cortex-M0 */
102 /* Initialize blkCnt with number of samples */
105 #endif /* #ifndef ARM_MATH_CM0 */
110 /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
111 /* Calculate dot product and then store the result in a temporary buffer. */
112 sum += ((q63_t) * pSrcA++ * *pSrcB++) >> 14u;
114 /* Decrement the loop counter */
118 /* Store the result in the destination buffer in 16.48 format */
123 * @} end of dot_prod group