blob: 6e05a513ec8ede9bea8a8f32c6e704e2eb10db9f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Viet-Hoa Do6829e022024-01-16 16:23:24 +00002 * Copyright (c) 2016-2024 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 */
Viet-Hoa Do6829e022024-01-16 16:23:24 +000024#ifndef ACL_SRC_CORE_CL_CL_KERNELS_HELPERS_H
25#define ACL_SRC_CORE_CL_CL_KERNELS_HELPERS_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
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
SiCong Lia3cf2412022-07-01 15:01:10 +010047#define GPU_ARCH_VALHALL 0x300
Usama Arife2428a02019-05-09 11:03:17 +010048
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000049/** Concatenate two inputs.
50 *
51 * @param[in] a The first input to be concatenated
52 * @param[in] b The second input to be concatenated
53 *
54 * @return The concatenated output
55 */
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010056#define CONCAT(a, b) a##b
57
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000058/** Expand the given vector
59 *
60 * @param[in] x The vector to be expanded
61 *
62 * @return The expanded output
63 */
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010064#define EXPAND(x) x
65
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000066/** Clamp the given value between an upper and lower bound.
67 *
68 * @param[in] x The value to be clamped
69 * @param[in] min_val The lower bound
70 * @param[in] max_val The upper bound
71 *
72 * @return The clamped value.
73 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074#define CLAMP(x, min_val, max_val) min(max(x, min_val), max_val)
75
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000076/** REVn reverses the given vector whose size is n.
77 * @name REVn
78 *
79 * @param[in] x The vector to be reversed
80 *
81 * @return The reversed vector
82 * @{
83 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010084#define REV1(x) ((x))
85#define REV2(x) ((x).s10)
86#define REV3(x) ((x).s210)
87#define REV4(x) ((x).s3210)
88#define REV8(x) ((x).s76543210)
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010089#define REV16(x) ((x).sFEDCBA9876543210)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000090/** @} */ // end of group REVn
Giorgio Arena5c4a8e92019-08-28 17:55:07 +010091
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +000092/** Reverse the given vector.
93 * @name REVERSE
94 *
95 * @param[in] x The vector to be reversed
96 * @param[in] s The size of the vector
97 *
98 * @return The reversed vector
99 * @{
100 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100101#define REVERSE_STR(x, s) REV##s((x))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102#define REVERSE(x, s) REVERSE_STR(x, s)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000103/** @} */ // end of group REVERSE
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100104
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000105/** Circular-right-shift (rotate-right) the vector of size s by the amount of n.
106 * @name ROTs_n
107 *
108 * @param[in] x The vector to be shifted
109 *
110 * @return The shifted vector
111 * @{
112 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100113#define ROT1_0(x) ((x))
Giorgio Arena93240222020-12-23 11:55:29 +0000114#define ROT1_1(x) ((x))
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100115
116#define ROT2_0(x) ((x))
117#define ROT2_1(x) ((x).s10)
Giorgio Arena93240222020-12-23 11:55:29 +0000118#define ROT2_2(x) ((x))
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100119
120#define ROT3_0(x) ((x))
121#define ROT3_1(x) ((x).s201)
122#define ROT3_2(x) ((x).s120)
Giorgio Arena93240222020-12-23 11:55:29 +0000123#define ROT3_3(x) ((x))
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100124
125#define ROT4_0(x) ((x))
126#define ROT4_1(x) ((x).s3012)
127#define ROT4_2(x) ((x).s2301)
128#define ROT4_3(x) ((x).s1230)
Giorgio Arena93240222020-12-23 11:55:29 +0000129#define ROT4_4(x) ((x))
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100130
131#define ROT8_0(x) ((x))
132#define ROT8_1(x) ((x).s70123456)
133#define ROT8_2(x) ((x).s67012345)
134#define ROT8_3(x) ((x).s56701234)
135#define ROT8_4(x) ((x).s45670123)
136#define ROT8_5(x) ((x).s34567012)
137#define ROT8_6(x) ((x).s23456701)
138#define ROT8_7(x) ((x).s12345670)
Giorgio Arena93240222020-12-23 11:55:29 +0000139#define ROT8_8(x) ((x))
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100140
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141#define ROT16_0(x) ((x))
142#define ROT16_1(x) ((x).sF0123456789ABCDE)
143#define ROT16_2(x) ((x).sEF0123456789ABCD)
144#define ROT16_3(x) ((x).sDEF0123456789ABC)
145#define ROT16_4(x) ((x).sCDEF0123456789AB)
146#define ROT16_5(x) ((x).sBCDEF0123456789A)
147#define ROT16_6(x) ((x).sABCDEF0123456789)
148#define ROT16_7(x) ((x).s9ABCDEF012345678)
149#define ROT16_8(x) ((x).s89ABCDEF01234567)
150#define ROT16_9(x) ((x).s789ABCDEF0123456)
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100151#define ROT16_10(x) ((x).s6789ABCDEF012345)
152#define ROT16_11(x) ((x).s56789ABCDEF01234)
153#define ROT16_12(x) ((x).s456789ABCDEF0123)
154#define ROT16_13(x) ((x).s3456789ABCDEF012)
155#define ROT16_14(x) ((x).s23456789ABCDEF01)
156#define ROT16_15(x) ((x).s123456789ABCDEF0)
Giorgio Arena93240222020-12-23 11:55:29 +0000157#define ROT16_16(x) ((x))
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000158/** @} */ // end of group ROTs_n
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100159
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000160/** Circular-right-shift (rotate-right) the given vector by the given amount.
161 * @name ROTATE
162 *
163 * @param[in] x The vector to be shifted
164 * @param[in] s The size of the vector
165 * @param[in] n The amount to be shifted
166 *
167 * @return The shifted vector
168 * @{
169 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100170#define ROTATE_STR(x, s, n) ROT##s##_##n(x)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100171#define ROTATE(x, s, n) ROTATE_STR(x, s, n)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000172/** @} */ // end of group ROTATE
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100173
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000174/** Creates a vector of size n filled with offset values corresponding to the location of each element.
175 * @name V_OFFSn
176 *
177 * @param[in] dt The data type of the output vector
178 *
179 * @return The vector filled with offset values
180 * @{
181 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100182#define V_OFFS1(dt) (dt##1)(0)
183#define V_OFFS2(dt) (dt##2)(0, 1)
184#define V_OFFS3(dt) (dt##3)(0, 1, 2)
185#define V_OFFS4(dt) (dt##4)(0, 1, 2, 3)
186#define V_OFFS8(dt) (dt##8)(0, 1, 2, 3, 4, 5, 6, 7)
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000187#define V_OFFS16(dt) (dt##16)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000188/** @} */ // end of group V_OFFSn
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100189
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000190/** Create a vector filled with offset values corresponding to the location of each element.
191 * @name VEC_OFFS
192 *
193 * @param[in] dt The data type of the output vector
194 * @param[in] s The size of the output vector
195 *
196 * @return The vector filled with offset values
197 * @{
198 */
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100199#define VEC_OFFS_STR(dt, s) V_OFFS##s(dt)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100200#define VEC_OFFS(dt, s) VEC_OFFS_STR(dt, s)
Sang-Hoon Parkbfd75d62019-10-30 14:56:17 +0000201/** @} */ // end of group VEC_OFFS
Giorgio Arena5c4a8e92019-08-28 17:55:07 +0100202
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100203#define VLOAD_STR(size) vload##size
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100204#define VLOAD(size) VLOAD_STR(size)
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100205
Giorgio Arenabde2f352021-09-07 14:15:28 +0100206/** Extended partial vload that correctly handles scalar values as well.
207 * Load the **lower** 0 to (n-1)th elements of the given vector while minimising the amount of load ops
208 * @name VLOAD_PARTIAL
209 *
210 * @note With this macro, the passed data can be both a vector and a scalar
211 * @note @p load_size needs to be <= @p size
212 * eg 1: Valid
213 * VLOAD_PARTIAL(16, 15) ...;
214 * eg 2: Invalid
215 * VLOAD_PARTIAL(4, 7) ...;
216 *
217 * @param[in] size The width of @p DATA. Supported values: 1(scalar), 2, 3, 4, 8, 16
218 * @param[in] load_size The number of lower elements to load. Supported values: 1-16, but has to be <= @p size
219 * @{
220 */
221#define VLOAD_PARTIAL_STR(size, load_size) vload_partial_##size##_##load_size
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100222#define VLOAD_PARTIAL(size, load_size) VLOAD_PARTIAL_STR(size, load_size)
Giorgio Arenabde2f352021-09-07 14:15:28 +0100223
224#define NO_LOAD(data, offs, ptr) \
225 { \
226 }
227
228// Size == 1 (scalar)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100229#define vload_partial_1_0 NO_LOAD
230#define vload_partial_1_1 vload1
231#define vload_partial_1_2 NO_LOAD
232#define vload_partial_1_3 NO_LOAD
233#define vload_partial_1_4 NO_LOAD
234#define vload_partial_1_5 NO_LOAD
235#define vload_partial_1_6 NO_LOAD
236#define vload_partial_1_7 NO_LOAD
237#define vload_partial_1_8 NO_LOAD
238#define vload_partial_1_9 NO_LOAD
Giorgio Arenabde2f352021-09-07 14:15:28 +0100239#define vload_partial_1_10 NO_LOAD
240#define vload_partial_1_11 NO_LOAD
241#define vload_partial_1_12 NO_LOAD
242#define vload_partial_1_13 NO_LOAD
243#define vload_partial_1_14 NO_LOAD
244#define vload_partial_1_15 NO_LOAD
245#define vload_partial_1_16 NO_LOAD
246// Size == 2
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100247#define vload_partial_2_0 NO_LOAD
248#define vload_partial_2_1 vload_partial_1
249#define vload_partial_2_2 vload_partial_2
250#define vload_partial_2_3 NO_LOAD
251#define vload_partial_2_4 NO_LOAD
252#define vload_partial_2_5 NO_LOAD
253#define vload_partial_2_6 NO_LOAD
254#define vload_partial_2_7 NO_LOAD
255#define vload_partial_2_8 NO_LOAD
256#define vload_partial_2_9 NO_LOAD
Giorgio Arenabde2f352021-09-07 14:15:28 +0100257#define vload_partial_2_10 NO_LOAD
258#define vload_partial_2_11 NO_LOAD
259#define vload_partial_2_12 NO_LOAD
260#define vload_partial_2_13 NO_LOAD
261#define vload_partial_2_14 NO_LOAD
262#define vload_partial_2_15 NO_LOAD
263#define vload_partial_2_16 NO_LOAD
264// Size == 3
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100265#define vload_partial_3_0 NO_LOAD
266#define vload_partial_3_1 vload_partial_1
267#define vload_partial_3_2 vload_partial_2
268#define vload_partial_3_3 vload_partial_3
269#define vload_partial_3_4 NO_LOAD
270#define vload_partial_3_5 NO_LOAD
271#define vload_partial_3_6 NO_LOAD
272#define vload_partial_3_7 NO_LOAD
273#define vload_partial_3_8 NO_LOAD
274#define vload_partial_3_9 NO_LOAD
Giorgio Arenabde2f352021-09-07 14:15:28 +0100275#define vload_partial_3_10 NO_LOAD
276#define vload_partial_3_11 NO_LOAD
277#define vload_partial_3_12 NO_LOAD
278#define vload_partial_3_13 NO_LOAD
279#define vload_partial_3_14 NO_LOAD
280#define vload_partial_3_15 NO_LOAD
281#define vload_partial_3_16 NO_LOAD
282// Size == 4
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100283#define vload_partial_4_0 NO_LOAD
284#define vload_partial_4_1 vload_partial_1
285#define vload_partial_4_2 vload_partial_2
286#define vload_partial_4_3 vload_partial_3
287#define vload_partial_4_4 vload_partial_4
288#define vload_partial_4_5 NO_LOAD
289#define vload_partial_4_6 NO_LOAD
290#define vload_partial_4_7 NO_LOAD
291#define vload_partial_4_8 NO_LOAD
292#define vload_partial_4_9 NO_LOAD
Giorgio Arenabde2f352021-09-07 14:15:28 +0100293#define vload_partial_4_10 NO_LOAD
294#define vload_partial_4_11 NO_LOAD
295#define vload_partial_4_12 NO_LOAD
296#define vload_partial_4_13 NO_LOAD
297#define vload_partial_4_14 NO_LOAD
298#define vload_partial_4_15 NO_LOAD
299#define vload_partial_4_16 NO_LOAD
300// Size == 8
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100301#define vload_partial_8_0 NO_LOAD
302#define vload_partial_8_1 vload_partial_1
303#define vload_partial_8_2 vload_partial_2
304#define vload_partial_8_3 vload_partial_3
305#define vload_partial_8_4 vload_partial_4
306#define vload_partial_8_5 vload_partial_5
307#define vload_partial_8_6 vload_partial_6
308#define vload_partial_8_7 vload_partial_7
309#define vload_partial_8_8 vload_partial_8
310#define vload_partial_8_9 NO_LOAD
Giorgio Arenabde2f352021-09-07 14:15:28 +0100311#define vload_partial_8_10 NO_LOAD
312#define vload_partial_8_11 NO_LOAD
313#define vload_partial_8_12 NO_LOAD
314#define vload_partial_8_13 NO_LOAD
315#define vload_partial_8_14 NO_LOAD
316#define vload_partial_8_15 NO_LOAD
317#define vload_partial_8_16 NO_LOAD
318// Size == 16
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100319#define vload_partial_16_0 NO_LOAD
320#define vload_partial_16_1 vload_partial_1
321#define vload_partial_16_2 vload_partial_2
322#define vload_partial_16_3 vload_partial_3
323#define vload_partial_16_4 vload_partial_4
324#define vload_partial_16_5 vload_partial_5
325#define vload_partial_16_6 vload_partial_6
326#define vload_partial_16_7 vload_partial_7
327#define vload_partial_16_8 vload_partial_8
328#define vload_partial_16_9 vload_partial_9
Giorgio Arenabde2f352021-09-07 14:15:28 +0100329#define vload_partial_16_10 vload_partial_10
330#define vload_partial_16_11 vload_partial_11
331#define vload_partial_16_12 vload_partial_12
332#define vload_partial_16_13 vload_partial_13
333#define vload_partial_16_14 vload_partial_14
334#define vload_partial_16_15 vload_partial_15
335#define vload_partial_16_16 vload_partial_16
336
337/** Partial vload. Load the **lower** 0 to (n-1)th elements of the given vector while minimising the amount of vload ops
338 * @name vload_partial_n
339 *
340 * @note @p DATA needs to be a vector not a scalar
341 * @note n needs to be <= the vector width of the input variable @p DATA
342 * eg 1: Valid
343 * vload_partial_15(var:float16, 0, 0xabcd);
344 * eg 2: Invalid
345 * vload_partial_7(var:float4, 0, 0xabcd);
346 *
347 * @note in cases n == 1, 2, 3, 4, 8, 16, no extra vload is invoked, thus there's no performance penalty.
348 *
349 * @param[in] DATA The name of the variable where to load the values
350 * @param[in] OFFSET Offset in n
351 * @param[in] PTR The base pointer
352 * @{
353 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100354#define vload_partial_1(DATA, OFFSET, PTR) DATA.s0 = vload1(OFFSET, PTR);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100355
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100356#define vload_partial_2(DATA, OFFSET, PTR) DATA.s01 = vload2(OFFSET, PTR);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100357
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100358#define vload_partial_3(DATA, OFFSET, PTR) DATA.s012 = vload3(OFFSET, PTR);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100359
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100360#define vload_partial_4(DATA, OFFSET, PTR) DATA.s0123 = vload4(OFFSET, PTR);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100361
Giorgio Arena88bd35d2021-09-08 12:26:27 +0100362#define vload_partial_5(DATA, OFFSET, PTR) \
363 vload_partial_4(DATA.s0123, OFFSET, PTR); \
364 DATA.s4 = vload1(OFFSET, PTR + 4);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100365
Giorgio Arena88bd35d2021-09-08 12:26:27 +0100366#define vload_partial_6(DATA, OFFSET, PTR) \
367 vload_partial_4(DATA.s0123, OFFSET, PTR); \
368 vload_partial_2(DATA.s45, OFFSET, PTR + 4);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100369
Giorgio Arena88bd35d2021-09-08 12:26:27 +0100370#define vload_partial_7(DATA, OFFSET, PTR) \
371 vload_partial_4(DATA.s0123, OFFSET, PTR); \
372 vload_partial_3(DATA.s456, OFFSET, PTR + 4);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100373
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100374#define vload_partial_8(DATA, OFFSET, PTR) DATA.s01234567 = vload8(OFFSET, PTR);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100375
Giorgio Arena88bd35d2021-09-08 12:26:27 +0100376#define vload_partial_9(DATA, OFFSET, PTR) \
377 vload_partial_8(DATA.s01234567, OFFSET, PTR); \
378 DATA.s8 = vload1(OFFSET, PTR + 8);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100379
Giorgio Arena88bd35d2021-09-08 12:26:27 +0100380#define vload_partial_10(DATA, OFFSET, PTR) \
381 vload_partial_8(DATA.s01234567, OFFSET, PTR); \
382 vload_partial_2(DATA.s89, OFFSET, PTR + 8);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100383
Giorgio Arena88bd35d2021-09-08 12:26:27 +0100384#define vload_partial_11(DATA, OFFSET, PTR) \
385 vload_partial_8(DATA.s01234567, OFFSET, PTR); \
386 vload_partial_3(DATA.s89A, OFFSET, PTR + 8);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100387
Giorgio Arena88bd35d2021-09-08 12:26:27 +0100388#define vload_partial_12(DATA, OFFSET, PTR) \
389 vload_partial_8(DATA.s01234567, OFFSET, PTR); \
390 vload_partial_4(DATA.s89AB, OFFSET, PTR + 8);
Adnan AlSinan3e155a52021-12-10 12:34:02 +0000391// For vload_partial_{13,14,15}, an 8-vector size has been passed, because vectors size of size 5,6,7 are not supported
Giorgio Arena88bd35d2021-09-08 12:26:27 +0100392#define vload_partial_13(DATA, OFFSET, PTR) \
393 vload_partial_8(DATA.s01234567, OFFSET, PTR); \
Adnan AlSinan3e155a52021-12-10 12:34:02 +0000394 vload_partial_5(DATA.s89ABCDEF, OFFSET, PTR + 8);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100395
Giorgio Arena88bd35d2021-09-08 12:26:27 +0100396#define vload_partial_14(DATA, OFFSET, PTR) \
397 vload_partial_8(DATA.s01234567, OFFSET, PTR); \
Adnan AlSinan3e155a52021-12-10 12:34:02 +0000398 vload_partial_6(DATA.s89ABCDEF, OFFSET, PTR + 8);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100399
Giorgio Arena88bd35d2021-09-08 12:26:27 +0100400#define vload_partial_15(DATA, OFFSET, PTR) \
401 vload_partial_8(DATA.s01234567, OFFSET, PTR); \
Adnan AlSinan3e155a52021-12-10 12:34:02 +0000402 vload_partial_7(DATA.s89ABCDEF, OFFSET, PTR + 8);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100403
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100404#define vload_partial_16(DATA, OFFSET, PTR) DATA = vload16(OFFSET, PTR);
Giorgio Arenabde2f352021-09-07 14:15:28 +0100405/** @} */ // end of groupd vload_partial_n
406/** @} */ // end of groupd VLOAD_PARTIAL
407
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100408#define PIXEL_UNIT4 1
409#define PIXEL_UNIT8 2
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100410#define PIXEL_UNIT16 4
411
412/** Utility macro to convert a vector size in pixel unit.
413 *
414 * @name CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT
415 *
416 * @param[in] vec_size Vector size. Only 4,8 and 16 is supported
417 *
418 * @return The pixel unit (number of pixels)
419 * @{
420 */
421#define CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT_STR(vec_size) PIXEL_UNIT##vec_size
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100422#define CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT(vec_size) CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT_STR(vec_size)
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100423/** @} */ // end of group CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT
424
425#define read_image2d_floatx1(img, x_coord, y_coord) (float4)(read_imagef(img, (int2)(x_coord, y_coord)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100426#define read_image2d_floatx2(img, x_coord, y_coord) \
427 (float8)(read_imagef(img, (int2)(x_coord, y_coord)), read_imagef(img, (int2)(x_coord + 1, y_coord)));
428#define read_image2d_floatx4(img, x_coord, y_coord) \
429 (float16)(read_imagef(img, (int2)(x_coord, y_coord)), read_imagef(img, (int2)(x_coord + 1, y_coord)), \
430 read_imagef(img, (int2)(x_coord + 2, y_coord)), read_imagef(img, (int2)(x_coord + 3, y_coord)));
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100431
432#if defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
433#define read_image2d_halfx1(img, x_coord, y_coord) (half4)(read_imageh(img, (int2)(x_coord, y_coord)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100434#define read_image2d_halfx2(img, x_coord, y_coord) \
435 (half8)(read_imageh(img, (int2)(x_coord, y_coord)), read_imageh(img, (int2)(x_coord + 1, y_coord)));
436#define read_image2d_halfx4(img, x_coord, y_coord) \
437 (half16)(read_imageh(img, (int2)(x_coord, y_coord)), read_imageh(img, (int2)(x_coord + 1, y_coord)), \
438 read_imageh(img, (int2)(x_coord + 2, y_coord)), read_imageh(img, (int2)(x_coord + 3, y_coord)));
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100439#endif // defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
440
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000441#define write_image2d_floatx1(img, x_coord, y_coord, values) (write_imagef(img, (int2)(x_coord, y_coord), values));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100442#define write_image2d_floatx2(img, x_coord, y_coord, values) \
443 (write_imagef(img, (int2)(x_coord, y_coord), values.s0123), \
444 write_imagef(img, (int2)(x_coord + 1, y_coord), values.s4567));
445#define write_image2d_floatx4(img, x_coord, y_coord, values) \
446 (write_imagef(img, (int2)(x_coord, y_coord), values.s0123), \
447 write_imagef(img, (int2)(x_coord + 1, y_coord), values.s4567), \
448 write_imagef(img, (int2)(x_coord + 2, y_coord), values.s89AB), \
449 write_imagef(img, (int2)(x_coord + 3, y_coord), values.sCDEF));
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000450
451#if defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
452#define write_image2d_halfx1(img, x_coord, y_coord, values) (write_imageh(img, (int2)(x_coord, y_coord), values));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100453#define write_image2d_halfx2(img, x_coord, y_coord, values) \
454 (write_imageh(img, (int2)(x_coord, y_coord), values.s0123), \
455 write_imageh(img, (int2)(x_coord + 1, y_coord), values.s4567));
456#define write_image2d_halfx4(img, x_coord, y_coord, values) \
457 (write_imageh(img, (int2)(x_coord, y_coord), values.s0123), \
458 write_imageh(img, (int2)(x_coord + 1, y_coord), values.s4567), \
459 write_imageh(img, (int2)(x_coord + 2, y_coord), values.s89AB), \
460 write_imageh(img, (int2)(x_coord + 3, y_coord), values.sCDEF));
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000461#endif // defined(ARM_COMPUTE_OPENCL_FP16_ENABLED) && defined(cl_khr_fp16)
462
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100463/** Utility macro to read a 2D OpenCL image object.
464 *
465 * @note Coordinates are not normalized
466 *
467 * @param[in] data_type Data type
468 * @param[in] n0 Number of pixel to read. Only 1,2 and 4 is supported
469 * @param[in] img OpenCL image object
470 * @param[in] x_coord The x coordinate for the top-left pixel
471 * @param[in] y_coord The y coordinate for the top-left pixel
472 *
473 * @return Pixels from the 2D OpenCL image object
474 * @{
475 */
476#define READ_IMAGE2D_STR(data_type, n0, img, x_coord, y_coord) read_image2d_##data_type##x##n0(img, x_coord, y_coord)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100477#define READ_IMAGE2D(data_type, n0, img, x_coord, y_coord) READ_IMAGE2D_STR(data_type, n0, img, x_coord, y_coord)
ramy.elgammal@arm.coma2561f02023-06-16 20:45:48 +0100478/** @} */
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100479
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000480/** Utility macro to write a 2D OpenCL image object.
481 *
482 * @note Coordinates are not normalized
483 *
484 * @param[in] data_type Data type
485 * @param[in] n0 Number of pixel to write. Only 1,2 and 4 is supported
486 * @param[in] img OpenCL image object
487 * @param[in] x_coord The x coordinate for the top-left pixel
488 * @param[in] y_coord The y coordinate for the top-left pixel
489 * @param[in] values Values to write
490 *
491 * @{
492 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100493#define WRITE_IMAGE2D_STR(data_type, n0, img, x_coord, y_coord, values) \
494 write_image2d_##data_type##x##n0(img, x_coord, y_coord, values)
495#define WRITE_IMAGE2D(data_type, n0, img, x_coord, y_coord, values) \
496 WRITE_IMAGE2D_STR(data_type, n0, img, x_coord, y_coord, values)
ramy.elgammal@arm.coma2561f02023-06-16 20:45:48 +0100497/** @} */
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000498
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100499#define VSTORE_STR(size) vstore##size
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100500#define VSTORE(size) VSTORE_STR(size)
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100501
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100502#define float1 float
503#define half1 half
504#define char1 char
505#define uchar1 uchar
506#define short1 short
Usama Arif0681e3b2019-04-25 14:28:07 +0100507#define ushort1 ushort
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100508#define int1 int
509#define uint1 uint
510#define long1 long
511#define ulong1 ulong
Usama Arif0681e3b2019-04-25 14:28:07 +0100512#define double1 double
513
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100514#define vload1(OFFSET, PTR) *(OFFSET + PTR)
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100515#define vstore1(DATA, OFFSET, PTR) *(OFFSET + PTR) = DATA
Manuel Bottini0d0028c2018-10-02 16:41:52 +0100516
SiCong Li3a501662020-06-26 10:02:06 +0100517/** Extended partial vstore that correctly handles scalar values as well.
518 * Store the **lower** 0 to (n-1)th elements of the given vector while minimising the amount of vstore ops
519 * @name VSTORE_PARTIAL
520 *
521 * @note With this macro, the passed data can be both a vector and a scalar
522 * @note @p store_size needs to be <= @p size
523 * eg 1: Valid
524 * VSTORE_PARTIAL(16, 15) ...;
525 * eg 2: Invalid
526 * VSTORE_PARTIAL(4, 7) ...;
527 *
528 * @param[in] size The width of @p DATA. Supported values: 1(scalar), 2, 3, 4, 8, 16
529 * @param[in] store_size The number of lower elements to store. Supported values: 1-16, but has to be <= @p size
530 * @{
531 */
532#define VSTORE_PARTIAL_STR(size, store_size) vstore_partial_##size##_##store_size
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100533#define VSTORE_PARTIAL(size, store_size) VSTORE_PARTIAL_STR(size, store_size)
SiCong Li3a501662020-06-26 10:02:06 +0100534
Giorgio Arenad304adb2020-10-02 10:20:11 +0100535#define NO_STORE(data, offs, ptr) \
536 { \
537 }
538
SiCong Li3a501662020-06-26 10:02:06 +0100539// Size == 1 (scalar)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100540#define vstore_partial_1_0 NO_STORE
541#define vstore_partial_1_1 vstore1
542#define vstore_partial_1_2 NO_STORE
543#define vstore_partial_1_3 NO_STORE
544#define vstore_partial_1_4 NO_STORE
545#define vstore_partial_1_5 NO_STORE
546#define vstore_partial_1_6 NO_STORE
547#define vstore_partial_1_7 NO_STORE
548#define vstore_partial_1_8 NO_STORE
549#define vstore_partial_1_9 NO_STORE
SiCong Li0ea50e32020-11-05 09:18:11 +0000550#define vstore_partial_1_10 NO_STORE
551#define vstore_partial_1_11 NO_STORE
552#define vstore_partial_1_12 NO_STORE
553#define vstore_partial_1_13 NO_STORE
554#define vstore_partial_1_14 NO_STORE
555#define vstore_partial_1_15 NO_STORE
556#define vstore_partial_1_16 NO_STORE
SiCong Li3a501662020-06-26 10:02:06 +0100557// Size == 2
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100558#define vstore_partial_2_0 NO_STORE
559#define vstore_partial_2_1 vstore_partial_1
560#define vstore_partial_2_2 vstore_partial_2
561#define vstore_partial_2_3 NO_STORE
562#define vstore_partial_2_4 NO_STORE
563#define vstore_partial_2_5 NO_STORE
564#define vstore_partial_2_6 NO_STORE
565#define vstore_partial_2_7 NO_STORE
566#define vstore_partial_2_8 NO_STORE
567#define vstore_partial_2_9 NO_STORE
SiCong Li0ea50e32020-11-05 09:18:11 +0000568#define vstore_partial_2_10 NO_STORE
569#define vstore_partial_2_11 NO_STORE
570#define vstore_partial_2_12 NO_STORE
571#define vstore_partial_2_13 NO_STORE
572#define vstore_partial_2_14 NO_STORE
573#define vstore_partial_2_15 NO_STORE
574#define vstore_partial_2_16 NO_STORE
SiCong Li3a501662020-06-26 10:02:06 +0100575// Size == 3
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100576#define vstore_partial_3_0 NO_STORE
577#define vstore_partial_3_1 vstore_partial_1
578#define vstore_partial_3_2 vstore_partial_2
579#define vstore_partial_3_3 vstore_partial_3
580#define vstore_partial_3_4 NO_STORE
581#define vstore_partial_3_5 NO_STORE
582#define vstore_partial_3_6 NO_STORE
583#define vstore_partial_3_7 NO_STORE
584#define vstore_partial_3_8 NO_STORE
585#define vstore_partial_3_9 NO_STORE
SiCong Li0ea50e32020-11-05 09:18:11 +0000586#define vstore_partial_3_10 NO_STORE
587#define vstore_partial_3_11 NO_STORE
588#define vstore_partial_3_12 NO_STORE
589#define vstore_partial_3_13 NO_STORE
590#define vstore_partial_3_14 NO_STORE
591#define vstore_partial_3_15 NO_STORE
592#define vstore_partial_3_16 NO_STORE
SiCong Li3a501662020-06-26 10:02:06 +0100593// Size == 4
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100594#define vstore_partial_4_0 NO_STORE
595#define vstore_partial_4_1 vstore_partial_1
596#define vstore_partial_4_2 vstore_partial_2
597#define vstore_partial_4_3 vstore_partial_3
598#define vstore_partial_4_4 vstore_partial_4
599#define vstore_partial_4_5 NO_STORE
600#define vstore_partial_4_6 NO_STORE
601#define vstore_partial_4_7 NO_STORE
602#define vstore_partial_4_8 NO_STORE
603#define vstore_partial_4_9 NO_STORE
SiCong Li0ea50e32020-11-05 09:18:11 +0000604#define vstore_partial_4_10 NO_STORE
605#define vstore_partial_4_11 NO_STORE
606#define vstore_partial_4_12 NO_STORE
607#define vstore_partial_4_13 NO_STORE
608#define vstore_partial_4_14 NO_STORE
609#define vstore_partial_4_15 NO_STORE
610#define vstore_partial_4_16 NO_STORE
SiCong Li3a501662020-06-26 10:02:06 +0100611// Size == 8
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100612#define vstore_partial_8_0 NO_STORE
613#define vstore_partial_8_1 vstore_partial_1
614#define vstore_partial_8_2 vstore_partial_2
615#define vstore_partial_8_3 vstore_partial_3
616#define vstore_partial_8_4 vstore_partial_4
617#define vstore_partial_8_5 vstore_partial_5
618#define vstore_partial_8_6 vstore_partial_6
619#define vstore_partial_8_7 vstore_partial_7
620#define vstore_partial_8_8 vstore_partial_8
621#define vstore_partial_8_9 NO_STORE
SiCong Li0ea50e32020-11-05 09:18:11 +0000622#define vstore_partial_8_10 NO_STORE
623#define vstore_partial_8_11 NO_STORE
624#define vstore_partial_8_12 NO_STORE
625#define vstore_partial_8_13 NO_STORE
626#define vstore_partial_8_14 NO_STORE
627#define vstore_partial_8_15 NO_STORE
628#define vstore_partial_8_16 NO_STORE
SiCong Li3a501662020-06-26 10:02:06 +0100629// Size == 16
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100630#define vstore_partial_16_0 NO_STORE
631#define vstore_partial_16_1 vstore_partial_1
632#define vstore_partial_16_2 vstore_partial_2
633#define vstore_partial_16_3 vstore_partial_3
634#define vstore_partial_16_4 vstore_partial_4
635#define vstore_partial_16_5 vstore_partial_5
636#define vstore_partial_16_6 vstore_partial_6
637#define vstore_partial_16_7 vstore_partial_7
638#define vstore_partial_16_8 vstore_partial_8
639#define vstore_partial_16_9 vstore_partial_9
SiCong Li3a501662020-06-26 10:02:06 +0100640#define vstore_partial_16_10 vstore_partial_10
641#define vstore_partial_16_11 vstore_partial_11
642#define vstore_partial_16_12 vstore_partial_12
643#define vstore_partial_16_13 vstore_partial_13
644#define vstore_partial_16_14 vstore_partial_14
645#define vstore_partial_16_15 vstore_partial_15
646#define vstore_partial_16_16 vstore_partial_16
647
648/** Partial vstore. Store the **lower** 0 to (n-1)th elements of the given vector while minimising the amount of vstore ops
649 * @name vstore_partial_n
650 *
651 * @note @p DATA needs to be a vector not a scalar
652 * @note n needs to be <= the vector width of the input variable @p DATA
653 * eg 1: Valid
654 * vstore_partial_15(var:float16, 0, 0xabcd);
655 * eg 2: Invalid
656 * vstore_partial_7(var:float4, 0, 0xabcd);
657 *
658 * @note in cases n == 1, 2, 3, 4, 8, 16, no extra vstore is invoked, thus there's no performance penalty.
659 *
660 * @param[in] DATA The name of the variable
661 * @param[in] OFFSET Offset in n
662 * @param[in] PTR The base pointer
663 * @{
664 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100665#define vstore_partial_1(DATA, OFFSET, PTR) vstore1(DATA.s0, OFFSET, PTR);
SiCong Li3a501662020-06-26 10:02:06 +0100666
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100667#define vstore_partial_2(DATA, OFFSET, PTR) vstore2(DATA.s01, OFFSET, PTR);
SiCong Li3a501662020-06-26 10:02:06 +0100668
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100669#define vstore_partial_3(DATA, OFFSET, PTR) vstore3(DATA.s012, OFFSET, PTR);
SiCong Li3a501662020-06-26 10:02:06 +0100670
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100671#define vstore_partial_4(DATA, OFFSET, PTR) vstore4(DATA.s0123, OFFSET, PTR);
SiCong Li3a501662020-06-26 10:02:06 +0100672
673#define vstore_partial_5(DATA, OFFSET, PTR) \
674 vstore_partial_4(DATA.s0123, OFFSET, PTR); \
SiCong Li3b64e3e2020-07-28 09:01:28 +0100675 vstore1(DATA.s4, OFFSET, PTR + 4);
SiCong Li3a501662020-06-26 10:02:06 +0100676
677#define vstore_partial_6(DATA, OFFSET, PTR) \
678 vstore_partial_4(DATA.s0123, OFFSET, PTR); \
679 vstore_partial_2(DATA.s45, OFFSET, PTR + 4);
680
681#define vstore_partial_7(DATA, OFFSET, PTR) \
682 vstore_partial_4(DATA.s0123, OFFSET, PTR); \
683 vstore_partial_3(DATA.s456, OFFSET, PTR + 4);
684
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100685#define vstore_partial_8(DATA, OFFSET, PTR) vstore8(DATA.s01234567, OFFSET, PTR);
SiCong Li3a501662020-06-26 10:02:06 +0100686
687#define vstore_partial_9(DATA, OFFSET, PTR) \
688 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
SiCong Li3b64e3e2020-07-28 09:01:28 +0100689 vstore1(DATA.s8, OFFSET, PTR + 8);
SiCong Li3a501662020-06-26 10:02:06 +0100690
691#define vstore_partial_10(DATA, OFFSET, PTR) \
692 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
693 vstore_partial_2(DATA.s89, OFFSET, PTR + 8);
694
695#define vstore_partial_11(DATA, OFFSET, PTR) \
696 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
697 vstore_partial_3(DATA.s89a, OFFSET, PTR + 8);
698
699#define vstore_partial_12(DATA, OFFSET, PTR) \
700 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
701 vstore_partial_4(DATA.s89ab, OFFSET, PTR + 8);
702
703#define vstore_partial_13(DATA, OFFSET, PTR) \
704 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
Giorgio Arenad304adb2020-10-02 10:20:11 +0100705 vstore_partial_5(DATA.s89abcdef, OFFSET, PTR + 8);
SiCong Li3a501662020-06-26 10:02:06 +0100706
707#define vstore_partial_14(DATA, OFFSET, PTR) \
708 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
Giorgio Arenad304adb2020-10-02 10:20:11 +0100709 vstore_partial_6(DATA.s89abcdef, OFFSET, PTR + 8);
SiCong Li3a501662020-06-26 10:02:06 +0100710
711#define vstore_partial_15(DATA, OFFSET, PTR) \
712 vstore_partial_8(DATA.s01234567, OFFSET, PTR); \
Giorgio Arenad304adb2020-10-02 10:20:11 +0100713 vstore_partial_7(DATA.s89abcdef, OFFSET, PTR + 8);
SiCong Li3a501662020-06-26 10:02:06 +0100714
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100715#define vstore_partial_16(DATA, OFFSET, PTR) vstore16(DATA, OFFSET, PTR);
SiCong Li3a501662020-06-26 10:02:06 +0100716/** @} */ // end of groupd vstore_partial_n
717/** @} */ // end of groupd VSTORE_PARTIAL
718
Gian Marco Iodice0c17aa22019-09-27 09:23:15 +0100719// Convert built-in functions with _sat modifier are not supported in floating point so we create defines
720// without _sat to overcome this issue
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100721#define convert_float_sat convert_float
722#define convert_float1_sat convert_float
723#define convert_float2_sat convert_float2
724#define convert_float3_sat convert_float3
725#define convert_float4_sat convert_float4
726#define convert_float8_sat convert_float8
Gian Marco Iodice0c17aa22019-09-27 09:23:15 +0100727#define convert_float16_sat convert_float16
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100728#define convert_half_sat convert_float
729#define convert_half1_sat convert_half
730#define convert_half2_sat convert_half2
731#define convert_half3_sat convert_half3
732#define convert_half4_sat convert_half4
733#define convert_half8_sat convert_half8
734#define convert_half16_sat convert_half16
Gian Marco Iodice0c17aa22019-09-27 09:23:15 +0100735
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100736#define convert_float1 convert_float
737#define convert_half1 convert_half
738#define convert_char1 convert_char
739#define convert_uchar1 convert_uchar
740#define convert_short1 convert_short
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100741#define convert_ushort1 convert_ushort
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100742#define convert_int1 convert_int
743#define convert_uint1 convert_uint
744#define convert_long1 convert_long
745#define convert_ulong1 convert_ulong
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100746#define convert_double1 convert_double
747
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100748#define convert_char1_sat convert_char_sat
749#define convert_uchar1_sat convert_uchar_sat
750#define convert_uchar2_sat convert_uchar2_sat
751#define convert_uchar3_sat convert_uchar3_sat
752#define convert_uchar4_sat convert_uchar4_sat
753#define convert_uchar8_sat convert_uchar8_sat
Sheri Zhang4f1650f2021-04-15 12:58:20 +0100754#define convert_uchar16_sat convert_uchar16_sat
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100755#define convert_short1_sat convert_short_sat
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100756#define convert_ushort1_sat convert_ushort_sat
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100757#define convert_int1_sat convert_int_sat
758#define convert_uint1_sat convert_uint_sat
759#define convert_long1_sat convert_long_sat
760#define convert_ulong1_sat convert_ulong_sat
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100761#define convert_double1_sat convert_double_sat
762
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100763#define VEC_DATA_TYPE_STR(type, size) type##size
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100764#define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100765
766#define CONVERT_STR(x, type) (convert_##type((x)))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100767#define CONVERT(x, type) CONVERT_STR(x, type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100768
769#define CONVERT_SAT_STR(x, type) (convert_##type##_sat((x)))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100770#define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100771
772#define CONVERT_SAT_ROUND_STR(x, type, round) (convert_##type##_sat_##round((x)))
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100773#define CONVERT_SAT_ROUND(x, type, round) CONVERT_SAT_ROUND_STR(x, type, round)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100774
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100775#define select_vec_dt_uchar(size) uchar##size
776#define select_vec_dt_char(size) char##size
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000777#define select_vec_dt_ushort(size) ushort##size
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100778#define select_vec_dt_short(size) short##size
779#define select_vec_dt_half(size) short##size
780#define select_vec_dt_uint(size) uint##size
781#define select_vec_dt_int(size) int##size
782#define select_vec_dt_float(size) int##size
783#define select_vec_dt_ulong(size) ulong##size
784#define select_vec_dt_long(size) long##size
Giorgio Arenad056e572020-10-12 11:53:51 +0100785
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000786#define SELECT_VEC_DATA_TYPE_STR(type, size) select_vec_dt_##type(size)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100787#define SELECT_VEC_DATA_TYPE(type, size) SELECT_VEC_DATA_TYPE_STR(type, size)
788#define SELECT_DATA_TYPE(type) SELECT_VEC_DATA_TYPE_STR(type, 1)
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000789
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100790#define signed_int_vec_dt_uchar(size) char##size
791#define signed_int_vec_dt_char(size) char##size
Giorgio Arenae36208c2021-01-21 14:53:56 +0000792#define signed_int_vec_dt_ushort(size) short##size
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100793#define signed_int_vec_dt_short(size) short##size
794#define signed_int_vec_dt_half(size) short##size
795#define signed_int_vec_dt_uint(size) int##size
796#define signed_int_vec_dt_int(size) int##size
797#define signed_int_vec_dt_float(size) int##size
798#define signed_int_vec_dt_ulong(size) long##size
799#define signed_int_vec_dt_long(size) long##size
Giorgio Arenae36208c2021-01-21 14:53:56 +0000800
801#define SIGNED_INT_VEC_DATA_TYPE_STR(type, size) signed_int_vec_dt_##type(size)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100802#define SIGNED_INT_VEC_DATA_TYPE(type, size) SIGNED_INT_VEC_DATA_TYPE_STR(type, size)
803#define SIGNED_INT_DATA_TYPE(type) SIGNED_INT_VEC_DATA_TYPE_STR(type, 1)
Giorgio Arenae36208c2021-01-21 14:53:56 +0000804
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100805#define sum_reduce_1(x) (x)
806#define sum_reduce_2(x) ((x).s0) + ((x).s1)
807#define sum_reduce_3(x) sum_reduce_2((x).s01) + ((x).s2)
808#define sum_reduce_4(x) sum_reduce_2((x).s01) + sum_reduce_2((x).s23)
809#define sum_reduce_8(x) sum_reduce_4((x).s0123) + sum_reduce_4((x).s4567)
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000810#define sum_reduce_16(x) sum_reduce_8((x).s01234567) + sum_reduce_8((x).s89ABCDEF)
811
812#define SUM_REDUCE_STR(x, size) sum_reduce_##size(x)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100813#define SUM_REDUCE(x, size) SUM_REDUCE_STR(x, size)
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000814
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100815#define prod_reduce_1(x) (x)
816#define prod_reduce_2(x) ((x).s0) * ((x).s1)
817#define prod_reduce_3(x) prod_reduce_2((x).s01) * ((x).s2)
818#define prod_reduce_4(x) prod_reduce_2((x).s01) * prod_reduce_2((x).s23)
819#define prod_reduce_8(x) prod_reduce_4((x).s0123) * prod_reduce_4((x).s4567)
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100820#define prod_reduce_16(x) prod_reduce_8((x).s01234567) * prod_reduce_8((x).s89ABCDEF)
821
822#define PROD_REDUCE_STR(x, size) prod_reduce_##size(x)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100823#define PROD_REDUCE(x, size) PROD_REDUCE_STR(x, size)
Giorgio Arena3ecf9fe2021-04-28 16:11:51 +0100824
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100825#define max_reduce_1(x) (x)
826#define max_reduce_2(x) max(((x).s0), ((x).s1))
827#define max_reduce_3(x) max(max_reduce_2((x).s01), ((x).s2))
828#define max_reduce_4(x) max(max_reduce_2((x).s01), max_reduce_2((x).s23))
829#define max_reduce_8(x) max(max_reduce_4((x).s0123), max_reduce_4((x).s4567))
Giorgio Arena2d1a8352020-10-26 15:04:08 +0000830#define max_reduce_16(x) max(max_reduce_8((x).s01234567), max_reduce_8((x).s89ABCDEF))
831
832#define MAX_REDUCE_STR(x, size) max_reduce_##size(x)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100833#define MAX_REDUCE(x, size) MAX_REDUCE_STR(x, size)
Giorgio Arenad056e572020-10-12 11:53:51 +0100834
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100835#define min_reduce_1(x) (x)
836#define min_reduce_2(x) min(((x).s0), ((x).s1))
837#define min_reduce_3(x) min(min_reduce_2((x).s01), ((x).s2))
838#define min_reduce_4(x) min(min_reduce_2((x).s01), min_reduce_2((x).s23))
839#define min_reduce_8(x) min(min_reduce_4((x).s0123), min_reduce_4((x).s4567))
Gunes Bayir338ef462023-07-18 15:57:23 +0100840#define min_reduce_16(x) min(min_reduce_8((x).s01234567), min_reduce_8((x).s89ABCDEF))
841
842#define MIN_REDUCE_STR(x, size) min_reduce_##size(x)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100843#define MIN_REDUCE(x, size) MIN_REDUCE_STR(x, size)
Gunes Bayir338ef462023-07-18 15:57:23 +0100844
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100845#define VECTOR_DECLARATION(name) \
846 __global uchar *name##_ptr, uint name##_stride_x, uint name##_step_x, uint name##_offset_first_element_in_bytes
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100847
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100848#define IMAGE_DECLARATION(name) \
849 __global uchar *name##_ptr, uint name##_stride_x, uint name##_step_x, uint name##_stride_y, uint name##_step_y, \
850 uint name##_offset_first_element_in_bytes
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100851
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100852#define TENSOR3D_DECLARATION(name) \
853 __global uchar *name##_ptr, uint name##_stride_x, uint name##_step_x, uint name##_stride_y, uint name##_step_y, \
854 uint name##_stride_z, uint name##_step_z, uint name##_offset_first_element_in_bytes
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100855
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100856#define TENSOR4D_DECLARATION(name) \
857 __global uchar *name##_ptr, uint name##_stride_x, uint name##_step_x, uint name##_stride_y, uint name##_step_y, \
858 uint name##_stride_z, uint name##_step_z, uint name##_stride_w, uint name##_step_w, \
859 uint name##_offset_first_element_in_bytes
steniu01868e5412017-07-17 23:16:00 +0100860
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100861#define TENSOR5D_DECLARATION(name) \
862 __global uchar *name##_ptr, uint name##_stride_x, uint name##_step_x, uint name##_stride_y, uint name##_step_y, \
863 uint name##_stride_z, uint name##_step_z, uint name##_stride_w, uint name##_step_w, uint name##_stride_v, \
864 uint name##_step_v, uint name##_offset_first_element_in_bytes
ramelg0137515692022-02-26 22:06:20 +0000865
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100866#define CONVERT_TO_VECTOR_STRUCT(name) \
867 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x)
868
869#define CONVERT_TO_VECTOR_STRUCT_NO_STEP(name) \
870 update_vector_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0)
871
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100872#define CONVERT_TO_IMAGE_STRUCT(name) \
873 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, \
874 name##_stride_y, name##_step_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100875
876#define CONVERT_TO_IMAGE_STRUCT_NO_STEP(name) \
877 update_image_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, name##_stride_y, 0)
878
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100879#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
880 update_image_from_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, \
881 name##_step_x, name##_stride_y, name##_step_y, name##_stride_z, \
882 name##_step_z)
steniu01868e5412017-07-17 23:16:00 +0100883
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100884#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(name) \
885 update_image_from_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, \
886 name##_stride_y, 0, name##_stride_z, name##_step_z)
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100887
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100888#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
889 update_image_from_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, \
890 name##_step_x, name##_stride_y, name##_step_y, name##_stride_z, \
891 name##_step_z)
steniu010d523cc2017-07-13 14:24:23 +0100892
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100893#define CONVERT_TO_TENSOR3D_STRUCT(name) \
894 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, \
895 name##_stride_y, name##_step_y, name##_stride_z, name##_step_z)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100896
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100897#define CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(name) \
898 update_tensor3D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, 0, \
899 name##_stride_y, 0, name##_stride_z, 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100900
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100901#define CONVERT_TO_TENSOR4D_STRUCT(name, mod_size) \
902 update_tensor4D_workitem_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, \
903 name##_stride_y, name##_step_y, name##_stride_z, name##_step_z, name##_stride_w, \
904 name##_step_w, mod_size)
steniu01868e5412017-07-17 23:16:00 +0100905
Viet-Hoa Do6829e022024-01-16 16:23:24 +0000906#define CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(name) \
907 update_tensor4D_workitem_no_step_ptr(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, \
908 name##_stride_y, name##_stride_z, name##_stride_w)
steniu01868e5412017-07-17 23:16:00 +0100909
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100910#define CONVERT_TO_TENSOR3D_STRUCT_NO_UPDATE_PTR(name) \
911 tensor3D_ptr_no_update(name##_ptr, name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, \
912 name##_stride_y, name##_step_y, name##_stride_z, name##_step_z)
Gian Marco Iodice4d81d752020-07-14 15:05:31 +0100913
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100914/** Structure to hold Vector information */
915typedef struct Vector
916{
917 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
918 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
919 int stride_x; /**< Stride of the image in X dimension (in bytes) */
920} Vector;
921
922/** Structure to hold Image information */
923typedef struct Image
924{
925 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
926 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
927 int stride_x; /**< Stride of the image in X dimension (in bytes) */
928 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
929} Image;
930
931/** Structure to hold 3D tensor information */
932typedef struct Tensor3D
933{
934 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
935 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
936 int stride_x; /**< Stride of the image in X dimension (in bytes) */
937 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
938 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
939} Tensor3D;
940
steniu01868e5412017-07-17 23:16:00 +0100941/** Structure to hold 4D tensor information */
942typedef struct Tensor4D
943{
944 __global uchar *ptr; /**< Pointer to the starting postion of the buffer */
945 int offset_first_element_in_bytes; /**< The offset of the first element in the source image */
946 int stride_x; /**< Stride of the image in X dimension (in bytes) */
947 int stride_y; /**< Stride of the image in Y dimension (in bytes) */
948 int stride_z; /**< Stride of the image in Z dimension (in bytes) */
949 int stride_w; /**< Stride of the image in W dimension (in bytes) */
950} Tensor4D;
951
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100952/** Wrap vector information into an Vector structure, and make the pointer point at this workitem's data.
953 *
954 * @param[in] ptr Pointer to the starting postion of the buffer
955 * @param[in] offset_first_element_in_bytes The offset of the first element in the source vector
956 * @param[in] stride_x Stride of the vector in X dimension (in bytes)
957 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
958 *
959 * @return An image object
960 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100961inline Vector
962update_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 +0100963{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100964 Vector vector = {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100965 .ptr = ptr,
966 .offset_first_element_in_bytes = offset_first_element_in_bytes,
967 .stride_x = stride_x,
968 };
969 vector.ptr += vector.offset_first_element_in_bytes + get_global_id(0) * step_x;
970 return vector;
971}
972
973/** Wrap image information into an Image structure, and make the pointer point at this workitem's data.
974 *
975 * @param[in] ptr Pointer to the starting postion of the buffer
976 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
977 * @param[in] stride_x Stride of the image in X dimension (in bytes)
978 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
979 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
980 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
981 *
982 * @return An image object
983 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100984inline Image update_image_workitem_ptr(
985 __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 +0100986{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100987 Image img = {.ptr = ptr,
988 .offset_first_element_in_bytes = offset_first_element_in_bytes,
989 .stride_x = stride_x,
990 .stride_y = stride_y};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100991 img.ptr += img.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y;
992 return img;
993}
994
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100995/** Wrap 3D tensor information into an image structure, and make the pointer point at this workitem's data.
996 *
997 * @param[in] ptr Pointer to the starting postion of the buffer
998 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
999 * @param[in] stride_x Stride of the image in X dimension (in bytes)
1000 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
1001 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
1002 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
1003 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
1004 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
1005 *
1006 * @return A 3D tensor object
1007 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001008inline Image update_image_from_tensor3D_workitem_ptr(__global uchar *ptr,
1009 uint offset_first_element_in_bytes,
1010 uint stride_x,
1011 uint step_x,
1012 uint stride_y,
1013 uint step_y,
1014 uint stride_z,
1015 uint step_z)
Anthony Barbier7ff47a32017-07-11 16:54:04 +01001016{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001017 Image img = {.ptr = ptr,
1018 .offset_first_element_in_bytes = offset_first_element_in_bytes,
1019 .stride_x = stride_x,
1020 .stride_y = stride_y};
1021 img.ptr += img.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y +
1022 get_global_id(2) * step_z;
Anthony Barbier7ff47a32017-07-11 16:54:04 +01001023 return img;
1024}
1025
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001026/** Wrap 3D tensor information into an tensor structure, and make the pointer point at this workitem's data.
1027 *
1028 * @param[in] ptr Pointer to the starting postion of the buffer
1029 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
1030 * @param[in] stride_x Stride of the image in X dimension (in bytes)
1031 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
1032 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
1033 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
1034 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
1035 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
1036 *
1037 * @return A 3D tensor object
1038 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001039inline Tensor3D update_tensor3D_workitem_ptr(__global uchar *ptr,
1040 uint offset_first_element_in_bytes,
1041 uint stride_x,
1042 uint step_x,
1043 uint stride_y,
1044 uint step_y,
1045 uint stride_z,
1046 uint step_z)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001047{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001048 Tensor3D tensor = {.ptr = ptr,
1049 .offset_first_element_in_bytes = offset_first_element_in_bytes,
1050 .stride_x = stride_x,
1051 .stride_y = stride_y,
1052 .stride_z = stride_z};
1053 tensor.ptr += tensor.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y +
1054 get_global_id(2) * step_z;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001055 return tensor;
1056}
1057
Gian Marco Iodice4d81d752020-07-14 15:05:31 +01001058/** Wrap 3D tensor information into an tensor structure.
1059 *
1060 * @param[in] ptr Pointer to the starting postion of the buffer
1061 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
1062 * @param[in] stride_x Stride of the image in X dimension (in bytes)
1063 * @param[in] step_x stride_x * number of elements along X processed per workitem(in bytes)
1064 * @param[in] stride_y Stride of the image in Y dimension (in bytes)
1065 * @param[in] step_y stride_y * number of elements along Y processed per workitem(in bytes)
1066 * @param[in] stride_z Stride of the image in Z dimension (in bytes)
1067 * @param[in] step_z stride_z * number of elements along Z processed per workitem(in bytes)
1068 *
1069 * @return A 3D tensor object
1070 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001071inline Tensor3D tensor3D_ptr_no_update(__global uchar *ptr,
1072 uint offset_first_element_in_bytes,
1073 uint stride_x,
1074 uint step_x,
1075 uint stride_y,
1076 uint step_y,
1077 uint stride_z,
1078 uint step_z)
Gian Marco Iodice4d81d752020-07-14 15:05:31 +01001079{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001080 Tensor3D tensor = {.ptr = ptr,
1081 .offset_first_element_in_bytes = offset_first_element_in_bytes,
1082 .stride_x = stride_x,
1083 .stride_y = stride_y,
1084 .stride_z = stride_z};
Gian Marco Iodice4d81d752020-07-14 15:05:31 +01001085 return tensor;
1086}
1087
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001088inline Tensor4D update_tensor4D_workitem_ptr(__global uchar *ptr,
1089 uint offset_first_element_in_bytes,
1090 uint stride_x,
1091 uint step_x,
1092 uint stride_y,
1093 uint step_y,
1094 uint stride_z,
1095 uint step_z,
1096 uint stride_w,
1097 uint step_w,
1098 uint mod_size)
steniu01868e5412017-07-17 23:16:00 +01001099{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001100 Tensor4D tensor = {.ptr = ptr,
1101 .offset_first_element_in_bytes = offset_first_element_in_bytes,
1102 .stride_x = stride_x,
1103 .stride_y = stride_y,
1104 .stride_z = stride_z,
1105 .stride_w = stride_w};
steniu01868e5412017-07-17 23:16:00 +01001106
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001107 tensor.ptr += tensor.offset_first_element_in_bytes + get_global_id(0) * step_x + get_global_id(1) * step_y +
1108 (get_global_id(2) % mod_size) * step_z + (get_global_id(2) / mod_size) * step_w;
steniu01868e5412017-07-17 23:16:00 +01001109 return tensor;
1110}
1111
Viet-Hoa Do6829e022024-01-16 16:23:24 +00001112inline Tensor4D update_tensor4D_workitem_no_step_ptr(
1113 __global uchar *ptr, uint offset_first_element_in_bytes, uint stride_x, uint stride_y, uint stride_z, uint stride_w)
1114{
1115 Tensor4D tensor = {.ptr = ptr,
1116 .offset_first_element_in_bytes = offset_first_element_in_bytes,
1117 .stride_x = stride_x,
1118 .stride_y = stride_y,
1119 .stride_z = stride_z,
1120 .stride_w = stride_w};
1121
1122 tensor.ptr += tensor.offset_first_element_in_bytes;
1123 return tensor;
1124}
1125
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001126/** Get the pointer position of a Vector
1127 *
1128 * @param[in] vec Pointer to the starting position of the buffer
1129 * @param[in] x Relative X position
1130 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +01001131inline __global const uchar *vector_offset(const Vector *vec, int x)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001132{
1133 return vec->ptr + x * vec->stride_x;
1134}
1135
1136/** Get the pointer position of a Image
1137 *
1138 * @param[in] img Pointer to the starting position of the buffer
1139 * @param[in] x Relative X position
1140 * @param[in] y Relative Y position
1141 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +01001142inline __global uchar *offset(const Image *img, int x, int y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001143{
1144 return img->ptr + x * img->stride_x + y * img->stride_y;
1145}
1146
1147/** Get the pointer position of a Tensor3D
1148 *
Gian Marco Iodice3a623242017-07-25 10:25:53 +01001149 * @param[in] tensor Pointer to the starting position of the buffer
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001150 * @param[in] x Relative X position
1151 * @param[in] y Relative Y position
1152 * @param[in] z Relative Z position
1153 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +01001154inline __global const uchar *tensor3D_offset(const Tensor3D *tensor, int x, int y, int z)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001155{
1156 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z;
1157}
1158
steniu01868e5412017-07-17 23:16:00 +01001159/** Get the pointer position of a Tensor4D
1160 *
1161 * @param[in] tensor Pointer to the starting position of the buffer
1162 * @param[in] x Relative X position
1163 * @param[in] y Relative Y position
1164 * @param[in] z Relative Z position
1165 * @param[in] w Relative W position
1166 */
Georgios Pinitasaf7f7402018-10-25 16:04:40 +01001167inline __global const uchar *tensor4D_offset(const Tensor4D *tensor, int x, int y, int z, int w)
steniu01868e5412017-07-17 23:16:00 +01001168{
1169 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z + w * tensor->stride_w;
1170}
1171
Gian Marco Iodice4d81d752020-07-14 15:05:31 +01001172/** Get the offset for a given linear index of a Tensor3D
1173 *
1174 * @param[in] tensor Pointer to the starting position of the buffer
1175 * @param[in] width Width of the input tensor
1176 * @param[in] height Height of the input tensor
1177 * @param[in] depth Depth of the input tensor
1178 * @param[in] index Linear index
1179 */
1180inline __global const uchar *tensor3D_index2ptr(const Tensor3D *tensor, uint width, uint height, uint depth, uint index)
1181{
1182 uint num_elements = width * height;
1183
1184 const uint z = index / num_elements;
1185
1186 index %= num_elements;
1187
1188 const uint y = index / width;
1189
1190 index %= width;
1191
1192 const uint x = index;
1193
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001194 return tensor->ptr + x * tensor->stride_x + y * tensor->stride_y + z * tensor->stride_z +
1195 tensor->offset_first_element_in_bytes;
Gian Marco Iodice4d81d752020-07-14 15:05:31 +01001196}
1197
Viet-Hoa Do6829e022024-01-16 16:23:24 +00001198#endif // ACL_SRC_CORE_CL_CL_KERNELS_HELPERS_H