arm_elementwise_mul_s8.c 5.9 KB

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