arm_mat_sub_f32.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_sub_f32.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. #include "dsp/matrix_functions.h"
  29. /**
  30. @ingroup groupMatrix
  31. */
  32. /**
  33. @defgroup MatrixSub Matrix Subtraction
  34. Subtract two matrices.
  35. @par Subraction of two 3 x 3 matrices
  36. \f[
  37. \begin{pmatrix}
  38. a_{1,1} & a_{1,2} & a_{1,3} \\
  39. a_{2,1} & a_{2,2} & a_{2,3} \\
  40. a_{3,1} & a_{3,2} & a_{3,3} \\
  41. \end{pmatrix}
  42. -
  43. \begin{pmatrix}
  44. b_{1,1} & b_{1,2} & b_{1,3} \\
  45. b_{2,1} & b_{2,2} & b_{2,3} \\
  46. b_{3,1} & b_{3,2} & b_{3,3} \\
  47. \end{pmatrix}
  48. =
  49. \begin{pmatrix}
  50. a_{1,1}-b_{1,1} & a_{1,2}-b_{1,2} & a_{1,3}-b_{1,3} \\
  51. a_{2,1}-b_{2,1} & a_{2,2}-b_{2,2} & a_{2,3}-b_{2,3} \\
  52. a_{3,1}-b_{3,1} & a_{3,2}-b_{3,2} & a_{3,3}-b_{3,3} \\
  53. \end{pmatrix}
  54. \f]
  55. The functions check to make sure that
  56. <code>pSrcA</code>, <code>pSrcB</code>, and <code>pDst</code> have the same
  57. number of rows and columns.
  58. */
  59. /**
  60. @addtogroup MatrixSub
  61. @{
  62. */
  63. /**
  64. @brief Floating-point matrix subtraction.
  65. @param[in] pSrcA points to the first input matrix structure
  66. @param[in] pSrcB points to the second input matrix structure
  67. @param[out] pDst points to output matrix structure
  68. @return execution status
  69. - \ref ARM_MATH_SUCCESS : Operation successful
  70. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  71. */
  72. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  73. arm_status arm_mat_sub_f32(
  74. const arm_matrix_instance_f32 * pSrcA,
  75. const arm_matrix_instance_f32 * pSrcB,
  76. arm_matrix_instance_f32 * pDst)
  77. {
  78. arm_status status; /* status of matrix subtraction */
  79. uint32_t numSamples; /* total number of elements in the matrix */
  80. float32_t *pDataA, *pDataB, *pDataDst;
  81. f32x4_t vecA, vecB, vecDst = { 0 };
  82. float32_t const *pSrcAVec;
  83. float32_t const *pSrcBVec;
  84. uint32_t blkCnt; /* loop counters */
  85. pDataA = pSrcA->pData;
  86. pDataB = pSrcB->pData;
  87. pDataDst = pDst->pData;
  88. pSrcAVec = (float32_t const *) pDataA;
  89. pSrcBVec = (float32_t const *) pDataB;
  90. #ifdef ARM_MATH_MATRIX_CHECK
  91. /* Check for matrix mismatch condition */
  92. if ((pSrcA->numRows != pSrcB->numRows) ||
  93. (pSrcA->numCols != pSrcB->numCols) ||
  94. (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
  95. {
  96. /* Set status as ARM_MATH_SIZE_MISMATCH */
  97. status = ARM_MATH_SIZE_MISMATCH;
  98. }
  99. else
  100. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  101. {
  102. /*
  103. * Total number of samples in the input matrix
  104. */
  105. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  106. blkCnt = numSamples >> 2;
  107. while (blkCnt > 0U)
  108. {
  109. /* C(m,n) = A(m,n) + B(m,n) */
  110. /* sub and then store the results in the destination buffer. */
  111. vecA = vld1q(pSrcAVec);
  112. pSrcAVec += 4;
  113. vecB = vld1q(pSrcBVec);
  114. pSrcBVec += 4;
  115. vecDst = vsubq(vecA, vecB);
  116. vst1q(pDataDst, vecDst);
  117. pDataDst += 4;
  118. /*
  119. * Decrement the blockSize loop counter
  120. */
  121. blkCnt--;
  122. }
  123. /*
  124. * tail
  125. * (will be merged thru tail predication)
  126. */
  127. blkCnt = numSamples & 3;
  128. if (blkCnt > 0U)
  129. {
  130. mve_pred16_t p0 = vctp32q(blkCnt);
  131. vecA = vld1q(pSrcAVec);
  132. vecB = vld1q(pSrcBVec);
  133. vecDst = vsubq_m(vecDst, vecA, vecB, p0);
  134. vstrwq_p(pDataDst, vecDst, p0);
  135. }
  136. status = ARM_MATH_SUCCESS;
  137. }
  138. /* Return to application */
  139. return (status);
  140. }
  141. #else
  142. #if defined(ARM_MATH_NEON)
  143. arm_status arm_mat_sub_f32(
  144. const arm_matrix_instance_f32 * pSrcA,
  145. const arm_matrix_instance_f32 * pSrcB,
  146. arm_matrix_instance_f32 * pDst)
  147. {
  148. float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  149. float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  150. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  151. uint32_t numSamples; /* total number of elements in the matrix */
  152. uint32_t blkCnt; /* loop counters */
  153. arm_status status; /* status of matrix subtraction */
  154. #ifdef ARM_MATH_MATRIX_CHECK
  155. /* Check for matrix mismatch condition */
  156. if ((pSrcA->numRows != pSrcB->numRows) ||
  157. (pSrcA->numCols != pSrcB->numCols) ||
  158. (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
  159. {
  160. /* Set status as ARM_MATH_SIZE_MISMATCH */
  161. status = ARM_MATH_SIZE_MISMATCH;
  162. }
  163. else
  164. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  165. {
  166. float32x4_t vec1;
  167. float32x4_t vec2;
  168. float32x4_t res;
  169. /* Total number of samples in the input matrix */
  170. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  171. blkCnt = numSamples >> 2U;
  172. /* Compute 4 outputs at a time.
  173. ** a second loop below computes the remaining 1 to 3 samples. */
  174. while (blkCnt > 0U)
  175. {
  176. /* C(m,n) = A(m,n) - B(m,n) */
  177. /* Subtract and then store the results in the destination buffer. */
  178. /* Read values from source A */
  179. vec1 = vld1q_f32(pIn1);
  180. vec2 = vld1q_f32(pIn2);
  181. res = vsubq_f32(vec1, vec2);
  182. vst1q_f32(pOut, res);
  183. /* Update pointers to process next samples */
  184. pIn1 += 4U;
  185. pIn2 += 4U;
  186. pOut += 4U;
  187. /* Decrement the loop counter */
  188. blkCnt--;
  189. }
  190. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  191. ** No loop unrolling is used. */
  192. blkCnt = numSamples % 0x4U;
  193. while (blkCnt > 0U)
  194. {
  195. /* C(m,n) = A(m,n) - B(m,n) */
  196. /* Subtract and then store the results in the destination buffer. */
  197. *pOut++ = (*pIn1++) - (*pIn2++);
  198. /* Decrement the loop counter */
  199. blkCnt--;
  200. }
  201. /* Set status as ARM_MATH_SUCCESS */
  202. status = ARM_MATH_SUCCESS;
  203. }
  204. /* Return to application */
  205. return (status);
  206. }
  207. #else
  208. arm_status arm_mat_sub_f32(
  209. const arm_matrix_instance_f32 * pSrcA,
  210. const arm_matrix_instance_f32 * pSrcB,
  211. arm_matrix_instance_f32 * pDst)
  212. {
  213. float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  214. float32_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  215. float32_t *pOut = pDst->pData; /* output data matrix pointer */
  216. uint32_t numSamples; /* total number of elements in the matrix */
  217. uint32_t blkCnt; /* loop counters */
  218. arm_status status; /* status of matrix subtraction */
  219. #ifdef ARM_MATH_MATRIX_CHECK
  220. /* Check for matrix mismatch condition */
  221. if ((pSrcA->numRows != pSrcB->numRows) ||
  222. (pSrcA->numCols != pSrcB->numCols) ||
  223. (pSrcA->numRows != pDst->numRows) ||
  224. (pSrcA->numCols != pDst->numCols) )
  225. {
  226. /* Set status as ARM_MATH_SIZE_MISMATCH */
  227. status = ARM_MATH_SIZE_MISMATCH;
  228. }
  229. else
  230. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  231. {
  232. /* Total number of samples in input matrix */
  233. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  234. #if defined (ARM_MATH_LOOPUNROLL)
  235. /* Loop unrolling: Compute 4 outputs at a time */
  236. blkCnt = numSamples >> 2U;
  237. while (blkCnt > 0U)
  238. {
  239. /* C(m,n) = A(m,n) - B(m,n) */
  240. /* Subtract and store result in destination buffer. */
  241. *pOut++ = (*pInA++) - (*pInB++);
  242. *pOut++ = (*pInA++) - (*pInB++);
  243. *pOut++ = (*pInA++) - (*pInB++);
  244. *pOut++ = (*pInA++) - (*pInB++);
  245. /* Decrement loop counter */
  246. blkCnt--;
  247. }
  248. /* Loop unrolling: Compute remaining outputs */
  249. blkCnt = numSamples % 0x4U;
  250. #else
  251. /* Initialize blkCnt with number of samples */
  252. blkCnt = numSamples;
  253. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  254. while (blkCnt > 0U)
  255. {
  256. /* C(m,n) = A(m,n) - B(m,n) */
  257. /* Subtract and store result in destination buffer. */
  258. *pOut++ = (*pInA++) - (*pInB++);
  259. /* Decrement loop counter */
  260. blkCnt--;
  261. }
  262. /* Set status as ARM_MATH_SUCCESS */
  263. status = ARM_MATH_SUCCESS;
  264. }
  265. /* Return to application */
  266. return (status);
  267. }
  268. #endif /* #if defined(ARM_MATH_NEON) */
  269. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  270. /**
  271. @} end of MatrixSub group
  272. */