blob: cf3cb78e04f00a22f7fadecb65c7df095868ce25 [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
33#define VEC_DATA_TYPE_STR(type, size) type##size
34#define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size)
35
36#define CONVERT_STR(x, type) (convert_##type((x)))
37#define CONVERT(x, type) CONVERT_STR(x, type)
38
39#define CONVERT_SAT_STR(x, type) (convert_##type##_sat((x)))
40#define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type)
41
42#define CONVERT_SAT_ROUND_STR(x, type, round) (convert_##type##_sat_##round((x)))
43#define CONVERT_SAT_ROUND(x, type, round) CONVERT_SAT_ROUND_STR(x, type, round)
44
45#define VECTOR_DECLARATION(name) \
46 __global uchar *name##_ptr, \
47 uint name##_stride_x, \
48 uint name##_step_x, \
49 uint name##_offset_first_element_in_bytes
50
51#define IMAGE_DECLARATION(name) \
52 __global uchar *name##_ptr, \
53 uint name##_stride_x, \
54 uint name##_step_x, \
55 uint name##_stride_y, \
56 uint name##_step_y, \
57 uint name##_offset_first_element_in_bytes
58
59#define TENSOR3D_DECLARATION(name) \
60 __global uchar *name##_ptr, \
61 uint name##_stride_x, \
62 uint name##_step_x, \
63 uint name##_stride_y, \
64 uint name##_step_y, \
65 uint name##_stride_z, \
66 uint name##_step_z, \
67 uint name##_offset_first_element_in_bytes
68
69#define CONVERT_TO_VECTOR_STRUCT(name) \
70 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x)
71
72#define CONVERT_TO_VECTOR_STRUCT_NO_STEP(name) \
73 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0)
74
75#define CONVERT_TO_IMAGE_STRUCT(name) \
76 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y)
77
78#define CONVERT_TO_IMAGE_STRUCT_NO_STEP(name) \
79 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0)
80
81#define CONVERT_TO_TENSOR3D_STRUCT(name) \
82 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
83 name##_stride_z, name##_step_z)
84
85#define CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(name) \
86 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0, name##_stride_z, 0)
87
88/** Structure to hold Vector information */
89typedef struct Vector
90{
91 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
92 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
93 int stride_x; /**< Stride of the image in X dimension (in bytes) */
94} Vector;
95
96/** Structure to hold Image information */
97typedef struct Image
98{
99 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
100 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
101 int stride_x; /**< Stride of the image in X dimension (in bytes) */
102 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
103} Image;
104
105/** Structure to hold 3D tensor information */
106typedef struct Tensor3D
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 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
113} Tensor3D;
114
115/** Wrap vector information into an Vector structure, and make the pointer point at this workitem's data.
116 *
117 * @param[in] ptr Pointer to the starting postion of the buffer
118 * @param[in] offset_first_element_in_bytes The offset of the first element in the source vector
119 * @param[in] stride_x Stride of the vector in X dimension (in bytes)
120 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
121 *
122 * @return An image object
123 */
124Vector inline update_vector_workitem_ptr(__global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint step_x)
125{
126 Vector vector =
127 {
128 .ptr = ptr,
129 .offset_first_element_in_bytes = offset_first_element_in_bytes,
130 .stride_x = stride_x,
131 };
132 vector.ptr += vector.offset_first_element_in_bytes + get_global_id(0) * step_x;
133 return vector;
134}
135
136/** Wrap image information into an Image structure, and make the pointer point at this workitem's data.
137 *
138 * @param[in] ptr Pointer to the starting postion of the buffer
139 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
140 * @param[in] stride_x Stride of the image in X dimension (in bytes)
141 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
142 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
143 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
144 *
145 * @return An image object
146 */
147Image 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)
148{
149 Image img =
150 {
151 .ptr = ptr,
152 .offset_first_element_in_bytes = offset_first_element_in_bytes,
153 .stride_x = stride_x,
154 .stride_y = stride_y
155 };
156 img.ptr += img.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y;
157 return img;
158}
159
160/** Wrap 3D tensor information into an tensor structure, and make the pointer point at this workitem's data.
161 *
162 * @param[in] ptr Pointer to the starting postion of the buffer
163 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
164 * @param[in] stride_x Stride of the image in X dimension (in bytes)
165 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
166 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
167 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
168 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
169 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
170 *
171 * @return A 3D tensor object
172 */
173Tensor3D 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)
174{
175 Tensor3D tensor =
176 {
177 .ptr = ptr,
178 .offset_first_element_in_bytes = offset_first_element_in_bytes,
179 .stride_x = stride_x,
180 .stride_y = stride_y,
181 .stride_z = stride_z
182 };
183 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;
184 return tensor;
185}
186
187/** Get the pointer position of a Vector
188 *
189 * @param[in] vec Pointer to the starting position of the buffer
190 * @param[in] x Relative X position
191 */
192__global inline const uchar *vector_offset(const Vector *vec, int x)
193{
194 return vec->ptr + x * vec->stride_x;
195}
196
197/** Get the pointer position of a Image
198 *
199 * @param[in] img Pointer to the starting position of the buffer
200 * @param[in] x Relative X position
201 * @param[in] y Relative Y position
202 */
203__global inline uchar *offset(const Image *img, int x, int y)
204{
205 return img->ptr + x * img->stride_x + y * img->stride_y;
206}
207
208/** Get the pointer position of a Tensor3D
209 *
210 * @param[in] tensor Pointer to the starting postion of the buffer
211 * @param[in] x Relative X position
212 * @param[in] y Relative Y position
213 * @param[in] z Relative Z position
214 */
215__global inline const uchar *tensor3D_offset(const Tensor3D *tensor, int x, int y, int z)
216{
217 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z;
218}
219
220#endif // _HELPER_H