arm_mat_sub_f32.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_sub_f32.c
  4. * Description: Floating-point matrix subtraction
  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 MatrixSub Matrix Subtraction
  34. Subtract two matrices.
  35. \image html MatrixSubtraction.gif "Subraction 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 MatrixSub
  42. @{
  43. */
  44. /**
  45. @brief Floating-point matrix subtraction.
  46. @param[in] pSrcA points to the first input matrix structure
  47. @param[in] pSrcB points to the 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. 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. float32_t inA1, inA2, inB1, inB2, out1, out2; /* temporary variables */
  63. uint32_t numSamples; /* total number of elements in the matrix */
  64. uint32_t blkCnt; /* loop counters */
  65. arm_status status; /* status of matrix subtraction */
  66. #ifdef ARM_MATH_MATRIX_CHECK
  67. /* Check for matrix mismatch condition */
  68. if ((pSrcA->numRows != pSrcB->numRows) ||
  69. (pSrcA->numCols != pSrcB->numCols) ||
  70. (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
  71. {
  72. /* Set status as ARM_MATH_SIZE_MISMATCH */
  73. status = ARM_MATH_SIZE_MISMATCH;
  74. }
  75. else
  76. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  77. {
  78. float32x4_t vec1;
  79. float32x4_t vec2;
  80. float32x4_t res;
  81. /* Total number of samples in the input matrix */
  82. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  83. blkCnt = numSamples >> 2U;
  84. /* Compute 4 outputs at a time.
  85. ** a second loop below computes the remaining 1 to 3 samples. */
  86. while (blkCnt > 0U)
  87. {
  88. /* C(m,n) = A(m,n) - B(m,n) */
  89. /* Subtract and then store the results in the destination buffer. */
  90. /* Read values from source A */
  91. vec1 = vld1q_f32(pIn1);
  92. vec2 = vld1q_f32(pIn2);
  93. res = vsubq_f32(vec1, vec2);
  94. vst1q_f32(pOut, res);
  95. /* Update pointers to process next samples */
  96. pIn1 += 4U;
  97. pIn2 += 4U;
  98. pOut += 4U;
  99. /* Decrement the loop counter */
  100. blkCnt--;
  101. }
  102. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  103. ** No loop unrolling is used. */
  104. blkCnt = numSamples % 0x4U;
  105. while (blkCnt > 0U)
  106. {
  107. /* C(m,n) = A(m,n) - B(m,n) */
  108. /* Subtract and then store the results in the destination buffer. */
  109. *pOut++ = (*pIn1++) - (*pIn2++);
  110. /* Decrement the loop counter */
  111. blkCnt--;
  112. }
  113. /* Set status as ARM_MATH_SUCCESS */
  114. status = ARM_MATH_SUCCESS;
  115. }
  116. /* Return to application */
  117. return (status);
  118. }
  119. #else
  120. arm_status arm_mat_sub_f32(
  121. const arm_matrix_instance_f32 * pSrcA,
  122. const arm_matrix_instance_f32 * pSrcB,
  123. arm_matrix_instance_f32 * pDst)
  124. {
  125. float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  126. float32_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  127. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  128. uint32_t numSamples; /* total number of elements in the matrix */
  129. uint32_t blkCnt; /* loop counters */
  130. arm_status status; /* status of matrix subtraction */
  131. #ifdef ARM_MATH_MATRIX_CHECK
  132. /* Check for matrix mismatch condition */
  133. if ((pSrcA->numRows != pSrcB->numRows) ||
  134. (pSrcA->numCols != pSrcB->numCols) ||
  135. (pSrcA->numRows != pDst->numRows) ||
  136. (pSrcA->numCols != pDst->numCols) )
  137. {
  138. /* Set status as ARM_MATH_SIZE_MISMATCH */
  139. status = ARM_MATH_SIZE_MISMATCH;
  140. }
  141. else
  142. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  143. {
  144. /* Total number of samples in input matrix */
  145. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  146. #if defined (ARM_MATH_LOOPUNROLL)
  147. /* Loop unrolling: Compute 4 outputs at a time */
  148. blkCnt = numSamples >> 2U;
  149. while (blkCnt > 0U)
  150. {
  151. /* C(m,n) = A(m,n) - B(m,n) */
  152. /* Subtract and store result in destination buffer. */
  153. *pOut++ = (*pInA++) - (*pInB++);
  154. *pOut++ = (*pInA++) - (*pInB++);
  155. *pOut++ = (*pInA++) - (*pInB++);
  156. *pOut++ = (*pInA++) - (*pInB++);
  157. /* Decrement loop counter */
  158. blkCnt--;
  159. }
  160. /* Loop unrolling: Compute remaining outputs */
  161. blkCnt = numSamples % 0x4U;
  162. #else
  163. /* Initialize blkCnt with number of samples */
  164. blkCnt = numSamples;
  165. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  166. while (blkCnt > 0U)
  167. {
  168. /* C(m,n) = A(m,n) - B(m,n) */
  169. /* Subtract and store result in destination buffer. */
  170. *pOut++ = (*pInA++) - (*pInB++);
  171. /* Decrement loop counter */
  172. blkCnt--;
  173. }
  174. /* Set status as ARM_MATH_SUCCESS */
  175. status = ARM_MATH_SUCCESS;
  176. }
  177. /* Return to application */
  178. return (status);
  179. }
  180. #endif /* #if defined(ARM_MATH_NEON) */
  181. /**
  182. @} end of MatrixSub group
  183. */