/ Drivers / CMSIS / DSP / Source / BasicMathFunctions / arm_dot_prod_q7.c
arm_dot_prod_q7.c
  1  /* ----------------------------------------------------------------------
  2   * Project:      CMSIS DSP Library
  3   * Title:        arm_dot_prod_q7.c
  4   * Description:  Q7 dot product
  5   *
  6   * $Date:        23 April 2021
  7   * $Revision:    V1.9.0
  8   *
  9   * Target Processor: Cortex-M and Cortex-A cores
 10   * -------------------------------------------------------------------- */
 11  /*
 12   * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
 13   *
 14   * SPDX-License-Identifier: Apache-2.0
 15   *
 16   * Licensed under the Apache License, Version 2.0 (the License); you may
 17   * not use this file except in compliance with the License.
 18   * You may obtain a copy of the License at
 19   *
 20   * www.apache.org/licenses/LICENSE-2.0
 21   *
 22   * Unless required by applicable law or agreed to in writing, software
 23   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 24   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 25   * See the License for the specific language governing permissions and
 26   * limitations under the License.
 27   */
 28  
 29  #include "dsp/basic_math_functions.h"
 30  
 31  /**
 32    @ingroup groupMath
 33   */
 34  
 35  /**
 36    @addtogroup BasicDotProd
 37    @{
 38   */
 39  
 40  /**
 41    @brief         Dot product of Q7 vectors.
 42    @param[in]     pSrcA      points to the first input vector
 43    @param[in]     pSrcB      points to the second input vector
 44    @param[in]     blockSize  number of samples in each vector
 45    @param[out]    result     output result returned here
 46    @return        none
 47  
 48    @par           Scaling and Overflow Behavior
 49                     The intermediate multiplications are in 1.7 x 1.7 = 2.14 format and these
 50                     results are added to an accumulator in 18.14 format.
 51                     Nonsaturating additions are used and there is no danger of wrap around as long as
 52                     the vectors are less than 2^18 elements long.
 53                     The return result is in 18.14 format.
 54   */
 55  
 56  #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
 57  
 58  #include "arm_helium_utils.h"
 59  
 60  void arm_dot_prod_q7(
 61      const q7_t * pSrcA,
 62      const q7_t * pSrcB,
 63      uint32_t blockSize,
 64      q31_t * result)
 65  {
 66      uint32_t  blkCnt;           /* loop counters */
 67      q7x16_t vecA;
 68      q7x16_t vecB;
 69      q31_t     sum = 0;
 70  
 71      /* Compute 16 outputs at a time */
 72      blkCnt = blockSize >> 4;
 73      while (blkCnt > 0U)
 74      {
 75          /*
 76           * C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1]
 77           * Calculate dot product and then store the result in a temporary buffer.
 78           */
 79          vecA = vld1q(pSrcA);
 80          vecB = vld1q(pSrcB);
 81          sum = vmladavaq(sum, vecA, vecB);
 82          /*
 83           * Decrement the blockSize loop counter
 84           */
 85          blkCnt--;
 86          /*
 87           * advance vector source and destination pointers
 88           */
 89          pSrcA += 16;
 90          pSrcB += 16;
 91      }
 92      /*
 93       * tail
 94       */
 95      blkCnt = blockSize & 0xF;
 96      if (blkCnt > 0U)
 97      {
 98          mve_pred16_t p0 = vctp8q(blkCnt);
 99          vecA = vld1q(pSrcA);
100          vecB = vld1q(pSrcB);
101          sum = vmladavaq_p(sum, vecA, vecB, p0);
102      }
103  
104      *result = sum;
105  }
106  #else
107  void arm_dot_prod_q7(
108    const q7_t * pSrcA,
109    const q7_t * pSrcB,
110          uint32_t blockSize,
111          q31_t * result)
112  {
113          uint32_t blkCnt;                               /* Loop counter */
114          q31_t sum = 0;                                 /* Temporary return variable */
115  
116  #if defined (ARM_MATH_LOOPUNROLL)
117  
118  #if defined (ARM_MATH_DSP)
119    q31_t input1, input2;                          /* Temporary variables */
120    q31_t inA1, inA2, inB1, inB2;                  /* Temporary variables */
121  #endif
122  
123    /* Loop unrolling: Compute 4 outputs at a time */
124    blkCnt = blockSize >> 2U;
125  
126    while (blkCnt > 0U)
127    {
128      /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
129  
130  #if defined (ARM_MATH_DSP)
131      /* read 4 samples at a time from sourceA */
132      input1 = read_q7x4_ia (&pSrcA);
133      /* read 4 samples at a time from sourceB */
134      input2 = read_q7x4_ia (&pSrcB);
135  
136      /* extract two q7_t samples to q15_t samples */
137      inA1 = __SXTB16(__ROR(input1, 8));
138      /* extract reminaing two samples */
139      inA2 = __SXTB16(input1);
140      /* extract two q7_t samples to q15_t samples */
141      inB1 = __SXTB16(__ROR(input2, 8));
142      /* extract reminaing two samples */
143      inB2 = __SXTB16(input2);
144  
145      /* multiply and accumulate two samples at a time */
146      sum = __SMLAD(inA1, inB1, sum);
147      sum = __SMLAD(inA2, inB2, sum);
148  #else
149      sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
150      sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
151      sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
152      sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
153  #endif
154  
155      /* Decrement loop counter */
156      blkCnt--;
157    }
158  
159    /* Loop unrolling: Compute remaining outputs */
160    blkCnt = blockSize % 0x4U;
161  
162  #else
163  
164    /* Initialize blkCnt with number of samples */
165    blkCnt = blockSize;
166  
167  #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
168  
169    while (blkCnt > 0U)
170    {
171      /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */
172  
173      /* Calculate dot product and store result in a temporary buffer. */
174  //#if defined (ARM_MATH_DSP)
175  //    sum  = __SMLAD(*pSrcA++, *pSrcB++, sum);
176  //#else
177      sum += (q31_t) ((q15_t) *pSrcA++ * *pSrcB++);
178  //#endif
179  
180      /* Decrement loop counter */
181      blkCnt--;
182    }
183  
184    /* Store result in destination buffer in 18.14 format */
185    *result = sum;
186  }
187  #endif /* defined(ARM_MATH_MVEI) */
188  
189  /**
190    @} end of BasicDotProd group
191   */