2 * Copyright (c) 2016-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #ifndef ARM_COMPUTE_HELPER_H
25 #define ARM_COMPUTE_HELPER_H
27 #if defined(ARM_COMPUTE_OPENCL_FP16_ENABLED)
28 #pragma OPENCL EXTENSION cl_khr_fp16 : enable
29 #endif // defined(ARM_COMPUTE_OPENCL_FP16_ENABLED)
31 #if defined(ARM_COMPUTE_DEBUG_ENABLED)
32 #if defined(cl_arm_printf)
33 #pragma OPENCL EXTENSION cl_arm_printf : enable
34 #endif // defined(cl_arm_printf)
35 #endif // defined(ARM_COMPUTE_DEBUG_ENABLED)
39 #define CLAMP(x, min_val, max_val) min(max(x, min_val), max_val)
41 #define VLOAD_STR(size) vload##size
42 #define VLOAD(size) VLOAD_STR(size)
44 #define VSTORE_STR(size) vstore##size
45 #define VSTORE(size) VSTORE_STR(size)
47 #define VEC_DATA_TYPE_STR(type, size) type##size
48 #define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size)
50 #define CL_VEC_DATA_TYPE_STR(type, size) type##size
51 #define CL_VEC_DATA_TYPE(type, size) CL_VEC_DATA_TYPE_STR(type, size)
53 #define CONVERT_STR(x, type) (convert_##type((x)))
54 #define CONVERT(x, type) CONVERT_STR(x, type)
56 #define CONVERT_SAT_STR(x, type) (convert_##type##_sat((x)))
57 #define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type)
59 #define CONVERT_SAT_ROUND_STR(x, type, round) (convert_##type##_sat_##round((x)))
60 #define CONVERT_SAT_ROUND(x, type, round) CONVERT_SAT_ROUND_STR(x, type, round)
62 #define VECTOR_DECLARATION(name) \
63 __global uchar *name##_ptr, \
64 uint name##_stride_x, \
66 uint name##_offset_first_element_in_bytes
68 #define IMAGE_DECLARATION(name) \
69 __global uchar *name##_ptr, \
70 uint name##_stride_x, \
72 uint name##_stride_y, \
74 uint name##_offset_first_element_in_bytes
76 #define TENSOR3D_DECLARATION(name) \
77 __global uchar *name##_ptr, \
78 uint name##_stride_x, \
80 uint name##_stride_y, \
82 uint name##_stride_z, \
84 uint name##_offset_first_element_in_bytes
86 #define TENSOR4D_DECLARATION(name) \
87 __global uchar *name##_ptr, \
88 uint name##_stride_x, \
90 uint name##_stride_y, \
92 uint name##_stride_z, \
94 uint name##_stride_w, \
96 uint name##_offset_first_element_in_bytes
98 #define CONVERT_TO_VECTOR_STRUCT(name) \
99 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x)
101 #define CONVERT_TO_VECTOR_STRUCT_NO_STEP(name) \
102 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0)
104 #define CONVERT_TO_IMAGE_STRUCT(name) \
105 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y)
107 #define CONVERT_TO_IMAGE_STRUCT_NO_STEP(name) \
108 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0)
110 #define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
111 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)
113 #define CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(name) \
114 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)
116 #define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
117 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)
119 #define CONVERT_TO_TENSOR3D_STRUCT(name) \
120 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
121 name##_stride_z, name##_step_z)
123 #define CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(name) \
124 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0, name##_stride_z, 0)
126 #define CONVERT_TO_TENSOR4D_STRUCT(name, mod_size) \
127 update_tensor4D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
128 name##_stride_z, name##_step_z, name##_stride_w, name##_step_w, mod_size)
130 #define CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(name, mod_size) \
131 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)
133 /** Structure to hold Vector information */
134 typedef struct Vector
136 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
137 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
138 int stride_x; /**< Stride of the image in X dimension (in bytes) */
141 /** Structure to hold Image information */
144 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
145 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
146 int stride_x; /**< Stride of the image in X dimension (in bytes) */
147 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
150 /** Structure to hold 3D tensor information */
151 typedef struct Tensor3D
153 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
154 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
155 int stride_x; /**< Stride of the image in X dimension (in bytes) */
156 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
157 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
160 /** Structure to hold 4D tensor information */
161 typedef struct Tensor4D
163 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
164 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
165 int stride_x; /**< Stride of the image in X dimension (in bytes) */
166 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
167 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
168 int stride_w; /**< Stride of the image in W dimension (in bytes) */
171 /** Wrap vector information into an Vector structure, and make the pointer point at this workitem's data.
173 * @param[in] ptr Pointer to the starting postion of the buffer
174 * @param[in] offset_first_element_in_bytes The offset of the first element in the source vector
175 * @param[in] stride_x Stride of the vector in X dimension (in bytes)
176 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
178 * @return An image object
180 Vector inline update_vector_workitem_ptr(__global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint step_x)
185 .offset_first_element_in_bytes = offset_first_element_in_bytes,
186 .stride_x = stride_x,
188 vector.ptr += vector.offset_first_element_in_bytes + get_global_id(0) * step_x;
192 /** Wrap image information into an Image structure, and make the pointer point at this workitem's data.
194 * @param[in] ptr Pointer to the starting postion of the buffer
195 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
196 * @param[in] stride_x Stride of the image in X dimension (in bytes)
197 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
198 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
199 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
201 * @return An image object
203 Image 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)
208 .offset_first_element_in_bytes = offset_first_element_in_bytes,
209 .stride_x = stride_x,
212 img.ptr += img.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y;
216 /** Wrap 3D tensor information into an image structure, and make the pointer point at this workitem's data.
218 * @param[in] ptr Pointer to the starting postion of the buffer
219 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
220 * @param[in] stride_x Stride of the image in X dimension (in bytes)
221 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
222 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
223 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
224 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
225 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
227 * @return A 3D tensor object
229 Image 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)
234 .offset_first_element_in_bytes = offset_first_element_in_bytes,
235 .stride_x = stride_x,
238 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;
242 /** Wrap 3D tensor information into an tensor structure, and make the pointer point at this workitem's data.
244 * @param[in] ptr Pointer to the starting postion of the buffer
245 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
246 * @param[in] stride_x Stride of the image in X dimension (in bytes)
247 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
248 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
249 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
250 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
251 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
253 * @return A 3D tensor object
255 Tensor3D 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)
260 .offset_first_element_in_bytes = offset_first_element_in_bytes,
261 .stride_x = stride_x,
262 .stride_y = stride_y,
265 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;
269 Tensor4D 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,
276 .offset_first_element_in_bytes = offset_first_element_in_bytes,
277 .stride_x = stride_x,
278 .stride_y = stride_y,
279 .stride_z = stride_z,
283 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;
287 /** Get the pointer position of a Vector
289 * @param[in] vec Pointer to the starting position of the buffer
290 * @param[in] x Relative X position
292 __global inline const uchar *vector_offset(const Vector *vec, int x)
294 return vec->ptr + x * vec->stride_x;
297 /** Get the pointer position of a Image
299 * @param[in] img Pointer to the starting position of the buffer
300 * @param[in] x Relative X position
301 * @param[in] y Relative Y position
303 __global inline uchar *offset(const Image *img, int x, int y)
305 return img->ptr + x * img->stride_x + y * img->stride_y;
308 /** Get the pointer position of a Tensor3D
310 * @param[in] tensor Pointer to the starting position of the buffer
311 * @param[in] x Relative X position
312 * @param[in] y Relative Y position
313 * @param[in] z Relative Z position
315 __global inline const uchar *tensor3D_offset(const Tensor3D *tensor, int x, int y, int z)
317 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z;
320 /** Get the pointer position of a Tensor4D
322 * @param[in] tensor Pointer to the starting position of the buffer
323 * @param[in] x Relative X position
324 * @param[in] y Relative Y position
325 * @param[in] z Relative Z position
326 * @param[in] w Relative W position
328 __global inline const uchar *tensor4D_offset(const Tensor4D *tensor, int x, int y, int z, int w)
330 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z + w * tensor->stride_w;