arm_mat_sub_q31.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_sub_q31.c
  4. * Description: Q31 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. @addtogroup MatrixSub
  34. @{
  35. */
  36. /**
  37. @brief Q31 matrix subtraction.
  38. @param[in] pSrcA points to the first input matrix structure
  39. @param[in] pSrcB points to the second input matrix structure
  40. @param[out] pDst points to output matrix structure
  41. @return execution status
  42. - \ref ARM_MATH_SUCCESS : Operation successful
  43. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  44. @par Scaling and Overflow Behavior
  45. The function uses saturating arithmetic.
  46. Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] are saturated.
  47. */
  48. arm_status arm_mat_sub_q31(
  49. const arm_matrix_instance_q31 * pSrcA,
  50. const arm_matrix_instance_q31 * pSrcB,
  51. arm_matrix_instance_q31 * pDst)
  52. {
  53. q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  54. q31_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  55. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  56. uint32_t numSamples; /* total number of elements in the matrix */
  57. uint32_t blkCnt; /* loop counters */
  58. arm_status status; /* status of matrix subtraction */
  59. #ifdef ARM_MATH_MATRIX_CHECK
  60. /* Check for matrix mismatch condition */
  61. if ((pSrcA->numRows != pSrcB->numRows) ||
  62. (pSrcA->numCols != pSrcB->numCols) ||
  63. (pSrcA->numRows != pDst->numRows) ||
  64. (pSrcA->numCols != pDst->numCols) )
  65. {
  66. /* Set status as ARM_MATH_SIZE_MISMATCH */
  67. status = ARM_MATH_SIZE_MISMATCH;
  68. }
  69. else
  70. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  71. {
  72. /* Total number of samples in input matrix */
  73. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  74. #if defined (ARM_MATH_LOOPUNROLL)
  75. /* Loop unrolling: Compute 4 outputs at a time */
  76. blkCnt = numSamples >> 2U;
  77. while (blkCnt > 0U)
  78. {
  79. /* C(m,n) = A(m,n) - B(m,n) */
  80. /* Subtract, saturate and then store the results in the destination buffer. */
  81. *pOut++ = __QSUB(*pInA++, *pInB++);
  82. *pOut++ = __QSUB(*pInA++, *pInB++);
  83. *pOut++ = __QSUB(*pInA++, *pInB++);
  84. *pOut++ = __QSUB(*pInA++, *pInB++);
  85. /* Decrement loop counter */
  86. blkCnt--;
  87. }
  88. /* Loop unrolling: Compute remaining outputs */
  89. blkCnt = numSamples % 0x4U;
  90. #else
  91. /* Initialize blkCnt with number of samples */
  92. blkCnt = numSamples;
  93. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  94. while (blkCnt > 0U)
  95. {
  96. /* C(m,n) = A(m,n) - B(m,n) */
  97. /* Subtract, saturate and store result in destination buffer. */
  98. *pOut++ = __QSUB(*pInA++, *pInB++);
  99. /* Decrement loop counter */
  100. blkCnt--;
  101. }
  102. /* Set status as ARM_MATH_SUCCESS */
  103. status = ARM_MATH_SUCCESS;
  104. }
  105. /* Return to application */
  106. return (status);
  107. }
  108. /**
  109. @} end of MatrixSub group
  110. */