arm_elementwise_add_s8.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2010-2020 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: February 27, 2020
  24. * $Revision: V.2.0.1
  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. #if defined(ARM_MATH_MVEI)
  33. #include "arm_helium_utils.h"
  34. #endif
  35. #if defined(ARM_MATH_MVEI)
  36. #define SAT_INPUT_VECT(__INPUT_V, __MULT, __SHIFT) \
  37. __INPUT_V = arm_sat_doubling_high_mult_mve(__INPUT_V, __MULT); \
  38. __INPUT_V = arm_divide_by_power_of_two_mve(__INPUT_V, -__SHIFT);
  39. #endif
  40. #define SAT_INPUT(__INPUT, __MULT, __SHIFT) \
  41. __INPUT = arm_nn_sat_doubling_high_mult(__INPUT, __MULT); \
  42. __INPUT = arm_nn_divide_by_power_of_two(__INPUT, -__SHIFT);
  43. /**
  44. * @ingroup groupNN
  45. */
  46. /**
  47. * @addtogroup BasicMath
  48. * @{
  49. */
  50. /*
  51. * s8 element wise add
  52. *
  53. * Refer header file for details.
  54. *
  55. */
  56. /* Note: __SHIFT is expected to be <=0 */
  57. arm_status
  58. arm_elementwise_add_s8(const int8_t *input_1_vect,
  59. const int8_t *input_2_vect,
  60. const int32_t input_1_offset,
  61. const int32_t input_1_mult,
  62. const int32_t input_1_shift,
  63. const int32_t input_2_offset,
  64. const int32_t input_2_mult,
  65. const int32_t input_2_shift,
  66. const int32_t left_shift,
  67. int8_t *output,
  68. const int32_t out_offset,
  69. const int32_t out_mult,
  70. const int32_t out_shift,
  71. const int32_t out_activation_min,
  72. const int32_t out_activation_max,
  73. const uint32_t block_size)
  74. {
  75. #if defined(ARM_MATH_MVEI)
  76. int32_t count = (int32_t)block_size;
  77. while (count > 0)
  78. {
  79. int32x4_t vect_1;
  80. int32x4_t vect_2;
  81. mve_pred16_t p = vctp32q((uint32_t)count);
  82. vect_1 = vldrbq_z_s32(input_1_vect, p);
  83. vect_2 = vldrbq_z_s32(input_2_vect, p);
  84. vect_1 = vaddq_s32(vect_1, vdupq_n_s32(input_1_offset));
  85. vect_2 = vaddq_s32(vect_2, vdupq_n_s32(input_2_offset));
  86. vect_1 = vshlq_r_s32(vect_1, left_shift);
  87. vect_2 = vshlq_r_s32(vect_2, left_shift);
  88. SAT_INPUT_VECT(vect_1, input_1_mult, input_1_shift);
  89. SAT_INPUT_VECT(vect_2, input_2_mult, input_2_shift);
  90. vect_1 = vaddq_s32(vect_1, vect_2);
  91. SAT_INPUT_VECT(vect_1, out_mult, out_shift);
  92. vect_1 = vaddq_n_s32(vect_1, out_offset);
  93. vect_1 = vmaxq_s32(vect_1, vdupq_n_s32(out_activation_min));
  94. vect_1 = vminq_s32(vect_1, vdupq_n_s32(out_activation_max));
  95. input_1_vect += 4;
  96. input_2_vect += 4;
  97. vstrbq_p_s32(output, vect_1, p);
  98. output += 4;
  99. count -= 4;
  100. }
  101. #else
  102. uint32_t loop_count;
  103. int32_t input_1;
  104. int32_t input_2;
  105. int32_t sum;
  106. #if defined(ARM_MATH_DSP)
  107. int32_t a_1, b_1, a_2, b_2;
  108. int32_t offset_1_packed, offset_2_packed;
  109. int8_t r1, r2, r3, r4;
  110. offset_1_packed = (input_1_offset << 16U) | (input_1_offset & 0x0FFFFL);
  111. offset_2_packed = (input_2_offset << 16U) | (input_2_offset & 0x0FFFFL);
  112. loop_count = block_size >> 2;
  113. while (loop_count > 0U)
  114. {
  115. /* 4 outputs are calculated in one loop. The order of calculation is follows the order of output sign extension
  116. intrinsic */
  117. input_1_vect = read_and_pad_reordered(input_1_vect, &b_1, &a_1);
  118. input_2_vect = read_and_pad_reordered(input_2_vect, &b_2, &a_2);
  119. a_1 = __SADD16(a_1, offset_1_packed);
  120. b_1 = __SADD16(b_1, offset_1_packed);
  121. a_2 = __SADD16(a_2, offset_2_packed);
  122. b_2 = __SADD16(b_2, offset_2_packed);
  123. /* Sum 1 */
  124. input_1 = (int16_t)(b_1 & 0x0FFFFL) << left_shift;
  125. SAT_INPUT(input_1, input_1_mult, input_1_shift);
  126. input_2 = (int16_t)(b_2 & 0x0FFFFL) << left_shift;
  127. SAT_INPUT(input_2, input_2_mult, input_2_shift);
  128. sum = input_1 + input_2;
  129. SAT_INPUT(sum, out_mult, out_shift);
  130. sum += out_offset;
  131. sum = MAX(sum, out_activation_min);
  132. sum = MIN(sum, out_activation_max);
  133. r1 = (q7_t)sum;
  134. /* Sum 3 */
  135. input_1 = (int16_t)((b_1 >> 16) & 0x0FFFFL) << left_shift;
  136. SAT_INPUT(input_1, input_1_mult, input_1_shift);
  137. input_2 = (int16_t)((b_2 >> 16) & 0x0FFFFL) << left_shift;
  138. SAT_INPUT(input_2, input_2_mult, input_2_shift);
  139. sum = input_1 + input_2;
  140. SAT_INPUT(sum, out_mult, out_shift);
  141. sum += out_offset;
  142. sum = MAX(sum, out_activation_min);
  143. sum = MIN(sum, out_activation_max);
  144. r3 = (q7_t)sum;
  145. /* Sum 2 */
  146. input_1 = (int16_t)(a_1 & 0x0FFFFL) << left_shift;
  147. SAT_INPUT(input_1, input_1_mult, input_1_shift);
  148. input_2 = (int16_t)(a_2 & 0x0FFFFL) << left_shift;
  149. SAT_INPUT(input_2, input_2_mult, input_2_shift);
  150. sum = input_1 + input_2;
  151. SAT_INPUT(sum, out_mult, out_shift);
  152. sum += out_offset;
  153. sum = MAX(sum, out_activation_min);
  154. sum = MIN(sum, out_activation_max);
  155. r2 = (q7_t)sum;
  156. /* Sum 4 */
  157. input_1 = (int16_t)((a_1 >> 16) & 0x0FFFFL) << left_shift;
  158. SAT_INPUT(input_1, input_1_mult, input_1_shift);
  159. input_2 = (int16_t)((a_2 >> 16) & 0x0FFFFL) << left_shift;
  160. SAT_INPUT(input_2, input_2_mult, input_2_shift);
  161. sum = input_1 + input_2;
  162. SAT_INPUT(sum, out_mult, out_shift);
  163. sum += out_offset;
  164. sum = MAX(sum, out_activation_min);
  165. sum = MIN(sum, out_activation_max);
  166. r4 = (q7_t)sum;
  167. write_q7x4_ia(&output, __PACKq7(r1, r2, r3, r4));
  168. loop_count--;
  169. }
  170. loop_count = block_size & 0x3;
  171. #else
  172. loop_count = block_size;
  173. #endif
  174. while (loop_count > 0U)
  175. {
  176. /* C = A + B */
  177. input_1 = (*input_1_vect++ + input_1_offset) << left_shift;
  178. input_2 = (*input_2_vect++ + input_2_offset) << left_shift;
  179. input_1 = arm_nn_sat_doubling_high_mult(input_1, input_1_mult);
  180. input_1 = arm_nn_divide_by_power_of_two(input_1, -input_1_shift);
  181. input_2 = arm_nn_sat_doubling_high_mult(input_2, input_2_mult);
  182. input_2 = arm_nn_divide_by_power_of_two(input_2, -input_2_shift);
  183. sum = input_1 + input_2;
  184. SAT_INPUT(sum, out_mult, out_shift);
  185. sum += out_offset;
  186. sum = MAX(sum, out_activation_min);
  187. sum = MIN(sum, out_activation_max);
  188. *output++ = (q7_t)sum;
  189. /* Decrement loop counter */
  190. loop_count--;
  191. }
  192. #endif /* ARM_MATH_MVEI */
  193. return (ARM_MATH_SUCCESS);
  194. }
  195. /**
  196. * @} end of BasicMath group
  197. */