Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_mat_add_q31.c Source File

arm_mat_add_q31.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_mat_add_q31.c
00004  * Description:  Q31 matrix addition
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 groupMatrix
00033  */
00034 
00035 /**
00036  * @addtogroup MatrixAdd
00037  * @{
00038  */
00039 
00040 /**
00041  * @brief Q31 matrix addition.
00042  * @param[in]       *pSrcA points to the first input matrix structure
00043  * @param[in]       *pSrcB points to the second input matrix structure
00044  * @param[out]      *pDst points to output matrix structure
00045  * @return          The function returns either
00046  * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
00047  *
00048  * <b>Scaling and Overflow Behavior:</b>
00049  * \par
00050  * The function uses saturating arithmetic.
00051  * Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] will be saturated.
00052  */
00053 
00054 arm_status arm_mat_add_q31(
00055   const arm_matrix_instance_q31 * pSrcA,
00056   const arm_matrix_instance_q31 * pSrcB,
00057   arm_matrix_instance_q31 * pDst)
00058 {
00059   q31_t *pIn1 = pSrcA->pData;                    /* input data matrix pointer A */
00060   q31_t *pIn2 = pSrcB->pData;                    /* input data matrix pointer B */
00061   q31_t *pOut = pDst->pData;                     /* output data matrix pointer */
00062   q31_t inA1, inB1;                              /* temporary variables */
00063 
00064 #if defined (ARM_MATH_DSP)
00065 
00066   q31_t inA2, inB2;                              /* temporary variables */
00067   q31_t out1, out2;                              /* temporary variables */
00068 
00069 #endif //      #if defined (ARM_MATH_DSP)
00070 
00071   uint32_t numSamples;                           /* total number of elements in the matrix  */
00072   uint32_t blkCnt;                               /* loop counters */
00073   arm_status status;                             /* status of matrix addition */
00074 
00075 #ifdef ARM_MATH_MATRIX_CHECK
00076   /* Check for matrix mismatch condition */
00077   if ((pSrcA->numRows != pSrcB->numRows) ||
00078      (pSrcA->numCols != pSrcB->numCols) ||
00079      (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
00080   {
00081     /* Set status as ARM_MATH_SIZE_MISMATCH */
00082     status = ARM_MATH_SIZE_MISMATCH;
00083   }
00084   else
00085 #endif
00086   {
00087     /* Total number of samples in the input matrix */
00088     numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
00089 
00090 #if defined (ARM_MATH_DSP)
00091 
00092     /* Run the below code for Cortex-M4 and Cortex-M3 */
00093 
00094     /* Loop Unrolling */
00095     blkCnt = numSamples >> 2U;
00096 
00097 
00098     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00099      ** a second loop below computes the remaining 1 to 3 samples. */
00100     while (blkCnt > 0U)
00101     {
00102       /* C(m,n) = A(m,n) + B(m,n) */
00103       /* Add, saturate and then store the results in the destination buffer. */
00104       /* Read values from source A */
00105       inA1 = pIn1[0];
00106 
00107       /* Read values from source B */
00108       inB1 = pIn2[0];
00109 
00110       /* Read values from source A */
00111       inA2 = pIn1[1];
00112 
00113       /* Add and saturate */
00114       out1 = __QADD(inA1, inB1);
00115 
00116       /* Read values from source B */
00117       inB2 = pIn2[1];
00118 
00119       /* Read values from source A */
00120       inA1 = pIn1[2];
00121 
00122       /* Add and saturate */
00123       out2 = __QADD(inA2, inB2);
00124 
00125       /* Read values from source B */
00126       inB1 = pIn2[2];
00127 
00128       /* Store result in destination */
00129       pOut[0] = out1;
00130       pOut[1] = out2;
00131 
00132       /* Read values from source A */
00133       inA2 = pIn1[3];
00134 
00135       /* Read values from source B */
00136       inB2 = pIn2[3];
00137 
00138       /* Add and saturate */
00139       out1 = __QADD(inA1, inB1);
00140       out2 = __QADD(inA2, inB2);
00141 
00142       /* Store result in destination */
00143       pOut[2] = out1;
00144       pOut[3] = out2;
00145 
00146       /* update pointers to process next sampels */
00147       pIn1 += 4U;
00148       pIn2 += 4U;
00149       pOut += 4U;
00150 
00151       /* Decrement the loop counter */
00152       blkCnt--;
00153     }
00154 
00155     /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
00156      ** No loop unrolling is used. */
00157     blkCnt = numSamples % 0x4U;
00158 
00159 #else
00160 
00161     /* Run the below code for Cortex-M0 */
00162 
00163     /* Initialize blkCnt with number of samples */
00164     blkCnt = numSamples;
00165 
00166 
00167 #endif /* #if defined (ARM_MATH_DSP) */
00168 
00169     while (blkCnt > 0U)
00170     {
00171       /* C(m,n) = A(m,n) + B(m,n) */
00172       /* Add, saturate and then store the results in the destination buffer. */
00173       inA1 = *pIn1++;
00174       inB1 = *pIn2++;
00175 
00176       inA1 = __QADD(inA1, inB1);
00177 
00178       /* Decrement the loop counter */
00179       blkCnt--;
00180 
00181       *pOut++ = inA1;
00182 
00183     }
00184 
00185     /* set status as ARM_MATH_SUCCESS */
00186     status = ARM_MATH_SUCCESS;
00187   }
00188 
00189   /* Return to application */
00190   return (status);
00191 }
00192 
00193 /**
00194  * @} end of MatrixAdd group
00195  */
00196