arm_elementwise_mul_acc_s16.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2022, 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
  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_elementwise_mul_acc_s16
  21. * Description: Accumulative element wise multiplication
  22. *
  23. * $Date: 19 January 2024
  24. * $Revision: V.1.0.0
  25. *
  26. * Target : Arm(R) M-Profile Architecture
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. /**
  32. * @ingroup Public
  33. */
  34. /**
  35. * @addtogroup groupElementwise
  36. * @{
  37. */
  38. /**
  39. * @brief s16 element wise accumulative multiplication of two vectors
  40. *
  41. * @note Refer header file for details.
  42. *
  43. */
  44. arm_cmsis_nn_status arm_elementwise_mul_acc_s16(const int16_t *input_1_vect,
  45. const int16_t *input_2_vect,
  46. const int32_t input_1_offset,
  47. const int32_t input_2_offset,
  48. int16_t *output,
  49. const int32_t out_offset,
  50. const int32_t out_mult,
  51. const int32_t out_shift,
  52. const int32_t out_activation_min,
  53. const int32_t out_activation_max,
  54. const int32_t block_size)
  55. {
  56. (void)input_1_offset;
  57. (void)input_2_offset;
  58. (void)out_offset;
  59. int32_t loop_count;
  60. const int32_t activation_max = (out_activation_max > 0) ? out_activation_max : NN_Q15_MAX;
  61. const int32_t activation_min = (out_activation_max > 0) ? out_activation_min : NN_Q15_MIN;
  62. #if defined(ARM_MATH_MVEI)
  63. loop_count = block_size;
  64. while (loop_count > 0)
  65. {
  66. mve_pred16_t pred = vctp32q(loop_count);
  67. int32x4_t input_1 = vldrhq_z_s32(input_1_vect, pred);
  68. int32x4_t input_2 = vldrhq_z_s32(input_2_vect, pred);
  69. int32x4_t res_0 = vmulq_s32(input_1, input_2);
  70. res_0 = arm_requantize_mve_32x4(res_0, vdupq_n_s32(out_mult), vdupq_n_s32(out_shift));
  71. res_0 = vaddq_s32(res_0, vldrhq_z_s32(output, pred));
  72. res_0 = vmaxq_s32(res_0, vdupq_n_s32(activation_min));
  73. res_0 = vminq_s32(res_0, vdupq_n_s32(activation_max));
  74. vstrhq_p_s32(output, res_0, pred);
  75. input_1_vect += 4;
  76. input_2_vect += 4;
  77. output += 4;
  78. loop_count -= 4;
  79. }
  80. #else
  81. int32_t input_1;
  82. int32_t input_2;
  83. int32_t mul_res;
  84. int32_t two_halfword_1, two_halfword_2;
  85. int16_t mul_1, mul_2;
  86. loop_count = block_size / 2;
  87. while (loop_count > 0)
  88. {
  89. two_halfword_1 = arm_nn_read_q15x2_ia(&input_1_vect);
  90. two_halfword_2 = arm_nn_read_q15x2_ia(&input_2_vect);
  91. #if defined(ARM_MATH_DSP)
  92. mul_res = SMULBB(two_halfword_1, two_halfword_2);
  93. #else
  94. input_1 = (int16_t)(two_halfword_1 & 0xFFFF);
  95. input_2 = (int16_t)(two_halfword_2 & 0xFFFF);
  96. mul_res = input_1 * input_2;
  97. #endif
  98. mul_res = arm_nn_requantize(mul_res, out_mult, out_shift);
  99. mul_res += output[0];
  100. mul_res = MAX(mul_res, activation_min);
  101. mul_res = MIN(mul_res, activation_max);
  102. mul_1 = (int16_t)mul_res;
  103. #if defined(ARM_MATH_DSP)
  104. mul_res = SMULTT(two_halfword_1, two_halfword_2);
  105. #else
  106. input_1 = (int16_t)(two_halfword_1 >> 16);
  107. input_2 = (int16_t)(two_halfword_2 >> 16);
  108. mul_res = input_1 * input_2;
  109. #endif
  110. mul_res = arm_nn_requantize(mul_res, out_mult, out_shift);
  111. mul_res += output[1];
  112. mul_res = MAX(mul_res, activation_min);
  113. mul_res = MIN(mul_res, activation_max);
  114. mul_2 = (int16_t)mul_res;
  115. arm_nn_write_q15x2_ia(&output, PACK_Q15x2_32x1(mul_1, mul_2));
  116. loop_count--;
  117. }
  118. loop_count = block_size & 0x1;
  119. while (loop_count > 0)
  120. {
  121. input_1 = *input_1_vect++;
  122. input_2 = *input_2_vect++;
  123. mul_res = input_1 * input_2;
  124. mul_res = arm_nn_requantize(mul_res, out_mult, out_shift);
  125. mul_res += output[0];
  126. mul_res = MAX(mul_res, activation_min);
  127. mul_res = MIN(mul_res, activation_max);
  128. *output++ = (int16_t)mul_res;
  129. loop_count--;
  130. }
  131. #endif // #if defined(ARM_MATH_MVEI)
  132. return ARM_CMSIS_NN_SUCCESS;
  133. }
  134. /**
  135. * @} end of Doxygen group
  136. */