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_minimum_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_min_no_broadcast_s8(const int8_t * input_1,const int8_t * input_2,int8_t * output,int32_t flat_size)44 arm_min_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, vminq_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 ? in2 : in1;
66 --flat_size;
67 }
68 #endif
69
70 return ARM_CMSIS_NN_SUCCESS;
71 }
72
73 static arm_cmsis_nn_status
arm_min_scalar_s8(const int8_t * input_1,const int8_t * input_2,int8_t * output,int32_t flat_size)74 arm_min_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
83 int8x16_t vec = vldrbq_z_s8(input_2, p);
84 input_2 += 16;
85
86 vstrbq_p_s8(output, vminq_s8(scalar_vec, vec), p);
87 output += 16;
88 flat_size -= 16;
89 }
90 #else
91 int8_t in1 = *input_1;
92 while (flat_size > 0)
93 {
94 int8_t in2 = *input_2++;
95 *output++ = in1 >= in2 ? in2 : in1;
96 --flat_size;
97 }
98 #endif
99 return ARM_CMSIS_NN_SUCCESS;
100 }
101
102 /*
103 * s8 minimum
104 *
105 * Refer header file for details.
106 *
107 */
arm_minimum_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)108 arm_cmsis_nn_status arm_minimum_s8(const cmsis_nn_context *ctx,
109 const int8_t *input_1_data,
110 const cmsis_nn_dims *input_1_dims,
111 const int8_t *input_2_data,
112 const cmsis_nn_dims *input_2_dims,
113 int8_t *output_data,
114 const cmsis_nn_dims *output_dims)
115 {
116 (void)ctx;
117 const int32_t output_batch = output_dims->n;
118 const int32_t output_height = output_dims->h;
119 const int32_t output_width = output_dims->w;
120
121 const int32_t input_1_batch = input_1_dims->n;
122 const int32_t input_1_height = input_1_dims->h;
123 const int32_t input_1_width = input_1_dims->w;
124 const int32_t input_1_channels = input_1_dims->c;
125
126 const int32_t input_2_batch = input_2_dims->n;
127 const int32_t input_2_height = input_2_dims->h;
128 const int32_t input_2_width = input_2_dims->w;
129 const int32_t input_2_channels = input_2_dims->c;
130
131 int32_t flat_size_1 = input_1_batch * input_1_height * input_1_width * input_1_channels;
132 int32_t flat_size_2 = input_2_batch * input_2_height * input_2_width * input_2_channels;
133
134 if (arm_check_broadcast_required(input_1_dims, input_2_dims))
135 {
136 if (flat_size_1 == 1)
137 {
138 // arm_min_scalar expects the tensor with the scalar value to be provided first
139 arm_min_scalar_s8(input_1_data, input_2_data, output_data, flat_size_2);
140 }
141 else if (flat_size_2 == 1)
142 {
143 // arm_min_scalar expects the tensor with the scalar value to be provided first
144 arm_min_scalar_s8(input_2_data, input_1_data, output_data, flat_size_1);
145 }
146 else
147 {
148 int32_t width_1_diff = input_1_width >= input_2_width ? 0 : input_1_channels;
149 int32_t width_2_diff = input_2_width >= input_1_width ? 0 : input_2_channels;
150
151 int32_t height_1_diff =
152 input_1_height >= input_2_height ? width_1_diff : -input_1_width * (input_1_channels - width_1_diff);
153 int32_t height_2_diff =
154 input_2_height >= input_1_height ? width_2_diff : -input_2_width * (input_2_channels - width_2_diff);
155
156 int32_t batch_1_diff =
157 input_1_batch >= input_2_batch ? input_1_channels * input_1_width * input_1_height : 0;
158 int32_t batch_2_diff =
159 input_2_batch >= input_1_batch ? input_2_channels * input_2_width * input_2_height : 0;
160
161 for (int32_t i_out_batch = 0; i_out_batch < output_batch; i_out_batch++)
162 {
163 const int8_t *input_1_ptr = input_1_data;
164 const int8_t *input_2_ptr = input_2_data;
165 flat_size_1 = input_1_height * input_1_width * input_1_channels;
166 flat_size_2 = input_2_height * input_2_width * input_2_channels;
167 if (input_1_height == input_2_height && input_1_width == input_2_width &&
168 input_1_channels == input_2_channels)
169 {
170 arm_min_no_broadcast_s8(input_1_ptr, input_2_ptr, output_data, flat_size_1);
171 output_data += flat_size_1;
172 }
173 else if (flat_size_1 == 1)
174 {
175 arm_min_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_min_scalar_s8(input_2_ptr, input_1_ptr, output_data, flat_size_1);
181 output_data += flat_size_1;
182 }
183 else
184 {
185 flat_size_1 = input_1_width * input_1_channels;
186 flat_size_2 = input_2_width * input_2_channels;
187 for (int32_t i_out_height = 0; i_out_height < output_height; i_out_height++)
188 {
189 if (input_1_width == input_2_width && input_1_channels == input_2_channels)
190 {
191 arm_min_no_broadcast_s8(input_1_ptr, input_2_ptr, output_data, flat_size_1);
192 output_data += flat_size_1;
193 input_1_ptr += flat_size_1;
194 input_2_ptr += flat_size_1;
195 }
196 else if (flat_size_1 == 1)
197 {
198 // arm_min_scalar expects the tensor with the scalar value to be provided first
199 arm_min_scalar_s8(input_1_ptr, input_2_ptr, output_data, flat_size_2);
200 output_data += flat_size_2;
201 ++input_1_ptr;
202 input_2_ptr += flat_size_2;
203 }
204 else if (flat_size_2 == 1)
205 {
206 // arm_min_scalar expects the tensor with the scalar value to be provided first
207 arm_min_scalar_s8(input_2_ptr, input_1_ptr, output_data, flat_size_1);
208 output_data += flat_size_1;
209 ++input_2_ptr;
210 input_1_ptr += flat_size_1;
211 }
212 else
213 {
214 for (int32_t i_out_width = 0; i_out_width < output_width; i_out_width++)
215 {
216 if (input_1_channels == input_2_channels)
217 {
218 arm_min_no_broadcast_s8(input_1_ptr, input_2_ptr, output_data, input_1_channels);
219 output_data += input_1_channels;
220 input_1_ptr += input_1_channels;
221 input_2_ptr += input_1_channels;
222 }
223 else if (input_1_channels == 1)
224 {
225 // arm_min_scalar expects the tensor with the scalar value to be provided first
226 arm_min_scalar_s8(input_1_ptr, input_2_ptr, output_data, input_2_channels);
227 output_data += input_2_channels;
228 input_1_ptr++;
229 input_2_ptr += input_2_channels;
230 }
231 else if (input_2_channels == 1)
232 {
233 // arm_min_scalar expects the tensor with the scalar value to be provided first
234 arm_min_scalar_s8(input_2_ptr, input_1_ptr, output_data, input_1_channels);
235 output_data += input_1_channels;
236 input_1_ptr += input_1_channels;
237 input_2_ptr++;
238 }
239 input_1_ptr -= width_1_diff;
240 input_2_ptr -= width_2_diff;
241 }
242 }
243 input_1_ptr += height_1_diff;
244 input_2_ptr += height_2_diff;
245 }
246 }
247 input_1_data += batch_1_diff;
248 input_2_data += batch_2_diff;
249 }
250 }
251 }
252 else
253 {
254 arm_min_no_broadcast_s8(input_1_data, input_2_data, output_data, flat_size_1);
255 }
256
257 return (ARM_CMSIS_NN_SUCCESS);
258 }
259
260 /**
261 * @} end of Doxygen group
262 */
263