blob: 10f04e9e310ef4e930963b5339036747d89f3b86 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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
Giorgio Arenad304adb2020-10-02 10:20:11 +010027#include "load_store_utility.h"
28
Georgios Pinitasdaa38552018-08-28 17:43:18 +010029#if defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#pragma OPENCL EXTENSION cl_khr_fp16 : enable
Georgios Pinitasdaa38552018-08-28 17:43:18 +010031#endif // defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
Matthew Bentham6f31f8c2017-10-27 11:50:06 +010032
Georgios Pinitasdaa38552018-08-28 17:43:18 +010033#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Michalis Spyroue03342e2018-01-15 14:39:13 +000034#pragma OPENCL EXTENSION cl_arm_integer_dot_product_int8 : enable
Georgios Pinitasdaa38552018-08-28 17:43:18 +010035#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Michalis Spyroue03342e2018-01-15 14:39:13 +000036
Georgios Pinitasdaa38552018-08-28 17:43:18 +010037#if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Giorgio Arenaeff8d952018-07-02 15:29:57 +010038#pragma OPENCL EXTENSION cl_arm_integer_dot_product_accumulate_int8 : enable
Georgios Pinitasdaa38552018-08-28 17:43:18 +010039#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Giorgio Arenaeff8d952018-07-02 15:29:57 +010040
Georgios Pinitasdaa38552018-08-28 17:43:18 +010041#if defined(ARM_COMPUTE_DEBUG_ENABLED) && defined(cl_arm_printf)
steniu01f01f9de2017-09-27 17:00:11 +010042#pragma OPENCL EXTENSION cl_arm_printf : enable
Georgios Pinitas238c97c2018-08-31 17:28:29 +010043#endif // defined(ARM_COMPUTE_DEBUG_ENABLED) && defined(cl_arm_printf)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
Usama Arife2428a02019-05-09 11:03:17 +010045#define GPU_ARCH_MIDGARD 0x100
46#define GPU_ARCH_BIFROST 0x200
47
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000048/** Concatenate two inputs.
49 *
50 * @param[in] a The first input to be concatenated
51 * @param[in] b The second input to be concatenated
52 *
53 * @return The concatenated output
54 */
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010055#define CONCAT(a, b) a##b
56
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000057/** Expand the given vector
58 *
59 * @param[in] x The vector to be expanded
60 *
61 * @return The expanded output
62 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010063#define EXPAND(x) x
64
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000065/** Clamp the given value between an upper and lower bound.
66 *
67 * @param[in] x The value to be clamped
68 * @param[in] min_val The lower bound
69 * @param[in] max_val The upper bound
70 *
71 * @return The clamped value.
72 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073#define CLAMP(x, min_val, max_val) min(max(x, min_val), max_val)
74
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000075/** REVn reverses the given vector whose size is n.
76 * @name REVn
77 *
78 * @param[in] x The vector to be reversed
79 *
80 * @return The reversed vector
81 * @{
82 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010083#define REV1(x) ((x))
84#define REV2(x) ((x).s10)
85#define REV3(x) ((x).s210)
86#define REV4(x) ((x).s3210)
87#define REV8(x) ((x).s76543210)
88#define REV16(x) ((x).sFEDCBA9876543210)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000089/** @} */ // end of group REVn
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010090
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000091/** Reverse the given vector.
92 * @name REVERSE
93 *
94 * @param[in] x The vector to be reversed
95 * @param[in] s The size of the vector
96 *
97 * @return The reversed vector
98 * @{
99 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100100#define REVERSE_STR(x, s) REV##s((x))
101#define REVERSE(x, s) REVERSE_STR(x, s)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000102/** @} */ // end of group REVERSE
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100103
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000104/** Circular-right-shift (rotate-right) the vector of size s by the amount of n.
105 * @name ROTs_n
106 *
107 * @param[in] x The vector to be shifted
108 *
109 * @return The shifted vector
110 * @{
111 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100112#define ROT1_0(x) ((x))
113
114#define ROT2_0(x) ((x))
115#define ROT2_1(x) ((x).s10)
116
117#define ROT3_0(x) ((x))
118#define ROT3_1(x) ((x).s201)
119#define ROT3_2(x) ((x).s120)
120
121#define ROT4_0(x) ((x))
122#define ROT4_1(x) ((x).s3012)
123#define ROT4_2(x) ((x).s2301)
124#define ROT4_3(x) ((x).s1230)
125
126#define ROT8_0(x) ((x))
127#define ROT8_1(x) ((x).s70123456)
128#define ROT8_2(x) ((x).s67012345)
129#define ROT8_3(x) ((x).s56701234)
130#define ROT8_4(x) ((x).s45670123)
131#define ROT8_5(x) ((x).s34567012)
132#define ROT8_6(x) ((x).s23456701)
133#define ROT8_7(x) ((x).s12345670)
134
135#define ROT16_0(x) ((x))
136#define ROT16_1(x) ((x).sF0123456789ABCDE)
137#define ROT16_2(x) ((x).sEF0123456789ABCD)
138#define ROT16_3(x) ((x).sDEF0123456789ABC)
139#define ROT16_4(x) ((x).sCDEF0123456789AB)
140#define ROT16_5(x) ((x).sBCDEF0123456789A)
141#define ROT16_6(x) ((x).sABCDEF0123456789)
142#define ROT16_7(x) ((x).s9ABCDEF012345678)
143#define ROT16_8(x) ((x).s89ABCDEF01234567)
144#define ROT16_9(x) ((x).s789ABCDEF0123456)
145#define ROT16_10(x) ((x).s6789ABCDEF012345)
146#define ROT16_11(x) ((x).s56789ABCDEF01234)
147#define ROT16_12(x) ((x).s456789ABCDEF0123)
148#define ROT16_13(x) ((x).s3456789ABCDEF012)
149#define ROT16_14(x) ((x).s23456789ABCDEF01)
150#define ROT16_15(x) ((x).s123456789ABCDEF0)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000151/** @} */ // end of group ROTs_n
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100152
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000153/** Circular-right-shift (rotate-right) the given vector by the given amount.
154 * @name ROTATE
155 *
156 * @param[in] x The vector to be shifted
157 * @param[in] s The size of the vector
158 * @param[in] n The amount to be shifted
159 *
160 * @return The shifted vector
161 * @{
162 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100163#define ROTATE_STR(x, s, n) ROT##s##_##n(x)
164#define ROTATE(x, s, n) ROTATE_STR(x, s, n)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000165/** @} */ // end of group ROTATE
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100166
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000167/** Creates a vector of size n filled with offset values corresponding to the location of each element.
168 * @name V_OFFSn
169 *
170 * @param[in] dt The data type of the output vector
171 *
172 * @return The vector filled with offset values
173 * @{
174 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100175#define V_OFFS1(dt) (dt)(0)
176#define V_OFFS2(dt) (dt)(0, 1)
177#define V_OFFS3(dt) (dt)(0, 1, 3)
178#define V_OFFS4(dt) (dt)(0, 1, 2, 3)
179#define V_OFFS8(dt) (dt)(0, 1, 2, 3, 4, 5, 6, 7)
180#define V_OFFS16(dt) (dt)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000181/** @} */ // end of group V_OFFSn
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100182
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000183/** Create a vector filled with offset values corresponding to the location of each element.
184 * @name VEC_OFFS
185 *
186 * @param[in] dt The data type of the output vector
187 * @param[in] s The size of the output vector
188 *
189 * @return The vector filled with offset values
190 * @{
191 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100192#define VEC_OFFS_STR(dt, s) V_OFFS##s(dt)
193#define VEC_OFFS(dt, s) VEC_OFFS_STR(dt, s)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000194/** @} */ // end of group VEC_OFFS
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100195
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100196#define VLOAD_STR(size) vload##size
197#define VLOAD(size) VLOAD_STR(size)
198
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100199#define PIXEL_UNIT4 1
200#define PIXEL_UNIT8 2
201#define PIXEL_UNIT16 4
202
203/** Utility macro to convert a vector size in pixel unit.
204 *
205 * @name CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT
206 *
207 * @param[in] vec_size Vector size. Only 4,8 and 16 is supported
208 *
209 * @return The pixel unit (number of pixels)
210 * @{
211 */
212#define CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT_STR(vec_size) PIXEL_UNIT##vec_size
213#define CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT(vec_size) CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT_STR(vec_size)
214/** @} */ // end of group CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT
215
216#define read_image2d_floatx1(img, x_coord, y_coord) (float4)(read_imagef(img, (int2)(x_coord, y_coord)));
217#define read_image2d_floatx2(img, x_coord, y_coord) (float8)(read_imagef(img, (int2)(x_coord, y_coord)), read_imagef(img, (int2)(x_coord + 1, y_coord)));
218#define read_image2d_floatx4(img, x_coord, y_coord) (float16)(read_imagef(img, (int2)(x_coord, y_coord)), read_imagef(img, (int2)(x_coord + 1, y_coord)), read_imagef(img, (int2)(x_coord + 2, y_coord)), read_imagef(img, (int2)(x_coord + 3, y_coord)));
219
220#if defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
221#define read_image2d_halfx1(img, x_coord, y_coord) (half4)(read_imageh(img, (int2)(x_coord, y_coord)));
222#define read_image2d_halfx2(img, x_coord, y_coord) (half8)(read_imageh(img, (int2)(x_coord, y_coord)), read_imageh(img, (int2)(x_coord + 1, y_coord)));
223#define read_image2d_halfx4(img, x_coord, y_coord) (half16)(read_imageh(img, (int2)(x_coord, y_coord)), read_imageh(img, (int2)(x_coord + 1, y_coord)), read_imageh(img, (int2)(x_coord + 2, y_coord)), read_imageh(img, (int2)(x_coord + 3, y_coord)));
224#endif // defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
225
226/** Utility macro to read a 2D OpenCL image object.
227 *
228 * @note Coordinates are not normalized
229 *
230 * @param[in] data_type Data type
231 * @param[in] n0 Number of pixel to read. Only 1,2 and 4 is supported
232 * @param[in] img OpenCL image object
233 * @param[in] x_coord The x coordinate for the top-left pixel
234 * @param[in] y_coord The y coordinate for the top-left pixel
235 *
236 * @return Pixels from the 2D OpenCL image object
237 * @{
238 */
239#define READ_IMAGE2D_STR(data_type, n0, img, x_coord, y_coord) read_image2d_##data_type##x##n0(img, x_coord, y_coord)
240#define READ_IMAGE2D(data_type, n0, img, x_coord, y_coord) READ_IMAGE2D_STR(data_type, n0, img, x_coord, y_coord)
241
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100242#define VSTORE_STR(size) vstore##size
243#define VSTORE(size) VSTORE_STR(size)
244
Manuel Bottini0d0028c2018-10-02 16:41:52 +0100245#define float1 float
246#define half1 half
Usama Arif0681e3b2019-04-25 14:28:07 +0100247#define char1 char
248#define uchar1 uchar
249#define short1 short
250#define ushort1 ushort
251#define int1 int
252#define uint1 uint
253#define long1 long
254#define ulong1 ulong
255#define double1 double
256
257#define vload1(OFFSET, PTR) *(OFFSET + PTR)
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100258#define vstore1(DATA, OFFSET, PTR) *(OFFSET + PTR) = DATA
Manuel Bottini0d0028c2018-10-02 16:41:52 +0100259
SiCong Li3a501662020-06-26 10:02:06 +0100260/** Extended partial vstore that correctly handles scalar values as well.
261 * Store the **lower** 0 to (n-1)th elements of the given vector while minimising the amount of vstore ops
262 * @name VSTORE_PARTIAL
263 *
264 * @note With this macro, the passed data can be both a vector and a scalar
265 * @note @p store_size needs to be <= @p size
266 * eg 1: Valid
267 * VSTORE_PARTIAL(16, 15) ...;
268 * eg 2: Invalid
269 * VSTORE_PARTIAL(4, 7) ...;
270 *
271 * @param[in] size The width of @p DATA. Supported values: 1(scalar), 2, 3, 4, 8, 16
272 * @param[in] store_size The number of lower elements to store. Supported values: 1-16, but has to be <= @p size
273 * @{
274 */
275#define VSTORE_PARTIAL_STR(size, store_size) vstore_partial_##size##_##store_size
276#define VSTORE_PARTIAL(size, store_size) VSTORE_PARTIAL_STR(size, store_size)
277
Giorgio Arenad304adb2020-10-02 10:20:11 +0100278#define NO_STORE(data, offs, ptr) \
279 { \
280 }
281
SiCong Li3a501662020-06-26 10:02:06 +0100282// Size == 1 (scalar)
Giorgio Arenad304adb2020-10-02 10:20:11 +0100283#define vstore_partial_1_0 NO_STORE
SiCong Li3a501662020-06-26 10:02:06 +0100284#define vstore_partial_1_1 vstore1
285// Size == 2
Giorgio Arenad304adb2020-10-02 10:20:11 +0100286#define vstore_partial_2_0 NO_STORE
SiCong Li3a501662020-06-26 10:02:06 +0100287#define vstore_partial_2_1 vstore_partial_1
288#define vstore_partial_2_2 vstore_partial_2
289// Size == 3
Giorgio Arenad304adb2020-10-02 10:20:11 +0100290#define vstore_partial_3_0 NO_STORE
SiCong Li3a501662020-06-26 10:02:06 +0100291#define vstore_partial_3_1 vstore_partial_1
292#define vstore_partial_3_2 vstore_partial_2
293#define vstore_partial_3_3 vstore_partial_3
294// Size == 4
Giorgio Arenad304adb2020-10-02 10:20:11 +0100295#define vstore_partial_4_0 NO_STORE
SiCong Li3a501662020-06-26 10:02:06 +0100296#define vstore_partial_4_1 vstore_partial_1
297#define vstore_partial_4_2 vstore_partial_2
298#define vstore_partial_4_3 vstore_partial_3
299#define vstore_partial_4_4 vstore_partial_4
300// Size == 8
Giorgio Arenad304adb2020-10-02 10:20:11 +0100301#define vstore_partial_8_0 NO_STORE
SiCong Li3a501662020-06-26 10:02:06 +0100302#define vstore_partial_8_1 vstore_partial_1
303#define vstore_partial_8_2 vstore_partial_2
304#define vstore_partial_8_3 vstore_partial_3
305#define vstore_partial_8_4 vstore_partial_4
306#define vstore_partial_8_5 vstore_partial_5
307#define vstore_partial_8_6 vstore_partial_6
308#define vstore_partial_8_7 vstore_partial_7
309#define vstore_partial_8_8 vstore_partial_8
310// Size == 16
Giorgio Arenad304adb2020-10-02 10:20:11 +0100311#define vstore_partial_16_0 NO_STORE
SiCong Li3a501662020-06-26 10:02:06 +0100312#define vstore_partial_16_1 vstore_partial_1
313#define vstore_partial_16_2 vstore_partial_2
314#define vstore_partial_16_3 vstore_partial_3
315#define vstore_partial_16_4 vstore_partial_4
316#define vstore_partial_16_5 vstore_partial_5
317#define vstore_partial_16_6 vstore_partial_6
318#define vstore_partial_16_7 vstore_partial_7
319#define vstore_partial_16_8 vstore_partial_8
320#define vstore_partial_16_9 vstore_partial_9
321#define vstore_partial_16_10 vstore_partial_10
322#define vstore_partial_16_11 vstore_partial_11
323#define vstore_partial_16_12 vstore_partial_12
324#define vstore_partial_16_13 vstore_partial_13
325#define vstore_partial_16_14 vstore_partial_14
326#define vstore_partial_16_15 vstore_partial_15
327#define vstore_partial_16_16 vstore_partial_16
328
329/** Partial vstore. Store the **lower** 0 to (n-1)th elements of the given vector while minimising the amount of vstore ops
330 * @name vstore_partial_n
331 *
332 * @note @p DATA needs to be a vector not a scalar
333 * @note n needs to be <= the vector width of the input variable @p DATA
334 * eg 1: Valid
335 * vstore_partial_15(var:float16, 0, 0xabcd);
336 * eg 2: Invalid
337 * vstore_partial_7(var:float4, 0, 0xabcd);
338 *
339 * @note in cases n == 1, 2, 3, 4, 8, 16, no extra vstore is invoked, thus there's no performance penalty.
340 *
341 * @param[in] DATA The name of the variable
342 * @param[in] OFFSET Offset in n
343 * @param[in] PTR The base pointer
344 * @{
345 */
346#define vstore_partial_1(DATA, OFFSET, PTR) \
347 vstore1(DATA.s0, OFFSET, PTR);
348
349#define vstore_partial_2(DATA, OFFSET, PTR) \
350 vstore2(DATA.s01, OFFSET, PTR);
351
352#define vstore_partial_3(DATA, OFFSET, PTR) \
353 vstore3(DATA.s012, OFFSET, PTR);
354
355#define vstore_partial_4(DATA, OFFSET, PTR) \
356 vstore4(DATA.s0123, OFFSET, PTR);
357
358#define vstore_partial_5(DATA, OFFSET, PTR) \
359 vstore_partial_4(DATA.s0123, OFFSET, PTR); \
SiCong Li3b64e3e2020-07-28 09:01:28 +0100360 vstore1(DATA.s4, OFFSET, PTR + 4);
SiCong Li3a501662020-06-26 10:02:06 +0100361
362#define vstore_partial_6(DATA, OFFSET, PTR) \
363 vstore_partial_4(DATA.s0123, OFFSET, PTR); \
364 vstore_partial_2(DATA.s45, OFFSET, PTR + 4);
365
366#define vstore_partial_7(DATA, OFFSET, PTR) \
367 vstore_partial_4(DATA.s0123, OFFSET, PTR); \
368 vstore_partial_3(DATA.s456, OFFSET, PTR + 4);
369
370#define vstore_partial_8(DATA, OFFSET, PTR) \
371 vstore8(DATA.s01234567, OFFSET, PTR);
372
373#define vstore_partial_9(DATA, OFFSET, PTR) \
374 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
SiCong Li3b64e3e2020-07-28 09:01:28 +0100375 vstore1(DATA.s8, OFFSET, PTR + 8);
SiCong Li3a501662020-06-26 10:02:06 +0100376
377#define vstore_partial_10(DATA, OFFSET, PTR) \
378 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
379 vstore_partial_2(DATA.s89, OFFSET, PTR + 8);
380
381#define vstore_partial_11(DATA, OFFSET, PTR) \
382 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
383 vstore_partial_3(DATA.s89a, OFFSET, PTR + 8);
384
385#define vstore_partial_12(DATA, OFFSET, PTR) \
386 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
387 vstore_partial_4(DATA.s89ab, OFFSET, PTR + 8);
388
389#define vstore_partial_13(DATA, OFFSET, PTR) \
390 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
Giorgio Arenad304adb2020-10-02 10:20:11 +0100391 vstore_partial_5(DATA.s89abcdef, OFFSET, PTR + 8);
SiCong Li3a501662020-06-26 10:02:06 +0100392
393#define vstore_partial_14(DATA, OFFSET, PTR) \
394 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
Giorgio Arenad304adb2020-10-02 10:20:11 +0100395 vstore_partial_6(DATA.s89abcdef, OFFSET, PTR + 8);
SiCong Li3a501662020-06-26 10:02:06 +0100396
397#define vstore_partial_15(DATA, OFFSET, PTR) \
398 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
Giorgio Arenad304adb2020-10-02 10:20:11 +0100399 vstore_partial_7(DATA.s89abcdef, OFFSET, PTR + 8);
SiCong Li3a501662020-06-26 10:02:06 +0100400
401#define vstore_partial_16(DATA, OFFSET, PTR) \
402 vstore16(DATA, OFFSET, PTR);
403/** @} */ // end of groupd vstore_partial_n
404/** @} */ // end of groupd VSTORE_PARTIAL
405
Gian Marco Iodice0c17aa22019-09-27 09:23:15 +0100406// Convert built-in functions with _sat modifier are not supported in floating point so we create defines
407// without _sat to overcome this issue
408#define convert_float_sat convert_float
409#define convert_float1_sat convert_float
410#define convert_float2_sat convert_float2
411#define convert_float3_sat convert_float3
412#define convert_float4_sat convert_float4
413#define convert_float8_sat convert_float8
414#define convert_float16_sat convert_float16
415#define convert_half_sat convert_float
416#define convert_half1_sat convert_half
417#define convert_half2_sat convert_half2
418#define convert_half3_sat convert_half3
419#define convert_half4_sat convert_half4
420#define convert_half8_sat convert_half8
421#define convert_half16_sat convert_half16
422
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100423#define convert_float1 convert_float
424#define convert_half1 convert_half
425#define convert_char1 convert_char
426#define convert_uchar1 convert_uchar
427#define convert_short1 convert_short
428#define convert_ushort1 convert_ushort
429#define convert_int1 convert_int
430#define convert_uint1 convert_uint
431#define convert_long1 convert_long
432#define convert_ulong1 convert_ulong
433#define convert_double1 convert_double
434
435#define convert_char1_sat convert_char_sat
436#define convert_uchar1_sat convert_uchar_sat
437#define convert_short1_sat convert_short_sat
438#define convert_ushort1_sat convert_ushort_sat
439#define convert_int1_sat convert_int_sat
440#define convert_uint1_sat convert_uint_sat
441#define convert_long1_sat convert_long_sat
442#define convert_ulong1_sat convert_ulong_sat
443#define convert_double1_sat convert_double_sat
444
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100445#define VEC_DATA_TYPE_STR(type, size) type##size
446#define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size)
447
Chunosovd6afedc2017-11-06 22:09:45 +0700448#define CL_VEC_DATA_TYPE_STR(type, size) type##size
449#define CL_VEC_DATA_TYPE(type, size) CL_VEC_DATA_TYPE_STR(type, size)
450
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100451#define CONVERT_STR(x, type) (convert_##type((x)))
452#define CONVERT(x, type) CONVERT_STR(x, type)
453
454#define CONVERT_SAT_STR(x, type) (convert_##type##_sat((x)))
455#define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type)
456
457#define CONVERT_SAT_ROUND_STR(x, type, round) (convert_##type##_sat_##round((x)))
458#define CONVERT_SAT_ROUND(x, type, round) CONVERT_SAT_ROUND_STR(x, type, round)
459
460#define VECTOR_DECLARATION(name) \
461 __global uchar *name##_ptr, \
462 uint name##_stride_x, \
463 uint name##_step_x, \
464 uint name##_offset_first_element_in_bytes
465
466#define IMAGE_DECLARATION(name) \
467 __global uchar *name##_ptr, \
468 uint name##_stride_x, \
469 uint name##_step_x, \
470 uint name##_stride_y, \
471 uint name##_step_y, \
472 uint name##_offset_first_element_in_bytes
473
474#define TENSOR3D_DECLARATION(name) \
475 __global uchar *name##_ptr, \
476 uint name##_stride_x, \
477 uint name##_step_x, \
478 uint name##_stride_y, \
479 uint name##_step_y, \
480 uint name##_stride_z, \
481 uint name##_step_z, \
482 uint name##_offset_first_element_in_bytes
483
steniu01868e5412017-07-17 23:16:00 +0100484#define TENSOR4D_DECLARATION(name) \
485 __global uchar *name##_ptr, \
486 uint name##_stride_x, \
487 uint name##_step_x, \
488 uint name##_stride_y, \
489 uint name##_step_y, \
490 uint name##_stride_z, \
491 uint name##_step_z, \
492 uint name##_stride_w, \
493 uint name##_step_w, \
494 uint name##_offset_first_element_in_bytes
495
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100496#define CONVERT_TO_VECTOR_STRUCT(name) \
497 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x)
498
499#define CONVERT_TO_VECTOR_STRUCT_NO_STEP(name) \
500 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0)
501
502#define CONVERT_TO_IMAGE_STRUCT(name) \
503 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y)
504
505#define CONVERT_TO_IMAGE_STRUCT_NO_STEP(name) \
506 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0)
507
steniu01868e5412017-07-17 23:16:00 +0100508#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
509 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)
510
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100511#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(name) \
512 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)
513
steniu010d523cc2017-07-13 14:24:23 +0100514#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
515 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)
516
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100517#define CONVERT_TO_TENSOR3D_STRUCT(name) \
518 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
519 name##_stride_z, name##_step_z)
520
521#define CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(name) \
522 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0, name##_stride_z, 0)
523
steniu01868e5412017-07-17 23:16:00 +0100524#define CONVERT_TO_TENSOR4D_STRUCT(name, mod_size) \
525 update_tensor4D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
Michalis Spyrou5237e012018-01-17 09:40:27 +0000526 name##_stride_z, name##_step_z, name##_stride_w, name##_step_w, mod_size)
steniu01868e5412017-07-17 23:16:00 +0100527
528#define CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(name, mod_size) \
529 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)
530
Gian Marco Iodice4d81d752020-07-14 15:05:31 +0100531#define CONVERT_TO_TENSOR3D_STRUCT_NO_UPDATE_PTR(name) \
532 tensor3D_ptr_no_update(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
533 name##_stride_z, name##_step_z)
534
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100535/** Structure to hold Vector information */
536typedef struct Vector
537{
538 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
539 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
540 int stride_x; /**< Stride of the image in X dimension (in bytes) */
541} Vector;
542
543/** Structure to hold Image information */
544typedef struct Image
545{
546 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
547 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
548 int stride_x; /**< Stride of the image in X dimension (in bytes) */
549 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
550} Image;
551
552/** Structure to hold 3D tensor information */
553typedef struct Tensor3D
554{
555 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
556 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
557 int stride_x; /**< Stride of the image in X dimension (in bytes) */
558 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
559 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
560} Tensor3D;
561
steniu01868e5412017-07-17 23:16:00 +0100562/** Structure to hold 4D tensor information */
563typedef struct Tensor4D
564{
565 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
566 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
567 int stride_x; /**< Stride of the image in X dimension (in bytes) */
568 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
569 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
570 int stride_w; /**< Stride of the image in W dimension (in bytes) */
571} Tensor4D;
572
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100573/** Wrap vector information into an Vector structure, and make the pointer point at this workitem's data.
574 *
575 * @param[in] ptr Pointer to the starting postion of the buffer
576 * @param[in] offset_first_element_in_bytes The offset of the first element in the source vector
577 * @param[in] stride_x Stride of the vector in X dimension (in bytes)
578 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
579 *
580 * @return An image object
581 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100582inline Vector update_vector_workitem_ptr(__global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint step_x)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100583{
584 Vector vector =
585 {
586 .ptr = ptr,
587 .offset_first_element_in_bytes = offset_first_element_in_bytes,
588 .stride_x = stride_x,
589 };
590 vector.ptr += vector.offset_first_element_in_bytes + get_global_id(0) * step_x;
591 return vector;
592}
593
594/** Wrap image information into an Image structure, and make the pointer point at this workitem's data.
595 *
596 * @param[in] ptr Pointer to the starting postion of the buffer
597 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
598 * @param[in] stride_x Stride of the image in X dimension (in bytes)
599 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
600 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
601 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
602 *
603 * @return An image object
604 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100605inline Image 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)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100606{
607 Image img =
608 {
609 .ptr = ptr,
610 .offset_first_element_in_bytes = offset_first_element_in_bytes,
611 .stride_x = stride_x,
612 .stride_y = stride_y
613 };
614 img.ptr += img.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y;
615 return img;
616}
617
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100618/** Wrap 3D tensor information into an image structure, and make the pointer point at this workitem's data.
619 *
620 * @param[in] ptr Pointer to the starting postion of the buffer
621 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
622 * @param[in] stride_x Stride of the image in X dimension (in bytes)
623 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
624 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
625 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
626 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
627 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
628 *
629 * @return A 3D tensor object
630 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100631inline Image 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)
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100632{
633 Image img =
634 {
635 .ptr = ptr,
636 .offset_first_element_in_bytes = offset_first_element_in_bytes,
637 .stride_x = stride_x,
638 .stride_y = stride_y
639 };
640 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;
641 return img;
642}
643
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100644/** Wrap 3D tensor information into an tensor structure, and make the pointer point at this workitem's data.
645 *
646 * @param[in] ptr Pointer to the starting postion of the buffer
647 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
648 * @param[in] stride_x Stride of the image in X dimension (in bytes)
649 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
650 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
651 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
652 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
653 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
654 *
655 * @return A 3D tensor object
656 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100657inline Tensor3D 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)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100658{
659 Tensor3D tensor =
660 {
661 .ptr = ptr,
662 .offset_first_element_in_bytes = offset_first_element_in_bytes,
663 .stride_x = stride_x,
664 .stride_y = stride_y,
665 .stride_z = stride_z
666 };
667 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;
668 return tensor;
669}
670
Gian Marco Iodice4d81d752020-07-14 15:05:31 +0100671/** Wrap 3D tensor information into an tensor structure.
672 *
673 * @param[in] ptr Pointer to the starting postion of the buffer
674 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
675 * @param[in] stride_x Stride of the image in X dimension (in bytes)
676 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
677 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
678 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
679 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
680 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
681 *
682 * @return A 3D tensor object
683 */
684inline Tensor3D tensor3D_ptr_no_update(__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)
685{
686 Tensor3D tensor =
687 {
688 .ptr = ptr,
689 .offset_first_element_in_bytes = offset_first_element_in_bytes,
690 .stride_x = stride_x,
691 .stride_y = stride_y,
692 .stride_z = stride_z
693 };
694 return tensor;
695}
696
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100697inline Tensor4D 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,
steniu01868e5412017-07-17 23:16:00 +0100698 uint step_w,
699 uint mod_size)
700{
701 Tensor4D tensor =
702 {
703 .ptr = ptr,
704 .offset_first_element_in_bytes = offset_first_element_in_bytes,
705 .stride_x = stride_x,
706 .stride_y = stride_y,
707 .stride_z = stride_z,
708 .stride_w = stride_w
709 };
710
711 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;
712 return tensor;
713}
714
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100715/** Get the pointer position of a Vector
716 *
717 * @param[in] vec Pointer to the starting position of the buffer
718 * @param[in] x Relative X position
719 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100720inline __global const uchar *vector_offset(const Vector *vec, int x)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100721{
722 return vec->ptr + x * vec->stride_x;
723}
724
725/** Get the pointer position of a Image
726 *
727 * @param[in] img Pointer to the starting position of the buffer
728 * @param[in] x Relative X position
729 * @param[in] y Relative Y position
730 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100731inline __global uchar *offset(const Image *img, int x, int y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100732{
733 return img->ptr + x * img->stride_x + y * img->stride_y;
734}
735
736/** Get the pointer position of a Tensor3D
737 *
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100738 * @param[in] tensor Pointer to the starting position of the buffer
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100739 * @param[in] x Relative X position
740 * @param[in] y Relative Y position
741 * @param[in] z Relative Z position
742 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100743inline __global const uchar *tensor3D_offset(const Tensor3D *tensor, int x, int y, int z)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100744{
745 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z;
746}
747
steniu01868e5412017-07-17 23:16:00 +0100748/** Get the pointer position of a Tensor4D
749 *
750 * @param[in] tensor Pointer to the starting position of the buffer
751 * @param[in] x Relative X position
752 * @param[in] y Relative Y position
753 * @param[in] z Relative Z position
754 * @param[in] w Relative W position
755 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100756inline __global const uchar *tensor4D_offset(const Tensor4D *tensor, int x, int y, int z, int w)
steniu01868e5412017-07-17 23:16:00 +0100757{
758 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z + w * tensor->stride_w;
759}
760
Gian Marco Iodice4d81d752020-07-14 15:05:31 +0100761/** Get the offset for a given linear index of a Tensor3D
762 *
763 * @param[in] tensor Pointer to the starting position of the buffer
764 * @param[in] width Width of the input tensor
765 * @param[in] height Height of the input tensor
766 * @param[in] depth Depth of the input tensor
767 * @param[in] index Linear index
768 */
769inline __global const uchar *tensor3D_index2ptr(const Tensor3D *tensor, uint width, uint height, uint depth, uint index)
770{
771 uint num_elements = width * height;
772
773 const uint z = index / num_elements;
774
775 index %= num_elements;
776
777 const uint y = index / width;
778
779 index %= width;
780
781 const uint x = index;
782
783 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z + tensor->offset_first_element_in_bytes;
784}
785
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100786#endif // _HELPER_H