1 /*
2 * SPDX-FileCopyrightText: Copyright 2024 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_s16_batch_offset
22 * Description: Element wise multiplication
23 *
24 * $Date: 18 March 2024
25 * $Revision: V.1.0.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 * @brief s16 element wise multiplication of batches of two vectors
45 *
46 * @note Refer header file for details.
47 *
48 */
arm_elementwise_mul_s16_batch_offset(const int16_t * input_1_vect,const int16_t * input_2_vect,int16_t * output,const int32_t out_offset,const int32_t out_mult,const int32_t out_shift,const int32_t block_size,const int32_t batch_size,const int32_t batch_offset)49 arm_cmsis_nn_status arm_elementwise_mul_s16_batch_offset(const int16_t *input_1_vect,
50 const int16_t *input_2_vect,
51 int16_t *output,
52 const int32_t out_offset,
53 const int32_t out_mult,
54 const int32_t out_shift,
55 const int32_t block_size,
56 const int32_t batch_size,
57 const int32_t batch_offset)
58 {
59
60 int32_t loop_count;
61
62 for (int i = 0; i < batch_size; i++)
63 {
64
65 #if defined(ARM_MATH_MVEI)
66
67 const int16_t *input_1_ptr = input_1_vect;
68 const int16_t *input_2_ptr = input_2_vect;
69 int16_t *output_ptr = output;
70
71 loop_count = block_size;
72
73 while (loop_count > 0)
74 {
75 mve_pred16_t pred = vctp32q(loop_count);
76
77 int32x4_t input_1 = vldrhq_z_s32(input_1_ptr, pred);
78 int32x4_t input_2 = vldrhq_z_s32(input_2_ptr, pred);
79
80 int32x4_t res_0 = vmulq_s32(input_1, input_2);
81
82 res_0 = arm_requantize_mve_32x4(res_0, vdupq_n_s32(out_mult), vdupq_n_s32(out_shift));
83 res_0 = vaddq_n_s32(res_0, out_offset);
84
85 res_0 = vmaxq_s32(res_0, vdupq_n_s32(NN_Q15_MIN));
86 res_0 = vminq_s32(res_0, vdupq_n_s32(NN_Q15_MAX));
87
88 vstrhq_p_s32(output_ptr, res_0, pred);
89 input_1_ptr += 4;
90 input_2_ptr += 4;
91
92 output_ptr += 4;
93 loop_count -= 4;
94 }
95
96 input_1_vect += block_size;
97 input_2_vect += block_size;
98 output += block_size;
99
100 #else
101 int32_t input_1;
102 int32_t input_2;
103 int32_t mul_res;
104 int32_t two_halfword_1, two_halfword_2;
105 int16_t mul_1, mul_2;
106 loop_count = block_size / 2;
107
108 while (loop_count > 0)
109 {
110 two_halfword_1 = arm_nn_read_q15x2_ia(&input_1_vect);
111 two_halfword_2 = arm_nn_read_q15x2_ia(&input_2_vect);
112
113 #if defined(ARM_MATH_DSP)
114 mul_res = SMULBB(two_halfword_1, two_halfword_2);
115 #else
116 input_1 = (int16_t)(two_halfword_1 & 0xFFFF);
117 input_2 = (int16_t)(two_halfword_2 & 0xFFFF);
118 mul_res = input_1 * input_2;
119 #endif
120 mul_res = arm_nn_requantize(mul_res, out_mult, out_shift) + out_offset;
121 mul_res = MAX(mul_res, NN_Q15_MIN);
122 mul_res = MIN(mul_res, NN_Q15_MAX);
123 mul_1 = (int16_t)mul_res;
124
125 #if defined(ARM_MATH_DSP)
126 mul_res = SMULTT(two_halfword_1, two_halfword_2);
127 #else
128 input_1 = (int16_t)(two_halfword_1 >> 16);
129 input_2 = (int16_t)(two_halfword_2 >> 16);
130 mul_res = input_1 * input_2;
131 #endif
132 mul_res = arm_nn_requantize(mul_res, out_mult, out_shift) + out_offset;
133 mul_res = MAX(mul_res, NN_Q15_MIN);
134 mul_res = MIN(mul_res, NN_Q15_MAX);
135 mul_2 = (int16_t)mul_res;
136
137 arm_nn_write_q15x2_ia(&output, PACK_Q15x2_32x1(mul_1, mul_2));
138
139 loop_count--;
140 }
141
142 if (block_size & 0x1)
143 {
144 /* C = A * B */
145
146 input_1 = *input_1_vect++;
147 input_2 = *input_2_vect++;
148
149 mul_res = input_1 * input_2;
150 mul_res = arm_nn_requantize(mul_res, out_mult, out_shift) + out_offset;
151
152 mul_res = MAX(mul_res, NN_Q15_MIN);
153 mul_res = MIN(mul_res, NN_Q15_MAX);
154
155 *output++ = (int16_t)mul_res;
156 }
157 #endif // #if defined(ARM_MATH_MVEI)
158
159 output += (batch_offset - 1) * block_size;
160 }
161 return ARM_CMSIS_NN_SUCCESS;
162 }
163
164 /**
165 * @} end of Doxygen group
166 */
167