arm_elementwise_add_s8.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2010-2019 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_elementwise_add_s8
  21. * Description: Element wise add
  22. *
  23. * $Date: 7. August 2019
  24. * $Revision: V.1.0.0
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_math.h"
  30. #include "arm_nnfunctions.h"
  31. #include "arm_nnsupportfunctions.h"
  32. /**
  33. * @ingroup groupNN
  34. */
  35. /**
  36. * @addtogroup BasicMath
  37. * @{
  38. */
  39. /*
  40. * s8 element wise add
  41. *
  42. * Refer header file for details.
  43. *
  44. */
  45. #define SAT_INPUT(__INPUT, __MULT, __SHIFT) \
  46. __INPUT = arm_nn_sat_doubling_high_mult(__INPUT, __MULT); \
  47. __INPUT = arm_nn_divide_by_power_of_two(__INPUT, -__SHIFT);
  48. arm_status
  49. arm_elementwise_add_s8(const int8_t *input_1_vect,
  50. const int8_t *input_2_vect,
  51. const int32_t input_1_offset,
  52. const int32_t input_1_mult,
  53. const int32_t input_1_shift,
  54. const int32_t input_2_offset,
  55. const int32_t input_2_mult,
  56. const int32_t input_2_shift,
  57. const int32_t left_shift,
  58. int8_t *output,
  59. const int32_t out_offset,
  60. const int32_t out_mult,
  61. const int32_t out_shift,
  62. const int32_t out_activation_min,
  63. const int32_t out_activation_max,
  64. const uint32_t block_size)
  65. {
  66. uint32_t loop_count;
  67. int32_t input_1;
  68. int32_t input_2;
  69. int32_t sum;
  70. #if defined(ARM_MATH_LOOPUNROLL) && defined(ARM_MATH_DSP)
  71. int32_t a_1, b_1, a_2, b_2;
  72. int32_t offset_1_packed, offset_2_packed;
  73. int8_t r1, r2, r3, r4;
  74. offset_1_packed = (input_1_offset << 16U) | (input_1_offset & 0x0FFFFL);
  75. offset_2_packed = (input_2_offset << 16U) | (input_2_offset & 0x0FFFFL);
  76. loop_count = block_size >> 2;
  77. while (loop_count > 0U)
  78. {
  79. /* 4 outputs are calculated in one loop. The order of calculation is follows the order of output sign extension
  80. intrinsic */
  81. input_1_vect = read_and_pad_reordered(input_1_vect, &b_1, &a_1);
  82. input_2_vect = read_and_pad_reordered(input_2_vect, &b_2, &a_2);
  83. a_1 = __SADD16(a_1, offset_1_packed);
  84. b_1 = __SADD16(b_1, offset_1_packed);
  85. a_2 = __SADD16(a_2, offset_2_packed);
  86. b_2 = __SADD16(b_2, offset_2_packed);
  87. /* Sum 1 */
  88. input_1 = (int16_t)(b_1 & 0x0FFFFL) << left_shift;
  89. SAT_INPUT(input_1, input_1_mult, input_1_shift);
  90. input_2 = (int16_t)(b_2 & 0x0FFFFL) << left_shift;
  91. SAT_INPUT(input_2, input_2_mult, input_2_shift);
  92. sum = input_1 + input_2;
  93. sum = arm_nn_requantize(sum, out_mult, out_shift) + out_offset;
  94. sum = MAX(sum, out_activation_min);
  95. sum = MIN(sum, out_activation_max);
  96. r1 = (q7_t)sum;
  97. /* Sum 3 */
  98. input_1 = (int16_t)((b_1 >> 16) & 0x0FFFFL) << left_shift;
  99. SAT_INPUT(input_1, input_1_mult, input_1_shift);
  100. input_2 = (int16_t)((b_2 >> 16) & 0x0FFFFL) << left_shift;
  101. SAT_INPUT(input_2, input_2_mult, input_2_shift);
  102. sum = input_1 + input_2;
  103. sum = arm_nn_requantize(sum, out_mult, out_shift) + out_offset;
  104. sum = MAX(sum, out_activation_min);
  105. sum = MIN(sum, out_activation_max);
  106. r3 = (q7_t)sum;
  107. /* Sum 2 */
  108. input_1 = (int16_t)(a_1 & 0x0FFFFL) << left_shift;
  109. SAT_INPUT(input_1, input_1_mult, input_1_shift);
  110. input_2 = (int16_t)(a_2 & 0x0FFFFL) << left_shift;
  111. SAT_INPUT(input_2, input_2_mult, input_2_shift);
  112. sum = input_1 + input_2;
  113. sum = arm_nn_requantize(sum, out_mult, out_shift) + out_offset;
  114. sum = MAX(sum, out_activation_min);
  115. sum = MIN(sum, out_activation_max);
  116. r2 = (q7_t)sum;
  117. /* Sum 4 */
  118. input_1 = (int16_t)((a_1 >> 16) & 0x0FFFFL) << left_shift;
  119. SAT_INPUT(input_1, input_1_mult, input_1_shift);
  120. input_2 = (int16_t)((a_2 >> 16) & 0x0FFFFL) << left_shift;
  121. SAT_INPUT(input_2, input_2_mult, input_2_shift);
  122. sum = input_1 + input_2;
  123. sum = arm_nn_requantize(sum, out_mult, out_shift) + out_offset;
  124. sum = MAX(sum, out_activation_min);
  125. sum = MIN(sum, out_activation_max);
  126. r4 = (q7_t)sum;
  127. write_q7x4_ia(&output, __PACKq7(r1, r2, r3, r4));
  128. loop_count--;
  129. }
  130. loop_count = block_size & 0x3;
  131. #else
  132. loop_count = block_size;
  133. #endif
  134. while (loop_count > 0U)
  135. {
  136. /* C = A + B */
  137. input_1 = (*input_1_vect++ + input_1_offset) << left_shift;
  138. input_2 = (*input_2_vect++ + input_2_offset) << left_shift;
  139. input_1 = arm_nn_sat_doubling_high_mult(input_1, input_1_mult);
  140. input_1 = arm_nn_divide_by_power_of_two(input_1, -input_1_shift);
  141. input_2 = arm_nn_sat_doubling_high_mult(input_2, input_2_mult);
  142. input_2 = arm_nn_divide_by_power_of_two(input_2, -input_2_shift);
  143. sum = input_1 + input_2;
  144. sum = arm_nn_requantize(sum, out_mult, out_shift) + out_offset;
  145. sum = MAX(sum, out_activation_min);
  146. sum = MIN(sum, out_activation_max);
  147. *output++ = (q7_t)sum;
  148. /* Decrement loop counter */
  149. loop_count--;
  150. }
  151. return (ARM_MATH_SUCCESS);
  152. }
  153. /**
  154. * @} end of BasicMath group
  155. */