arm_elementwise_add_s16.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2022 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_add_s16
  21. * Description: Elementwise add
  22. *
  23. * $Date: 24 Oct 2022
  24. * $Revision: V.2.2.0
  25. *
  26. * Target Processor: Cortex-M CPUs
  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. * s16 elementwise add
  40. *
  41. * Refer header file for details.
  42. *
  43. */
  44. /* Note: __SHIFT is expected to be <=0 */
  45. arm_cmsis_nn_status arm_elementwise_add_s16(const int16_t *input_1_vect,
  46. const int16_t *input_2_vect,
  47. const int32_t input_1_offset,
  48. const int32_t input_1_mult,
  49. const int32_t input_1_shift,
  50. const int32_t input_2_offset,
  51. const int32_t input_2_mult,
  52. const int32_t input_2_shift,
  53. const int32_t left_shift,
  54. int16_t *output,
  55. const int32_t out_offset,
  56. const int32_t out_mult,
  57. const int32_t out_shift,
  58. const int32_t out_activation_min,
  59. const int32_t out_activation_max,
  60. const int32_t block_size)
  61. {
  62. (void)input_1_offset;
  63. (void)input_2_offset;
  64. (void)out_offset;
  65. #if defined(ARM_MATH_MVEI)
  66. int32_t count = block_size;
  67. while (count > 0)
  68. {
  69. mve_pred16_t pred = vctp32q(count);
  70. int32x4_t vect_1 = vldrhq_z_s32(input_1_vect, pred);
  71. int32x4_t vect_2 = vldrhq_z_s32(input_2_vect, pred);
  72. vect_1 = vshlq_r_s32(vect_1, left_shift);
  73. vect_2 = vshlq_r_s32(vect_2, left_shift);
  74. vect_1 = arm_requantize_mve(vect_1, input_1_mult, input_1_shift);
  75. vect_2 = arm_requantize_mve(vect_2, input_2_mult, input_2_shift);
  76. vect_1 = vaddq_s32(vect_1, vect_2);
  77. vect_1 = arm_requantize_mve(vect_1, out_mult, out_shift);
  78. vect_1 = vmaxq_s32(vect_1, vdupq_n_s32(out_activation_min));
  79. vect_1 = vminq_s32(vect_1, vdupq_n_s32(out_activation_max));
  80. input_1_vect += 4;
  81. input_2_vect += 4;
  82. vstrhq_p_s32(output, vect_1, pred);
  83. output += 4;
  84. count -= 4;
  85. }
  86. #else // #if defined(ARM_MATH_MVEI)
  87. int32_t input_1;
  88. int32_t input_2;
  89. int32_t sum;
  90. int32_t two_halfword_1, two_halfword_2;
  91. int16_t sum_1, sum_2;
  92. int32_t loop_count = block_size / 2;
  93. while (loop_count > 0)
  94. {
  95. two_halfword_1 = arm_nn_read_q15x2_ia(&input_1_vect);
  96. two_halfword_2 = arm_nn_read_q15x2_ia(&input_2_vect);
  97. input_1 = (int16_t)(two_halfword_1 & 0xFFFF) << left_shift;
  98. input_1 = arm_nn_requantize(input_1, input_1_mult, input_1_shift);
  99. input_2 = (int16_t)(two_halfword_2 & 0xFFFF) << left_shift;
  100. input_2 = arm_nn_requantize(input_2, input_2_mult, input_2_shift);
  101. sum = input_1 + input_2;
  102. sum = arm_nn_requantize(sum, out_mult, out_shift);
  103. sum = MAX(sum, out_activation_min);
  104. sum = MIN(sum, out_activation_max);
  105. sum_1 = (int16_t)sum;
  106. input_1 = (int16_t)(two_halfword_1 >> 16) << left_shift;
  107. input_1 = arm_nn_requantize(input_1, input_1_mult, input_1_shift);
  108. input_2 = (int16_t)(two_halfword_2 >> 16) << left_shift;
  109. input_2 = arm_nn_requantize(input_2, input_2_mult, input_2_shift);
  110. sum = input_1 + input_2;
  111. sum = arm_nn_requantize(sum, out_mult, out_shift);
  112. sum = MAX(sum, out_activation_min);
  113. sum = MIN(sum, out_activation_max);
  114. sum_2 = (int16_t)sum;
  115. arm_nn_write_q15x2_ia(&output, PACK_Q15x2_32x1(sum_1, sum_2));
  116. loop_count--;
  117. }
  118. loop_count = block_size & 0x1;
  119. while (loop_count > 0)
  120. {
  121. /* C = A + B */
  122. input_1 = *input_1_vect++ << left_shift;
  123. input_2 = *input_2_vect++ << left_shift;
  124. input_1 = arm_nn_requantize(input_1, input_1_mult, input_1_shift);
  125. input_2 = arm_nn_requantize(input_2, input_2_mult, input_2_shift);
  126. sum = input_1 + input_2;
  127. sum = arm_nn_requantize(sum, out_mult, out_shift);
  128. sum = MAX(sum, out_activation_min);
  129. sum = MIN(sum, out_activation_max);
  130. *output++ = (int16_t)sum;
  131. /* Decrement loop counter */
  132. loop_count--;
  133. }
  134. #endif // #if defined(ARM_MATH_MVEI)
  135. return (ARM_CMSIS_NN_SUCCESS);
  136. }
  137. /**
  138. * @} end of Doxygen group
  139. */