arm_mat_add_f32.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_add_f32.c
  4. * Description: Floating-point matrix addition
  5. *
  6. * $Date: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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. Adds two matrices.
  35. \image html MatrixAddition.gif "Addition of two 3 x 3 matrices"
  36. The functions check to make sure that
  37. <code>pSrcA</code>, <code>pSrcB</code>, and <code>pDst</code> have the same
  38. number of rows and columns.
  39. */
  40. /**
  41. @addtogroup MatrixAdd
  42. @{
  43. */
  44. /**
  45. @brief Floating-point matrix addition.
  46. @param[in] pSrcA points to first input matrix structure
  47. @param[in] pSrcB points to second input matrix structure
  48. @param[out] pDst points to output matrix structure
  49. @return execution status
  50. - \ref ARM_MATH_SUCCESS : Operation successful
  51. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  52. */
  53. #if defined(ARM_MATH_NEON)
  54. /*
  55. Neon version is assuming the matrix is small enough.
  56. So no blocking is used for taking into account cache effects.
  57. For big matrix, there exist better libraries for Neon.
  58. */
  59. arm_status arm_mat_add_f32(
  60. const arm_matrix_instance_f32 * pSrcA,
  61. const arm_matrix_instance_f32 * pSrcB,
  62. arm_matrix_instance_f32 * pDst)
  63. {
  64. float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  65. float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  66. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  67. float32_t inA1, inA2, inB1, inB2, out1, out2; /* temporary variables */
  68. uint32_t numSamples; /* total number of elements in the matrix */
  69. uint32_t blkCnt; /* loop counters */
  70. arm_status status; /* status of matrix addition */
  71. #ifdef ARM_MATH_MATRIX_CHECK
  72. /* Check for matrix mismatch condition */
  73. if ((pSrcA->numRows != pSrcB->numRows) ||
  74. (pSrcA->numCols != pSrcB->numCols) ||
  75. (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
  76. {
  77. /* Set status as ARM_MATH_SIZE_MISMATCH */
  78. status = ARM_MATH_SIZE_MISMATCH;
  79. }
  80. else
  81. #endif
  82. {
  83. float32x4_t vec1;
  84. float32x4_t vec2;
  85. float32x4_t res;
  86. /* Total number of samples in the input matrix */
  87. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  88. blkCnt = numSamples >> 2U;
  89. /* Compute 4 outputs at a time.
  90. ** a second loop below computes the remaining 1 to 3 samples. */
  91. while (blkCnt > 0U)
  92. {
  93. /* C(m,n) = A(m,n) + B(m,n) */
  94. /* Add and then store the results in the destination buffer. */
  95. vec1 = vld1q_f32(pIn1);
  96. vec2 = vld1q_f32(pIn2);
  97. res = vaddq_f32(vec1, vec2);
  98. vst1q_f32(pOut, res);
  99. /* update pointers to process next samples */
  100. pIn1 += 4U;
  101. pIn2 += 4U;
  102. pOut += 4U;
  103. /* Decrement the loop counter */
  104. blkCnt--;
  105. }
  106. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  107. ** No loop unrolling is used. */
  108. blkCnt = numSamples % 0x4U;
  109. while (blkCnt > 0U)
  110. {
  111. /* C(m,n) = A(m,n) + B(m,n) */
  112. /* Add and then store the results in the destination buffer. */
  113. *pOut++ = (*pIn1++) + (*pIn2++);
  114. /* Decrement the loop counter */
  115. blkCnt--;
  116. }
  117. /* set status as ARM_MATH_SUCCESS */
  118. status = ARM_MATH_SUCCESS;
  119. }
  120. /* Return to application */
  121. return (status);
  122. }
  123. #else
  124. arm_status arm_mat_add_f32(
  125. const arm_matrix_instance_f32 * pSrcA,
  126. const arm_matrix_instance_f32 * pSrcB,
  127. arm_matrix_instance_f32 * pDst)
  128. {
  129. float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  130. float32_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  131. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  132. uint32_t numSamples; /* total number of elements in the matrix */
  133. uint32_t blkCnt; /* loop counters */
  134. arm_status status; /* status of matrix addition */
  135. #ifdef ARM_MATH_MATRIX_CHECK
  136. /* Check for matrix mismatch condition */
  137. if ((pSrcA->numRows != pSrcB->numRows) ||
  138. (pSrcA->numCols != pSrcB->numCols) ||
  139. (pSrcA->numRows != pDst->numRows) ||
  140. (pSrcA->numCols != pDst->numCols) )
  141. {
  142. /* Set status as ARM_MATH_SIZE_MISMATCH */
  143. status = ARM_MATH_SIZE_MISMATCH;
  144. }
  145. else
  146. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  147. {
  148. /* Total number of samples in input matrix */
  149. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  150. #if defined (ARM_MATH_LOOPUNROLL)
  151. /* Loop unrolling: Compute 4 outputs at a time */
  152. blkCnt = numSamples >> 2U;
  153. while (blkCnt > 0U)
  154. {
  155. /* C(m,n) = A(m,n) + B(m,n) */
  156. /* Add and store result in destination buffer. */
  157. *pOut++ = *pInA++ + *pInB++;
  158. *pOut++ = *pInA++ + *pInB++;
  159. *pOut++ = *pInA++ + *pInB++;
  160. *pOut++ = *pInA++ + *pInB++;
  161. /* Decrement loop counter */
  162. blkCnt--;
  163. }
  164. /* Loop unrolling: Compute remaining outputs */
  165. blkCnt = numSamples % 0x4U;
  166. #else
  167. /* Initialize blkCnt with number of samples */
  168. blkCnt = numSamples;
  169. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  170. while (blkCnt > 0U)
  171. {
  172. /* C(m,n) = A(m,n) + B(m,n) */
  173. /* Add and store result in destination buffer. */
  174. *pOut++ = *pInA++ + *pInB++;
  175. /* Decrement loop counter */
  176. blkCnt--;
  177. }
  178. /* Set status as ARM_MATH_SUCCESS */
  179. status = ARM_MATH_SUCCESS;
  180. }
  181. /* Return to application */
  182. return (status);
  183. }
  184. #endif /* #if defined(ARM_MATH_NEON) */
  185. /**
  186. @} end of MatrixAdd group
  187. */