arm_elementwise_mul_s16_s8.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_s16_s8.c
  21. * Description: Elementwise multiplication of 16 bit input with 8 bit output
  22. *
  23. * $Date: 20 January 2023
  24. * $Revision: V.2.0.0
  25. *
  26. * Target : Arm(R) M-Profile Architecture
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnsupportfunctions.h"
  30. /**
  31. * @ingroup groupSupport
  32. */
  33. /**
  34. * @addtogroup BasicMath
  35. * @{
  36. */
  37. /*
  38. * s16 elementwise multiplication with s8 output
  39. *
  40. * Refer header file for details.
  41. *
  42. */
  43. arm_cmsis_nn_status arm_elementwise_mul_s16_s8(const int16_t *input_1_vect,
  44. const int16_t *input_2_vect,
  45. int8_t *output,
  46. const int32_t out_offset,
  47. const int32_t out_mult,
  48. const int32_t out_shift,
  49. const int32_t block_size,
  50. const int32_t batch_size,
  51. const int32_t batch_offset)
  52. {
  53. for (int i = 0; i < batch_size; i++)
  54. {
  55. int32_t loop_count = block_size;
  56. #if defined(ARM_MATH_MVEI)
  57. const int16_t *input_1_ptr = input_1_vect;
  58. const int16_t *input_2_ptr = input_2_vect;
  59. int8_t *output_ptr = output;
  60. while (loop_count > 0)
  61. {
  62. mve_pred16_t pred = vctp32q(loop_count);
  63. int32x4_t input_1 = vldrhq_z_s32(input_1_ptr, pred);
  64. int32x4_t input_2 = vldrhq_z_s32(input_2_ptr, pred);
  65. int32x4_t res_0 = vmulq_s32(input_1, input_2);
  66. res_0 = arm_requantize_mve_32x4(res_0, vdupq_n_s32(out_mult), vdupq_n_s32(out_shift));
  67. res_0 = vaddq_n_s32(res_0, out_offset);
  68. res_0 = vmaxq_s32(res_0, vdupq_n_s32(NN_Q7_MIN));
  69. res_0 = vminq_s32(res_0, vdupq_n_s32(NN_Q7_MAX));
  70. vstrbq_p_s32(output_ptr, res_0, pred);
  71. input_1_ptr += 4;
  72. input_2_ptr += 4;
  73. output_ptr += 4;
  74. loop_count -= 4;
  75. }
  76. input_1_vect += block_size;
  77. input_2_vect += block_size;
  78. output += block_size;
  79. #else
  80. #if defined(ARM_MATH_DSP)
  81. while (loop_count > 1)
  82. {
  83. int32_t input_1 = arm_nn_read_q15x2_ia(&input_1_vect);
  84. int32_t input_2 = arm_nn_read_q15x2_ia(&input_2_vect);
  85. int32_t mul_res = SMULBB(input_1, input_2);
  86. mul_res = arm_nn_requantize(mul_res, out_mult, out_shift) + out_offset;
  87. mul_res = CLAMP(mul_res, NN_Q7_MAX, NN_Q7_MIN);
  88. int32_t mul = (int16_t)(mul_res & 0xFF);
  89. mul_res = SMULTT(input_1, input_2);
  90. mul_res = arm_nn_requantize(mul_res, out_mult, out_shift) + out_offset;
  91. mul_res = CLAMP(mul_res, NN_Q7_MAX, NN_Q7_MIN);
  92. mul |= (int16_t)mul_res << 8;
  93. arm_nn_write_s8x2_ia(&output, mul);
  94. loop_count -= 2;
  95. }
  96. #endif
  97. for (int j = 0; j < loop_count; j++, input_1_vect++, input_2_vect++, output++)
  98. {
  99. /* C = A * B */
  100. int32_t mul_res = (*input_1_vect) * (*input_2_vect);
  101. mul_res = arm_nn_requantize(mul_res, out_mult, out_shift) + out_offset;
  102. mul_res = CLAMP(mul_res, NN_Q7_MAX, NN_Q7_MIN);
  103. *output = (int8_t)mul_res;
  104. }
  105. #endif
  106. output += (batch_offset - 1) * block_size;
  107. }
  108. return ARM_CMSIS_NN_SUCCESS;
  109. }
  110. /**
  111. * @} end of BasicMath group
  112. */