arm_elementwise_add_s8.c 7.9 KB

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