blob: 0b6d92dfd0404c4b87c8fce11130513b49d98042 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
3 *
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
27#pragma OPENCL EXTENSION cl_khr_fp16 : enable
28
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010029#define EXPAND(x) x
30
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#define CLAMP(x, min_val, max_val) min(max(x, min_val), max_val)
32
Georgios Pinitasac4e8732017-07-05 17:02:25 +010033#define VLOAD_STR(size) vload##size
34#define VLOAD(size) VLOAD_STR(size)
35
36#define VSTORE_STR(size) vstore##size
37#define VSTORE(size) VSTORE_STR(size)
38
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039#define VEC_DATA_TYPE_STR(type, size) type##size
40#define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size)
41
42#define CONVERT_STR(x, type) (convert_##type((x)))
43#define CONVERT(x, type) CONVERT_STR(x, type)
44
45#define CONVERT_SAT_STR(x, type) (convert_##type##_sat((x)))
46#define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type)
47
48#define CONVERT_SAT_ROUND_STR(x, type, round) (convert_##type##_sat_##round((x)))
49#define CONVERT_SAT_ROUND(x, type, round) CONVERT_SAT_ROUND_STR(x, type, round)
50
51#define VECTOR_DECLARATION(name) \
52 __global uchar *name##_ptr, \
53 uint name##_stride_x, \
54 uint name##_step_x, \
55 uint name##_offset_first_element_in_bytes
56
57#define IMAGE_DECLARATION(name) \
58 __global uchar *name##_ptr, \
59 uint name##_stride_x, \
60 uint name##_step_x, \
61 uint name##_stride_y, \
62 uint name##_step_y, \
63 uint name##_offset_first_element_in_bytes
64
65#define TENSOR3D_DECLARATION(name) \
66 __global uchar *name##_ptr, \
67 uint name##_stride_x, \
68 uint name##_step_x, \
69 uint name##_stride_y, \
70 uint name##_step_y, \
71 uint name##_stride_z, \
72 uint name##_step_z, \
73 uint name##_offset_first_element_in_bytes
74
75#define CONVERT_TO_VECTOR_STRUCT(name) \
76 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x)
77
78#define CONVERT_TO_VECTOR_STRUCT_NO_STEP(name) \
79 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0)
80
81#define CONVERT_TO_IMAGE_STRUCT(name) \
82 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y)
83
84#define CONVERT_TO_IMAGE_STRUCT_NO_STEP(name) \
85 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0)
86
Anthony Barbier7ff47a32017-07-11 16:54:04 +010087#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(name) \
88 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)
89
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090#define CONVERT_TO_TENSOR3D_STRUCT(name) \
91 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
92 name##_stride_z, name##_step_z)
93
94#define CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(name) \
95 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0, name##_stride_z, 0)
96
97/** Structure to hold Vector information */
98typedef struct Vector
99{
100 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
101 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
102 int stride_x; /**< Stride of the image in X dimension (in bytes) */
103} Vector;
104
105/** Structure to hold Image information */
106typedef struct Image
107{
108 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
109 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
110 int stride_x; /**< Stride of the image in X dimension (in bytes) */
111 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
112} Image;
113
114/** Structure to hold 3D tensor information */
115typedef struct Tensor3D
116{
117 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
118 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
119 int stride_x; /**< Stride of the image in X dimension (in bytes) */
120 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
121 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
122} Tensor3D;
123
124/** Wrap vector information into an Vector structure, and make the pointer point at this workitem's data.
125 *
126 * @param[in] ptr Pointer to the starting postion of the buffer
127 * @param[in] offset_first_element_in_bytes The offset of the first element in the source vector
128 * @param[in] stride_x Stride of the vector in X dimension (in bytes)
129 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
130 *
131 * @return An image object
132 */
133Vector inline update_vector_workitem_ptr(__global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint step_x)
134{
135 Vector vector =
136 {
137 .ptr = ptr,
138 .offset_first_element_in_bytes = offset_first_element_in_bytes,
139 .stride_x = stride_x,
140 };
141 vector.ptr += vector.offset_first_element_in_bytes + get_global_id(0) * step_x;
142 return vector;
143}
144
145/** Wrap image information into an Image structure, and make the pointer point at this workitem's data.
146 *
147 * @param[in] ptr Pointer to the starting postion of the buffer
148 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
149 * @param[in] stride_x Stride of the image in X dimension (in bytes)
150 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
151 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
152 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
153 *
154 * @return An image object
155 */
156Image 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)
157{
158 Image img =
159 {
160 .ptr = ptr,
161 .offset_first_element_in_bytes = offset_first_element_in_bytes,
162 .stride_x = stride_x,
163 .stride_y = stride_y
164 };
165 img.ptr += img.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y;
166 return img;
167}
168
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100169/** Wrap 3D tensor information into an image structure, and make the pointer point at this workitem's data.
170 *
171 * @param[in] ptr Pointer to the starting postion of the buffer
172 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
173 * @param[in] stride_x Stride of the image in X dimension (in bytes)
174 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
175 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
176 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
177 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
178 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
179 *
180 * @return A 3D tensor object
181 */
182Image 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)
183{
184 Image img =
185 {
186 .ptr = ptr,
187 .offset_first_element_in_bytes = offset_first_element_in_bytes,
188 .stride_x = stride_x,
189 .stride_y = stride_y
190 };
191 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;
192 return img;
193}
194
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195/** Wrap 3D tensor information into an tensor structure, and make the pointer point at this workitem's data.
196 *
197 * @param[in] ptr Pointer to the starting postion of the buffer
198 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
199 * @param[in] stride_x Stride of the image in X dimension (in bytes)
200 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
201 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
202 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
203 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
204 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
205 *
206 * @return A 3D tensor object
207 */
208Tensor3D 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)
209{
210 Tensor3D tensor =
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 .stride_z = stride_z
217 };
218 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;
219 return tensor;
220}
221
222/** Get the pointer position of a Vector
223 *
224 * @param[in] vec Pointer to the starting position of the buffer
225 * @param[in] x Relative X position
226 */
227__global inline const uchar *vector_offset(const Vector *vec, int x)
228{
229 return vec->ptr + x * vec->stride_x;
230}
231
232/** Get the pointer position of a Image
233 *
234 * @param[in] img Pointer to the starting position of the buffer
235 * @param[in] x Relative X position
236 * @param[in] y Relative Y position
237 */
238__global inline uchar *offset(const Image *img, int x, int y)
239{
240 return img->ptr + x * img->stride_x + y * img->stride_y;
241}
242
243/** Get the pointer position of a Tensor3D
244 *
245 * @param[in] tensor Pointer to the starting postion of the buffer
246 * @param[in] x Relative X position
247 * @param[in] y Relative Y position
248 * @param[in] z Relative Z position
249 */
250__global inline const uchar *tensor3D_offset(const Tensor3D *tensor, int x, int y, int z)
251{
252 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z;
253}
254
255#endif // _HELPER_H