arm_mat_sub_f32.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_sub_f32.c
  4. * Description: Floating-point matrix subtraction
  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. #include "arm_math.h"
  29. /**
  30. * @ingroup groupMatrix
  31. */
  32. /**
  33. * @defgroup MatrixSub Matrix Subtraction
  34. *
  35. * Subtract two matrices.
  36. * \image html MatrixSubtraction.gif "Subraction of two 3 x 3 matrices"
  37. *
  38. * The functions check to make sure that
  39. * <code>pSrcA</code>, <code>pSrcB</code>, and <code>pDst</code> have the same
  40. * number of rows and columns.
  41. */
  42. /**
  43. * @addtogroup MatrixSub
  44. * @{
  45. */
  46. /**
  47. * @brief Floating-point matrix subtraction
  48. * @param[in] *pSrcA points to the first input matrix structure
  49. * @param[in] *pSrcB points to the second input matrix structure
  50. * @param[out] *pDst points to output matrix structure
  51. * @return The function returns either
  52. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  53. */
  54. arm_status arm_mat_sub_f32(
  55. const arm_matrix_instance_f32 * pSrcA,
  56. const arm_matrix_instance_f32 * pSrcB,
  57. arm_matrix_instance_f32 * pDst)
  58. {
  59. float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  60. float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  61. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  62. #if defined (ARM_MATH_DSP)
  63. float32_t inA1, inA2, inB1, inB2, out1, out2; /* temporary variables */
  64. #endif // #if defined (ARM_MATH_DSP)
  65. uint32_t numSamples; /* total number of elements in the matrix */
  66. uint32_t blkCnt; /* loop counters */
  67. arm_status status; /* status of matrix subtraction */
  68. #ifdef ARM_MATH_MATRIX_CHECK
  69. /* Check for matrix mismatch condition */
  70. if ((pSrcA->numRows != pSrcB->numRows) ||
  71. (pSrcA->numCols != pSrcB->numCols) ||
  72. (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
  73. {
  74. /* Set status as ARM_MATH_SIZE_MISMATCH */
  75. status = ARM_MATH_SIZE_MISMATCH;
  76. }
  77. else
  78. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  79. {
  80. /* Total number of samples in the input matrix */
  81. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  82. #if defined (ARM_MATH_DSP)
  83. /* Run the below code for Cortex-M4 and Cortex-M3 */
  84. /* Loop Unrolling */
  85. blkCnt = numSamples >> 2U;
  86. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  87. ** a second loop below computes the remaining 1 to 3 samples. */
  88. while (blkCnt > 0U)
  89. {
  90. /* C(m,n) = A(m,n) - B(m,n) */
  91. /* Subtract and then store the results in the destination buffer. */
  92. /* Read values from source A */
  93. inA1 = pIn1[0];
  94. /* Read values from source B */
  95. inB1 = pIn2[0];
  96. /* Read values from source A */
  97. inA2 = pIn1[1];
  98. /* out = sourceA - sourceB */
  99. out1 = inA1 - inB1;
  100. /* Read values from source B */
  101. inB2 = pIn2[1];
  102. /* Read values from source A */
  103. inA1 = pIn1[2];
  104. /* out = sourceA - sourceB */
  105. out2 = inA2 - inB2;
  106. /* Read values from source B */
  107. inB1 = pIn2[2];
  108. /* Store result in destination */
  109. pOut[0] = out1;
  110. pOut[1] = out2;
  111. /* Read values from source A */
  112. inA2 = pIn1[3];
  113. /* Read values from source B */
  114. inB2 = pIn2[3];
  115. /* out = sourceA - sourceB */
  116. out1 = inA1 - inB1;
  117. /* out = sourceA - sourceB */
  118. out2 = inA2 - inB2;
  119. /* Store result in destination */
  120. pOut[2] = out1;
  121. /* Store result in destination */
  122. pOut[3] = out2;
  123. /* update pointers to process next sampels */
  124. pIn1 += 4U;
  125. pIn2 += 4U;
  126. pOut += 4U;
  127. /* Decrement the loop counter */
  128. blkCnt--;
  129. }
  130. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  131. ** No loop unrolling is used. */
  132. blkCnt = numSamples % 0x4U;
  133. #else
  134. /* Run the below code for Cortex-M0 */
  135. /* Initialize blkCnt with number of samples */
  136. blkCnt = numSamples;
  137. #endif /* #if defined (ARM_MATH_DSP) */
  138. while (blkCnt > 0U)
  139. {
  140. /* C(m,n) = A(m,n) - B(m,n) */
  141. /* Subtract and then store the results in the destination buffer. */
  142. *pOut++ = (*pIn1++) - (*pIn2++);
  143. /* Decrement the loop counter */
  144. blkCnt--;
  145. }
  146. /* Set status as ARM_MATH_SUCCESS */
  147. status = ARM_MATH_SUCCESS;
  148. }
  149. /* Return to application */
  150. return (status);
  151. }
  152. /**
  153. * @} end of MatrixSub group
  154. */