arm_mat_sub_f16.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mat_sub_f16.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_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. /**
  31. @ingroup groupMatrix
  32. */
  33. /**
  34. @addtogroup MatrixSub
  35. @{
  36. */
  37. /**
  38. @brief Floating-point matrix subtraction.
  39. @param[in] pSrcA points to the first input matrix structure
  40. @param[in] pSrcB points to the second input matrix structure
  41. @param[out] pDst points to output matrix structure
  42. @return execution status
  43. - \ref ARM_MATH_SUCCESS : Operation successful
  44. - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
  45. */
  46. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  47. arm_status arm_mat_sub_f16(
  48. const arm_matrix_instance_f16 * pSrcA,
  49. const arm_matrix_instance_f16 * pSrcB,
  50. arm_matrix_instance_f16 * pDst)
  51. {
  52. arm_status status; /* status of matrix subtraction */
  53. uint32_t numSamples; /* total number of elements in the matrix */
  54. float16_t *pDataA, *pDataB, *pDataDst;
  55. f16x8_t vecA, vecB, vecDst;
  56. float16_t const *pSrcAVec;
  57. float16_t const *pSrcBVec;
  58. uint32_t blkCnt; /* loop counters */
  59. pDataA = pSrcA->pData;
  60. pDataB = pSrcB->pData;
  61. pDataDst = pDst->pData;
  62. pSrcAVec = (float16_t const *) pDataA;
  63. pSrcBVec = (float16_t const *) pDataB;
  64. #ifdef ARM_MATH_MATRIX_CHECK
  65. /* Check for matrix mismatch condition */
  66. if ((pSrcA->numRows != pSrcB->numRows) ||
  67. (pSrcA->numCols != pSrcB->numCols) ||
  68. (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols))
  69. {
  70. /* Set status as ARM_MATH_SIZE_MISMATCH */
  71. status = ARM_MATH_SIZE_MISMATCH;
  72. }
  73. else
  74. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  75. {
  76. /*
  77. * Total number of samples in the input matrix
  78. */
  79. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  80. blkCnt = numSamples >> 3;
  81. while (blkCnt > 0U)
  82. {
  83. /* C(m,n) = A(m,n) + B(m,n) */
  84. /* sub and then store the results in the destination buffer. */
  85. vecA = vld1q(pSrcAVec);
  86. pSrcAVec += 8;
  87. vecB = vld1q(pSrcBVec);
  88. pSrcBVec += 8;
  89. vecDst = vsubq(vecA, vecB);
  90. vst1q(pDataDst, vecDst);
  91. pDataDst += 8;
  92. /*
  93. * Decrement the blockSize loop counter
  94. */
  95. blkCnt--;
  96. }
  97. /*
  98. * tail
  99. * (will be merged thru tail predication)
  100. */
  101. blkCnt = numSamples & 7;
  102. if (blkCnt > 0U)
  103. {
  104. mve_pred16_t p0 = vctp16q(blkCnt);
  105. vecA = vld1q(pSrcAVec);
  106. vecB = vld1q(pSrcBVec);
  107. vecDst = vsubq_m(vecDst, vecA, vecB, p0);
  108. vstrhq_p(pDataDst, vecDst, p0);
  109. }
  110. status = ARM_MATH_SUCCESS;
  111. }
  112. /* Return to application */
  113. return (status);
  114. }
  115. #else
  116. arm_status arm_mat_sub_f16(
  117. const arm_matrix_instance_f16 * pSrcA,
  118. const arm_matrix_instance_f16 * pSrcB,
  119. arm_matrix_instance_f16 * pDst)
  120. {
  121. float16_t *pInA = pSrcA->pData; /* input data matrix pointer A */
  122. float16_t *pInB = pSrcB->pData; /* input data matrix pointer B */
  123. float16_t *pOut = pDst->pData; /* output data matrix pointer */
  124. uint32_t numSamples; /* total number of elements in the matrix */
  125. uint32_t blkCnt; /* loop counters */
  126. arm_status status; /* status of matrix subtraction */
  127. #ifdef ARM_MATH_MATRIX_CHECK
  128. /* Check for matrix mismatch condition */
  129. if ((pSrcA->numRows != pSrcB->numRows) ||
  130. (pSrcA->numCols != pSrcB->numCols) ||
  131. (pSrcA->numRows != pDst->numRows) ||
  132. (pSrcA->numCols != pDst->numCols) )
  133. {
  134. /* Set status as ARM_MATH_SIZE_MISMATCH */
  135. status = ARM_MATH_SIZE_MISMATCH;
  136. }
  137. else
  138. #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
  139. {
  140. /* Total number of samples in input matrix */
  141. numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols;
  142. #if defined (ARM_MATH_LOOPUNROLL)
  143. /* Loop unrolling: Compute 4 outputs at a time */
  144. blkCnt = numSamples >> 2U;
  145. while (blkCnt > 0U)
  146. {
  147. /* C(m,n) = A(m,n) - B(m,n) */
  148. /* Subtract and store result in destination buffer. */
  149. *pOut++ = (_Float16)(*pInA++) - (_Float16)(*pInB++);
  150. *pOut++ = (_Float16)(*pInA++) - (_Float16)(*pInB++);
  151. *pOut++ = (_Float16)(*pInA++) - (_Float16)(*pInB++);
  152. *pOut++ = (_Float16)(*pInA++) - (_Float16)(*pInB++);
  153. /* Decrement loop counter */
  154. blkCnt--;
  155. }
  156. /* Loop unrolling: Compute remaining outputs */
  157. blkCnt = numSamples % 0x4U;
  158. #else
  159. /* Initialize blkCnt with number of samples */
  160. blkCnt = numSamples;
  161. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  162. while (blkCnt > 0U)
  163. {
  164. /* C(m,n) = A(m,n) - B(m,n) */
  165. /* Subtract and store result in destination buffer. */
  166. *pOut++ = (_Float16)(*pInA++) - (_Float16)(*pInB++);
  167. /* Decrement loop counter */
  168. blkCnt--;
  169. }
  170. /* Set status as ARM_MATH_SUCCESS */
  171. status = ARM_MATH_SUCCESS;
  172. }
  173. /* Return to application */
  174. return (status);
  175. }
  176. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  177. /**
  178. @} end of MatrixSub group
  179. */
  180. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */