blob: d3ede32bc92058e5c71b27353f72aa0396081c22 [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
Georgios Pinitasdaa38552018-08-28 17:43:18 +010027#if defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#pragma OPENCL EXTENSION cl_khr_fp16 : enable
Georgios Pinitasdaa38552018-08-28 17:43:18 +010029#endif // defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
Matthew Bentham6f31f8c2017-10-27 11:50:06 +010030
Georgios Pinitasdaa38552018-08-28 17:43:18 +010031#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Michalis Spyroue03342e2018-01-15 14:39:13 +000032#pragma OPENCL EXTENSION cl_arm_integer_dot_product_int8 : enable
Georgios Pinitasdaa38552018-08-28 17:43:18 +010033#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Michalis Spyroue03342e2018-01-15 14:39:13 +000034
Georgios Pinitasdaa38552018-08-28 17:43:18 +010035#if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Giorgio Arenaeff8d952018-07-02 15:29:57 +010036#pragma OPENCL EXTENSION cl_arm_integer_dot_product_accumulate_int8 : enable
Georgios Pinitasdaa38552018-08-28 17:43:18 +010037#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Giorgio Arenaeff8d952018-07-02 15:29:57 +010038
Georgios Pinitasdaa38552018-08-28 17:43:18 +010039#if defined(ARM_COMPUTE_DEBUG_ENABLED) && defined(cl_arm_printf)
steniu01f01f9de2017-09-27 17:00:11 +010040#pragma OPENCL EXTENSION cl_arm_printf : enable
Georgios Pinitas238c97c2018-08-31 17:28:29 +010041#endif // defined(ARM_COMPUTE_DEBUG_ENABLED) && defined(cl_arm_printf)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042
Usama Arife2428a02019-05-09 11:03:17 +010043#define GPU_ARCH_MIDGARD 0x100
44#define GPU_ARCH_BIFROST 0x200
45
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000046/** Concatenate two inputs.
47 *
48 * @param[in] a The first input to be concatenated
49 * @param[in] b The second input to be concatenated
50 *
51 * @return The concatenated output
52 */
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010053#define CONCAT(a, b) a##b
54
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000055/** Expand the given vector
56 *
57 * @param[in] x The vector to be expanded
58 *
59 * @return The expanded output
60 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010061#define EXPAND(x) x
62
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000063/** Clamp the given value between an upper and lower bound.
64 *
65 * @param[in] x The value to be clamped
66 * @param[in] min_val The lower bound
67 * @param[in] max_val The upper bound
68 *
69 * @return The clamped value.
70 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071#define CLAMP(x, min_val, max_val) min(max(x, min_val), max_val)
72
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000073/** REVn reverses the given vector whose size is n.
74 * @name REVn
75 *
76 * @param[in] x The vector to be reversed
77 *
78 * @return The reversed vector
79 * @{
80 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010081#define REV1(x) ((x))
82#define REV2(x) ((x).s10)
83#define REV3(x) ((x).s210)
84#define REV4(x) ((x).s3210)
85#define REV8(x) ((x).s76543210)
86#define REV16(x) ((x).sFEDCBA9876543210)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000087/** @} */ // end of group REVn
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010088
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000089/** Reverse the given vector.
90 * @name REVERSE
91 *
92 * @param[in] x The vector to be reversed
93 * @param[in] s The size of the vector
94 *
95 * @return The reversed vector
96 * @{
97 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010098#define REVERSE_STR(x, s) REV##s((x))
99#define REVERSE(x, s) REVERSE_STR(x, s)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000100/** @} */ // end of group REVERSE
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100101
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000102/** Circular-right-shift (rotate-right) the vector of size s by the amount of n.
103 * @name ROTs_n
104 *
105 * @param[in] x The vector to be shifted
106 *
107 * @return The shifted vector
108 * @{
109 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100110#define ROT1_0(x) ((x))
111
112#define ROT2_0(x) ((x))
113#define ROT2_1(x) ((x).s10)
114
115#define ROT3_0(x) ((x))
116#define ROT3_1(x) ((x).s201)
117#define ROT3_2(x) ((x).s120)
118
119#define ROT4_0(x) ((x))
120#define ROT4_1(x) ((x).s3012)
121#define ROT4_2(x) ((x).s2301)
122#define ROT4_3(x) ((x).s1230)
123
124#define ROT8_0(x) ((x))
125#define ROT8_1(x) ((x).s70123456)
126#define ROT8_2(x) ((x).s67012345)
127#define ROT8_3(x) ((x).s56701234)
128#define ROT8_4(x) ((x).s45670123)
129#define ROT8_5(x) ((x).s34567012)
130#define ROT8_6(x) ((x).s23456701)
131#define ROT8_7(x) ((x).s12345670)
132
133#define ROT16_0(x) ((x))
134#define ROT16_1(x) ((x).sF0123456789ABCDE)
135#define ROT16_2(x) ((x).sEF0123456789ABCD)
136#define ROT16_3(x) ((x).sDEF0123456789ABC)
137#define ROT16_4(x) ((x).sCDEF0123456789AB)
138#define ROT16_5(x) ((x).sBCDEF0123456789A)
139#define ROT16_6(x) ((x).sABCDEF0123456789)
140#define ROT16_7(x) ((x).s9ABCDEF012345678)
141#define ROT16_8(x) ((x).s89ABCDEF01234567)
142#define ROT16_9(x) ((x).s789ABCDEF0123456)
143#define ROT16_10(x) ((x).s6789ABCDEF012345)
144#define ROT16_11(x) ((x).s56789ABCDEF01234)
145#define ROT16_12(x) ((x).s456789ABCDEF0123)
146#define ROT16_13(x) ((x).s3456789ABCDEF012)
147#define ROT16_14(x) ((x).s23456789ABCDEF01)
148#define ROT16_15(x) ((x).s123456789ABCDEF0)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000149/** @} */ // end of group ROTs_n
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100150
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000151/** Circular-right-shift (rotate-right) the given vector by the given amount.
152 * @name ROTATE
153 *
154 * @param[in] x The vector to be shifted
155 * @param[in] s The size of the vector
156 * @param[in] n The amount to be shifted
157 *
158 * @return The shifted vector
159 * @{
160 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100161#define ROTATE_STR(x, s, n) ROT##s##_##n(x)
162#define ROTATE(x, s, n) ROTATE_STR(x, s, n)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000163/** @} */ // end of group ROTATE
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100164
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000165/** Creates a vector of size n filled with offset values corresponding to the location of each element.
166 * @name V_OFFSn
167 *
168 * @param[in] dt The data type of the output vector
169 *
170 * @return The vector filled with offset values
171 * @{
172 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100173#define V_OFFS1(dt) (dt)(0)
174#define V_OFFS2(dt) (dt)(0, 1)
175#define V_OFFS3(dt) (dt)(0, 1, 3)
176#define V_OFFS4(dt) (dt)(0, 1, 2, 3)
177#define V_OFFS8(dt) (dt)(0, 1, 2, 3, 4, 5, 6, 7)
178#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 +0000179/** @} */ // end of group V_OFFSn
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100180
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000181/** Create a vector filled with offset values corresponding to the location of each element.
182 * @name VEC_OFFS
183 *
184 * @param[in] dt The data type of the output vector
185 * @param[in] s The size of the output vector
186 *
187 * @return The vector filled with offset values
188 * @{
189 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100190#define VEC_OFFS_STR(dt, s) V_OFFS##s(dt)
191#define VEC_OFFS(dt, s) VEC_OFFS_STR(dt, s)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000192/** @} */ // end of group VEC_OFFS
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100193
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100194#define VLOAD_STR(size) vload##size
195#define VLOAD(size) VLOAD_STR(size)
196
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100197#define PIXEL_UNIT4 1
198#define PIXEL_UNIT8 2
199#define PIXEL_UNIT16 4
200
201/** Utility macro to convert a vector size in pixel unit.
202 *
203 * @name CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT
204 *
205 * @param[in] vec_size Vector size. Only 4,8 and 16 is supported
206 *
207 * @return The pixel unit (number of pixels)
208 * @{
209 */
210#define CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT_STR(vec_size) PIXEL_UNIT##vec_size
211#define CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT(vec_size) CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT_STR(vec_size)
212/** @} */ // end of group CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT
213
214#define read_image2d_floatx1(img, x_coord, y_coord) (float4)(read_imagef(img, (int2)(x_coord, y_coord)));
215#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)));
216#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)));
217
218#if defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
219#define read_image2d_halfx1(img, x_coord, y_coord) (half4)(read_imageh(img, (int2)(x_coord, y_coord)));
220#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)));
221#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)));
222#endif // defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
223
224/** Utility macro to read a 2D OpenCL image object.
225 *
226 * @note Coordinates are not normalized
227 *
228 * @param[in] data_type Data type
229 * @param[in] n0 Number of pixel to read. Only 1,2 and 4 is supported
230 * @param[in] img OpenCL image object
231 * @param[in] x_coord The x coordinate for the top-left pixel
232 * @param[in] y_coord The y coordinate for the top-left pixel
233 *
234 * @return Pixels from the 2D OpenCL image object
235 * @{
236 */
237#define READ_IMAGE2D_STR(data_type, n0, img, x_coord, y_coord) read_image2d_##data_type##x##n0(img, x_coord, y_coord)
238#define READ_IMAGE2D(data_type, n0, img, x_coord, y_coord) READ_IMAGE2D_STR(data_type, n0, img, x_coord, y_coord)
239
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100240#define VSTORE_STR(size) vstore##size
241#define VSTORE(size) VSTORE_STR(size)
242
Manuel Bottini0d0028c2018-10-02 16:41:52 +0100243#define float1 float
244#define half1 half
Usama Arif0681e3b2019-04-25 14:28:07 +0100245#define char1 char
246#define uchar1 uchar
247#define short1 short
248#define ushort1 ushort
249#define int1 int
250#define uint1 uint
251#define long1 long
252#define ulong1 ulong
253#define double1 double
254
255#define vload1(OFFSET, PTR) *(OFFSET + PTR)
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100256#define vstore1(DATA, OFFSET, PTR) *(OFFSET + PTR) = DATA
Manuel Bottini0d0028c2018-10-02 16:41:52 +0100257
SiCong Li3a501662020-06-26 10:02:06 +0100258/** Extended partial vstore that correctly handles scalar values as well.
259 * Store the **lower** 0 to (n-1)th elements of the given vector while minimising the amount of vstore ops
260 * @name VSTORE_PARTIAL
261 *
262 * @note With this macro, the passed data can be both a vector and a scalar
263 * @note @p store_size needs to be <= @p size
264 * eg 1: Valid
265 * VSTORE_PARTIAL(16, 15) ...;
266 * eg 2: Invalid
267 * VSTORE_PARTIAL(4, 7) ...;
268 *
269 * @param[in] size The width of @p DATA. Supported values: 1(scalar), 2, 3, 4, 8, 16
270 * @param[in] store_size The number of lower elements to store. Supported values: 1-16, but has to be <= @p size
271 * @{
272 */
273#define VSTORE_PARTIAL_STR(size, store_size) vstore_partial_##size##_##store_size
274#define VSTORE_PARTIAL(size, store_size) VSTORE_PARTIAL_STR(size, store_size)
275
276// Size == 1 (scalar)
277#define vstore_partial_1_1 vstore1
278// Size == 2
279#define vstore_partial_2_1 vstore_partial_1
280#define vstore_partial_2_2 vstore_partial_2
281// Size == 3
282#define vstore_partial_3_1 vstore_partial_1
283#define vstore_partial_3_2 vstore_partial_2
284#define vstore_partial_3_3 vstore_partial_3
285// Size == 4
286#define vstore_partial_4_1 vstore_partial_1
287#define vstore_partial_4_2 vstore_partial_2
288#define vstore_partial_4_3 vstore_partial_3
289#define vstore_partial_4_4 vstore_partial_4
290// Size == 8
291#define vstore_partial_8_1 vstore_partial_1
292#define vstore_partial_8_2 vstore_partial_2
293#define vstore_partial_8_3 vstore_partial_3
294#define vstore_partial_8_4 vstore_partial_4
295#define vstore_partial_8_5 vstore_partial_5
296#define vstore_partial_8_6 vstore_partial_6
297#define vstore_partial_8_7 vstore_partial_7
298#define vstore_partial_8_8 vstore_partial_8
299// Size == 16
300#define vstore_partial_16_1 vstore_partial_1
301#define vstore_partial_16_2 vstore_partial_2
302#define vstore_partial_16_3 vstore_partial_3
303#define vstore_partial_16_4 vstore_partial_4
304#define vstore_partial_16_5 vstore_partial_5
305#define vstore_partial_16_6 vstore_partial_6
306#define vstore_partial_16_7 vstore_partial_7
307#define vstore_partial_16_8 vstore_partial_8
308#define vstore_partial_16_9 vstore_partial_9
309#define vstore_partial_16_10 vstore_partial_10
310#define vstore_partial_16_11 vstore_partial_11
311#define vstore_partial_16_12 vstore_partial_12
312#define vstore_partial_16_13 vstore_partial_13
313#define vstore_partial_16_14 vstore_partial_14
314#define vstore_partial_16_15 vstore_partial_15
315#define vstore_partial_16_16 vstore_partial_16
316
317/** Partial vstore. Store the **lower** 0 to (n-1)th elements of the given vector while minimising the amount of vstore ops
318 * @name vstore_partial_n
319 *
320 * @note @p DATA needs to be a vector not a scalar
321 * @note n needs to be <= the vector width of the input variable @p DATA
322 * eg 1: Valid
323 * vstore_partial_15(var:float16, 0, 0xabcd);
324 * eg 2: Invalid
325 * vstore_partial_7(var:float4, 0, 0xabcd);
326 *
327 * @note in cases n == 1, 2, 3, 4, 8, 16, no extra vstore is invoked, thus there's no performance penalty.
328 *
329 * @param[in] DATA The name of the variable
330 * @param[in] OFFSET Offset in n
331 * @param[in] PTR The base pointer
332 * @{
333 */
334#define vstore_partial_1(DATA, OFFSET, PTR) \
335 vstore1(DATA.s0, OFFSET, PTR);
336
337#define vstore_partial_2(DATA, OFFSET, PTR) \
338 vstore2(DATA.s01, OFFSET, PTR);
339
340#define vstore_partial_3(DATA, OFFSET, PTR) \
341 vstore3(DATA.s012, OFFSET, PTR);
342
343#define vstore_partial_4(DATA, OFFSET, PTR) \
344 vstore4(DATA.s0123, OFFSET, PTR);
345
346#define vstore_partial_5(DATA, OFFSET, PTR) \
347 vstore_partial_4(DATA.s0123, OFFSET, PTR); \
SiCong Li3b64e3e2020-07-28 09:01:28 +0100348 vstore1(DATA.s4, OFFSET, PTR + 4);
SiCong Li3a501662020-06-26 10:02:06 +0100349
350#define vstore_partial_6(DATA, OFFSET, PTR) \
351 vstore_partial_4(DATA.s0123, OFFSET, PTR); \
352 vstore_partial_2(DATA.s45, OFFSET, PTR + 4);
353
354#define vstore_partial_7(DATA, OFFSET, PTR) \
355 vstore_partial_4(DATA.s0123, OFFSET, PTR); \
356 vstore_partial_3(DATA.s456, OFFSET, PTR + 4);
357
358#define vstore_partial_8(DATA, OFFSET, PTR) \
359 vstore8(DATA.s01234567, OFFSET, PTR);
360
361#define vstore_partial_9(DATA, OFFSET, PTR) \
362 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
SiCong Li3b64e3e2020-07-28 09:01:28 +0100363 vstore1(DATA.s8, OFFSET, PTR + 8);
SiCong Li3a501662020-06-26 10:02:06 +0100364
365#define vstore_partial_10(DATA, OFFSET, PTR) \
366 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
367 vstore_partial_2(DATA.s89, OFFSET, PTR + 8);
368
369#define vstore_partial_11(DATA, OFFSET, PTR) \
370 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
371 vstore_partial_3(DATA.s89a, OFFSET, PTR + 8);
372
373#define vstore_partial_12(DATA, OFFSET, PTR) \
374 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
375 vstore_partial_4(DATA.s89ab, OFFSET, PTR + 8);
376
377#define vstore_partial_13(DATA, OFFSET, PTR) \
378 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
379 vstore_partial_5(DATA.s89abc, OFFSET, PTR + 8);
380
381#define vstore_partial_14(DATA, OFFSET, PTR) \
382 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
383 vstore_partial_6(DATA.s89abcd, OFFSET, PTR + 8);
384
385#define vstore_partial_15(DATA, OFFSET, PTR) \
386 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
387 vstore_partial_7(DATA.s89abcde, OFFSET, PTR + 8);
388
389#define vstore_partial_16(DATA, OFFSET, PTR) \
390 vstore16(DATA, OFFSET, PTR);
391/** @} */ // end of groupd vstore_partial_n
392/** @} */ // end of groupd VSTORE_PARTIAL
393
Gian Marco Iodice0c17aa22019-09-27 09:23:15 +0100394// Convert built-in functions with _sat modifier are not supported in floating point so we create defines
395// without _sat to overcome this issue
396#define convert_float_sat convert_float
397#define convert_float1_sat convert_float
398#define convert_float2_sat convert_float2
399#define convert_float3_sat convert_float3
400#define convert_float4_sat convert_float4
401#define convert_float8_sat convert_float8
402#define convert_float16_sat convert_float16
403#define convert_half_sat convert_float
404#define convert_half1_sat convert_half
405#define convert_half2_sat convert_half2
406#define convert_half3_sat convert_half3
407#define convert_half4_sat convert_half4
408#define convert_half8_sat convert_half8
409#define convert_half16_sat convert_half16
410
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100411#define convert_float1 convert_float
412#define convert_half1 convert_half
413#define convert_char1 convert_char
414#define convert_uchar1 convert_uchar
415#define convert_short1 convert_short
416#define convert_ushort1 convert_ushort
417#define convert_int1 convert_int
418#define convert_uint1 convert_uint
419#define convert_long1 convert_long
420#define convert_ulong1 convert_ulong
421#define convert_double1 convert_double
422
423#define convert_char1_sat convert_char_sat
424#define convert_uchar1_sat convert_uchar_sat
425#define convert_short1_sat convert_short_sat
426#define convert_ushort1_sat convert_ushort_sat
427#define convert_int1_sat convert_int_sat
428#define convert_uint1_sat convert_uint_sat
429#define convert_long1_sat convert_long_sat
430#define convert_ulong1_sat convert_ulong_sat
431#define convert_double1_sat convert_double_sat
432
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100433#define VEC_DATA_TYPE_STR(type, size) type##size
434#define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size)
435
Chunosovd6afedc2017-11-06 22:09:45 +0700436#define CL_VEC_DATA_TYPE_STR(type, size) type##size
437#define CL_VEC_DATA_TYPE(type, size) CL_VEC_DATA_TYPE_STR(type, size)
438
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100439#define CONVERT_STR(x, type) (convert_##type((x)))
440#define CONVERT(x, type) CONVERT_STR(x, type)
441
442#define CONVERT_SAT_STR(x, type) (convert_##type##_sat((x)))
443#define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type)
444
445#define CONVERT_SAT_ROUND_STR(x, type, round) (convert_##type##_sat_##round((x)))
446#define CONVERT_SAT_ROUND(x, type, round) CONVERT_SAT_ROUND_STR(x, type, round)
447
448#define VECTOR_DECLARATION(name) \
449 __global uchar *name##_ptr, \
450 uint name##_stride_x, \
451 uint name##_step_x, \
452 uint name##_offset_first_element_in_bytes
453
454#define IMAGE_DECLARATION(name) \
455 __global uchar *name##_ptr, \
456 uint name##_stride_x, \
457 uint name##_step_x, \
458 uint name##_stride_y, \
459 uint name##_step_y, \
460 uint name##_offset_first_element_in_bytes
461
462#define TENSOR3D_DECLARATION(name) \
463 __global uchar *name##_ptr, \
464 uint name##_stride_x, \
465 uint name##_step_x, \
466 uint name##_stride_y, \
467 uint name##_step_y, \
468 uint name##_stride_z, \
469 uint name##_step_z, \
470 uint name##_offset_first_element_in_bytes
471
steniu01868e5412017-07-17 23:16:00 +0100472#define TENSOR4D_DECLARATION(name) \
473 __global uchar *name##_ptr, \
474 uint name##_stride_x, \
475 uint name##_step_x, \
476 uint name##_stride_y, \
477 uint name##_step_y, \
478 uint name##_stride_z, \
479 uint name##_step_z, \
480 uint name##_stride_w, \
481 uint name##_step_w, \
482 uint name##_offset_first_element_in_bytes
483
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100484#define CONVERT_TO_VECTOR_STRUCT(name) \
485 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x)
486
487#define CONVERT_TO_VECTOR_STRUCT_NO_STEP(name) \
488 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0)
489
490#define CONVERT_TO_IMAGE_STRUCT(name) \
491 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y)
492
493#define CONVERT_TO_IMAGE_STRUCT_NO_STEP(name) \
494 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0)
495
steniu01868e5412017-07-17 23:16:00 +0100496#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
497 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)
498
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100499#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(name) \
500 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)
501
steniu010d523cc2017-07-13 14:24:23 +0100502#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
503 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)
504
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100505#define CONVERT_TO_TENSOR3D_STRUCT(name) \
506 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
507 name##_stride_z, name##_step_z)
508
509#define CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(name) \
510 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0, name##_stride_z, 0)
511
steniu01868e5412017-07-17 23:16:00 +0100512#define CONVERT_TO_TENSOR4D_STRUCT(name, mod_size) \
513 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 +0000514 name##_stride_z, name##_step_z, name##_stride_w, name##_step_w, mod_size)
steniu01868e5412017-07-17 23:16:00 +0100515
516#define CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(name, mod_size) \
517 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)
518
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100519/** Structure to hold Vector information */
520typedef struct Vector
521{
522 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
523 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
524 int stride_x; /**< Stride of the image in X dimension (in bytes) */
525} Vector;
526
527/** Structure to hold Image information */
528typedef struct Image
529{
530 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
531 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
532 int stride_x; /**< Stride of the image in X dimension (in bytes) */
533 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
534} Image;
535
536/** Structure to hold 3D tensor information */
537typedef struct Tensor3D
538{
539 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
540 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
541 int stride_x; /**< Stride of the image in X dimension (in bytes) */
542 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
543 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
544} Tensor3D;
545
steniu01868e5412017-07-17 23:16:00 +0100546/** Structure to hold 4D tensor information */
547typedef struct Tensor4D
548{
549 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
550 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
551 int stride_x; /**< Stride of the image in X dimension (in bytes) */
552 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
553 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
554 int stride_w; /**< Stride of the image in W dimension (in bytes) */
555} Tensor4D;
556
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100557/** Wrap vector information into an Vector structure, and make the pointer point at this workitem's data.
558 *
559 * @param[in] ptr Pointer to the starting postion of the buffer
560 * @param[in] offset_first_element_in_bytes The offset of the first element in the source vector
561 * @param[in] stride_x Stride of the vector in X dimension (in bytes)
562 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
563 *
564 * @return An image object
565 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100566inline 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 +0100567{
568 Vector vector =
569 {
570 .ptr = ptr,
571 .offset_first_element_in_bytes = offset_first_element_in_bytes,
572 .stride_x = stride_x,
573 };
574 vector.ptr += vector.offset_first_element_in_bytes + get_global_id(0) * step_x;
575 return vector;
576}
577
578/** Wrap image information into an Image structure, and make the pointer point at this workitem's data.
579 *
580 * @param[in] ptr Pointer to the starting postion of the buffer
581 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
582 * @param[in] stride_x Stride of the image in X dimension (in bytes)
583 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
584 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
585 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
586 *
587 * @return An image object
588 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100589inline 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 +0100590{
591 Image img =
592 {
593 .ptr = ptr,
594 .offset_first_element_in_bytes = offset_first_element_in_bytes,
595 .stride_x = stride_x,
596 .stride_y = stride_y
597 };
598 img.ptr += img.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y;
599 return img;
600}
601
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100602/** Wrap 3D tensor information into an image structure, and make the pointer point at this workitem's data.
603 *
604 * @param[in] ptr Pointer to the starting postion of the buffer
605 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
606 * @param[in] stride_x Stride of the image in X dimension (in bytes)
607 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
608 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
609 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
610 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
611 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
612 *
613 * @return A 3D tensor object
614 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100615inline 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 +0100616{
617 Image img =
618 {
619 .ptr = ptr,
620 .offset_first_element_in_bytes = offset_first_element_in_bytes,
621 .stride_x = stride_x,
622 .stride_y = stride_y
623 };
624 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;
625 return img;
626}
627
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100628/** Wrap 3D tensor information into an tensor structure, and make the pointer point at this workitem's data.
629 *
630 * @param[in] ptr Pointer to the starting postion of the buffer
631 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
632 * @param[in] stride_x Stride of the image in X dimension (in bytes)
633 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
634 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
635 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
636 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
637 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
638 *
639 * @return A 3D tensor object
640 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100641inline 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 +0100642{
643 Tensor3D tensor =
644 {
645 .ptr = ptr,
646 .offset_first_element_in_bytes = offset_first_element_in_bytes,
647 .stride_x = stride_x,
648 .stride_y = stride_y,
649 .stride_z = stride_z
650 };
651 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;
652 return tensor;
653}
654
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100655inline 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 +0100656 uint step_w,
657 uint mod_size)
658{
659 Tensor4D 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 .stride_w = stride_w
667 };
668
669 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;
670 return tensor;
671}
672
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100673/** Get the pointer position of a Vector
674 *
675 * @param[in] vec Pointer to the starting position of the buffer
676 * @param[in] x Relative X position
677 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100678inline __global const uchar *vector_offset(const Vector *vec, int x)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100679{
680 return vec->ptr + x * vec->stride_x;
681}
682
683/** Get the pointer position of a Image
684 *
685 * @param[in] img Pointer to the starting position of the buffer
686 * @param[in] x Relative X position
687 * @param[in] y Relative Y position
688 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100689inline __global uchar *offset(const Image *img, int x, int y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100690{
691 return img->ptr + x * img->stride_x + y * img->stride_y;
692}
693
694/** Get the pointer position of a Tensor3D
695 *
Gian Marco Iodice3a623242017-07-25 10:25:53 +0100696 * @param[in] tensor Pointer to the starting position of the buffer
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100697 * @param[in] x Relative X position
698 * @param[in] y Relative Y position
699 * @param[in] z Relative Z position
700 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100701inline __global const uchar *tensor3D_offset(const Tensor3D *tensor, int x, int y, int z)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100702{
703 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z;
704}
705
steniu01868e5412017-07-17 23:16:00 +0100706/** Get the pointer position of a Tensor4D
707 *
708 * @param[in] tensor Pointer to the starting position of the buffer
709 * @param[in] x Relative X position
710 * @param[in] y Relative Y position
711 * @param[in] z Relative Z position
712 * @param[in] w Relative W position
713 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +0100714inline __global const uchar *tensor4D_offset(const Tensor4D *tensor, int x, int y, int z, int w)
steniu01868e5412017-07-17 23:16:00 +0100715{
716 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z + w * tensor->stride_w;
717}
718
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100719#endif // _HELPER_H