arm_mat_add_q15.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_add_q15.c
  4. * Description: Q15 matrix addition
  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 MatrixAdd
  34. * @{
  35. */
  36. /**
  37. * @brief Q15 matrix addition.
  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 Q15 range [0x8000 0x7FFF] will be saturated.
  48. */
  49. arm_status arm_mat_add_q15(
  50. const arm_matrix_instance_q15 * pSrcA,
  51. const arm_matrix_instance_q15 * pSrcB,
  52. arm_matrix_instance_q15 * pDst)
  53. {
  54. q15_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  55. q15_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  56. q15_t *pOut = pDst->pData; /* output data matrix pointer */
  57. uint16_t numSamples; /* total number of elements in the matrix */
  58. uint32_t blkCnt; /* loop counters */
  59. arm_status status; /* status of matrix addition */
  60. #ifdef ARM_MATH_MATRIX_CHECK
  61. /* Check for matrix mismatch condition */
  62. if ((pSrcA->numRows != pSrcB->numRows) ||
  63. (pSrcA->numCols != pSrcB->numCols) ||
  64. (pSrcA->numRows != pDst->numRows) || (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 the input matrix */
  73. numSamples = (uint16_t) (pSrcA->numRows * pSrcA->numCols);
  74. #if defined (ARM_MATH_DSP)
  75. /* Run the below code for Cortex-M4 and Cortex-M3 */
  76. /* Loop unrolling */
  77. blkCnt = (uint32_t) numSamples >> 2U;
  78. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  79. ** a second loop below computes the remaining 1 to 3 samples. */
  80. while (blkCnt > 0U)
  81. {
  82. /* C(m,n) = A(m,n) + B(m,n) */
  83. /* Add, Saturate and then store the results in the destination buffer. */
  84. *__SIMD32(pOut)++ = __QADD16(*__SIMD32(pInA)++, *__SIMD32(pInB)++);
  85. *__SIMD32(pOut)++ = __QADD16(*__SIMD32(pInA)++, *__SIMD32(pInB)++);
  86. /* Decrement the loop counter */
  87. blkCnt--;
  88. }
  89. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  90. ** No loop unrolling is used. */
  91. blkCnt = (uint32_t) numSamples % 0x4U;
  92. /* q15 pointers of input and output are initialized */
  93. while (blkCnt > 0U)
  94. {
  95. /* C(m,n) = A(m,n) + B(m,n) */
  96. /* Add, Saturate and then store the results in the destination buffer. */
  97. *pOut++ = (q15_t) __QADD16(*pInA++, *pInB++);
  98. /* Decrement the loop counter */
  99. blkCnt--;
  100. }
  101. #else
  102. /* Run the below code for Cortex-M0 */
  103. /* Initialize blkCnt with number of samples */
  104. blkCnt = (uint32_t) numSamples;
  105. /* q15 pointers of input and output are initialized */
  106. while (blkCnt > 0U)
  107. {
  108. /* C(m,n) = A(m,n) + B(m,n) */
  109. /* Add, Saturate and then store the results in the destination buffer. */
  110. *pOut++ = (q15_t) __SSAT(((q31_t) * pInA++ + *pInB++), 16);
  111. /* Decrement the loop counter */
  112. blkCnt--;
  113. }
  114. #endif /* #if defined (ARM_MATH_DSP) */
  115. /* set status as ARM_MATH_SUCCESS */
  116. status = ARM_MATH_SUCCESS;
  117. }
  118. /* Return to application */
  119. return (status);
  120. }
  121. /**
  122. * @} end of MatrixAdd group
  123. */