arm_mat_add_f32.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_add_f32.c
  4. * Description: Floating-point matrix addition
  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 MatrixAdd Matrix Addition
  34. *
  35. * Adds two matrices.
  36. * \image html MatrixAddition.gif "Addition 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 MatrixAdd
  44. * @{
  45. */
  46. /**
  47. * @brief Floating-point matrix addition.
  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_add_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 addition */
  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
  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. /* Loop unrolling */
  84. blkCnt = numSamples >> 2U;
  85. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  86. ** a second loop below computes the remaining 1 to 3 samples. */
  87. while (blkCnt > 0U)
  88. {
  89. /* C(m,n) = A(m,n) + B(m,n) */
  90. /* Add and then store the results in the destination buffer. */
  91. /* Read values from source A */
  92. inA1 = pIn1[0];
  93. /* Read values from source B */
  94. inB1 = pIn2[0];
  95. /* Read values from source A */
  96. inA2 = pIn1[1];
  97. /* out = sourceA + sourceB */
  98. out1 = inA1 + inB1;
  99. /* Read values from source B */
  100. inB2 = pIn2[1];
  101. /* Read values from source A */
  102. inA1 = pIn1[2];
  103. /* out = sourceA + sourceB */
  104. out2 = inA2 + inB2;
  105. /* Read values from source B */
  106. inB1 = pIn2[2];
  107. /* Store result in destination */
  108. pOut[0] = out1;
  109. pOut[1] = out2;
  110. /* Read values from source A */
  111. inA2 = pIn1[3];
  112. /* Read values from source B */
  113. inB2 = pIn2[3];
  114. /* out = sourceA + sourceB */
  115. out1 = inA1 + inB1;
  116. /* out = sourceA + sourceB */
  117. out2 = inA2 + inB2;
  118. /* Store result in destination */
  119. pOut[2] = out1;
  120. /* Store result in destination */
  121. pOut[3] = out2;
  122. /* update pointers to process next sampels */
  123. pIn1 += 4U;
  124. pIn2 += 4U;
  125. pOut += 4U;
  126. /* Decrement the loop counter */
  127. blkCnt--;
  128. }
  129. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  130. ** No loop unrolling is used. */
  131. blkCnt = numSamples % 0x4U;
  132. #else
  133. /* Run the below code for Cortex-M0 */
  134. /* Initialize blkCnt with number of samples */
  135. blkCnt = numSamples;
  136. #endif /* #if defined (ARM_MATH_DSP) */
  137. while (blkCnt > 0U)
  138. {
  139. /* C(m,n) = A(m,n) + B(m,n) */
  140. /* Add and then store the results in the destination buffer. */
  141. *pOut++ = (*pIn1++) + (*pIn2++);
  142. /* Decrement the loop counter */
  143. blkCnt--;
  144. }
  145. /* set status as ARM_MATH_SUCCESS */
  146. status = ARM_MATH_SUCCESS;
  147. }
  148. /* Return to application */
  149. return (status);
  150. }
  151. /**
  152. * @} end of MatrixAdd group
  153. */