/ Drivers / CMSIS / DSP / Source / MatrixFunctions / arm_mat_sub_f64.c
arm_mat_sub_f64.c
  1  /* ----------------------------------------------------------------------
  2   * Project:      CMSIS DSP Library
  3   * Title:        arm_mat_sub_f64.c
  4   * Description:  Floating-point matrix subtraction
  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/matrix_functions.h"
 30  
 31  /**
 32    @ingroup groupMatrix
 33   */
 34  
 35  /**
 36    @defgroup MatrixSub Matrix Subtraction
 37  
 38    Subtract two matrices.
 39    \image html MatrixSubtraction.gif "Subraction of two 3 x 3 matrices"
 40  
 41    The functions check to make sure that
 42    <code>pSrcA</code>, <code>pSrcB</code>, and <code>pDst</code> have the same
 43    number of rows and columns.
 44   */
 45  
 46  /**
 47    @addtogroup MatrixSub
 48    @{
 49   */
 50  
 51  /**
 52    @brief         Floating-point matrix subtraction.
 53    @param[in]     pSrcA      points to the first input matrix structure
 54    @param[in]     pSrcB      points to the second input matrix structure
 55    @param[out]    pDst       points to output matrix structure
 56    @return        execution status
 57                     - \ref ARM_MATH_SUCCESS       : Operation successful
 58                     - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
 59   */
 60  
 61  arm_status arm_mat_sub_f64(
 62    const arm_matrix_instance_f64 * pSrcA,
 63    const arm_matrix_instance_f64 * pSrcB,
 64          arm_matrix_instance_f64 * pDst)
 65  {
 66    float64_t *pInA = pSrcA->pData;                /* input data matrix pointer A */
 67    float64_t *pInB = pSrcB->pData;                /* input data matrix pointer B */
 68    float64_t *pOut = pDst->pData;                 /* output data matrix pointer */
 69  
 70    uint64_t numSamples;                           /* total number of elements in the matrix */
 71    uint64_t blkCnt;                               /* loop counters */
 72    arm_status status;                             /* status of matrix subtraction */
 73  
 74  #ifdef ARM_MATH_MATRIX_CHECK
 75  
 76    /* Check for matrix mismatch condition */
 77    if ((pSrcA->numRows != pSrcB->numRows) ||
 78        (pSrcA->numCols != pSrcB->numCols) ||
 79        (pSrcA->numRows != pDst->numRows)  ||
 80        (pSrcA->numCols != pDst->numCols)    )
 81    {
 82      /* Set status as ARM_MATH_SIZE_MISMATCH */
 83      status = ARM_MATH_SIZE_MISMATCH;
 84    }
 85    else
 86  
 87  #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
 88  
 89    {
 90      /* Total number of samples in input matrix */
 91      numSamples = (uint64_t) pSrcA->numRows * pSrcA->numCols;
 92  
 93  #if defined (ARM_MATH_LOOPUNROLL)
 94  
 95      /* Loop unrolling: Compute 4 outputs at a time */
 96      blkCnt = numSamples >> 2U;
 97  
 98      while (blkCnt > 0U)
 99      {
100        /* C(m,n) = A(m,n) - B(m,n) */
101  
102        /* Subtract and store result in destination buffer. */
103        *pOut++ = (*pInA++) - (*pInB++);
104        *pOut++ = (*pInA++) - (*pInB++);
105        *pOut++ = (*pInA++) - (*pInB++);
106        *pOut++ = (*pInA++) - (*pInB++);
107  
108        /* Decrement loop counter */
109        blkCnt--;
110      }
111  
112      /* Loop unrolling: Compute remaining outputs */
113      blkCnt = numSamples % 0x4U;
114  
115  #else
116  
117      /* Initialize blkCnt with number of samples */
118      blkCnt = numSamples;
119  
120  #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
121  
122      while (blkCnt > 0U)
123      {
124        /* C(m,n) = A(m,n) - B(m,n) */
125  
126        /* Subtract and store result in destination buffer. */
127        *pOut++ = (*pInA++) - (*pInB++);
128  
129        /* Decrement loop counter */
130        blkCnt--;
131      }
132  
133      /* Set status as ARM_MATH_SUCCESS */
134      status = ARM_MATH_SUCCESS;
135    }
136  
137    /* Return to application */
138    return (status);
139  }
140  
141  /**
142    @} end of MatrixSub group
143   */