Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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 |
steniu01 | f01f9de | 2017-09-27 17:00:11 +0100 | [diff] [blame^] | 28 | #if defined(ARM_COMPUTE_DEBUG_ENABLED) |
| 29 | #pragma OPENCL EXTENSION cl_arm_printf : enable |
| 30 | #endif // defined(ARM_COMPUTE_DEBUG_ENABLED) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 32 | #define EXPAND(x) x |
| 33 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #define CLAMP(x, min_val, max_val) min(max(x, min_val), max_val) |
| 35 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 36 | #define VLOAD_STR(size) vload##size |
| 37 | #define VLOAD(size) VLOAD_STR(size) |
| 38 | |
| 39 | #define VSTORE_STR(size) vstore##size |
| 40 | #define VSTORE(size) VSTORE_STR(size) |
| 41 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | #define VEC_DATA_TYPE_STR(type, size) type##size |
| 43 | #define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size) |
| 44 | |
| 45 | #define CONVERT_STR(x, type) (convert_##type((x))) |
| 46 | #define CONVERT(x, type) CONVERT_STR(x, type) |
| 47 | |
| 48 | #define CONVERT_SAT_STR(x, type) (convert_##type##_sat((x))) |
| 49 | #define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type) |
| 50 | |
| 51 | #define CONVERT_SAT_ROUND_STR(x, type, round) (convert_##type##_sat_##round((x))) |
| 52 | #define CONVERT_SAT_ROUND(x, type, round) CONVERT_SAT_ROUND_STR(x, type, round) |
| 53 | |
| 54 | #define VECTOR_DECLARATION(name) \ |
| 55 | __global uchar *name##_ptr, \ |
| 56 | uint name##_stride_x, \ |
| 57 | uint name##_step_x, \ |
| 58 | uint name##_offset_first_element_in_bytes |
| 59 | |
| 60 | #define IMAGE_DECLARATION(name) \ |
| 61 | __global uchar *name##_ptr, \ |
| 62 | uint name##_stride_x, \ |
| 63 | uint name##_step_x, \ |
| 64 | uint name##_stride_y, \ |
| 65 | uint name##_step_y, \ |
| 66 | uint name##_offset_first_element_in_bytes |
| 67 | |
| 68 | #define TENSOR3D_DECLARATION(name) \ |
| 69 | __global uchar *name##_ptr, \ |
| 70 | uint name##_stride_x, \ |
| 71 | uint name##_step_x, \ |
| 72 | uint name##_stride_y, \ |
| 73 | uint name##_step_y, \ |
| 74 | uint name##_stride_z, \ |
| 75 | uint name##_step_z, \ |
| 76 | uint name##_offset_first_element_in_bytes |
| 77 | |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 78 | #define TENSOR4D_DECLARATION(name) \ |
| 79 | __global uchar *name##_ptr, \ |
| 80 | uint name##_stride_x, \ |
| 81 | uint name##_step_x, \ |
| 82 | uint name##_stride_y, \ |
| 83 | uint name##_step_y, \ |
| 84 | uint name##_stride_z, \ |
| 85 | uint name##_step_z, \ |
| 86 | uint name##_stride_w, \ |
| 87 | uint name##_step_w, \ |
| 88 | uint name##_offset_first_element_in_bytes |
| 89 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | #define CONVERT_TO_VECTOR_STRUCT(name) \ |
| 91 | update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x) |
| 92 | |
| 93 | #define CONVERT_TO_VECTOR_STRUCT_NO_STEP(name) \ |
| 94 | update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0) |
| 95 | |
| 96 | #define CONVERT_TO_IMAGE_STRUCT(name) \ |
| 97 | update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y) |
| 98 | |
| 99 | #define CONVERT_TO_IMAGE_STRUCT_NO_STEP(name) \ |
| 100 | update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0) |
| 101 | |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 102 | #define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \ |
| 103 | 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) |
| 104 | |
Anthony Barbier | 7ff47a3 | 2017-07-11 16:54:04 +0100 | [diff] [blame] | 105 | #define CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(name) \ |
| 106 | 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) |
| 107 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 108 | #define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \ |
| 109 | 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) |
| 110 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | #define CONVERT_TO_TENSOR3D_STRUCT(name) \ |
| 112 | update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \ |
| 113 | name##_stride_z, name##_step_z) |
| 114 | |
| 115 | #define CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(name) \ |
| 116 | update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0, name##_stride_z, 0) |
| 117 | |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 118 | #define CONVERT_TO_TENSOR4D_STRUCT(name, mod_size) \ |
| 119 | update_tensor4D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \ |
| 120 | name##_stride_z, name##_step_z, name##_stride_w, name##_step_z, mod_size) |
| 121 | |
| 122 | #define CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(name, mod_size) \ |
| 123 | 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) |
| 124 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | /** Structure to hold Vector information */ |
| 126 | typedef struct Vector |
| 127 | { |
| 128 | __global uchar *ptr; /**< Pointer to the starting postion of the buffer */ |
| 129 | int offset_first_element_in_bytes; /**< The offset of the first element in the source image */ |
| 130 | int stride_x; /**< Stride of the image in X dimension (in bytes) */ |
| 131 | } Vector; |
| 132 | |
| 133 | /** Structure to hold Image information */ |
| 134 | typedef struct Image |
| 135 | { |
| 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) */ |
| 139 | int stride_y; /**< Stride of the image in Y dimension (in bytes) */ |
| 140 | } Image; |
| 141 | |
| 142 | /** Structure to hold 3D tensor information */ |
| 143 | typedef struct Tensor3D |
| 144 | { |
| 145 | __global uchar *ptr; /**< Pointer to the starting postion of the buffer */ |
| 146 | int offset_first_element_in_bytes; /**< The offset of the first element in the source image */ |
| 147 | int stride_x; /**< Stride of the image in X dimension (in bytes) */ |
| 148 | int stride_y; /**< Stride of the image in Y dimension (in bytes) */ |
| 149 | int stride_z; /**< Stride of the image in Z dimension (in bytes) */ |
| 150 | } Tensor3D; |
| 151 | |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 152 | /** Structure to hold 4D tensor information */ |
| 153 | typedef struct Tensor4D |
| 154 | { |
| 155 | __global uchar *ptr; /**< Pointer to the starting postion of the buffer */ |
| 156 | int offset_first_element_in_bytes; /**< The offset of the first element in the source image */ |
| 157 | int stride_x; /**< Stride of the image in X dimension (in bytes) */ |
| 158 | int stride_y; /**< Stride of the image in Y dimension (in bytes) */ |
| 159 | int stride_z; /**< Stride of the image in Z dimension (in bytes) */ |
| 160 | int stride_w; /**< Stride of the image in W dimension (in bytes) */ |
| 161 | } Tensor4D; |
| 162 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | /** Wrap vector information into an Vector structure, and make the pointer point at this workitem's data. |
| 164 | * |
| 165 | * @param[in] ptr Pointer to the starting postion of the buffer |
| 166 | * @param[in] offset_first_element_in_bytes The offset of the first element in the source vector |
| 167 | * @param[in] stride_x Stride of the vector in X dimension (in bytes) |
| 168 | * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes) |
| 169 | * |
| 170 | * @return An image object |
| 171 | */ |
| 172 | Vector inline update_vector_workitem_ptr(__global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint step_x) |
| 173 | { |
| 174 | Vector vector = |
| 175 | { |
| 176 | .ptr = ptr, |
| 177 | .offset_first_element_in_bytes = offset_first_element_in_bytes, |
| 178 | .stride_x = stride_x, |
| 179 | }; |
| 180 | vector.ptr += vector.offset_first_element_in_bytes + get_global_id(0) * step_x; |
| 181 | return vector; |
| 182 | } |
| 183 | |
| 184 | /** Wrap image information into an Image structure, and make the pointer point at this workitem's data. |
| 185 | * |
| 186 | * @param[in] ptr Pointer to the starting postion of the buffer |
| 187 | * @param[in] offset_first_element_in_bytes The offset of the first element in the source image |
| 188 | * @param[in] stride_x Stride of the image in X dimension (in bytes) |
| 189 | * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes) |
| 190 | * @param[in] stride_y Stride of the image in Y dimension (in bytes) |
| 191 | * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes) |
| 192 | * |
| 193 | * @return An image object |
| 194 | */ |
| 195 | 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) |
| 196 | { |
| 197 | Image img = |
| 198 | { |
| 199 | .ptr = ptr, |
| 200 | .offset_first_element_in_bytes = offset_first_element_in_bytes, |
| 201 | .stride_x = stride_x, |
| 202 | .stride_y = stride_y |
| 203 | }; |
| 204 | img.ptr += img.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y; |
| 205 | return img; |
| 206 | } |
| 207 | |
Anthony Barbier | 7ff47a3 | 2017-07-11 16:54:04 +0100 | [diff] [blame] | 208 | /** Wrap 3D tensor information into an image structure, and make the pointer point at this workitem's data. |
| 209 | * |
| 210 | * @param[in] ptr Pointer to the starting postion of the buffer |
| 211 | * @param[in] offset_first_element_in_bytes The offset of the first element in the source image |
| 212 | * @param[in] stride_x Stride of the image in X dimension (in bytes) |
| 213 | * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes) |
| 214 | * @param[in] stride_y Stride of the image in Y dimension (in bytes) |
| 215 | * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes) |
| 216 | * @param[in] stride_z Stride of the image in Z dimension (in bytes) |
| 217 | * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes) |
| 218 | * |
| 219 | * @return A 3D tensor object |
| 220 | */ |
| 221 | 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) |
| 222 | { |
| 223 | Image img = |
| 224 | { |
| 225 | .ptr = ptr, |
| 226 | .offset_first_element_in_bytes = offset_first_element_in_bytes, |
| 227 | .stride_x = stride_x, |
| 228 | .stride_y = stride_y |
| 229 | }; |
| 230 | 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; |
| 231 | return img; |
| 232 | } |
| 233 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 234 | /** Wrap 3D tensor information into an tensor structure, and make the pointer point at this workitem's data. |
| 235 | * |
| 236 | * @param[in] ptr Pointer to the starting postion of the buffer |
| 237 | * @param[in] offset_first_element_in_bytes The offset of the first element in the source image |
| 238 | * @param[in] stride_x Stride of the image in X dimension (in bytes) |
| 239 | * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes) |
| 240 | * @param[in] stride_y Stride of the image in Y dimension (in bytes) |
| 241 | * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes) |
| 242 | * @param[in] stride_z Stride of the image in Z dimension (in bytes) |
| 243 | * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes) |
| 244 | * |
| 245 | * @return A 3D tensor object |
| 246 | */ |
| 247 | 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) |
| 248 | { |
| 249 | Tensor3D tensor = |
| 250 | { |
| 251 | .ptr = ptr, |
| 252 | .offset_first_element_in_bytes = offset_first_element_in_bytes, |
| 253 | .stride_x = stride_x, |
| 254 | .stride_y = stride_y, |
| 255 | .stride_z = stride_z |
| 256 | }; |
| 257 | 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; |
| 258 | return tensor; |
| 259 | } |
| 260 | |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 261 | 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, |
| 262 | uint step_w, |
| 263 | uint mod_size) |
| 264 | { |
| 265 | Tensor4D tensor = |
| 266 | { |
| 267 | .ptr = ptr, |
| 268 | .offset_first_element_in_bytes = offset_first_element_in_bytes, |
| 269 | .stride_x = stride_x, |
| 270 | .stride_y = stride_y, |
| 271 | .stride_z = stride_z, |
| 272 | .stride_w = stride_w |
| 273 | }; |
| 274 | |
| 275 | 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; |
| 276 | return tensor; |
| 277 | } |
| 278 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 279 | /** Get the pointer position of a Vector |
| 280 | * |
| 281 | * @param[in] vec Pointer to the starting position of the buffer |
| 282 | * @param[in] x Relative X position |
| 283 | */ |
| 284 | __global inline const uchar *vector_offset(const Vector *vec, int x) |
| 285 | { |
| 286 | return vec->ptr + x * vec->stride_x; |
| 287 | } |
| 288 | |
| 289 | /** Get the pointer position of a Image |
| 290 | * |
| 291 | * @param[in] img Pointer to the starting position of the buffer |
| 292 | * @param[in] x Relative X position |
| 293 | * @param[in] y Relative Y position |
| 294 | */ |
| 295 | __global inline uchar *offset(const Image *img, int x, int y) |
| 296 | { |
| 297 | return img->ptr + x * img->stride_x + y * img->stride_y; |
| 298 | } |
| 299 | |
| 300 | /** Get the pointer position of a Tensor3D |
| 301 | * |
Gian Marco Iodice | 3a62324 | 2017-07-25 10:25:53 +0100 | [diff] [blame] | 302 | * @param[in] tensor Pointer to the starting position of the buffer |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 303 | * @param[in] x Relative X position |
| 304 | * @param[in] y Relative Y position |
| 305 | * @param[in] z Relative Z position |
| 306 | */ |
| 307 | __global inline const uchar *tensor3D_offset(const Tensor3D *tensor, int x, int y, int z) |
| 308 | { |
| 309 | return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z; |
| 310 | } |
| 311 | |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 312 | /** Get the pointer position of a Tensor4D |
| 313 | * |
| 314 | * @param[in] tensor Pointer to the starting position of the buffer |
| 315 | * @param[in] x Relative X position |
| 316 | * @param[in] y Relative Y position |
| 317 | * @param[in] z Relative Z position |
| 318 | * @param[in] w Relative W position |
| 319 | */ |
| 320 | __global inline const uchar *tensor4D_offset(const Tensor4D *tensor, int x, int y, int z, int w) |
| 321 | { |
| 322 | return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z + w * tensor->stride_w; |
| 323 | } |
| 324 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 325 | #endif // _HELPER_H |