Subversion Repositories dashGPS

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /* ----------------------------------------------------------------------
  2.  * Project:      CMSIS DSP Library
  3.  * Title:        arm_mat_mult_q31.c
  4.  * Description:  Q31 matrix multiplication
  5.  *
  6.  * $Date:        27. January 2017
  7.  * $Revision:    V.1.5.1
  8.  *
  9.  * Target Processor: Cortex-M cores
  10.  * -------------------------------------------------------------------- */
  11. /*
  12.  * Copyright (C) 2010-2017 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 "arm_math.h"
  30.  
  31. /**
  32.  * @ingroup groupMatrix
  33.  */
  34.  
  35. /**
  36.  * @addtogroup MatrixMult
  37.  * @{
  38.  */
  39.  
  40. /**
  41.  * @brief Q31 matrix multiplication
  42.  * @param[in]       *pSrcA points to the first input matrix structure
  43.  * @param[in]       *pSrcB points to the second input matrix structure
  44.  * @param[out]      *pDst points to output matrix structure
  45.  * @return              The function returns either
  46.  * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  47.  *
  48.  * @details
  49.  * <b>Scaling and Overflow Behavior:</b>
  50.  *
  51.  * \par
  52.  * The function is implemented using an internal 64-bit accumulator.
  53.  * The accumulator has a 2.62 format and maintains full precision of the intermediate
  54.  * multiplication results but provides only a single guard bit. There is no saturation
  55.  * on intermediate additions. Thus, if the accumulator overflows it wraps around and
  56.  * distorts the result. The input signals should be scaled down to avoid intermediate
  57.  * overflows. The input is thus scaled down by log2(numColsA) bits
  58.  * to avoid overflows, as a total of numColsA additions are performed internally.
  59.  * The 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result.
  60.  *
  61.  * \par
  62.  * See <code>arm_mat_mult_fast_q31()</code> for a faster but less precise implementation of this function for Cortex-M3 and Cortex-M4.
  63.  *
  64.  */
  65.  
  66. arm_status arm_mat_mult_q31(
  67.   const arm_matrix_instance_q31 * pSrcA,
  68.   const arm_matrix_instance_q31 * pSrcB,
  69.   arm_matrix_instance_q31 * pDst)
  70. {
  71.   q31_t *pIn1 = pSrcA->pData;                    /* input data matrix pointer A */
  72.   q31_t *pIn2 = pSrcB->pData;                    /* input data matrix pointer B */
  73.   q31_t *pInA = pSrcA->pData;                    /* input data matrix pointer A */
  74.   q31_t *pOut = pDst->pData;                     /* output data matrix pointer */
  75.   q31_t *px;                                     /* Temporary output data matrix pointer */
  76.   q63_t sum;                                     /* Accumulator */
  77.   uint16_t numRowsA = pSrcA->numRows;            /* number of rows of input matrix A    */
  78.   uint16_t numColsB = pSrcB->numCols;            /* number of columns of input matrix B */
  79.   uint16_t numColsA = pSrcA->numCols;            /* number of columns of input matrix A */
  80.  
  81. #if defined (ARM_MATH_DSP)
  82.  
  83.   /* Run the below code for Cortex-M4 and Cortex-M3 */
  84.  
  85.   uint16_t col, i = 0U, j, row = numRowsA, colCnt;      /* loop counters */
  86.   arm_status status;                             /* status of matrix multiplication */
  87.   q31_t a0, a1, a2, a3, b0, b1, b2, b3;
  88.  
  89. #ifdef ARM_MATH_MATRIX_CHECK
  90.  
  91.  
  92.   /* Check for matrix mismatch condition */
  93.   if ((pSrcA->numCols != pSrcB->numRows) ||
  94.      (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  95.   {
  96.     /* Set status as ARM_MATH_SIZE_MISMATCH */
  97.     status = ARM_MATH_SIZE_MISMATCH;
  98.   }
  99.   else
  100. #endif /*    #ifdef ARM_MATH_MATRIX_CHECK    */
  101.  
  102.   {
  103.     /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  104.     /* row loop */
  105.     do
  106.     {
  107.       /* Output pointer is set to starting address of the row being processed */
  108.       px = pOut + i;
  109.  
  110.       /* For every row wise process, the column loop counter is to be initiated */
  111.       col = numColsB;
  112.  
  113.       /* For every row wise process, the pIn2 pointer is set
  114.        ** to the starting address of the pSrcB data */
  115.       pIn2 = pSrcB->pData;
  116.  
  117.       j = 0U;
  118.  
  119.       /* column loop */
  120.       do
  121.       {
  122.         /* Set the variable sum, that acts as accumulator, to zero */
  123.         sum = 0;
  124.  
  125.         /* Initiate the pointer pIn1 to point to the starting address of pInA */
  126.         pIn1 = pInA;
  127.  
  128.         /* Apply loop unrolling and compute 4 MACs simultaneously. */
  129.         colCnt = numColsA >> 2;
  130.  
  131.  
  132.         /* matrix multiplication */
  133.         while (colCnt > 0U)
  134.         {
  135.           /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  136.           /* Perform the multiply-accumulates */
  137.           b0 = *pIn2;
  138.           pIn2 += numColsB;
  139.  
  140.           a0 = *pIn1++;
  141.           a1 = *pIn1++;
  142.  
  143.           b1 = *pIn2;
  144.           pIn2 += numColsB;
  145.           b2 = *pIn2;
  146.           pIn2 += numColsB;
  147.  
  148.           sum += (q63_t) a0 *b0;
  149.           sum += (q63_t) a1 *b1;
  150.  
  151.           a2 = *pIn1++;
  152.           a3 = *pIn1++;
  153.  
  154.           b3 = *pIn2;
  155.           pIn2 += numColsB;
  156.  
  157.           sum += (q63_t) a2 *b2;
  158.           sum += (q63_t) a3 *b3;
  159.  
  160.           /* Decrement the loop counter */
  161.           colCnt--;
  162.         }
  163.  
  164.         /* If the columns of pSrcA is not a multiple of 4, compute any remaining output samples here.
  165.          ** No loop unrolling is used. */
  166.         colCnt = numColsA % 0x4U;
  167.  
  168.         while (colCnt > 0U)
  169.         {
  170.           /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  171.           /* Perform the multiply-accumulates */
  172.           sum += (q63_t) * pIn1++ * *pIn2;
  173.           pIn2 += numColsB;
  174.  
  175.           /* Decrement the loop counter */
  176.           colCnt--;
  177.         }
  178.  
  179.         /* Convert the result from 2.62 to 1.31 format and store in destination buffer */
  180.         *px++ = (q31_t) (sum >> 31);
  181.  
  182.         /* Update the pointer pIn2 to point to the  starting address of the next column */
  183.         j++;
  184.         pIn2 = (pSrcB->pData) + j;
  185.  
  186.         /* Decrement the column loop counter */
  187.         col--;
  188.  
  189.       } while (col > 0U);
  190.  
  191. #else
  192.  
  193.   /* Run the below code for Cortex-M0 */
  194.  
  195.   q31_t *pInB = pSrcB->pData;                    /* input data matrix pointer B */
  196.   uint16_t col, i = 0U, row = numRowsA, colCnt;  /* loop counters */
  197.   arm_status status;                             /* status of matrix multiplication */
  198.  
  199.  
  200. #ifdef ARM_MATH_MATRIX_CHECK
  201.  
  202.   /* Check for matrix mismatch condition */
  203.   if ((pSrcA->numCols != pSrcB->numRows) ||
  204.      (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols))
  205.   {
  206.     /* Set status as ARM_MATH_SIZE_MISMATCH */
  207.     status = ARM_MATH_SIZE_MISMATCH;
  208.   }
  209.   else
  210. #endif /*    #ifdef ARM_MATH_MATRIX_CHECK    */
  211.  
  212.   {
  213.     /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */
  214.     /* row loop */
  215.     do
  216.     {
  217.       /* Output pointer is set to starting address of the row being processed */
  218.       px = pOut + i;
  219.  
  220.       /* For every row wise process, the column loop counter is to be initiated */
  221.       col = numColsB;
  222.  
  223.       /* For every row wise process, the pIn2 pointer is set
  224.        ** to the starting address of the pSrcB data */
  225.       pIn2 = pSrcB->pData;
  226.  
  227.       /* column loop */
  228.       do
  229.       {
  230.         /* Set the variable sum, that acts as accumulator, to zero */
  231.         sum = 0;
  232.  
  233.         /* Initiate the pointer pIn1 to point to the starting address of pInA */
  234.         pIn1 = pInA;
  235.  
  236.         /* Matrix A columns number of MAC operations are to be performed */
  237.         colCnt = numColsA;
  238.  
  239.         /* matrix multiplication */
  240.         while (colCnt > 0U)
  241.         {
  242.           /* c(m,n) = a(1,1)*b(1,1) + a(1,2) * b(2,1) + .... + a(m,p)*b(p,n) */
  243.           /* Perform the multiply-accumulates */
  244.           sum += (q63_t) * pIn1++ * *pIn2;
  245.           pIn2 += numColsB;
  246.  
  247.           /* Decrement the loop counter */
  248.           colCnt--;
  249.         }
  250.  
  251.         /* Convert the result from 2.62 to 1.31 format and store in destination buffer */
  252.         *px++ = (q31_t) clip_q63_to_q31(sum >> 31);
  253.  
  254.         /* Decrement the column loop counter */
  255.         col--;
  256.  
  257.         /* Update the pointer pIn2 to point to the  starting address of the next column */
  258.         pIn2 = pInB + (numColsB - col);
  259.  
  260.       } while (col > 0U);
  261.  
  262. #endif
  263.  
  264.       /* Update the pointer pInA to point to the  starting address of the next row */
  265.       i = i + numColsB;
  266.       pInA = pInA + numColsA;
  267.  
  268.       /* Decrement the row loop counter */
  269.       row--;
  270.  
  271.     } while (row > 0U);
  272.  
  273.     /* set status as ARM_MATH_SUCCESS */
  274.     status = ARM_MATH_SUCCESS;
  275.   }
  276.   /* Return to application */
  277.   return (status);
  278. }
  279.  
  280. /**
  281.  * @} end of MatrixMult group
  282.  */
  283.