arm_mat_sub_q31.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_sub_q31.c
  4. * Description: Q31 matrix subtraction
  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. * @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 The function returns either
  42. * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
  43. *
  44. * <b>Scaling and Overflow Behavior:</b>
  45. * \par
  46. * The function uses saturating arithmetic.
  47. * Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] will be saturated.
  48. */
  49. arm_status arm_mat_sub_q31(
  50. const arm_matrix_instance_q31 * pSrcA,
  51. const arm_matrix_instance_q31 * pSrcB,
  52. arm_matrix_instance_q31 * pDst)
  53. {
  54. q31_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */
  55. q31_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */
  56. q31_t *pOut = pDst->pData; /* output data matrix pointer */
  57. q31_t inA1, inB1; /* temporary variables */
  58. #if defined (ARM_MATH_DSP)
  59. q31_t inA2, inB2; /* temporary variables */
  60. q31_t out1, out2; /* temporary variables */
  61. #endif // #if defined (ARM_MATH_DSP)
  62. uint32_t numSamples; /* total number of elements in the matrix */
  63. uint32_t blkCnt; /* loop counters */
  64. arm_status status; /* status of matrix subtraction */
  65. #ifdef ARM_MATH_MATRIX_CHECK
  66. /* Check for matrix mismatch condition */
  67. if ((pSrcA->numRows != pSrcB->numRows) ||
  68. (pSrcA->numCols != pSrcB->numCols) ||
  69. (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
  70. {
  71. /* Set status as ARM_MATH_SIZE_MISMATCH */
  72. status = ARM_MATH_SIZE_MISMATCH;
  73. }
  74. else
  75. #endif
  76. {
  77. /* Total number of samples in the input matrix */
  78. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  79. #if defined (ARM_MATH_DSP)
  80. /* Run the below code for Cortex-M4 and Cortex-M3 */
  81. /* Loop Unrolling */
  82. blkCnt = numSamples >> 2U;
  83. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  84. ** a second loop below computes the remaining 1 to 3 samples. */
  85. while (blkCnt > 0U)
  86. {
  87. /* C(m,n) = A(m,n) - B(m,n) */
  88. /* Subtract, saturate and then store the results in the destination buffer. */
  89. /* Read values from source A */
  90. inA1 = pIn1[0];
  91. /* Read values from source B */
  92. inB1 = pIn2[0];
  93. /* Read values from source A */
  94. inA2 = pIn1[1];
  95. /* Subtract and saturate */
  96. out1 = __QSUB(inA1, inB1);
  97. /* Read values from source B */
  98. inB2 = pIn2[1];
  99. /* Read values from source A */
  100. inA1 = pIn1[2];
  101. /* Subtract and saturate */
  102. out2 = __QSUB(inA2, inB2);
  103. /* Read values from source B */
  104. inB1 = pIn2[2];
  105. /* Store result in destination */
  106. pOut[0] = out1;
  107. pOut[1] = out2;
  108. /* Read values from source A */
  109. inA2 = pIn1[3];
  110. /* Read values from source B */
  111. inB2 = pIn2[3];
  112. /* Subtract and saturate */
  113. out1 = __QSUB(inA1, inB1);
  114. /* Subtract and saturate */
  115. out2 = __QSUB(inA2, inB2);
  116. /* Store result in destination */
  117. pOut[2] = out1;
  118. pOut[3] = out2;
  119. /* update pointers to process next samples */
  120. pIn1 += 4U;
  121. pIn2 += 4U;
  122. pOut += 4U;
  123. /* Decrement the loop counter */
  124. blkCnt--;
  125. }
  126. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  127. ** No loop unrolling is used. */
  128. blkCnt = numSamples % 0x4U;
  129. #else
  130. /* Run the below code for Cortex-M0 */
  131. /* Initialize blkCnt with number of samples */
  132. blkCnt = numSamples;
  133. #endif /* #if defined (ARM_MATH_DSP) */
  134. while (blkCnt > 0U)
  135. {
  136. /* C(m,n) = A(m,n) - B(m,n) */
  137. /* Subtract, saturate and then store the results in the destination buffer. */
  138. inA1 = *pIn1++;
  139. inB1 = *pIn2++;
  140. inA1 = __QSUB(inA1, inB1);
  141. *pOut++ = inA1;
  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 MatrixSub group
  153. */