blob: 59b81d7f06c583ef8ecc3018afed5d4bd440ef92 [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
steniu010d523cc2017-07-13 14:24:23 +010090#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
91 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)
92
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093#define CONVERT_TO_TENSOR3D_STRUCT(name) \
94 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
95 name##_stride_z, name##_step_z)
96
97#define CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(name) \
98 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0, name##_stride_z, 0)
99
100/** Structure to hold Vector information */
101typedef struct Vector
102{
103 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
104 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
105 int stride_x; /**< Stride of the image in X dimension (in bytes) */
106} Vector;
107
108/** Structure to hold Image information */
109typedef struct Image
110{
111 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
112 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
113 int stride_x; /**< Stride of the image in X dimension (in bytes) */
114 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
115} Image;
116
117/** Structure to hold 3D tensor information */
118typedef struct Tensor3D
119{
120 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
121 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
122 int stride_x; /**< Stride of the image in X dimension (in bytes) */
123 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
124 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
125} Tensor3D;
126
127/** Wrap vector information into an Vector structure, and make the pointer point at this workitem's data.
128 *
129 * @param[in] ptr Pointer to the starting postion of the buffer
130 * @param[in] offset_first_element_in_bytes The offset of the first element in the source vector
131 * @param[in] stride_x Stride of the vector in X dimension (in bytes)
132 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
133 *
134 * @return An image object
135 */
136Vector inline update_vector_workitem_ptr(__global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint step_x)
137{
138 Vector vector =
139 {
140 .ptr = ptr,
141 .offset_first_element_in_bytes = offset_first_element_in_bytes,
142 .stride_x = stride_x,
143 };
144 vector.ptr += vector.offset_first_element_in_bytes + get_global_id(0) * step_x;
145 return vector;
146}
147
148/** Wrap image information into an Image structure, and make the pointer point at this workitem's data.
149 *
150 * @param[in] ptr Pointer to the starting postion of the buffer
151 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
152 * @param[in] stride_x Stride of the image in X dimension (in bytes)
153 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
154 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
155 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
156 *
157 * @return An image object
158 */
159Image 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)
160{
161 Image img =
162 {
163 .ptr = ptr,
164 .offset_first_element_in_bytes = offset_first_element_in_bytes,
165 .stride_x = stride_x,
166 .stride_y = stride_y
167 };
168 img.ptr += img.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y;
169 return img;
170}
171
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100172/** Wrap 3D tensor information into an image structure, and make the pointer point at this workitem's data.
173 *
174 * @param[in] ptr Pointer to the starting postion of the buffer
175 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
176 * @param[in] stride_x Stride of the image in X dimension (in bytes)
177 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
178 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
179 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
180 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
181 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
182 *
183 * @return A 3D tensor object
184 */
185Image 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)
186{
187 Image img =
188 {
189 .ptr = ptr,
190 .offset_first_element_in_bytes = offset_first_element_in_bytes,
191 .stride_x = stride_x,
192 .stride_y = stride_y
193 };
194 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;
195 return img;
196}
197
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198/** Wrap 3D tensor information into an tensor structure, and make the pointer point at this workitem's data.
199 *
200 * @param[in] ptr Pointer to the starting postion of the buffer
201 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
202 * @param[in] stride_x Stride of the image in X dimension (in bytes)
203 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
204 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
205 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
206 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
207 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
208 *
209 * @return A 3D tensor object
210 */
211Tensor3D 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)
212{
213 Tensor3D tensor =
214 {
215 .ptr = ptr,
216 .offset_first_element_in_bytes = offset_first_element_in_bytes,
217 .stride_x = stride_x,
218 .stride_y = stride_y,
219 .stride_z = stride_z
220 };
221 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;
222 return tensor;
223}
224
225/** Get the pointer position of a Vector
226 *
227 * @param[in] vec Pointer to the starting position of the buffer
228 * @param[in] x Relative X position
229 */
230__global inline const uchar *vector_offset(const Vector *vec, int x)
231{
232 return vec->ptr + x * vec->stride_x;
233}
234
235/** Get the pointer position of a Image
236 *
237 * @param[in] img Pointer to the starting position of the buffer
238 * @param[in] x Relative X position
239 * @param[in] y Relative Y position
240 */
241__global inline uchar *offset(const Image *img, int x, int y)
242{
243 return img->ptr + x * img->stride_x + y * img->stride_y;
244}
245
246/** Get the pointer position of a Tensor3D
247 *
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100248 * @param[in] tensor Pointer to the starting position of the buffer
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249 * @param[in] x Relative X position
250 * @param[in] y Relative Y position
251 * @param[in] z Relative Z position
252 */
253__global inline const uchar *tensor3D_offset(const Tensor3D *tensor, int x, int y, int z)
254{
255 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z;
256}
257
258#endif // _HELPER_H