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_maximum_s8
22 * Description: Minimum and Maximum
23 *
24 * $Date: 08 October 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 minimumMaximum
40 * @{
41 */
42
43 static arm_cmsis_nn_status
arm_max_no_broadcast_s8(const int8_t * input_1,const int8_t * input_2,int8_t * output,int32_t flat_size)44 arm_max_no_broadcast_s8(const int8_t *input_1, const int8_t *input_2, int8_t *output, int32_t flat_size)
45 {
46 #if defined(ARM_MATH_MVEI)
47 while (flat_size > 0)
48 {
49 mve_pred16_t p = vctp8q(flat_size);
50
51 int8x16_t vec1 = vldrbq_z_s8(input_1, p);
52 input_1 += 16;
53 int8x16_t vec2 = vldrbq_z_s8(input_2, p);
54 input_2 += 16;
55
56 vstrbq_p_s8(output, vmaxq_s8(vec1, vec2), p);
57 output += 16;
58 flat_size -= 16;
59 }
60 #else
61 while (flat_size > 0)
62 {
63 int8_t in1 = *input_1++;
64 int8_t in2 = *input_2++;
65 *output++ = in1 >= in2 ? in1 : in2;
66 --flat_size;
67 }
68 #endif
69
70 return ARM_CMSIS_NN_SUCCESS;
71 }
72
73 static arm_cmsis_nn_status
arm_max_scalar_s8(const int8_t * input_1,const int8_t * input_2,int8_t * output,int32_t flat_size)74 arm_max_scalar_s8(const int8_t *input_1, const int8_t *input_2, int8_t *output, int32_t flat_size)
75 {
76 #if defined(ARM_MATH_MVEI)
77 int8x16_t scalar_vec = vdupq_n_s8(*input_1);
78
79 while (flat_size > 0)
80 {
81 mve_pred16_t p = vctp8q(flat_size);
82 int8x16_t vec = vldrbq_z_s8(input_2, p);
83 input_2 += 16;
84
85 vstrbq_p_s8(output, vmaxq_s8(scalar_vec, vec), p);
86 output += 16;
87 flat_size -= 16;
88 }
89 #else
90 int8_t in1 = *input_1;
91 while (flat_size > 0)
92 {
93 int8_t in2 = *input_2++;
94 *output++ = in1 >= in2 ? in1 : in2;
95 --flat_size;
96 }
97 #endif
98 return ARM_CMSIS_NN_SUCCESS;
99 }
100
101 /*
102 * s8 maximum
103 *
104 * Refer header file for details.
105 *
106 */
arm_maximum_s8(const cmsis_nn_context * ctx,const int8_t * input_1_data,const cmsis_nn_dims * input_1_dims,const int8_t * input_2_data,const cmsis_nn_dims * input_2_dims,int8_t * output_data,const cmsis_nn_dims * output_dims)107 arm_cmsis_nn_status arm_maximum_s8(const cmsis_nn_context *ctx,
108 const int8_t *input_1_data,
109 const cmsis_nn_dims *input_1_dims,
110 const int8_t *input_2_data,
111 const cmsis_nn_dims *input_2_dims,
112 int8_t *output_data,
113 const cmsis_nn_dims *output_dims)
114 {
115 (void)ctx;
116 const int32_t output_batch = output_dims->n;
117 const int32_t output_height = output_dims->h;
118 const int32_t output_width = output_dims->w;
119
120 const int32_t input_1_batch = input_1_dims->n;
121 const int32_t input_1_height = input_1_dims->h;
122 const int32_t input_1_width = input_1_dims->w;
123 const int32_t input_1_channels = input_1_dims->c;
124
125 const int32_t input_2_batch = input_2_dims->n;
126 const int32_t input_2_height = input_2_dims->h;
127 const int32_t input_2_width = input_2_dims->w;
128 const int32_t input_2_channels = input_2_dims->c;
129
130 int32_t flat_size_1 = input_1_batch * input_1_height * input_1_width * input_1_channels;
131 int32_t flat_size_2 = input_2_batch * input_2_height * input_2_width * input_2_channels;
132
133 if (arm_check_broadcast_required(input_1_dims, input_2_dims))
134 {
135 if (flat_size_1 == 1)
136 {
137 // arm_max_scalar expects the tensor with the scalar value to be provided first
138 arm_max_scalar_s8(input_1_data, input_2_data, output_data, flat_size_2);
139 }
140 else if (flat_size_2 == 1)
141 {
142 // arm_max_scalar expects the tensor with the scalar value to be provided first
143 arm_max_scalar_s8(input_2_data, input_1_data, output_data, flat_size_1);
144 }
145 else
146 {
147 int32_t width_1_diff = input_1_width >= input_2_width ? 0 : input_1_channels;
148 int32_t width_2_diff = input_2_width >= input_1_width ? 0 : input_2_channels;
149
150 int32_t height_1_diff =
151 input_1_height >= input_2_height ? width_1_diff : -input_1_width * (input_1_channels - width_1_diff);
152 int32_t height_2_diff =
153 input_2_height >= input_1_height ? width_2_diff : -input_2_width * (input_2_channels - width_2_diff);
154
155 int32_t batch_1_diff =
156 input_1_batch >= input_2_batch ? input_1_channels * input_1_width * input_1_height : 0;
157 int32_t batch_2_diff =
158 input_2_batch >= input_1_batch ? input_2_channels * input_2_width * input_2_height : 0;
159
160 for (int32_t i_out_batch = 0; i_out_batch < output_batch; i_out_batch++)
161 {
162 const int8_t *input_1_ptr = input_1_data;
163 const int8_t *input_2_ptr = input_2_data;
164 flat_size_1 = input_1_height * input_1_width * input_1_channels;
165 flat_size_2 = input_2_height * input_2_width * input_2_channels;
166 if (input_1_height == input_2_height && input_1_width == input_2_width &&
167 input_1_channels == input_2_channels)
168 {
169 arm_max_no_broadcast_s8(input_1_ptr, input_2_ptr, output_data, flat_size_1);
170 output_data += flat_size_1;
171 }
172 else if (flat_size_1 == 1)
173 {
174 // arm_max_scalar expects the tensor with the scalar value to be provided first
175 arm_max_scalar_s8(input_1_ptr, input_2_ptr, output_data, flat_size_2);
176 output_data += flat_size_2;
177 }
178 else if (flat_size_2 == 1)
179 {
180 // arm_max_scalar expects the tensor with the scalar value to be provided first
181 arm_max_scalar_s8(input_2_ptr, input_1_ptr, output_data, flat_size_1);
182 output_data += flat_size_1;
183 }
184 else
185 {
186 flat_size_1 = input_1_width * input_1_channels;
187 flat_size_2 = input_2_width * input_2_channels;
188 for (int32_t i_out_height = 0; i_out_height < output_height; i_out_height++)
189 {
190 if (input_1_width == input_2_width && input_1_channels == input_2_channels)
191 {
192 arm_max_no_broadcast_s8(input_1_ptr, input_2_ptr, output_data, flat_size_1);
193 output_data += flat_size_1;
194 input_1_ptr += flat_size_1;
195 input_2_ptr += flat_size_1;
196 }
197 else if (flat_size_1 == 1)
198 {
199 // arm_max_scalar expects the tensor with the scalar value to be provided first
200 arm_max_scalar_s8(input_1_ptr, input_2_ptr, output_data, flat_size_2);
201 output_data += flat_size_2;
202 ++input_1_ptr;
203 input_2_ptr += flat_size_2;
204 }
205 else if (flat_size_2 == 1)
206 {
207 // arm_max_scalar expects the tensor with the scalar value to be provided first
208 arm_max_scalar_s8(input_2_ptr, input_1_ptr, output_data, flat_size_1);
209 output_data += flat_size_1;
210 ++input_2_ptr;
211 input_1_ptr += flat_size_1;
212 }
213 else
214 {
215 for (int32_t i_out_width = 0; i_out_width < output_width; i_out_width++)
216 {
217 if (input_1_channels == input_2_channels)
218 {
219 arm_max_no_broadcast_s8(input_1_ptr, input_2_ptr, output_data, input_1_channels);
220 output_data += input_1_channels;
221 input_1_ptr += input_1_channels;
222 input_2_ptr += input_1_channels;
223 }
224 else if (input_1_channels == 1)
225 {
226 // arm_max_scalar expects the tensor with the scalar value to be provided first
227 arm_max_scalar_s8(input_1_ptr, input_2_ptr, output_data, input_2_channels);
228 output_data += input_2_channels;
229 input_1_ptr++;
230 input_2_ptr += input_2_channels;
231 }
232 else if (input_2_channels == 1)
233 {
234 // arm_max_scalar expects the tensor with the scalar value to be provided first
235 arm_max_scalar_s8(input_2_ptr, input_1_ptr, output_data, input_1_channels);
236 output_data += input_1_channels;
237 input_1_ptr += input_1_channels;
238 input_2_ptr++;
239 }
240 input_1_ptr -= width_1_diff;
241 input_2_ptr -= width_2_diff;
242 }
243 }
244 input_1_ptr += height_1_diff;
245 input_2_ptr += height_2_diff;
246 }
247 }
248 input_1_data += batch_1_diff;
249 input_2_data += batch_2_diff;
250 }
251 }
252 }
253 else
254 {
255 arm_max_no_broadcast_s8(input_1_data, input_2_data, output_data, flat_size_1);
256 }
257
258 return (ARM_CMSIS_NN_SUCCESS);
259 }
260
261 /**
262 * @} end of Doxygen group
263 */
264