arm_nn_mult_q15.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C) 2010-2018 Arm Limited or its affiliates. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /* ----------------------------------------------------------------------
  19. * Project: CMSIS NN Library
  20. * Title: arm_nn_mult_q15.c
  21. * Description: Q15 vector multiplication with variable output shifts
  22. *
  23. * $Date: 13. July 2018
  24. * $Revision: V.1.0.0
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. /**
  31. * @ingroup groupSupport
  32. */
  33. /**
  34. * @addtogroup NNBasicMath
  35. * @{
  36. */
  37. /**
  38. * @brief Q7 vector multiplication with variable output shifts
  39. * @param[in] *pSrcA pointer to the first input vector
  40. * @param[in] *pSrcB pointer to the second input vector
  41. * @param[out] *pDst pointer to the output vector
  42. * @param[in] out_shift amount of right-shift for output
  43. * @param[in] blockSize number of samples in each vector
  44. * @return none.
  45. *
  46. * <b>Scaling and Overflow Behavior:</b>
  47. * \par
  48. * The function uses saturating arithmetic.
  49. * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.
  50. */
  51. void arm_nn_mult_q15(
  52. q15_t * pSrcA,
  53. q15_t * pSrcB,
  54. q15_t * pDst,
  55. const uint16_t out_shift,
  56. uint32_t blockSize)
  57. {
  58. uint32_t blkCnt; /* loop counters */
  59. #if defined (ARM_MATH_DSP)
  60. /* Run the below code for Cortex-M4 and Cortex-M3 */
  61. q31_t inA1, inA2, inB1, inB2; /* temporary input variables */
  62. q15_t out1, out2, out3, out4; /* temporary output variables */
  63. q31_t mul1, mul2, mul3, mul4; /* temporary variables */
  64. /* loop Unrolling */
  65. blkCnt = blockSize >> 2U;
  66. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  67. ** a second loop below computes the remaining 1 to 3 samples. */
  68. while (blkCnt > 0U)
  69. {
  70. /* read two samples at a time from sourceA */
  71. inA1 = *__SIMD32(pSrcA)++;
  72. /* read two samples at a time from sourceB */
  73. inB1 = *__SIMD32(pSrcB)++;
  74. /* read two samples at a time from sourceA */
  75. inA2 = *__SIMD32(pSrcA)++;
  76. /* read two samples at a time from sourceB */
  77. inB2 = *__SIMD32(pSrcB)++;
  78. /* multiply mul = sourceA * sourceB */
  79. mul1 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
  80. mul2 = (q31_t) ((q15_t) inA1 * (q15_t) inB1);
  81. mul3 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB2 >> 16));
  82. mul4 = (q31_t) ((q15_t) inA2 * (q15_t) inB2);
  83. /* saturate result to 16 bit */
  84. out1 = (q15_t) __SSAT((mul1 + NN_ROUND(out_shift)) >> out_shift, 16);
  85. out2 = (q15_t) __SSAT((mul2 + NN_ROUND(out_shift)) >> out_shift, 16);
  86. out3 = (q15_t) __SSAT((mul3 + NN_ROUND(out_shift)) >> out_shift, 16);
  87. out4 = (q15_t) __SSAT((mul4 + NN_ROUND(out_shift)) >> out_shift, 16);
  88. /* store the result */
  89. #ifndef ARM_MATH_BIG_ENDIAN
  90. *__SIMD32(pDst)++ = __PKHBT(out2, out1, 16);
  91. *__SIMD32(pDst)++ = __PKHBT(out4, out3, 16);
  92. #else
  93. *__SIMD32(pDst)++ = __PKHBT(out2, out1, 16);
  94. *__SIMD32(pDst)++ = __PKHBT(out4, out3, 16);
  95. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  96. /* Decrement the blockSize loop counter */
  97. blkCnt--;
  98. }
  99. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  100. ** No loop unrolling is used. */
  101. blkCnt = blockSize % 0x4U;
  102. #else
  103. /* Run the below code for Cortex-M0 */
  104. /* Initialize blkCnt with number of samples */
  105. blkCnt = blockSize;
  106. #endif /* #if defined (ARM_MATH_DSP) */
  107. while (blkCnt > 0U)
  108. {
  109. /* C = A * B */
  110. /* Multiply the inputs and store the result in the destination buffer */
  111. *pDst++ = (q15_t) __SSAT((((q31_t) (*pSrcA++) * (*pSrcB++) + NN_ROUND(out_shift)) >> out_shift), 16);
  112. /* Decrement the blockSize loop counter */
  113. blkCnt--;
  114. }
  115. }
  116. /**
  117. * @} end of NNBasicMath group
  118. */