The CMSIS DSP 5 library

Dependents:   Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more

Committer:
xorjoep
Date:
Thu Jun 21 11:56:27 2018 +0000
Revision:
3:4098b9d3d571
Parent:
1:24714b45cd1b
headers is a folder not a library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xorjoep 1:24714b45cd1b 1 /* ----------------------------------------------------------------------
xorjoep 1:24714b45cd1b 2 * Project: CMSIS DSP Library
xorjoep 1:24714b45cd1b 3 * Title: arm_dot_prod_q31.c
xorjoep 1:24714b45cd1b 4 * Description: Q31 dot product
xorjoep 1:24714b45cd1b 5 *
xorjoep 1:24714b45cd1b 6 * $Date: 27. January 2017
xorjoep 1:24714b45cd1b 7 * $Revision: V.1.5.1
xorjoep 1:24714b45cd1b 8 *
xorjoep 1:24714b45cd1b 9 * Target Processor: Cortex-M cores
xorjoep 1:24714b45cd1b 10 * -------------------------------------------------------------------- */
xorjoep 1:24714b45cd1b 11 /*
xorjoep 1:24714b45cd1b 12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
xorjoep 1:24714b45cd1b 13 *
xorjoep 1:24714b45cd1b 14 * SPDX-License-Identifier: Apache-2.0
xorjoep 1:24714b45cd1b 15 *
xorjoep 1:24714b45cd1b 16 * Licensed under the Apache License, Version 2.0 (the License); you may
xorjoep 1:24714b45cd1b 17 * not use this file except in compliance with the License.
xorjoep 1:24714b45cd1b 18 * You may obtain a copy of the License at
xorjoep 1:24714b45cd1b 19 *
xorjoep 1:24714b45cd1b 20 * www.apache.org/licenses/LICENSE-2.0
xorjoep 1:24714b45cd1b 21 *
xorjoep 1:24714b45cd1b 22 * Unless required by applicable law or agreed to in writing, software
xorjoep 1:24714b45cd1b 23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
xorjoep 1:24714b45cd1b 24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
xorjoep 1:24714b45cd1b 25 * See the License for the specific language governing permissions and
xorjoep 1:24714b45cd1b 26 * limitations under the License.
xorjoep 1:24714b45cd1b 27 */
xorjoep 1:24714b45cd1b 28
xorjoep 1:24714b45cd1b 29 #include "arm_math.h"
xorjoep 1:24714b45cd1b 30
xorjoep 1:24714b45cd1b 31 /**
xorjoep 1:24714b45cd1b 32 * @ingroup groupMath
xorjoep 1:24714b45cd1b 33 */
xorjoep 1:24714b45cd1b 34
xorjoep 1:24714b45cd1b 35 /**
xorjoep 1:24714b45cd1b 36 * @addtogroup dot_prod
xorjoep 1:24714b45cd1b 37 * @{
xorjoep 1:24714b45cd1b 38 */
xorjoep 1:24714b45cd1b 39
xorjoep 1:24714b45cd1b 40 /**
xorjoep 1:24714b45cd1b 41 * @brief Dot product of Q31 vectors.
xorjoep 1:24714b45cd1b 42 * @param[in] *pSrcA points to the first input vector
xorjoep 1:24714b45cd1b 43 * @param[in] *pSrcB points to the second input vector
xorjoep 1:24714b45cd1b 44 * @param[in] blockSize number of samples in each vector
xorjoep 1:24714b45cd1b 45 * @param[out] *result output result returned here
xorjoep 1:24714b45cd1b 46 * @return none.
xorjoep 1:24714b45cd1b 47 *
xorjoep 1:24714b45cd1b 48 * <b>Scaling and Overflow Behavior:</b>
xorjoep 1:24714b45cd1b 49 * \par
xorjoep 1:24714b45cd1b 50 * The intermediate multiplications are in 1.31 x 1.31 = 2.62 format and these
xorjoep 1:24714b45cd1b 51 * are truncated to 2.48 format by discarding the lower 14 bits.
xorjoep 1:24714b45cd1b 52 * The 2.48 result is then added without saturation to a 64-bit accumulator in 16.48 format.
xorjoep 1:24714b45cd1b 53 * There are 15 guard bits in the accumulator and there is no risk of overflow as long as
xorjoep 1:24714b45cd1b 54 * the length of the vectors is less than 2^16 elements.
xorjoep 1:24714b45cd1b 55 * The return result is in 16.48 format.
xorjoep 1:24714b45cd1b 56 */
xorjoep 1:24714b45cd1b 57
xorjoep 1:24714b45cd1b 58 void arm_dot_prod_q31(
xorjoep 1:24714b45cd1b 59 q31_t * pSrcA,
xorjoep 1:24714b45cd1b 60 q31_t * pSrcB,
xorjoep 1:24714b45cd1b 61 uint32_t blockSize,
xorjoep 1:24714b45cd1b 62 q63_t * result)
xorjoep 1:24714b45cd1b 63 {
xorjoep 1:24714b45cd1b 64 q63_t sum = 0; /* Temporary result storage */
xorjoep 1:24714b45cd1b 65 uint32_t blkCnt; /* loop counter */
xorjoep 1:24714b45cd1b 66
xorjoep 1:24714b45cd1b 67
xorjoep 1:24714b45cd1b 68 #if defined (ARM_MATH_DSP)
xorjoep 1:24714b45cd1b 69
xorjoep 1:24714b45cd1b 70 /* Run the below code for Cortex-M4 and Cortex-M3 */
xorjoep 1:24714b45cd1b 71 q31_t inA1, inA2, inA3, inA4;
xorjoep 1:24714b45cd1b 72 q31_t inB1, inB2, inB3, inB4;
xorjoep 1:24714b45cd1b 73
xorjoep 1:24714b45cd1b 74 /*loop Unrolling */
xorjoep 1:24714b45cd1b 75 blkCnt = blockSize >> 2U;
xorjoep 1:24714b45cd1b 76
xorjoep 1:24714b45cd1b 77 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
xorjoep 1:24714b45cd1b 78 ** a second loop below computes the remaining 1 to 3 samples. */
xorjoep 1:24714b45cd1b 79 while (blkCnt > 0U)
xorjoep 1:24714b45cd1b 80 {
xorjoep 1:24714b45cd1b 81 /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
xorjoep 1:24714b45cd1b 82 /* Calculate dot product and then store the result in a temporary buffer. */
xorjoep 1:24714b45cd1b 83 inA1 = *pSrcA++;
xorjoep 1:24714b45cd1b 84 inA2 = *pSrcA++;
xorjoep 1:24714b45cd1b 85 inA3 = *pSrcA++;
xorjoep 1:24714b45cd1b 86 inA4 = *pSrcA++;
xorjoep 1:24714b45cd1b 87 inB1 = *pSrcB++;
xorjoep 1:24714b45cd1b 88 inB2 = *pSrcB++;
xorjoep 1:24714b45cd1b 89 inB3 = *pSrcB++;
xorjoep 1:24714b45cd1b 90 inB4 = *pSrcB++;
xorjoep 1:24714b45cd1b 91
xorjoep 1:24714b45cd1b 92 sum += ((q63_t) inA1 * inB1) >> 14U;
xorjoep 1:24714b45cd1b 93 sum += ((q63_t) inA2 * inB2) >> 14U;
xorjoep 1:24714b45cd1b 94 sum += ((q63_t) inA3 * inB3) >> 14U;
xorjoep 1:24714b45cd1b 95 sum += ((q63_t) inA4 * inB4) >> 14U;
xorjoep 1:24714b45cd1b 96
xorjoep 1:24714b45cd1b 97 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 98 blkCnt--;
xorjoep 1:24714b45cd1b 99 }
xorjoep 1:24714b45cd1b 100
xorjoep 1:24714b45cd1b 101 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
xorjoep 1:24714b45cd1b 102 ** No loop unrolling is used. */
xorjoep 1:24714b45cd1b 103 blkCnt = blockSize % 0x4U;
xorjoep 1:24714b45cd1b 104
xorjoep 1:24714b45cd1b 105 #else
xorjoep 1:24714b45cd1b 106
xorjoep 1:24714b45cd1b 107 /* Run the below code for Cortex-M0 */
xorjoep 1:24714b45cd1b 108
xorjoep 1:24714b45cd1b 109 /* Initialize blkCnt with number of samples */
xorjoep 1:24714b45cd1b 110 blkCnt = blockSize;
xorjoep 1:24714b45cd1b 111
xorjoep 1:24714b45cd1b 112 #endif /* #if defined (ARM_MATH_DSP) */
xorjoep 1:24714b45cd1b 113
xorjoep 1:24714b45cd1b 114
xorjoep 1:24714b45cd1b 115 while (blkCnt > 0U)
xorjoep 1:24714b45cd1b 116 {
xorjoep 1:24714b45cd1b 117 /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
xorjoep 1:24714b45cd1b 118 /* Calculate dot product and then store the result in a temporary buffer. */
xorjoep 1:24714b45cd1b 119 sum += ((q63_t) * pSrcA++ * *pSrcB++) >> 14U;
xorjoep 1:24714b45cd1b 120
xorjoep 1:24714b45cd1b 121 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 122 blkCnt--;
xorjoep 1:24714b45cd1b 123 }
xorjoep 1:24714b45cd1b 124
xorjoep 1:24714b45cd1b 125 /* Store the result in the destination buffer in 16.48 format */
xorjoep 1:24714b45cd1b 126 *result = sum;
xorjoep 1:24714b45cd1b 127 }
xorjoep 1:24714b45cd1b 128
xorjoep 1:24714b45cd1b 129 /**
xorjoep 1:24714b45cd1b 130 * @} end of dot_prod group
xorjoep 1:24714b45cd1b 131 */