blob: dcc471ebce4daa3e3dc2059edb885d370ce3230a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou5237e012018-01-17 09:40:27 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ARM_COMPUTE_HELPER_H
25#define ARM_COMPUTE_HELPER_H
26
Matthew Bentham6f31f8c2017-10-27 11:50:06 +010027#if defined(ARM_COMPUTE_OPENCL_FP16_ENABLED)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#pragma OPENCL EXTENSION cl_khr_fp16 : enable
Matthew Bentham6f31f8c2017-10-27 11:50:06 +010029#endif // defined(ARM_COMPUTE_OPENCL_FP16_ENABLED)
30
Michalis Spyroue03342e2018-01-15 14:39:13 +000031#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED)
32#pragma OPENCL EXTENSION cl_arm_integer_dot_product_int8 : enable
Giorgio Arena6200fa42018-07-06 17:06:36 +010033#pragma OPENCL EXTENSION cl_arm_integer_dot_product_accumulate_int8 : enable
Michalis Spyroue03342e2018-01-15 14:39:13 +000034#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED)
35
steniu01f01f9de2017-09-27 17:00:11 +010036#if defined(ARM_COMPUTE_DEBUG_ENABLED)
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +010037#if defined(cl_arm_printf)
steniu01f01f9de2017-09-27 17:00:11 +010038#pragma OPENCL EXTENSION cl_arm_printf : enable
Vidhya Sudhan Loganathaneb8a3992018-04-10 12:23:22 +010039#endif // defined(cl_arm_printf)
steniu01f01f9de2017-09-27 17:00:11 +010040#endif // defined(ARM_COMPUTE_DEBUG_ENABLED)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010042#define EXPAND(x) x
43
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044#define CLAMP(x, min_val, max_val) min(max(x, min_val), max_val)
45
Georgios Pinitasac4e8732017-07-05 17:02:25 +010046#define VLOAD_STR(size) vload##size
47#define VLOAD(size) VLOAD_STR(size)
48
49#define VSTORE_STR(size) vstore##size
50#define VSTORE(size) VSTORE_STR(size)
51
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052#define VEC_DATA_TYPE_STR(type, size) type##size
53#define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size)
54
Chunosovd6afedc2017-11-06 22:09:45 +070055#define CL_VEC_DATA_TYPE_STR(type, size) type##size
56#define CL_VEC_DATA_TYPE(type, size) CL_VEC_DATA_TYPE_STR(type, size)
57
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058#define CONVERT_STR(x, type) (convert_##type((x)))
59#define CONVERT(x, type) CONVERT_STR(x, type)
60
61#define CONVERT_SAT_STR(x, type) (convert_##type##_sat((x)))
62#define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type)
63
64#define CONVERT_SAT_ROUND_STR(x, type, round) (convert_##type##_sat_##round((x)))
65#define CONVERT_SAT_ROUND(x, type, round) CONVERT_SAT_ROUND_STR(x, type, round)
66
67#define VECTOR_DECLARATION(name) \
68 __global uchar *name##_ptr, \
69 uint name##_stride_x, \
70 uint name##_step_x, \
71 uint name##_offset_first_element_in_bytes
72
73#define IMAGE_DECLARATION(name) \
74 __global uchar *name##_ptr, \
75 uint name##_stride_x, \
76 uint name##_step_x, \
77 uint name##_stride_y, \
78 uint name##_step_y, \
79 uint name##_offset_first_element_in_bytes
80
81#define TENSOR3D_DECLARATION(name) \
82 __global uchar *name##_ptr, \
83 uint name##_stride_x, \
84 uint name##_step_x, \
85 uint name##_stride_y, \
86 uint name##_step_y, \
87 uint name##_stride_z, \
88 uint name##_step_z, \
89 uint name##_offset_first_element_in_bytes
90
steniu01868e5412017-07-17 23:16:00 +010091#define TENSOR4D_DECLARATION(name) \
92 __global uchar *name##_ptr, \
93 uint name##_stride_x, \
94 uint name##_step_x, \
95 uint name##_stride_y, \
96 uint name##_step_y, \
97 uint name##_stride_z, \
98 uint name##_step_z, \
99 uint name##_stride_w, \
100 uint name##_step_w, \
101 uint name##_offset_first_element_in_bytes
102
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103#define CONVERT_TO_VECTOR_STRUCT(name) \
104 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x)
105
106#define CONVERT_TO_VECTOR_STRUCT_NO_STEP(name) \
107 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0)
108
109#define CONVERT_TO_IMAGE_STRUCT(name) \
110 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y)
111
112#define CONVERT_TO_IMAGE_STRUCT_NO_STEP(name) \
113 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0)
114
steniu01868e5412017-07-17 23:16:00 +0100115#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
116 update_image_from_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, name##_stride_z, name##_step_z)
117
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100118#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(name) \
119 update_image_from_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0, name##_stride_z, name##_step_z)
120
steniu010d523cc2017-07-13 14:24:23 +0100121#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
122 update_image_from_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, name##_stride_z, name##_step_z)
123
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124#define CONVERT_TO_TENSOR3D_STRUCT(name) \
125 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
126 name##_stride_z, name##_step_z)
127
128#define CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(name) \
129 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0, name##_stride_z, 0)
130
steniu01868e5412017-07-17 23:16:00 +0100131#define CONVERT_TO_TENSOR4D_STRUCT(name, mod_size) \
132 update_tensor4D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
Michalis Spyrou5237e012018-01-17 09:40:27 +0000133 name##_stride_z, name##_step_z, name##_stride_w, name##_step_w, mod_size)
steniu01868e5412017-07-17 23:16:00 +0100134
135#define CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(name, mod_size) \
136 update_tensor4D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0, name##_stride_z, 0, name##_stride_w, 0, mod_size)
137
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138/** Structure to hold Vector information */
139typedef struct Vector
140{
141 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
142 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
143 int stride_x; /**< Stride of the image in X dimension (in bytes) */
144} Vector;
145
146/** Structure to hold Image information */
147typedef struct Image
148{
149 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
150 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
151 int stride_x; /**< Stride of the image in X dimension (in bytes) */
152 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
153} Image;
154
155/** Structure to hold 3D tensor information */
156typedef struct Tensor3D
157{
158 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
159 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
160 int stride_x; /**< Stride of the image in X dimension (in bytes) */
161 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
162 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
163} Tensor3D;
164
steniu01868e5412017-07-17 23:16:00 +0100165/** Structure to hold 4D tensor information */
166typedef struct Tensor4D
167{
168 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
169 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
170 int stride_x; /**< Stride of the image in X dimension (in bytes) */
171 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
172 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
173 int stride_w; /**< Stride of the image in W dimension (in bytes) */
174} Tensor4D;
175
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176/** Wrap vector information into an Vector structure, and make the pointer point at this workitem's data.
177 *
178 * @param[in] ptr Pointer to the starting postion of the buffer
179 * @param[in] offset_first_element_in_bytes The offset of the first element in the source vector
180 * @param[in] stride_x Stride of the vector in X dimension (in bytes)
181 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
182 *
183 * @return An image object
184 */
185Vector inline update_vector_workitem_ptr(__global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint step_x)
186{
187 Vector vector =
188 {
189 .ptr = ptr,
190 .offset_first_element_in_bytes = offset_first_element_in_bytes,
191 .stride_x = stride_x,
192 };
193 vector.ptr += vector.offset_first_element_in_bytes + get_global_id(0) * step_x;
194 return vector;
195}
196
197/** Wrap image information into an Image structure, and make the pointer point at this workitem's data.
198 *
199 * @param[in] ptr Pointer to the starting postion of the buffer
200 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
201 * @param[in] stride_x Stride of the image in X dimension (in bytes)
202 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
203 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
204 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
205 *
206 * @return An image object
207 */
208Image inline update_image_workitem_ptr(__global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y)
209{
210 Image img =
211 {
212 .ptr = ptr,
213 .offset_first_element_in_bytes = offset_first_element_in_bytes,
214 .stride_x = stride_x,
215 .stride_y = stride_y
216 };
217 img.ptr += img.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y;
218 return img;
219}
220
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100221/** Wrap 3D tensor information into an image structure, and make the pointer point at this workitem's data.
222 *
223 * @param[in] ptr Pointer to the starting postion of the buffer
224 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
225 * @param[in] stride_x Stride of the image in X dimension (in bytes)
226 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
227 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
228 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
229 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
230 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
231 *
232 * @return A 3D tensor object
233 */
234Image inline update_image_from_tensor3D_workitem_ptr(__global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y, uint stride_z, uint step_z)
235{
236 Image img =
237 {
238 .ptr = ptr,
239 .offset_first_element_in_bytes = offset_first_element_in_bytes,
240 .stride_x = stride_x,
241 .stride_y = stride_y
242 };
243 img.ptr += img.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y + get_global_id(2) * step_z;
244 return img;
245}
246
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247/** Wrap 3D tensor information into an tensor structure, and make the pointer point at this workitem's data.
248 *
249 * @param[in] ptr Pointer to the starting postion of the buffer
250 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
251 * @param[in] stride_x Stride of the image in X dimension (in bytes)
252 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
253 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
254 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
255 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
256 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
257 *
258 * @return A 3D tensor object
259 */
260Tensor3D inline update_tensor3D_workitem_ptr(__global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y, uint stride_z, uint step_z)
261{
262 Tensor3D tensor =
263 {
264 .ptr = ptr,
265 .offset_first_element_in_bytes = offset_first_element_in_bytes,
266 .stride_x = stride_x,
267 .stride_y = stride_y,
268 .stride_z = stride_z
269 };
270 tensor.ptr += tensor.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y + get_global_id(2) * step_z;
271 return tensor;
272}
273
steniu01868e5412017-07-17 23:16:00 +0100274Tensor4D inline update_tensor4D_workitem_ptr(__global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y, uint stride_z, uint step_z, uint stride_w,
275 uint step_w,
276 uint mod_size)
277{
278 Tensor4D tensor =
279 {
280 .ptr = ptr,
281 .offset_first_element_in_bytes = offset_first_element_in_bytes,
282 .stride_x = stride_x,
283 .stride_y = stride_y,
284 .stride_z = stride_z,
285 .stride_w = stride_w
286 };
287
288 tensor.ptr += tensor.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y + (get_global_id(2) % mod_size) * step_z + (get_global_id(2) / mod_size) * step_w;
289 return tensor;
290}
291
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100292/** Get the pointer position of a Vector
293 *
294 * @param[in] vec Pointer to the starting position of the buffer
295 * @param[in] x Relative X position
296 */
297__global inline const uchar *vector_offset(const Vector *vec, int x)
298{
299 return vec->ptr + x * vec->stride_x;
300}
301
302/** Get the pointer position of a Image
303 *
304 * @param[in] img Pointer to the starting position of the buffer
305 * @param[in] x Relative X position
306 * @param[in] y Relative Y position
307 */
308__global inline uchar *offset(const Image *img, int x, int y)
309{
310 return img->ptr + x * img->stride_x + y * img->stride_y;
311}
312
313/** Get the pointer position of a Tensor3D
314 *
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100315 * @param[in] tensor Pointer to the starting position of the buffer
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316 * @param[in] x Relative X position
317 * @param[in] y Relative Y position
318 * @param[in] z Relative Z position
319 */
320__global inline const uchar *tensor3D_offset(const Tensor3D *tensor, int x, int y, int z)
321{
322 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z;
323}
324
steniu01868e5412017-07-17 23:16:00 +0100325/** Get the pointer position of a Tensor4D
326 *
327 * @param[in] tensor Pointer to the starting position of the buffer
328 * @param[in] x Relative X position
329 * @param[in] y Relative Y position
330 * @param[in] z Relative Z position
331 * @param[in] w Relative W position
332 */
333__global inline const uchar *tensor4D_offset(const Tensor4D *tensor, int x, int y, int z, int w)
334{
335 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z + w * tensor->stride_w;
336}
337
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338#endif // _HELPER_H