1 /*
2 * SPDX-FileCopyrightText: Copyright 2010-2023 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 /* ----------------------------------------------------------------------
20 * Project: CMSIS NN Library
21 * Title: arm_elementwise_mul_s8
22 * Description: Element wise multiplication
23 *
24 * $Date: 20 January 2023
25 * $Revision: V.2.2.0
26 *
27 * Target : Arm(R) M-Profile Architecture
28 *
29 * -------------------------------------------------------------------- */
30
31 #include "arm_nnfunctions.h"
32 #include "arm_nnsupportfunctions.h"
33
34 /**
35 * @ingroup Public
36 */
37
38 /**
39 * @addtogroup groupElementwise
40 * @{
41 */
42
43 /*
44 * s8 element wise multiplication of two vectors
45 *
46 * Refer header file for details.
47 *
48 */
49
arm_elementwise_mul_s8(const int8_t * input_1_vect,const int8_t * input_2_vect,const int32_t input_1_offset,const int32_t input_2_offset,int8_t * output,const int32_t out_offset,const int32_t out_mult,const int32_t out_shift,const int32_t out_activation_min,const int32_t out_activation_max,const int32_t block_size)50 arm_cmsis_nn_status arm_elementwise_mul_s8(const int8_t *input_1_vect,
51 const int8_t *input_2_vect,
52 const int32_t input_1_offset,
53 const int32_t input_2_offset,
54 int8_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
63 int32_t loop_count;
64 #if defined(ARM_MATH_MVEI)
65
66 loop_count = (block_size + 3) / 4;
67 uint32_t num_elements = block_size;
68
69 for (int i = 0; i < loop_count; i++)
70 {
71 mve_pred16_t p = vctp32q(num_elements);
72
73 int32x4_t input_1 = vldrbq_z_s32(input_1_vect, p);
74 input_1 = vaddq_n_s32(input_1, input_1_offset);
75
76 int32x4_t input_2 = vldrbq_z_s32(input_2_vect, p);
77 input_2 = vaddq_n_s32(input_2, input_2_offset);
78
79 int32x4_t res_0 = vmulq_s32(input_1, input_2);
80
81 res_0 = arm_requantize_mve_32x4(res_0, vdupq_n_s32(out_mult), vdupq_n_s32(out_shift));
82
83 res_0 += vdupq_n_s32(out_offset);
84
85 res_0 = vmaxq_s32(res_0, vdupq_n_s32(out_activation_min));
86 res_0 = vminq_s32(res_0, vdupq_n_s32(out_activation_max));
87
88 vstrbq_p_s32(output, res_0, p);
89 input_1_vect += 4;
90 input_2_vect += 4;
91 output += 4;
92 num_elements -= 4;
93 }
94
95 #else
96 int32_t input_1;
97 int32_t input_2;
98 int32_t mul_res;
99
100 #if defined(ARM_MATH_DSP)
101 int32_t a_1, b_1, a_2, b_2;
102
103 int32_t offset_1_packed, offset_2_packed;
104
105 int8_t r1, r2, r3, r4;
106
107 offset_1_packed = (input_1_offset << 16U) | (input_1_offset & 0x0FFFFL);
108 offset_2_packed = (input_2_offset << 16U) | (input_2_offset & 0x0FFFFL);
109
110 loop_count = block_size >> 2;
111
112 while (loop_count > 0)
113 {
114 /* 4 outputs are calculated in one loop. The order of calculation is follows the order of output sign extension
115 intrinsic */
116 input_1_vect = read_and_pad_reordered(input_1_vect, &b_1, &a_1);
117 input_2_vect = read_and_pad_reordered(input_2_vect, &b_2, &a_2);
118
119 a_1 = SADD16(a_1, offset_1_packed);
120 b_1 = SADD16(b_1, offset_1_packed);
121
122 a_2 = SADD16(a_2, offset_2_packed);
123 b_2 = SADD16(b_2, offset_2_packed);
124
125 /* Mul 1 */
126 mul_res = SMULBB(b_1, b_2);
127 mul_res = arm_nn_requantize(mul_res, out_mult, out_shift) + out_offset;
128
129 mul_res = MAX(mul_res, out_activation_min);
130 mul_res = MIN(mul_res, out_activation_max);
131 r1 = (int8_t)mul_res;
132
133 /* Mul 3 */
134 mul_res = SMULTT(b_1, b_2);
135 mul_res = arm_nn_requantize(mul_res, out_mult, out_shift) + out_offset;
136 mul_res = MAX(mul_res, out_activation_min);
137 mul_res = MIN(mul_res, out_activation_max);
138 r3 = (int8_t)mul_res;
139
140 /* Mul 2 */
141 mul_res = SMULBB(a_1, a_2);
142 mul_res = arm_nn_requantize(mul_res, out_mult, out_shift) + out_offset;
143 mul_res = MAX(mul_res, out_activation_min);
144 mul_res = MIN(mul_res, out_activation_max);
145 r2 = (int8_t)mul_res;
146
147 /* Mul 4 */
148 mul_res = SMULTT(a_1, a_2);
149 mul_res = arm_nn_requantize(mul_res, out_mult, out_shift) + out_offset;
150 mul_res = MAX(mul_res, out_activation_min);
151 mul_res = MIN(mul_res, out_activation_max);
152 r4 = (int8_t)mul_res;
153
154 arm_nn_write_s8x4_ia(&output, PACK_S8x4_32x1(r1, r2, r3, r4));
155
156 loop_count--;
157 }
158
159 loop_count = block_size & 0x3;
160 #else
161 loop_count = block_size;
162 #endif
163
164 while (loop_count > 0)
165 {
166 /* C = A * B */
167
168 input_1 = *input_1_vect++ + input_1_offset;
169 input_2 = *input_2_vect++ + input_2_offset;
170
171 mul_res = input_1 * input_2;
172 mul_res = arm_nn_requantize(mul_res, out_mult, out_shift) + out_offset;
173
174 mul_res = MAX(mul_res, out_activation_min);
175 mul_res = MIN(mul_res, out_activation_max);
176
177 *output++ = (int8_t)mul_res;
178
179 /* Decrement loop counter */
180 loop_count--;
181 }
182 #endif
183 return ARM_CMSIS_NN_SUCCESS;
184 }
185
186 /**
187 * @} end of Doxygen group
188 */
189