blob: 812960627712fa15356130873fbff6ed5c1f8391 [file] [log] [blame]
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001/*
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +00002 * Copyright (c) 2021-2023 Arm Limited.
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00003 *
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 */
Ramy Elgammal2b6ebfe2023-03-09 21:15:37 +000024#ifndef ACL_SRC_CORE_CL_CL_KERNELS_TILE_HELPERS
25#define ACL_SRC_CORE_CL_CL_KERNELS_TILE_HELPERS
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000026
Giorgio Arenabdd16d12021-05-13 16:58:51 +010027// *INDENT-OFF*
28// clang-format off
29
Gian Marco Iodice8155c022021-04-16 15:08:59 +010030#define TILE_VECTOR_SIZE1 1
31#define TILE_VECTOR_SIZE2 2
32#define TILE_VECTOR_SIZE3 3
33#define TILE_VECTOR_SIZE4 4
34#define TILE_VECTOR_SIZE5 8
35#define TILE_VECTOR_SIZE6 8
36#define TILE_VECTOR_SIZE7 8
37#define TILE_VECTOR_SIZE8 8
38#define TILE_VECTOR_SIZE9 16
39#define TILE_VECTOR_SIZE10 16
40#define TILE_VECTOR_SIZE11 16
41#define TILE_VECTOR_SIZE12 16
42#define TILE_VECTOR_SIZE13 16
43#define TILE_VECTOR_SIZE14 16
44#define TILE_VECTOR_SIZE15 16
45#define TILE_VECTOR_SIZE16 16
46
47#define TILE_VECTOR_TYPE1(DATA_TYPE) DATA_TYPE##1
48#define TILE_VECTOR_TYPE2(DATA_TYPE) DATA_TYPE##2
49#define TILE_VECTOR_TYPE3(DATA_TYPE) DATA_TYPE##3
50#define TILE_VECTOR_TYPE4(DATA_TYPE) DATA_TYPE##4
51#define TILE_VECTOR_TYPE5(DATA_TYPE) DATA_TYPE##8
52#define TILE_VECTOR_TYPE6(DATA_TYPE) DATA_TYPE##8
53#define TILE_VECTOR_TYPE7(DATA_TYPE) DATA_TYPE##8
54#define TILE_VECTOR_TYPE8(DATA_TYPE) DATA_TYPE##8
55#define TILE_VECTOR_TYPE9(DATA_TYPE) DATA_TYPE##16
56#define TILE_VECTOR_TYPE10(DATA_TYPE) DATA_TYPE##16
57#define TILE_VECTOR_TYPE11(DATA_TYPE) DATA_TYPE##16
58#define TILE_VECTOR_TYPE12(DATA_TYPE) DATA_TYPE##16
59#define TILE_VECTOR_TYPE13(DATA_TYPE) DATA_TYPE##16
60#define TILE_VECTOR_TYPE14(DATA_TYPE) DATA_TYPE##16
61#define TILE_VECTOR_TYPE15(DATA_TYPE) DATA_TYPE##16
62#define TILE_VECTOR_TYPE16(DATA_TYPE) DATA_TYPE##16
63
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000064/** Tile object
65 * A tile object is a 2D memory block and can be accessed using the following syntax:
66 * -# a[m0].v = access the the vector at row "m0" (OpenCL vector)
Ramy Elgammal404462a2022-11-08 02:14:46 +000067 * -# dst[m0].s[n0] = access the scalar element at row "m0" and column "n0" (scalar access)
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000068 *
69 * @param[in] DATA_TYPE Data type of the tile
70 * @param[in] H Number of tile rows
71 * @param[in] W Number of tile colums
72 * @param[in] BASENAME Tile's name
73 */
74#define TILE(DATA_TYPE, H, W, BASENAME) TILE_STR(DATA_TYPE, H, W, BASENAME)
75#define TILE_STR(DATA_TYPE, H, W, BASENAME) \
76 union { \
Gian Marco Iodice8155c022021-04-16 15:08:59 +010077 DATA_TYPE s[TILE_VECTOR_SIZE##W]; \
78 TILE_VECTOR_TYPE##W(DATA_TYPE) v; \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000079 } BASENAME[H]
80
Giorgio Arenabdd16d12021-05-13 16:58:51 +010081#define TENSOR4D_IMAGE(name) \
82 __read_only image2d_t name##_img, \
83 __global uchar *name##_ptr, \
84 uint name##_stride_x, \
85 uint name##_step_x, \
86 uint name##_stride_y, \
87 uint name##_step_y, \
88 uint name##_stride_z, \
89 uint name##_step_z, \
90 uint name##_stride_w, \
91 uint name##_step_w, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000092 uint name##_offset_first_element_in_bytes
93
Giorgio Arenabdd16d12021-05-13 16:58:51 +010094#define TENSOR4D_BUFFER(name) \
95 __global uchar *name##_ptr, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000096 uint name##_stride_x, \
97 uint name##_step_x, \
98 uint name##_stride_y, \
99 uint name##_step_y, \
100 uint name##_stride_z, \
101 uint name##_step_z, \
102 uint name##_stride_w, \
103 uint name##_step_w, \
104 uint name##_offset_first_element_in_bytes
105
106#define TENSOR4D_STR(name, type) TENSOR4D_##type(name)
107#define TENSOR4D(name, type) TENSOR4D_STR(name, type)
108
Adnan AlSinan17975a62021-11-08 17:46:39 +0000109#define TENSOR4D_T_IMAGE(name) \
110 __read_only image2d_t name##_img, \
111 __global uchar *name##_ptr, \
112 uint name##_stride_y, \
113 uint name##_stride_z, \
114 uint name##_stride_w, \
115 uint name##_c, \
116 uint name##_w, \
117 uint name##_h, \
118 uint name##_n, \
119 uint name##_offset_first_element_in_bytes
120
121#define TENSOR4D_T_BUFFER(name) \
122 __global uchar *name##_ptr, \
123 uint name##_stride_y, \
124 uint name##_stride_z, \
125 uint name##_stride_w, \
126 uint name##_c, \
127 uint name##_w, \
128 uint name##_h, \
129 uint name##_n, \
130 uint name##_offset_first_element_in_bytes
131
132#define TENSOR4D_T_STR(name, type) TENSOR4D_T_##type(name)
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000133
134/** Legacy tensor 4D arguments
135 *
136 * @param[in] name Tensor name. The tensor name is the prefix of the tensor components
137 * @param[in] type Tensor type (BUFFER or IMAGE)
138 */
Adnan AlSinan17975a62021-11-08 17:46:39 +0000139#define TENSOR4D_T(name, type) TENSOR4D_T_STR(name, type)
140
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000141#define TENSOR4D_RO_T_IMAGE(name) \
142 __read_only image2d_t name##_img, \
143 TENSOR4D_T_BUFFER(name)
144
145#define TENSOR4D_RO_T_BUFFER(name) TENSOR4D_T_BUFFER(name)
146
147#define TENSOR4D_RO_T_STR(name, type) TENSOR4D_RO_T_##type(name)
148
149/** Read-Only (RO) tensor 4D.
150 *
151 * @param[in] name Tensor name. The tensor name is the prefix of the tensor components
152 * @param[in] type Tensor type (BUFFER or IMAGE)
153 */
154#define TENSOR4D_RO_T(name, type) TENSOR4D_RO_T_STR(name, type)
155
156#define TENSOR4D_WO_T_IMAGE(name) \
157 __write_only image2d_t name##_img, \
158 TENSOR4D_T_BUFFER(name)
159
160#define TENSOR4D_WO_T_BUFFER(name) TENSOR4D_T_BUFFER(name)
161
162#define TENSOR4D_WO_T_STR(name, type) TENSOR4D_WO_T_##type(name)
163
164/** Write-Only (WO) tensor 4D.
165 *
166 * @param[in] name Tensor name. The tensor name is the prefix of the tensor components
167 * @param[in] type Tensor type (BUFFER or IMAGE)
168 */
169#define TENSOR4D_WO_T(name, type) TENSOR4D_WO_T_STR(name, type)
170
Gian Marco Iodice4fb56702021-11-10 11:18:50 +0000171#define TENSOR3D_T_IMAGE(name) \
172 __read_only image2d_t name##_img, \
173 __global uchar *name##_ptr, \
174 uint name##_stride_y, \
175 uint name##_stride_z, \
176 uint name##_w, \
177 uint name##_h, \
178 uint name##_n, \
179 uint name##_offset_first_element_in_bytes
180
181#define TENSOR3D_T_BUFFER(name) \
182 __global uchar *name##_ptr, \
183 uint name##_stride_y, \
184 uint name##_stride_z, \
185 uint name##_w, \
186 uint name##_h, \
187 uint name##_n, \
188 uint name##_offset_first_element_in_bytes
189
190#define TENSOR3D_T_STR(name, type) TENSOR3D_T_##type(name)
191#define TENSOR3D_T(name, type) TENSOR3D_T_STR(name, type)
192
Giorgio Arenaea8d2662021-05-20 11:36:56 +0100193#if !defined(UNROLL_WITH_PRAGMA)
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100194#define UNROLL_INCR(idx, step, macro) idx += (step); (macro)
195
196#define LOOP_UNROLLING_1(idx, step, macro) (macro)
197#define LOOP_UNROLLING_2(idx, step, macro) LOOP_UNROLLING_1(idx, step, macro); UNROLL_INCR(idx, step, macro)
198#define LOOP_UNROLLING_3(idx, step, macro) LOOP_UNROLLING_2(idx, step, macro); UNROLL_INCR(idx, step, macro)
199#define LOOP_UNROLLING_4(idx, step, macro) LOOP_UNROLLING_3(idx, step, macro); UNROLL_INCR(idx, step, macro)
200#define LOOP_UNROLLING_5(idx, step, macro) LOOP_UNROLLING_4(idx, step, macro); UNROLL_INCR(idx, step, macro)
201#define LOOP_UNROLLING_6(idx, step, macro) LOOP_UNROLLING_5(idx, step, macro); UNROLL_INCR(idx, step, macro)
202#define LOOP_UNROLLING_7(idx, step, macro) LOOP_UNROLLING_6(idx, step, macro); UNROLL_INCR(idx, step, macro)
203#define LOOP_UNROLLING_8(idx, step, macro) LOOP_UNROLLING_7(idx, step, macro); UNROLL_INCR(idx, step, macro)
204#define LOOP_UNROLLING_9(idx, step, macro) LOOP_UNROLLING_8(idx, step, macro); UNROLL_INCR(idx, step, macro)
205#define LOOP_UNROLLING_10(idx, step, macro) LOOP_UNROLLING_9(idx, step, macro); UNROLL_INCR(idx, step, macro)
206#define LOOP_UNROLLING_11(idx, step, macro) LOOP_UNROLLING_10(idx, step, macro); UNROLL_INCR(idx, step, macro)
207#define LOOP_UNROLLING_12(idx, step, macro) LOOP_UNROLLING_11(idx, step, macro); UNROLL_INCR(idx, step, macro)
208#define LOOP_UNROLLING_13(idx, step, macro) LOOP_UNROLLING_12(idx, step, macro); UNROLL_INCR(idx, step, macro)
209#define LOOP_UNROLLING_14(idx, step, macro) LOOP_UNROLLING_13(idx, step, macro); UNROLL_INCR(idx, step, macro)
210#define LOOP_UNROLLING_15(idx, step, macro) LOOP_UNROLLING_14(idx, step, macro); UNROLL_INCR(idx, step, macro)
211#define LOOP_UNROLLING_16(idx, step, macro) LOOP_UNROLLING_15(idx, step, macro); UNROLL_INCR(idx, step, macro)
212#define LOOP_UNROLLING_17(idx, step, macro) LOOP_UNROLLING_16(idx, step, macro); UNROLL_INCR(idx, step, macro)
213#define LOOP_UNROLLING_18(idx, step, macro) LOOP_UNROLLING_17(idx, step, macro); UNROLL_INCR(idx, step, macro)
214#define LOOP_UNROLLING_19(idx, step, macro) LOOP_UNROLLING_18(idx, step, macro); UNROLL_INCR(idx, step, macro)
215#define LOOP_UNROLLING_20(idx, step, macro) LOOP_UNROLLING_19(idx, step, macro); UNROLL_INCR(idx, step, macro)
216#define LOOP_UNROLLING_21(idx, step, macro) LOOP_UNROLLING_20(idx, step, macro); UNROLL_INCR(idx, step, macro)
217#define LOOP_UNROLLING_22(idx, step, macro) LOOP_UNROLLING_21(idx, step, macro); UNROLL_INCR(idx, step, macro)
218#define LOOP_UNROLLING_23(idx, step, macro) LOOP_UNROLLING_22(idx, step, macro); UNROLL_INCR(idx, step, macro)
219#define LOOP_UNROLLING_24(idx, step, macro) LOOP_UNROLLING_23(idx, step, macro); UNROLL_INCR(idx, step, macro)
220#define LOOP_UNROLLING_25(idx, step, macro) LOOP_UNROLLING_24(idx, step, macro); UNROLL_INCR(idx, step, macro)
221#define LOOP_UNROLLING_26(idx, step, macro) LOOP_UNROLLING_25(idx, step, macro); UNROLL_INCR(idx, step, macro)
222#define LOOP_UNROLLING_27(idx, step, macro) LOOP_UNROLLING_26(idx, step, macro); UNROLL_INCR(idx, step, macro)
223#define LOOP_UNROLLING_28(idx, step, macro) LOOP_UNROLLING_27(idx, step, macro); UNROLL_INCR(idx, step, macro)
224#define LOOP_UNROLLING_29(idx, step, macro) LOOP_UNROLLING_28(idx, step, macro); UNROLL_INCR(idx, step, macro)
225#define LOOP_UNROLLING_30(idx, step, macro) LOOP_UNROLLING_29(idx, step, macro); UNROLL_INCR(idx, step, macro)
226#define LOOP_UNROLLING_31(idx, step, macro) LOOP_UNROLLING_30(idx, step, macro); UNROLL_INCR(idx, step, macro)
227#define LOOP_UNROLLING_32(idx, step, macro) LOOP_UNROLLING_31(idx, step, macro); UNROLL_INCR(idx, step, macro)
228#define LOOP_UNROLLING_33(idx, step, macro) LOOP_UNROLLING_32(idx, step, macro); UNROLL_INCR(idx, step, macro)
229#define LOOP_UNROLLING_34(idx, step, macro) LOOP_UNROLLING_33(idx, step, macro); UNROLL_INCR(idx, step, macro)
230#define LOOP_UNROLLING_35(idx, step, macro) LOOP_UNROLLING_34(idx, step, macro); UNROLL_INCR(idx, step, macro)
231#define LOOP_UNROLLING_36(idx, step, macro) LOOP_UNROLLING_35(idx, step, macro); UNROLL_INCR(idx, step, macro)
232#define LOOP_UNROLLING_37(idx, step, macro) LOOP_UNROLLING_36(idx, step, macro); UNROLL_INCR(idx, step, macro)
233#define LOOP_UNROLLING_38(idx, step, macro) LOOP_UNROLLING_37(idx, step, macro); UNROLL_INCR(idx, step, macro)
234#define LOOP_UNROLLING_39(idx, step, macro) LOOP_UNROLLING_38(idx, step, macro); UNROLL_INCR(idx, step, macro)
235#define LOOP_UNROLLING_40(idx, step, macro) LOOP_UNROLLING_39(idx, step, macro); UNROLL_INCR(idx, step, macro)
236#define LOOP_UNROLLING_41(idx, step, macro) LOOP_UNROLLING_40(idx, step, macro); UNROLL_INCR(idx, step, macro)
237#define LOOP_UNROLLING_42(idx, step, macro) LOOP_UNROLLING_41(idx, step, macro); UNROLL_INCR(idx, step, macro)
238#define LOOP_UNROLLING_43(idx, step, macro) LOOP_UNROLLING_42(idx, step, macro); UNROLL_INCR(idx, step, macro)
239#define LOOP_UNROLLING_44(idx, step, macro) LOOP_UNROLLING_43(idx, step, macro); UNROLL_INCR(idx, step, macro)
240#define LOOP_UNROLLING_45(idx, step, macro) LOOP_UNROLLING_44(idx, step, macro); UNROLL_INCR(idx, step, macro)
241#define LOOP_UNROLLING_46(idx, step, macro) LOOP_UNROLLING_45(idx, step, macro); UNROLL_INCR(idx, step, macro)
242#define LOOP_UNROLLING_47(idx, step, macro) LOOP_UNROLLING_46(idx, step, macro); UNROLL_INCR(idx, step, macro)
243#define LOOP_UNROLLING_48(idx, step, macro) LOOP_UNROLLING_47(idx, step, macro); UNROLL_INCR(idx, step, macro)
244#define LOOP_UNROLLING_49(idx, step, macro) LOOP_UNROLLING_48(idx, step, macro); UNROLL_INCR(idx, step, macro)
245#define LOOP_UNROLLING_50(idx, step, macro) LOOP_UNROLLING_49(idx, step, macro); UNROLL_INCR(idx, step, macro)
246#define LOOP_UNROLLING_51(idx, step, macro) LOOP_UNROLLING_50(idx, step, macro); UNROLL_INCR(idx, step, macro)
247#define LOOP_UNROLLING_52(idx, step, macro) LOOP_UNROLLING_51(idx, step, macro); UNROLL_INCR(idx, step, macro)
248#define LOOP_UNROLLING_53(idx, step, macro) LOOP_UNROLLING_52(idx, step, macro); UNROLL_INCR(idx, step, macro)
249#define LOOP_UNROLLING_54(idx, step, macro) LOOP_UNROLLING_53(idx, step, macro); UNROLL_INCR(idx, step, macro)
250#define LOOP_UNROLLING_55(idx, step, macro) LOOP_UNROLLING_54(idx, step, macro); UNROLL_INCR(idx, step, macro)
251#define LOOP_UNROLLING_56(idx, step, macro) LOOP_UNROLLING_55(idx, step, macro); UNROLL_INCR(idx, step, macro)
252#define LOOP_UNROLLING_57(idx, step, macro) LOOP_UNROLLING_56(idx, step, macro); UNROLL_INCR(idx, step, macro)
253#define LOOP_UNROLLING_58(idx, step, macro) LOOP_UNROLLING_57(idx, step, macro); UNROLL_INCR(idx, step, macro)
254#define LOOP_UNROLLING_59(idx, step, macro) LOOP_UNROLLING_58(idx, step, macro); UNROLL_INCR(idx, step, macro)
255#define LOOP_UNROLLING_60(idx, step, macro) LOOP_UNROLLING_59(idx, step, macro); UNROLL_INCR(idx, step, macro)
256#define LOOP_UNROLLING_61(idx, step, macro) LOOP_UNROLLING_60(idx, step, macro); UNROLL_INCR(idx, step, macro)
257#define LOOP_UNROLLING_62(idx, step, macro) LOOP_UNROLLING_61(idx, step, macro); UNROLL_INCR(idx, step, macro)
258#define LOOP_UNROLLING_63(idx, step, macro) LOOP_UNROLLING_62(idx, step, macro); UNROLL_INCR(idx, step, macro)
259#define LOOP_UNROLLING_64(idx, step, macro) LOOP_UNROLLING_63(idx, step, macro); UNROLL_INCR(idx, step, macro)
260#define LOOP_UNROLLING_65(idx, step, macro) LOOP_UNROLLING_64(idx, step, macro); UNROLL_INCR(idx, step, macro)
261#define LOOP_UNROLLING_66(idx, step, macro) LOOP_UNROLLING_65(idx, step, macro); UNROLL_INCR(idx, step, macro)
262#define LOOP_UNROLLING_67(idx, step, macro) LOOP_UNROLLING_66(idx, step, macro); UNROLL_INCR(idx, step, macro)
263#define LOOP_UNROLLING_68(idx, step, macro) LOOP_UNROLLING_67(idx, step, macro); UNROLL_INCR(idx, step, macro)
264#define LOOP_UNROLLING_69(idx, step, macro) LOOP_UNROLLING_68(idx, step, macro); UNROLL_INCR(idx, step, macro)
265#define LOOP_UNROLLING_70(idx, step, macro) LOOP_UNROLLING_69(idx, step, macro); UNROLL_INCR(idx, step, macro)
266#define LOOP_UNROLLING_71(idx, step, macro) LOOP_UNROLLING_70(idx, step, macro); UNROLL_INCR(idx, step, macro)
267#define LOOP_UNROLLING_72(idx, step, macro) LOOP_UNROLLING_71(idx, step, macro); UNROLL_INCR(idx, step, macro)
268#define LOOP_UNROLLING_73(idx, step, macro) LOOP_UNROLLING_72(idx, step, macro); UNROLL_INCR(idx, step, macro)
269#define LOOP_UNROLLING_74(idx, step, macro) LOOP_UNROLLING_73(idx, step, macro); UNROLL_INCR(idx, step, macro)
270#define LOOP_UNROLLING_75(idx, step, macro) LOOP_UNROLLING_74(idx, step, macro); UNROLL_INCR(idx, step, macro)
271#define LOOP_UNROLLING_76(idx, step, macro) LOOP_UNROLLING_75(idx, step, macro); UNROLL_INCR(idx, step, macro)
272#define LOOP_UNROLLING_77(idx, step, macro) LOOP_UNROLLING_76(idx, step, macro); UNROLL_INCR(idx, step, macro)
273#define LOOP_UNROLLING_78(idx, step, macro) LOOP_UNROLLING_77(idx, step, macro); UNROLL_INCR(idx, step, macro)
274#define LOOP_UNROLLING_79(idx, step, macro) LOOP_UNROLLING_78(idx, step, macro); UNROLL_INCR(idx, step, macro)
275#define LOOP_UNROLLING_80(idx, step, macro) LOOP_UNROLLING_79(idx, step, macro); UNROLL_INCR(idx, step, macro)
276#define LOOP_UNROLLING_81(idx, step, macro) LOOP_UNROLLING_80(idx, step, macro); UNROLL_INCR(idx, step, macro)
277#define LOOP_UNROLLING_82(idx, step, macro) LOOP_UNROLLING_81(idx, step, macro); UNROLL_INCR(idx, step, macro)
278#define LOOP_UNROLLING_83(idx, step, macro) LOOP_UNROLLING_82(idx, step, macro); UNROLL_INCR(idx, step, macro)
279#define LOOP_UNROLLING_84(idx, step, macro) LOOP_UNROLLING_83(idx, step, macro); UNROLL_INCR(idx, step, macro)
280#define LOOP_UNROLLING_85(idx, step, macro) LOOP_UNROLLING_84(idx, step, macro); UNROLL_INCR(idx, step, macro)
281#define LOOP_UNROLLING_86(idx, step, macro) LOOP_UNROLLING_85(idx, step, macro); UNROLL_INCR(idx, step, macro)
282#define LOOP_UNROLLING_87(idx, step, macro) LOOP_UNROLLING_86(idx, step, macro); UNROLL_INCR(idx, step, macro)
283#define LOOP_UNROLLING_88(idx, step, macro) LOOP_UNROLLING_87(idx, step, macro); UNROLL_INCR(idx, step, macro)
284#define LOOP_UNROLLING_89(idx, step, macro) LOOP_UNROLLING_88(idx, step, macro); UNROLL_INCR(idx, step, macro)
285#define LOOP_UNROLLING_90(idx, step, macro) LOOP_UNROLLING_89(idx, step, macro); UNROLL_INCR(idx, step, macro)
286#define LOOP_UNROLLING_91(idx, step, macro) LOOP_UNROLLING_90(idx, step, macro); UNROLL_INCR(idx, step, macro)
287#define LOOP_UNROLLING_92(idx, step, macro) LOOP_UNROLLING_91(idx, step, macro); UNROLL_INCR(idx, step, macro)
288#define LOOP_UNROLLING_93(idx, step, macro) LOOP_UNROLLING_92(idx, step, macro); UNROLL_INCR(idx, step, macro)
289#define LOOP_UNROLLING_94(idx, step, macro) LOOP_UNROLLING_93(idx, step, macro); UNROLL_INCR(idx, step, macro)
290#define LOOP_UNROLLING_95(idx, step, macro) LOOP_UNROLLING_94(idx, step, macro); UNROLL_INCR(idx, step, macro)
291#define LOOP_UNROLLING_96(idx, step, macro) LOOP_UNROLLING_95(idx, step, macro); UNROLL_INCR(idx, step, macro)
292#define LOOP_UNROLLING_97(idx, step, macro) LOOP_UNROLLING_96(idx, step, macro); UNROLL_INCR(idx, step, macro)
293#define LOOP_UNROLLING_98(idx, step, macro) LOOP_UNROLLING_97(idx, step, macro); UNROLL_INCR(idx, step, macro)
294#define LOOP_UNROLLING_99(idx, step, macro) LOOP_UNROLLING_98(idx, step, macro); UNROLL_INCR(idx, step, macro)
295#define LOOP_UNROLLING_100(idx, step, macro) LOOP_UNROLLING_99(idx, step, macro); UNROLL_INCR(idx, step, macro)
296#define LOOP_UNROLLING_101(idx, step, macro) LOOP_UNROLLING_100(idx, step, macro); UNROLL_INCR(idx, step, macro)
297#define LOOP_UNROLLING_102(idx, step, macro) LOOP_UNROLLING_101(idx, step, macro); UNROLL_INCR(idx, step, macro)
298#define LOOP_UNROLLING_103(idx, step, macro) LOOP_UNROLLING_102(idx, step, macro); UNROLL_INCR(idx, step, macro)
299#define LOOP_UNROLLING_104(idx, step, macro) LOOP_UNROLLING_103(idx, step, macro); UNROLL_INCR(idx, step, macro)
300#define LOOP_UNROLLING_105(idx, step, macro) LOOP_UNROLLING_104(idx, step, macro); UNROLL_INCR(idx, step, macro)
301#define LOOP_UNROLLING_106(idx, step, macro) LOOP_UNROLLING_105(idx, step, macro); UNROLL_INCR(idx, step, macro)
302#define LOOP_UNROLLING_107(idx, step, macro) LOOP_UNROLLING_106(idx, step, macro); UNROLL_INCR(idx, step, macro)
303#define LOOP_UNROLLING_108(idx, step, macro) LOOP_UNROLLING_107(idx, step, macro); UNROLL_INCR(idx, step, macro)
304#define LOOP_UNROLLING_109(idx, step, macro) LOOP_UNROLLING_108(idx, step, macro); UNROLL_INCR(idx, step, macro)
305#define LOOP_UNROLLING_110(idx, step, macro) LOOP_UNROLLING_109(idx, step, macro); UNROLL_INCR(idx, step, macro)
306#define LOOP_UNROLLING_111(idx, step, macro) LOOP_UNROLLING_110(idx, step, macro); UNROLL_INCR(idx, step, macro)
307#define LOOP_UNROLLING_112(idx, step, macro) LOOP_UNROLLING_111(idx, step, macro); UNROLL_INCR(idx, step, macro)
308#define LOOP_UNROLLING_113(idx, step, macro) LOOP_UNROLLING_112(idx, step, macro); UNROLL_INCR(idx, step, macro)
309#define LOOP_UNROLLING_114(idx, step, macro) LOOP_UNROLLING_113(idx, step, macro); UNROLL_INCR(idx, step, macro)
310#define LOOP_UNROLLING_115(idx, step, macro) LOOP_UNROLLING_114(idx, step, macro); UNROLL_INCR(idx, step, macro)
311#define LOOP_UNROLLING_116(idx, step, macro) LOOP_UNROLLING_115(idx, step, macro); UNROLL_INCR(idx, step, macro)
312#define LOOP_UNROLLING_117(idx, step, macro) LOOP_UNROLLING_116(idx, step, macro); UNROLL_INCR(idx, step, macro)
313#define LOOP_UNROLLING_118(idx, step, macro) LOOP_UNROLLING_117(idx, step, macro); UNROLL_INCR(idx, step, macro)
314#define LOOP_UNROLLING_119(idx, step, macro) LOOP_UNROLLING_118(idx, step, macro); UNROLL_INCR(idx, step, macro)
315#define LOOP_UNROLLING_120(idx, step, macro) LOOP_UNROLLING_119(idx, step, macro); UNROLL_INCR(idx, step, macro)
316#define LOOP_UNROLLING_121(idx, step, macro) LOOP_UNROLLING_120(idx, step, macro); UNROLL_INCR(idx, step, macro)
317#define LOOP_UNROLLING_122(idx, step, macro) LOOP_UNROLLING_121(idx, step, macro); UNROLL_INCR(idx, step, macro)
318#define LOOP_UNROLLING_123(idx, step, macro) LOOP_UNROLLING_122(idx, step, macro); UNROLL_INCR(idx, step, macro)
319#define LOOP_UNROLLING_124(idx, step, macro) LOOP_UNROLLING_123(idx, step, macro); UNROLL_INCR(idx, step, macro)
320#define LOOP_UNROLLING_125(idx, step, macro) LOOP_UNROLLING_124(idx, step, macro); UNROLL_INCR(idx, step, macro)
321#define LOOP_UNROLLING_126(idx, step, macro) LOOP_UNROLLING_125(idx, step, macro); UNROLL_INCR(idx, step, macro)
322#define LOOP_UNROLLING_127(idx, step, macro) LOOP_UNROLLING_126(idx, step, macro); UNROLL_INCR(idx, step, macro)
323#define LOOP_UNROLLING_128(idx, step, macro) LOOP_UNROLLING_127(idx, step, macro); UNROLL_INCR(idx, step, macro)
324
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100325#define LOOP_UNROLLING_STR(type, idx, start, step, num, macro) \
326 { \
327 type idx = start; \
328 LOOP_UNROLLING_##num(idx, step, macro); \
329 }
Giorgio Arenaea8d2662021-05-20 11:36:56 +0100330#else // !defined(UNROLL_WITH_PRAGMA)
331#define LOOP_UNROLLING_STR(type, idx, start, step, num, macro) \
332 { \
333 _Pragma("unroll") \
334 for(type idx = start; idx < (num * step); idx += step) \
335 { \
336 (macro); \
337 } \
338 }
339#endif // !defined(UNROLL_WITH_PRAGMA)
340#define LOOP_UNROLLING(type, idx, start, step, num, macro) LOOP_UNROLLING_STR(type, idx, start, step, num, macro)
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000341
342/** Get the get_global_id with partial N0. This function is useful when the dimension is not multiple of N0 and we need to use a partial N0
343 * to avoid out-of-bound read/write
344 *
345 * @note PARTIAL_N0 is used for get_global_id(n) = 0.
346 *
347 * @param[in] IDX get_global_id index (0,1 and 2 only)
348 * @param[in] N0 Number of elements read/written on the IDX direction
349 * @param[in] PARTIAL_N0 Number of elements read/written on the IDX direction for get_global_id(IDX) = 0. If zero,
350 * the Number of elements read/written on the IDX direction for get_global_id(IDX) = 0 is N0
351 */
352#define GET_SPATIAL_IDX(IDX, N0, PARTIAL_N0) (max((int)(get_global_id(IDX) * N0 - (N0 - PARTIAL_N0) % N0), 0))
353
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000354/** Dot product integet 8bit function
355 *
356 * @note Performs: c += dot(a, b)
357 *
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100358 * @param[in] A_DATA_TYPE A (lhs) data type
359 * @param[in] B_DATA_TYPE B (rhs) data type
360 * @param[in] C_DATA_TYPE C (accumulator) data type
361 * @param[in] K0 Number of accumulations
362 * @param[in] a OpenCL vector a
363 * @param[in] b OpenCL vector b
364 * @param[in] c Scalar variable c
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000365 */
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100366#define DOT_PRODUCT_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, b, c) DOT_PRODUCT_INTEGER8_STR(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, b, c)
367#define DOT_PRODUCT_INTEGER8_STR(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, b, c) DOT_PRODUCT##K0##_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c)
368#define DOT_PRODUCT1_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000369 ({ \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100370 c += (C_DATA_TYPE)(a) * (C_DATA_TYPE)(b); \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000371 })
Viet-Hoa Do82169b32022-05-26 16:50:21 +0100372#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_khr_integer_dot_product)
373#define DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c += dot((A_DATA_TYPE##4)((a).s01, (A_DATA_TYPE##2)(0)), (B_DATA_TYPE##4)(((b).s01), (B_DATA_TYPE##2)(0)));
374#define DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c += dot((A_DATA_TYPE##4)((a).s012, (A_DATA_TYPE)0), (B_DATA_TYPE##4)(((b).s012), (B_DATA_TYPE)0));
375#define DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c += dot((a), (b));
376#elif defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8) // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_khr_integer_dot_product)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100377#define DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c = arm_dot_acc((A_DATA_TYPE##4)((a).s01, (A_DATA_TYPE##2)(0)), (B_DATA_TYPE##4)(((b).s01), (B_DATA_TYPE##2)(0)), (c));
378#define DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c = arm_dot_acc((A_DATA_TYPE##4)((a).s012, (A_DATA_TYPE)0), (B_DATA_TYPE##4)(((b).s012), (B_DATA_TYPE)0), (c));
379#define DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c = arm_dot_acc((a), (b), (c));
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000380#elif defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Michalis Spyrouc38ca382021-07-14 13:30:28 +0100381#define DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c += arm_dot((A_DATA_TYPE##4)((a).s01, (A_DATA_TYPE##2)(0)), (B_DATA_TYPE##4)(((b).s01), (B_DATA_TYPE##2)(0)));
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100382#define DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c += arm_dot((A_DATA_TYPE##4)((a).s012, (A_DATA_TYPE)0), (B_DATA_TYPE##4)(((b).s012), (B_DATA_TYPE)0));
383#define DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c += arm_dot((a), (b));
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000384#else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100385#define DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
386 ({ \
387 c += (C_DATA_TYPE)(a).s0 * (C_DATA_TYPE)(b).s0; \
388 c += (C_DATA_TYPE)(a).s1 * (C_DATA_TYPE)(b).s1; \
389 })
390#define DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
391 ({ \
392 DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c); \
393 c += (C_DATA_TYPE)(a).s2 * (C_DATA_TYPE)(b).s2; \
394 })
395#define DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, x, y, val) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000396 ({ \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100397 val += (C_DATA_TYPE)(x).s0 * (C_DATA_TYPE)(y).s0; \
398 val += (C_DATA_TYPE)(x).s1 * (C_DATA_TYPE)(y).s1; \
399 val += (C_DATA_TYPE)(x).s2 * (C_DATA_TYPE)(y).s2; \
400 val += (C_DATA_TYPE)(x).s3 * (C_DATA_TYPE)(y).s3; \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000401 })
402#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100403#define DOT_PRODUCT5_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
404 ({ \
405 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s0123), ((b).s0123), c); \
406 DOT_PRODUCT1_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s4), ((b).s4), c); \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000407 })
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100408#define DOT_PRODUCT6_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
409 ({ \
410 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s0123), ((b).s0123), c); \
411 DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s45), ((b).s45), c); \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000412 })
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100413#define DOT_PRODUCT7_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
414 ({ \
415 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s0123), ((b).s0123), c); \
416 DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s456), ((b).s456), c); \
417 })
418#define DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
419 ({ \
420 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).lo), ((b).lo), c); \
421 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).hi), ((b).hi), c); \
422 })
423#define DOT_PRODUCT9_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
424 ({ \
425 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
426 DOT_PRODUCT1_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s8), ((b).s8), c); \
427 })
428#define DOT_PRODUCT10_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
429 ({ \
430 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
431 DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89), ((b).s89), c); \
432 })
433#define DOT_PRODUCT11_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
434 ({ \
435 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
436 DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89A), ((b).s89A), c); \
437 })
438#define DOT_PRODUCT12_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
439 ({ \
440 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
441 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89AB), ((b).s89AB), c); \
442 })
443#define DOT_PRODUCT13_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
444 ({ \
445 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
Viet-Hoa Docd2502c2023-05-11 16:13:24 +0100446 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89AB), ((b).s89AB), c); \
447 DOT_PRODUCT1_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).sC), ((b).sC), c); \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100448 })
449#define DOT_PRODUCT14_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
450 ({ \
451 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
Viet-Hoa Docd2502c2023-05-11 16:13:24 +0100452 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89AB), ((b).s89AB), c); \
453 DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).sCD), ((b).sCD), c); \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100454 })
455#define DOT_PRODUCT15_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
456 ({ \
457 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
Viet-Hoa Docd2502c2023-05-11 16:13:24 +0100458 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89AB), ((b).s89AB), c); \
459 DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).sCDE), ((b).sCDE), c); \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100460 })
461#define DOT_PRODUCT16_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
462 ({ \
463 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).lo), ((b).lo), c); \
464 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).hi), ((b).hi), c); \
465 })
466
467/** Dot product integet 8bit function
468 *
469 * @note Performs: c += dot(a, b)
470 *
471 * @param[in] A_DATA_TYPE A (lhs) data type
472 * @param[in] B_DATA_TYPE B (rhs) data type
473 * @param[in] C_DATA_TYPE C (accumulator) data type
474 * @param[in] K0 Number of accumulations
475 * @param[in] a OpenCL vector a
476 * @param[in] c Scalar variable c
477 */
478#define REDUCE_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, c) REDUCE_INTEGER8_STR(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, c)
479#define REDUCE_INTEGER8_STR(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, c) DOT_PRODUCT_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, (TILE_VECTOR_TYPE##K0(B_DATA_TYPE))1, c)
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000480
481/** Load a vector from global memory (tensor)
482 *
483 * @param[in] DATA_TYPE Data type
484 * @param[in] WIDTH Number of dst columns
485 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
486 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
487 * @param[in] TENSOR Tensor basename
488 * @param[in] X Starting X position
489 * @param[in] Y Starting Y position
490 * @param[in] STRIDE_Y Stride Y (in bytes)
491 */
492#define V_LOAD(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, Y, STRIDE_Y) V_LOAD_STR(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, Y, STRIDE_Y)
493#define V_LOAD_STR(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, Y, STRIDE_Y) V_LOAD_##TENSOR_TYPE(DATA_TYPE, WIDTH, TENSOR, X, Y, STRIDE_Y)
494#define V_LOAD_BUFFER(DATA_TYPE, WIDTH, TENSOR, X, Y, STRIDE_Y) \
495 VLOAD(WIDTH) \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100496 (0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (X) * sizeof(DATA_TYPE) + (Y) * (STRIDE_Y)))
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000497#define V_LOAD_IMAGE(DATA_TYPE, WIDTH, TENSOR, X, Y, STRIDE_Y) READ_IMAGE2D(DATA_TYPE, CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT(WIDTH), TENSOR##_img, (X) / 4, (Y))
498
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000499/** Store a vector in global memory (tensor)
500 *
501 * @param[in] DATA_TYPE Data type
502 * @param[in] WIDTH Number of dst columns
503 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
504 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
505 * @param[in] TENSOR Tensor basename
506 * @param[in] X Starting X position
507 * @param[in] Y Starting Y position
508 * @param[in] STRIDE_Y Stride Y (in bytes)
509 * @param[in] VALUES Values to store in memory
510 */
511#define V_STORE(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, Y, STRIDE_Y, VALUES) V_STORE_STR(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, Y, STRIDE_Y, VALUES)
512#define V_STORE_STR(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, Y, STRIDE_Y, VALUES) V_STORE_##TENSOR_TYPE(DATA_TYPE, WIDTH, TENSOR, X, Y, STRIDE_Y, VALUES)
513#define V_STORE_BUFFER(DATA_TYPE, WIDTH, TENSOR, X, Y, STRIDE_Y, VALUES) \
514 VSTORE(WIDTH) \
515 (VALUES, 0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (X) * sizeof(DATA_TYPE) + (Y) * (STRIDE_Y)))
516#define V_STORE_IMAGE(DATA_TYPE, WIDTH, TENSOR, X, Y, STRIDE_Y, VALUES) WRITE_IMAGE2D(DATA_TYPE, CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT(WIDTH), TENSOR##_img, (X) / 4, (Y), VALUES)
517
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000518/** Load a tile from global memory (tensor)
519 *
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100520 * @param[in] DATA_TYPE Data type
521 * @param[in] HEIGHT Number of dst rows
522 * @param[in] WIDTH Number of dst columns
523 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
524 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
525 * @param[in] TENSOR Tensor basename
526 * @param[in] X Starting X position
527 * @param[in] Y Starting Y position
528 * @param[in] YI_MULTIPLIER Parameter used to multiply the internal row increment (_i).
529 * In common cases should be 1 but it becomes useful when we want to load rows which are multiple of STRIDE_Y. (e.g. loading the weights of convolution layer).
530 * In this case the address calculation is performed as: (Y + _i * Y_MULTIPLIER) * STRIDE_Y
531 * @param[in] STRIDE_Y Stride Y (in bytes) used to load each row.
532 * @param[out] dst Output tile
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000533 */
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100534#define T_LOAD(DATA_TYPE, HEIGHT, WIDTH, TENSOR_TYPE, TENSOR, X, Y, YI_MULTIPLIER, STRIDE_Y, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100535 ({ \
536 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
537 { \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100538 dst[_i].v = V_LOAD(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, ((Y) + _i * (int)(YI_MULTIPLIER)), STRIDE_Y); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100539 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000540 })
541
Gunes Bayir9d0c4de2023-04-13 18:22:58 +0100542/** Store a VECTOR variable (e.g. int4, int8, char2 etc.) to a specified column in the TILE object
543 *
544 * @param[in] VECTOR Vector variable to store
545 * @param[in, out] TILE Tile variable to store to
546 * @param[in] WIDTH Width of the vector variable, also height of the tile (e.g. 2 if char2)
547 * @param[in] COLUMN Column index of the tile
548 */
549#define COPY_VECTOR_TO_TILE_COLUMN(VECTOR, TILE, WIDTH, COLUMN) COPY_VECTOR_TO_TILE_COLUMN_STR(VECTOR, TILE, WIDTH, COLUMN)
550#define COPY_VECTOR_TO_TILE_COLUMN_STR(VECTOR, TILE, WIDTH, COLUMN) COPY_##WIDTH##_VECTOR_TO_TILE_COLUMN(VECTOR, TILE, COLUMN)
551#define COPY_1_VECTOR_TO_TILE_COLUMN(VECTOR, TILE, COLUMN) \
552 ({ \
553 TILE[0].s[COLUMN] = VECTOR; \
554 })
555
556#define COPY_2_VECTOR_TO_TILE_COLUMN(VECTOR, TILE, COLUMN) \
557 ({ \
558 TILE[0].s[COLUMN] = VECTOR.s0; \
559 TILE[1].s[COLUMN] = VECTOR.s1; \
560 })
561
562#define COPY_3_VECTOR_TO_TILE_COLUMN(VECTOR, TILE, COLUMN) \
563 ({ \
564 TILE[0].s[COLUMN] = VECTOR.s0; \
565 TILE[1].s[COLUMN] = VECTOR.s1; \
566 TILE[2].s[COLUMN] = VECTOR.s2; \
567 })
568
569#define COPY_4_VECTOR_TO_TILE_COLUMN(VECTOR, TILE, COLUMN) \
570 ({ \
571 TILE[0].s[COLUMN] = VECTOR.s0; \
572 TILE[1].s[COLUMN] = VECTOR.s1; \
573 TILE[2].s[COLUMN] = VECTOR.s2; \
574 TILE[3].s[COLUMN] = VECTOR.s3; \
575 })
576
577#define COPY_8_VECTOR_TO_TILE_COLUMN(VECTOR, TILE, COLUMN) \
578 ({ \
579 TILE[0].s[COLUMN] = VECTOR.s0; \
580 TILE[1].s[COLUMN] = VECTOR.s1; \
581 TILE[2].s[COLUMN] = VECTOR.s2; \
582 TILE[3].s[COLUMN] = VECTOR.s3; \
583 TILE[4].s[COLUMN] = VECTOR.s4; \
584 TILE[5].s[COLUMN] = VECTOR.s5; \
585 TILE[6].s[COLUMN] = VECTOR.s6; \
586 TILE[7].s[COLUMN] = VECTOR.s7; \
587 })
588
589#define COPY_16_VECTOR_TO_TILE_COLUMN(VECTOR, TILE, COLUMN) \
590 ({ \
591 TILE[0].s[COLUMN] = VECTOR.s0; \
592 TILE[1].s[COLUMN] = VECTOR.s1; \
593 TILE[2].s[COLUMN] = VECTOR.s2; \
594 TILE[3].s[COLUMN] = VECTOR.s3; \
595 TILE[4].s[COLUMN] = VECTOR.s4; \
596 TILE[5].s[COLUMN] = VECTOR.s5; \
597 TILE[6].s[COLUMN] = VECTOR.s6; \
598 TILE[7].s[COLUMN] = VECTOR.s7; \
599 TILE[8].s[COLUMN] = VECTOR.s8; \
600 TILE[9].s[COLUMN] = VECTOR.s9; \
601 TILE[10].s[COLUMN] = VECTOR.sA; \
602 TILE[11].s[COLUMN] = VECTOR.sB; \
603 TILE[12].s[COLUMN] = VECTOR.sC; \
604 TILE[13].s[COLUMN] = VECTOR.sD; \
605 TILE[14].s[COLUMN] = VECTOR.sE; \
606 TILE[15].s[COLUMN] = VECTOR.sF; \
607 })
608
609/** Load SRC_HEIGHT x SRC_WIDTH elements from global memory (tensor), and store them in a SRC_WIDTH x SRC_HEIGHT tile
610 *
611 * @param[in] DATA_TYPE Data type
612 * @param[in] SRC_HEIGHT Number of source rows, or number of columns of the output tile
613 * @param[in] SRC_WIDTH Number of source columns, or number of tile rows
614 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
615 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
616 * @param[in] TENSOR Tensor basename
617 * @param[in] X Starting X position
618 * @param[in] Y Starting Y position
619 * @param[in] YI_MULTIPLIER Parameter used to multiply the internal row increment (_i).
620 * In common cases should be 1 but it becomes useful when we want to load rows which are multiple of STRIDE_Y.
621 * (e.g. loading the weights of convolution layer).
622 * In this case the address calculation is performed as: (Y + _i * Y_MULTIPLIER) * STRIDE_Y
623 * @param[in] STRIDE_Y Stride Y (in bytes) used to load each row.
624 * @param[out] dst Output tile
625 */
626#define T_LOAD_TRANSPOSED(DATA_TYPE, SRC_HEIGHT, SRC_WIDTH, TENSOR_TYPE, TENSOR, X, Y, YI_MULTIPLIER, STRIDE_Y, dst) \
627 ({ \
628 LOOP_UNROLLING(int, _i, 0, 1, SRC_HEIGHT, \
629 { \
630 VEC_DATA_TYPE(DATA_TYPE, SRC_WIDTH) \
631 tmp = V_LOAD(DATA_TYPE, SRC_WIDTH, TENSOR_TYPE, TENSOR, X, ((Y) + _i * (int)(YI_MULTIPLIER)), STRIDE_Y); \
632 COPY_VECTOR_TO_TILE_COLUMN(tmp, dst, SRC_WIDTH, _i); \
633 }) \
634 })
635
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000636/** Load a tile from global memory (tensor) using an indirect Y index tile
637 *
638 * @param[in] DATA_TYPE Data type
639 * @param[in] HEIGHT Number of dst rows
640 * @param[in] WIDTH Number of dst columns
641 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
642 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
643 * @param[in] TENSOR Tensor basename
644 * @param[in] X Starting X position
645 * @param[in] STRIDE_Y Stride Y (in bytes)
646 * @param[in] indirect_y Indirect Y index tile
647 * @param[out] dst Output tile
648 */
649#define T_LOAD_INDIRECT(DATA_TYPE, HEIGHT, WIDTH, TENSOR_TYPE, TENSOR, X, STRIDE_Y, indirect_y, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100650 ({ \
651 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
652 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000653 dst[_i].v = V_LOAD(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, (indirect_y[_i].v), STRIDE_Y); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100654 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000655 })
656
Adnan AlSinan3e155a52021-12-10 12:34:02 +0000657/** Load a tile from global memory (tensor) using an indirect Y index tile and conditionally use a different length for the load
658 *
659 * @note If WIDTH1_CONDITION is true, the load will use the WIDTH1 length for the store
660 * @note The vectors are stored in reverse order so the invalid rows are overwritten by the valid ones
661 *
662 * @param[in] DATA_TYPE Data type
663 * @param[in] HEIGHT Number of dst rows
664 * @param[in] WIDTH0 Store width to use if WIDTH1_CONDITION = false
665 * @param[in] WIDTH1 Store width to use if WIDTH1_CONDITION = true
666 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
667 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
668 * @param[in] TENSOR Tensor basename
669 * @param[in] X Starting X position
670 * @param[in] STRIDE_Y Stride Y (in bytes) used to load each row.
671 * @param[in] WIDTH1_CONDITION Condition to select the WIDTH1 store
672 * @param[out] dst Output tile
673 * @param[out] indirect_y Indirect Y index tile
674 */
675#define T_LOAD_INDIRECT_WIDTH_SELECT(DATA_TYPE, HEIGHT, WIDTH0, WIDTH1, TENSOR_TYPE, TENSOR, X, STRIDE_Y, WIDTH1_CONDITION, dst, indirect_y) \
676 ({ \
677 if(WIDTH1_CONDITION) \
678 { \
679 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
680 { \
681 VLOAD_PARTIAL(WIDTH0, WIDTH1) \
682 (dst[HEIGHT - 1 - _i].v, 0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (X) * sizeof(DATA_TYPE) + (indirect_y[HEIGHT - 1 - _i].v) * STRIDE_Y)); \
683 }) \
684 } \
685 else \
686 { \
687 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
688 { \
689 dst[HEIGHT - 1 - _i].v = V_LOAD(DATA_TYPE, WIDTH0, TENSOR_TYPE, TENSOR, X, (indirect_y[HEIGHT - 1 - _i].v), STRIDE_Y); \
690 }) \
691 } \
692 })
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100693/** Load a tile from global memory (tensor) when the tensor is stored using a NHWC layout
694 *
695 * @param[in] DATA_TYPE Data type
696 * @param[in] TILE_HEIGHT Number of elements to load from Y (height) dimension
697 * @param[in] TILE_WIDTH Number of elements to load from X (width) dimension
698 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
699 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
700 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
701 * @param[in] TENSOR Tensor basename
702 * @param[in] B Starting batch index
703 * @param[in] Y Starting Y index
704 * @param[in] X Starting X index
705 * @param[in] C Starting C index
706 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
707 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
708 * @param[in] STRIDE_Y Stride Y (in bytes)
709 * @param[out] dst Output tile
710 */
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100711#define T_LOAD_NHWC(DATA_TYPE, TILE_HEIGHT, TILE_WIDTH, TILE_CHANNELS, TENSOR_TYPE, TENSOR, B, Y, X, C, TENSOR_WIDTH, TENSOR_HEIGHT, STRIDE_Y, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100712 ({ \
713 LOOP_UNROLLING(int, _yk, 0, 1, TILE_HEIGHT, \
714 { \
715 LOOP_UNROLLING(int, _xk, 0, 1, TILE_WIDTH, \
716 { \
717 int _src_y = (X) + _xk + ((Y) + _yk) * (TENSOR_WIDTH); \
718 _src_y += (B) * (int)(TENSOR_WIDTH) * (int)(TENSOR_HEIGHT); \
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100719 int _src_valid_y = (((X) + _xk) >= 0 && ((X) + _xk) < (int)(TENSOR_WIDTH) && ((Y) + _yk) >= 0 && ((Y) + _yk) < (int)(TENSOR_HEIGHT)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100720 if(_src_valid_y != 0) \
721 { \
722 dst[_xk + _yk * (TILE_WIDTH)].v = V_LOAD(DATA_TYPE, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, _src_y, STRIDE_Y); \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100723 } \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100724 }) \
725 }) \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100726 })
727
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100728/** Load a tile from global memory (tensor) when the tensor is stored using a NHWC layout with dilation for the X and Y increments
729 *
730 * @param[in] DATA_TYPE Data type
731 * @param[in] TILE_HEIGHT Number of elements to load from Y (height) dimension
732 * @param[in] TILE_WIDTH Number of elements to load from X (width) dimension
733 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
734 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
735 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
736 * @param[in] TENSOR Tensor basename
737 * @param[in] B Starting batch index
738 * @param[in] Y Starting Y index
739 * @param[in] X Starting X index
740 * @param[in] C Starting C index
741 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
742 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
743 * @param[in] DILATION_X Dilation for the X increment
744 * @param[in] DILATION_Y Dilation for the Y increment
745 * @param[in] BOUNDARY_CHECK Boundary check flag. If true, it checks for any out-of-bound reads
746 * @param[out] dst Output tile
747 */
748#define T_LOAD_NHWC_WITH_DILATION(DATA_TYPE, TILE_HEIGHT, TILE_WIDTH, TILE_CHANNELS, TENSOR_TYPE, TENSOR, B, Y, X, C, TENSOR_WIDTH, TENSOR_HEIGHT, DILATION_X, DILATION_Y, BOUNDARY_CHECK, dst) \
749 ({ \
750 LOOP_UNROLLING(int, _yk, 0, 1, TILE_HEIGHT, \
751 { \
752 LOOP_UNROLLING(int, _xk, 0, 1, TILE_WIDTH, \
753 { \
754 int _src_y = (X) + _xk * (DILATION_X); \
755 int _src_z = ((Y) + _yk * (DILATION_Y)); \
756 int _src_w = (B); \
757 bool _src_valid_y = (((X) + _xk * (DILATION_X)) >= 0) && (((X) + _xk * (DILATION_X)) < (int)(TENSOR_WIDTH)) && (((Y) + _yk * (DILATION_Y)) >= 0) && (((Y) + _yk * (DILATION_Y)) < (int)(TENSOR_HEIGHT)); \
758 if(!(BOUNDARY_CHECK)) \
759 { \
760 dst[_xk + _yk * (TILE_WIDTH)].v = VLOAD(TILE_CHANNELS) \
761 (0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (C) * sizeof(DATA_TYPE) + (_src_y) * (TENSOR##_stride_y) + (_src_z) * (TENSOR##_stride_z) + (_src_w) * (TENSOR##_stride_w))); \
762 } \
763 else \
764 { \
765 if(_src_valid_y) \
766 { \
767 dst[_xk + _yk * (TILE_WIDTH)].v = VLOAD(TILE_CHANNELS) \
768 (0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (C) * sizeof(DATA_TYPE) + (_src_y) * (TENSOR##_stride_y) + (_src_z) * (TENSOR##_stride_z) + (_src_w) * (TENSOR##_stride_w))); \
769 } \
770 } \
771 }) \
772 }) \
773 })
774
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100775/** Load a tile from global memory (tensor) when the tensor is stored using a NHWC layout using indirect X and Y coordinates
776 *
777 * @param[in] DATA_TYPE Data type
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100778 * @param[in] TILE_AREA Number of elements to load from Y (height) dimension * Number of elements to load from X (width) dimension
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100779 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
780 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
781 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
782 * @param[in] TENSOR Tensor basename
783 * @param[in] B Starting batch index
784 * @param[in] Y Starting Y index
785 * @param[in] X Starting X index
786 * @param[in] C Starting C index
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100787 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100788 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100789 * @param[in] STRIDE_Y Stride Y (in bytes)
790 * @param[out] xi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect X coordinate
791 * @param[out] yi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect Y coordinate
792 * @param[out] dst Output tile
793 */
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100794#define T_LOAD_NHWC_INDIRECT(DATA_TYPE, TILE_AREA, TILE_CHANNELS, TENSOR_TYPE, TENSOR, B, Y, X, C, TENSOR_WIDTH, TENSOR_HEIGHT, STRIDE_Y, xi, yi, dst) \
795 ({ \
796 LOOP_UNROLLING(int, _i, 0, 1, TILE_AREA, \
797 { \
798 int _src_y = (X) + xi[_i].v + ((Y) + yi[_i].v) * (TENSOR_WIDTH); \
799 _src_y += (B) * (int)(TENSOR_WIDTH) * (int)(TENSOR_HEIGHT); \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100800 int _src_valid_y = (((X) + xi[_i].v) >= 0 && ((X) + xi[_i].v) < (int)(TENSOR_WIDTH) && ((Y) + yi[_i].v) >= 0 && ((Y) + yi[_i].v) < (int)(TENSOR_HEIGHT)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100801 if(_src_valid_y != 0) \
802 { \
803 dst[_i].v = V_LOAD(DATA_TYPE, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, _src_y, STRIDE_Y); \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100804 } \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100805 }) \
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100806 })
807
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000808/** Load a tile from global memory (tensor) using an indirect buffer for the Y coordinates
809 *
810 * @param[in] DATA_TYPE Data type
811 * @param[in] TILE_AREA Number of elements to load from Y (height) dimension * Number of elements to load from X (width) dimension
812 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000813 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
814 * When TENSOR_TYPE=IMAGE, the if condition for the out-of-bound check can be skipped
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000815 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
816 * @param[in] TENSOR Tensor basename
817 * @param[in] C Starting C index
818 * @param[in] STRIDE_Y Stride Y (in bytes)
819 * @param[out] yi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect Y coordinate
820 * 16 is the maximum indirect buffer size.
821 * @param[out] dst Output tile
822 */
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000823#define T_LOAD2D_INDIRECT(DATA_TYPE, TILE_AREA, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, STRIDE_Y, yi, dst) T_LOAD2D_INDIRECT_STR(DATA_TYPE, TILE_AREA, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, STRIDE_Y, yi, dst)
824#define T_LOAD2D_INDIRECT_STR(DATA_TYPE, TILE_AREA, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, STRIDE_Y, yi, dst) T_LOAD2D_INDIRECT_##TENSOR_TYPE(DATA_TYPE, TILE_AREA, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, STRIDE_Y, yi, dst)
825#define T_LOAD2D_INDIRECT_BUFFER(DATA_TYPE, TILE_AREA, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, STRIDE_Y, yi, dst) \
826 ({ \
827 LOOP_UNROLLING(int, _i, 0, 1, TILE_AREA, \
828 { \
829 if(yi[0].s[_i] >= 0) \
830 { \
831 dst[_i].v = V_LOAD(DATA_TYPE, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, yi[0].s[_i], STRIDE_Y); \
832 } \
833 }) \
834 })
835
836#define T_LOAD2D_INDIRECT_IMAGE(DATA_TYPE, TILE_AREA, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, STRIDE_Y, yi, dst) \
837 ({ \
838 LOOP_UNROLLING(int, _i, 0, 1, TILE_AREA, \
839 { \
840 dst[_i].v = V_LOAD(DATA_TYPE, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, yi[0].s[_i], STRIDE_Y); \
841 }) \
Gian Marco Iodice3394f3e2022-09-16 14:14:21 +0100842 })
843
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100844/** Load a tile from global memory (tensor) when the tensor is stored using a NDHWC layout using indirect X, Y and Z coordinates
845 *
846 * @param[in] DATA_TYPE Data type
847 * @param[in] TILE_AREA Number of elements to load from Y (height) dimension * Number of elements to load from X (width) dimension
848 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
849 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
850 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
851 * @param[in] TENSOR Tensor basename
852 * @param[in] B Starting batch index
853 * @param[in] Z Starting Z index
854 * @param[in] Y Starting Y index
855 * @param[in] X Starting X index
856 * @param[in] C Starting C index
857 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
858 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
859 * @param[in] TENSOR_DEPTH Number of elements to load from Z (depth) dimension
860 * @param[in] STRIDE_Y Stride Y (in bytes)
861 * @param[out] xi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect X coordinate
862 * @param[out] yi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect Y coordinate
863 * @param[out] zi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect Z coordinate
864 * @param[out] dst Output tile
865 */
866#define T_LOAD_NDHWC_INDIRECT(DATA_TYPE, TILE_AREA, TILE_CHANNELS, TENSOR_TYPE, TENSOR, B, Z, Y, X, C, TENSOR_WIDTH, TENSOR_HEIGHT, TENSOR_DEPTH, STRIDE_Y, xi, yi, zi, dst) \
867 ({ \
868 LOOP_UNROLLING(int, _i, 0, 1, TILE_AREA, \
869 { \
870 int _src_y = (X) + xi[_i].v + ((Y) + yi[_i].v) * (TENSOR_WIDTH) + ((Z) + zi[_i].v) * (TENSOR_WIDTH * TENSOR_HEIGHT); \
871 _src_y += (B) * (int)(TENSOR_WIDTH) * (int)(TENSOR_HEIGHT) * (int)(TENSOR_DEPTH); \
872 int _src_valid_y = (((X) + xi[_i].v) >= 0 && ((X) + xi[_i].v) < (int)(TENSOR_WIDTH) && ((Y) + yi[_i].v) >= 0 && ((Y) + yi[_i].v) < (int)(TENSOR_HEIGHT) \
873 && ((Z) + zi[_i].v) >= 0 && ((Z) + zi[_i].v) < (int)(TENSOR_DEPTH)); \
874 if(_src_valid_y != 0) \
875 { \
876 dst[_i].v = V_LOAD(DATA_TYPE, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, _src_y, STRIDE_Y); \
877 } \
878 }) \
879 })
880
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000881/** Store a tile to global memory (tensor) using an indirect Y index tile and conditionally use a different length for the store
882 *
883 * @note If WIDTH1_CONDITION is true, the store will use the WIDTH1 length for the store
884 * @note The vectors are stored in reverse order so the invalid rows are overwritten by the valid ones
885 *
886 * @param[in] DATA_TYPE Data type
887 * @param[in] HEIGHT Number of src rows
888 * @param[in] WIDTH0 Store width to use if WIDTH1_CONDITION = false
889 * @param[in] WIDTH1 Store width to use if WIDTH1_CONDITION = true
890 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
891 * cl_image is not supported.
892 * @param[in] TENSOR Tensor basename
893 * @param[in] X Starting X position
894 * @param[in] STRIDE_Y Stride Y (in bytes)
895 * @param[in] WIDTH1_CONDITION Condition to select the WIDTH1 store
896 * @param[in] src Input tile
897 * @param[in] indirect_y Indirect Y index tile
898 */
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000899#define T_STORE_INDIRECT_WIDTH_SELECT(DATA_TYPE, HEIGHT, WIDTH0, WIDTH1, TENSOR_TYPE, TENSOR, X, STRIDE_Y, WIDTH1_CONDITION, src, indirect_y) \
900 ({ \
901 if(WIDTH1_CONDITION) \
902 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100903 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000904 { \
905 VSTORE_PARTIAL(WIDTH0, WIDTH1) \
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100906 (CONVERT(src[HEIGHT - 1 - _i].v, VEC_DATA_TYPE(DATA_TYPE, WIDTH0)), 0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (X) * sizeof(DATA_TYPE) + (indirect_y[HEIGHT - 1 - _i].v) * STRIDE_Y)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100907 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000908 } \
909 else \
910 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100911 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000912 { \
913 VSTORE(WIDTH0) \
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100914 (CONVERT(src[HEIGHT - 1 - _i].v, VEC_DATA_TYPE(DATA_TYPE, WIDTH0)), 0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (X) * sizeof(DATA_TYPE) + (indirect_y[HEIGHT - 1 - _i].v) * STRIDE_Y)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100915 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000916 } \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000917 })
918
919/** Offset correction for the QASYMM8 computation
920 *
921 * @param[in] ACC_DATA_TYPE Accumulator data type
922 * @param[in] M0 Number of src/dst rows
923 * @param[in] N0 Number of src/dst columns
924 * @param[in] K0 Number of src columns
925 * @param[in] SRC_OFFSET Source quantization offset
926 * @param[in] WEI_OFFSET Weights quantization shift
927 * @param[in] lhs LHS tile
928 * @param[in] rhs RHS tile
929 * @param[out] dst DST tile
930 */
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100931#define T_OFFSET_CORRECTION(ACC_DATA_TYPE, M0, N0, K0, SRC_OFFSET, WEI_OFFSET, lhs, rhs, dst) \
932 ({ \
933 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
934 { \
935 ACC_DATA_TYPE _tm = 0; \
936 LOOP_UNROLLING(int, _k0, 0, 1, K0, \
937 { \
938 _tm += ((ACC_DATA_TYPE)lhs[_m0].s[_k0] * (ACC_DATA_TYPE)WEI_OFFSET); \
939 }) \
940 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
941 { \
942 dst[_m0].s[_n0] += _tm; \
943 LOOP_UNROLLING(int, _k0, 0, 1, K0, \
944 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000945 dst[_m0].s[_n0] += ((ACC_DATA_TYPE)rhs[_n0].s[_k0] * (ACC_DATA_TYPE)SRC_OFFSET); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100946 }) \
947 }) \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100948 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000949 })
950
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100951/** 8-bit quantization with fixed-point scale
952 *
953 * @param[in] SRC_DATA_TYPE SRC data type
954 * @param[in] DST_DATA_TYPE DST data type
955 * @param[in] QUANTIZATION_TYPE Quantization type (PER_TENSOR or PER_CHANNEL)
956 * @param[in] M0 Number of src/dst rows
957 * @param[in] N0 Number of src/dst columns
958 * @param[in] DST_OFFSET Quantization offset used for both the per-tensor and per-channel quantization
959 * @param[in] DST_SHIFT Quantization shift for the per-tensor quantization
960 * @param[in] DST_MULTIPLIER Quantization multiplier for the per-tensor quantization
961 * @param[in] src Input tile
962 * @param[in] dst_multipliers Output multipliers tile for the per-channel quantization
963 * @param[in] dst_shifts Output shift tile for the per-channel quantization
964 * @param[out] dst Output tile
965 */
966#define T_QUANTIZE8(SRC_DATA_TYPE, DST_DATA_TYPE, QUANTIZATION_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst) T_QUANTIZE8_STR(SRC_DATA_TYPE, DST_DATA_TYPE, QUANTIZATION_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst)
967#define T_QUANTIZE8_STR(SRC_DATA_TYPE, DST_DATA_TYPE, QUANTIZATION_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst) T_QUANTIZE8_##QUANTIZATION_TYPE(SRC_DATA_TYPE, DST_DATA_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst)
968
969/** 8-bit per-tensor quantization with fixed-point scale
970 *
971 * @param[in] SRC_DATA_TYPE SRC data type
972 * @param[in] DST_DATA_TYPE DST data type
973 * @param[in] M0 Number of src/dst rows
974 * @param[in] N0 Number of src/dst columns
975 * @param[in] DST_OFFSET Quantization offset
976 * @param[in] DST_SHIFT Quantization shift for the per-tensor quantization
977 * @param[in] DST_MULTIPLIER Quantization multiplier for the per-tensor quantization
978 * @param[in] src Input tile
979 * @param[in] dst_multipliers (unused)
980 * @param[in] dst_shifts (unused)
981 * @param[out] dst Output tile
982 */
983#define T_QUANTIZE8_PER_TENSOR(SRC_DATA_TYPE, DST_DATA_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst) \
984 ({ \
985 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
986 { \
987 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
988 { \
989 SRC_DATA_TYPE _tmp = 0; \
990 SRC_DATA_TYPE _src = src[_m0].s[_n0]; \
991 _src *= select((SRC_DATA_TYPE)1, ((SRC_DATA_TYPE)1 << (SRC_DATA_TYPE)(-DST_SHIFT)), ((SRC_DATA_TYPE)DST_SHIFT < (SRC_DATA_TYPE)0)); \
992 SRC_DATA_TYPE overflow = _src == DST_MULTIPLIER && _src == INT_MIN; \
993 long a_64 = (long)(_src); \
994 long b_64 = (long)(DST_MULTIPLIER); \
995 long ab_64 = a_64 * b_64; \
996 long mask1 = 1 << 30; \
997 long mask2 = 1 - (1 << 30); \
998 long is_positive_or_zero = ab_64 >= 0; \
999 long nudge = select(mask2, mask1, is_positive_or_zero); \
1000 SRC_DATA_TYPE ab_x2_high32 = CONVERT((ab_64 + nudge) / (long)(1ll << 31), SRC_DATA_TYPE); \
1001 _tmp = select(ab_x2_high32, (SRC_DATA_TYPE)INT_MAX, overflow); \
1002 if(DST_SHIFT >= 0) \
1003 { \
Freddie Liardet767dbf92021-07-21 16:20:41 +01001004 long mask = ((((int)1) << DST_SHIFT) - (long)1); \
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001005 long threshold = _tmp < (int)0 ? (mask >> 1) + (long)1 : (mask >> 1) + 0; \
1006 _tmp = (_tmp & mask) > threshold ? (_tmp >> DST_SHIFT) + (int)1 : (_tmp >> DST_SHIFT); \
1007 } \
1008 _tmp += DST_OFFSET; \
1009 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
1010 }) \
1011 }) \
1012 })
1013
1014/** 8-bit per-channel quantization with fixed-point scale
1015 *
1016 * @param[in] SRC_DATA_TYPE SRC data type
1017 * @param[in] DST_DATA_TYPE DST data type
1018 * @param[in] M0 Number of src/dst rows
1019 * @param[in] N0 Number of src/dst columns
1020 * @param[in] DST_OFFSET Quantization offset
1021 * @param[in] DST_SHIFT (unused)
1022 * @param[in] DST_MULTIPLIER (unused)
1023 * @param[in] src Input tile
1024 * @param[in] dst_multipliers Output multipliers tile for the per-channel quantization
1025 * @param[in] dst_shifts Output shift tile for the per-channel quantization
1026 * @param[out] dst Output tile
1027 */
1028#define T_QUANTIZE8_PER_CHANNEL(SRC_DATA_TYPE, DST_DATA_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst) \
1029 ({ \
1030 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1031 { \
1032 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
1033 { \
1034 SRC_DATA_TYPE _tmp = 0; \
Pablo Marquez Tello96fb1942022-11-01 09:32:20 +00001035 SRC_DATA_TYPE _tmp2 = 0; \
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001036 SRC_DATA_TYPE _src = src[_m0].s[_n0]; \
1037 SRC_DATA_TYPE _dst_multiplier = dst_multipliers[0].s[_n0]; \
1038 SRC_DATA_TYPE _dst_shift = dst_shifts[0].s[_n0]; \
1039 _src *= select((SRC_DATA_TYPE)1, ((SRC_DATA_TYPE)1 << (SRC_DATA_TYPE)(-_dst_shift)), ((SRC_DATA_TYPE)_dst_shift < (SRC_DATA_TYPE)0)); \
1040 SRC_DATA_TYPE overflow = _src == _dst_multiplier && _src == INT_MIN; \
1041 long a_64 = (long)(_src); \
1042 long b_64 = (long)(_dst_multiplier); \
1043 long ab_64 = a_64 * b_64; \
1044 long mask1 = 1 << 30; \
1045 long mask2 = 1 - (1 << 30); \
1046 long is_positive_or_zero = ab_64 >= 0; \
1047 long nudge = select(mask2, mask1, is_positive_or_zero); \
1048 SRC_DATA_TYPE ab_x2_high32 = CONVERT((ab_64 + nudge) / (long)(1ll << 31), SRC_DATA_TYPE); \
1049 _tmp = select(ab_x2_high32, (SRC_DATA_TYPE)INT_MAX, overflow); \
Pablo Marquez Tello96fb1942022-11-01 09:32:20 +00001050 long mask = ((((int)1) << _dst_shift) - (int)1); \
1051 long threshold = (mask >> 1) + any(_tmp); \
1052 _tmp2 = _tmp >> _dst_shift; \
1053 _tmp2 += select(0, 1, (_tmp & mask) > threshold); \
1054 _tmp = select(_tmp, _tmp2, _dst_shift >= 0); \
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001055 _tmp += DST_OFFSET; \
1056 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
1057 }) \
1058 }) \
1059 })
1060
1061/** Quantized the 8-bit tile with fixed-point scale for asymmetric
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001062 *
1063 * @param[in] SRC_DATA_TYPE SRC data type
1064 * @param[in] DST_DATA_TYPE DST data type
1065 * @param[in] M0 Number of src/dst rows
1066 * @param[in] N0 Number of src/dst columns
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001067 * @param[in] DST_OFFSET Quantization offset used for both the per-tensor and per-channel quantization
1068 * @param[in] DST_SHIFT Quantization shift for the per-tensor quantization
1069 * @param[in] DST_MULTIPLIER Quantization multiplier for the per-tensor quantization
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001070 * @param[in] src Input tile
1071 * @param[out] dst Output tile
1072 */
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001073#define T_QUANTIZE8_ASYMMETRIC(SRC_DATA_TYPE, DST_DATA_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst) \
1074 ({ \
1075 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1076 { \
1077 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
1078 { \
1079 SRC_DATA_TYPE _tmp = 0; \
1080 SRC_DATA_TYPE _src = src[_m0].s[_n0]; \
1081 _src *= select((SRC_DATA_TYPE)1, ((SRC_DATA_TYPE)1 << (SRC_DATA_TYPE)(-DST_SHIFT)), ((SRC_DATA_TYPE)DST_SHIFT < (SRC_DATA_TYPE)0)); \
1082 SRC_DATA_TYPE overflow = _src == DST_MULTIPLIER && _src == INT_MIN; \
1083 long a_64 = (long)(_src); \
1084 long b_64 = (long)(DST_MULTIPLIER); \
1085 long ab_64 = a_64 * b_64; \
1086 long mask1 = 1 << 30; \
1087 long mask2 = 1 - (1 << 30); \
1088 long is_positive_or_zero = ab_64 >= 0; \
1089 long nudge = select(mask2, mask1, is_positive_or_zero); \
1090 SRC_DATA_TYPE ab_x2_high32 = CONVERT((ab_64 + nudge) / (long)(1ll << 31), SRC_DATA_TYPE); \
1091 _tmp = select(ab_x2_high32, (SRC_DATA_TYPE)INT_MAX, overflow); \
1092 if(DST_SHIFT >= 0) \
1093 { \
1094 long mask = ((((int)1) << DST_SHIFT) - (int)1); \
1095 long threshold = _tmp < (int)0 ? (mask >> 1) + (long)1 : (mask >> 1) + 0; \
1096 _tmp = (_tmp & mask) > threshold ? (_tmp >> DST_SHIFT) + (int)1 : (_tmp >> DST_SHIFT); \
1097 } \
1098 _tmp += DST_OFFSET; \
1099 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
1100 }) \
1101 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001102 })
1103
1104/** Conditional rowset (memset by row)
1105 *
1106 * @note Set the row to VALUE_TO_SET if the corresponding mask == 0
1107 *
1108 * @param[in] DATA_TYPE Data type
1109 * @param[in] M0 Number of LHS rows
1110 * @param[in] N0 Number of LHS columns
1111 * @param[in] VALUE_TO_SET Value to set the row
1112 * @param[in, out] a Input/output tile
1113 * @param[out] mask Mask to check for setting the row to VALUE_TO_SET
1114 */
1115#define T_ROWSET_MASK(DATA_TYPE, M0, N0, VALUE_TO_SET, a, mask) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001116 ({ \
1117 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1118 { \
1119 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
1120 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001121 a[_m0].s[_n0] = select((DATA_TYPE)(a[_m0].s[_n0]), (DATA_TYPE)(VALUE_TO_SET), (SELECT_DATA_TYPE(DATA_TYPE))(mask[_m0].v == (DATA_TYPE)0)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001122 }) \
1123 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001124 })
1125
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001126/** Element-wise activation for floating point types
Gian Marco Iodicea8903c82021-03-24 14:48:22 +00001127 *
1128 * @note Performs: activation(LHS) = DST
1129 *
1130 * @param[in] DATA_TYPE SRC/DST data type
1131 * @param[in] M0 Number of SRC/DST rows
1132 * @param[in] N0 Number of SRC/DST columns
1133 * @param[in] ACTIVATION_TYPE Activation type
1134 * @param[in] A_VAL A value used for the activation (e.g. tanh_op, brelu,..)
1135 * @param[in] B_VAL B value used for the activation (e.g. tanh_op, brelu,..)
1136 * @param[out] src SRC tile
1137 * @param[out] dst DST tile
1138 */
1139#define T_ACTIVATION(DATA_TYPE, M0, N0, ACTIVATION_TYPE, A_VAL, B_VAL, src, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001140 ({ \
1141 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1142 { \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +00001143 dst[_m0].v = ACTIVATION(ACTIVATION_TYPE, DATA_TYPE, N0, src[_m0].v, A_VAL, B_VAL); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001144 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +00001145 })
1146
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001147
Mohammed Suhail Munshic9eeee52023-06-30 15:43:29 +01001148// NOTE : A_VAL and B_VAL should be quantized values (using same quantization info as x)
1149// RELU Activation
1150#define relu_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_POINT, A_VAL, B_VAL, x) (max((DATA_TYPE)ZERO_POINT, x))
1151// Bounded RELU Activation
1152#define brelu_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_POINT, A_VAL, B_VAL, x) (min((DATA_TYPE)A_VAL, max((DATA_TYPE)ZERO_POINT, x)))
1153// Lower Upper Bounded RELU Activation
1154#define lu_brelu_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_POINT, A_VAL, B_VAL, x) (min(max(x, (DATA_TYPE)B_VAL), (DATA_TYPE)A_VAL))
1155// Hard Swish Activation
1156#define hard_swish_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_POINT, A_VAL, B_VAL, x) (x * ((min(max((DATA_TYPE)(x + (DATA_TYPE)3.f), (DATA_TYPE)0.f), (DATA_TYPE)6.f)) * (DATA_TYPE)0.166666667f))
1157// Identity Activation
1158#define identity_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_POINT, A_VAL, B_VAL, x) (x)
1159
1160#define ACT_OP_QUANTIZED(op, DATA_TYPE, VEC_SIZE, ZERO_POINT, A_VAL, B_VAL, x) op##_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_POINT, A_VAL, B_VAL, x)
1161#define ACTIVATION_QUANTIZED(op, DATA_TYPE, VEC_SIZE, ZERO_POINT, A_VAL, B_VAL, x) ACT_OP_QUANTIZED(op, DATA_TYPE, VEC_SIZE, ZERO_POINT, A_VAL, B_VAL, x)
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001162
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00001163#define V_ADD(A_VAL, B_VAL) ((A_VAL) + (B_VAL))
Ramy Elgammalec320d92022-12-14 09:20:09 +00001164#define V_SUB(A_VAL, B_VAL) ((A_VAL) - (B_VAL))
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00001165#define V_DIV(A_VAL, B_VAL) ((A_VAL) / (B_VAL))
Jakub Sujak7359a872023-01-05 14:24:13 +00001166#define V_MUL(A_VAL, B_VAL) ((A_VAL) * (B_VAL))
Michalis Spyroub1fcefd2022-06-15 19:02:28 +01001167
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001168/** Element-wise activation for quantized types
1169 *
1170 * @note Performs: activation(LHS) = DST
1171 *
1172 * @param[in] DATA_TYPE SRC/DST data type
1173 * @param[in] M0 Number of SRC/DST rows
1174 * @param[in] N0 Number of SRC/DST columns
1175 * @param[in] ACTIVATION_TYPE Activation type
Mohammed Suhail Munshic9eeee52023-06-30 15:43:29 +01001176 * @param[in] ZERO_POINT The zero value to consider in the computation
1177 * @param[in] A_VAL Quantized A value used for the activation (e.g. tanh_op, brelu,..)
1178 * @param[in] B_VAL Quantized B value used for the activation (e.g. tanh_op, brelu,..)
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001179 * @param[out] src SRC tile
1180 * @param[out] dst DST tile
1181 */
Mohammed Suhail Munshic9eeee52023-06-30 15:43:29 +01001182#define T_ACTIVATION_QUANTIZED(DATA_TYPE, M0, N0, ACTIVATION_TYPE, ZERO_POINT, A_VAL, B_VAL, src, dst) \
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001183 ({ \
1184 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1185 { \
Mohammed Suhail Munshic9eeee52023-06-30 15:43:29 +01001186 dst[_m0].v = ACTIVATION_QUANTIZED(ACTIVATION_TYPE, DATA_TYPE, N0, ZERO_POINT, A_VAL, B_VAL, src[_m0].v); \
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001187 }) \
1188 })
1189
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00001190/** Element-wise addition between two tiles
1191 *
1192 * @note Performs: LHS + RHS = DST
1193 *
1194 * @param[in] DATA_TYPE LHS/RHS/DST data type
1195 * @param[in] M0 Number of LHS rows
1196 * @param[in] N0 Number of LHS columns
1197 * @param[in] lhs LHS tile
1198 * @param[in] rhs Constant RHS tile
1199 * @param[out] dst DST tile
1200 */
1201#define T_ADD(DATA_TYPE, M0, N0, lhs, rhs, dst) \
1202 ({ \
1203 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1204 { \
1205 dst[_m0].v = lhs[_m0].v + rhs[_m0].v; \
1206 }) \
1207 })
1208
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001209/** Element-wise addition with a constant value
1210 *
1211 * @note Performs: LHS + constant = DST
1212 *
1213 * @param[in] DATA_TYPE LHS/RHS/DST data type
1214 * @param[in] M0 Number of LHS rows
1215 * @param[in] N0 Number of LHS columns
1216 * @param[in] lhs LHS tile
1217 * @param[in] rhs_constant Constant value
1218 * @param[out] dst DST tile
1219 */
1220#define T_ADD_CONSTANT(DATA_TYPE, M0, N0, lhs, rhs_constant, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001221 ({ \
1222 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1223 { \
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00001224 dst[_m0].v = lhs[_m0].v + (DATA_TYPE)rhs_constant; \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001225 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001226 })
1227
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00001228#define T_ELTWISE_BROADCAST_ADD_X(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE_BROADCAST_X(V_ADD, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +00001229#define T_ELTWISE_BROADCAST_LHS_X_ADD(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE_BROADCAST_LHS_X(V_ADD, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
1230#define T_ELTWISE_BROADCAST_RHS_X_ADD(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE_BROADCAST_X(V_ADD, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
1231
Ramy Elgammalec320d92022-12-14 09:20:09 +00001232#define T_ELTWISE_BROADCAST_LHS_X_SUB(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE_BROADCAST_LHS_X(V_SUB, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
1233#define T_ELTWISE_BROADCAST_RHS_X_SUB(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE_BROADCAST_X(V_SUB, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
1234
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00001235#define T_ELTWISE_BROADCAST_DIV_X(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE_BROADCAST_X(V_DIV, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
1236
Jakub Sujak7359a872023-01-05 14:24:13 +00001237#define T_ELTWISE_BROADCAST_LHS_X_MUL(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE_BROADCAST_LHS_X(V_MUL, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
1238#define T_ELTWISE_BROADCAST_RHS_X_MUL(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE_BROADCAST_X(V_MUL, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
1239
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00001240/** Element-wise scale with a constant value
1241 *
1242 * @note Performs: LHS * constant = DST
1243 *
1244 * @param[in] DATA_TYPE LHS/RHS/DST data type
1245 * @param[in] M0 Number of LHS rows
1246 * @param[in] N0 Number of LHS columns
1247 * @param[in] lhs LHS tile
1248 * @param[in] rhs_constant Constant value
1249 * @param[out] dst DST tile
1250 */
1251#define T_SCALE_CONSTANT(DATA_TYPE, M0, N0, lhs, rhs_constant, dst) \
1252 ({ \
1253 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1254 { \
1255 dst[_m0].v = lhs[_m0].v * (DATA_TYPE)rhs_constant; \
1256 }) \
1257 })
Michalis Spyroub1fcefd2022-06-15 19:02:28 +01001258
1259/** Element-wise operation with RHS broadcasted (RHS has the X dimension only)
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001260 *
Michalis Spyroub1fcefd2022-06-15 19:02:28 +01001261 * @note Performs: LHS OP RHS[broadcasted] = DST
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001262 * @note Both tiles must have same data type
1263 *
Michalis Spyroub1fcefd2022-06-15 19:02:28 +01001264 * @param[in] T_ELWISE_OP Elementwise operator to perform
Giorgio Arena945ae9e2021-10-13 11:13:04 +01001265 * @param[in] DST_DATA_TYPE DST data type
1266 * @param[in] M0 Number of LHS rows
1267 * @param[in] N0 Number of LHS columns
1268 * @param[in] lhs LHS tile
1269 * @param[in] rhs RHS tile
1270 * @param[out] dst DST tile
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001271 */
Michalis Spyroub1fcefd2022-06-15 19:02:28 +01001272#define T_ELTWISE_BROADCAST_X(T_ELWISE_OP, DST_DATA_TYPE, M0, N0, lhs, rhs, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001273 ({ \
1274 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1275 { \
Michalis Spyroub1fcefd2022-06-15 19:02:28 +01001276 dst[_m0].v = T_ELWISE_OP(CONVERT(lhs[_m0].v, VEC_DATA_TYPE(DST_DATA_TYPE, N0)), CONVERT(rhs[0].v, VEC_DATA_TYPE(DST_DATA_TYPE, N0))); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001277 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001278 })
1279
Viet-Hoa Dob3077fb2023-01-03 17:59:14 +00001280/** Element-wise operation with LHS broadcasted (LHS has the X dimension only)
1281 *
1282 * @note Performs: LHS[broadcasted] OP RHS = DST
1283 * @note Both tiles must have same data type
1284 *
1285 * @param[in] T_ELWISE_OP Elementwise operator to perform
1286 * @param[in] DST_DATA_TYPE DST data type
1287 * @param[in] M0 Number of RHS rows
1288 * @param[in] N0 Number of RHS columns
1289 * @param[in] lhs LHS tile
1290 * @param[in] rhs RHS tile
1291 * @param[out] dst DST tile
1292 */
1293#define T_ELTWISE_BROADCAST_LHS_X(T_ELWISE_OP, DST_DATA_TYPE, M0, N0, lhs, rhs, dst) \
1294 ({ \
1295 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1296 { \
1297 dst[_m0].v = T_ELWISE_OP(CONVERT(lhs[0].v, VEC_DATA_TYPE(DST_DATA_TYPE, N0)), CONVERT(rhs[_m0].v, VEC_DATA_TYPE(DST_DATA_TYPE, N0))); \
1298 }) \
1299 })
1300
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00001301#define T_ELTWISE_ADD(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE(V_ADD, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
Ramy Elgammalec320d92022-12-14 09:20:09 +00001302#define T_ELTWISE_SUB(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE(V_SUB, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00001303#define T_ELTWISE_DIV(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE(V_DIV, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
Jakub Sujak7359a872023-01-05 14:24:13 +00001304#define T_ELTWISE_MUL(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) T_ELTWISE(V_MUL, DST_DATA_TYPE, M0, N0, lhs, rhs, dst)
Michalis Spyroub1fcefd2022-06-15 19:02:28 +01001305
1306/** Element-wise operation between two tiles (LHS and RHS)
Michalis Spyrou06adbc52022-05-06 17:06:21 +01001307 *
Michalis Spyroub1fcefd2022-06-15 19:02:28 +01001308 * @note Performs: LHS OP RHS = DST
Michalis Spyrou06adbc52022-05-06 17:06:21 +01001309 * @note Both tiles must have same data type
1310 *
Michalis Spyroub1fcefd2022-06-15 19:02:28 +01001311 * @param[in] T_ELWISE_OP Elementwise operator to perform
Michalis Spyrou06adbc52022-05-06 17:06:21 +01001312 * @param[in] DST_DATA_TYPE DST data type
1313 * @param[in] M0 Number of LHS rows
1314 * @param[in] N0 Number of LHS columns
1315 * @param[in] lhs LHS tile
1316 * @param[in] rhs RHS tile
1317 * @param[out] dst DST tile
1318 */
Michalis Spyroub1fcefd2022-06-15 19:02:28 +01001319#define T_ELTWISE(T_ELWISE_OP, DST_DATA_TYPE, M0, N0, lhs, rhs, dst) \
Michalis Spyrou06adbc52022-05-06 17:06:21 +01001320 ({ \
1321 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1322 { \
Michalis Spyroub1fcefd2022-06-15 19:02:28 +01001323 dst[_m0].v = T_ELWISE_OP(CONVERT(lhs[_m0].v, VEC_DATA_TYPE(DST_DATA_TYPE, N0)), CONVERT(rhs[_m0].v, VEC_DATA_TYPE(DST_DATA_TYPE, N0))); \
1324 }) \
1325 })
1326
1327/** Floor operation on a tile
1328 *
1329 * @note Performs: floor(SRC) = DST
1330 * @note Both tiles must have same data type
1331 *
1332 * @param[in] DST_DATA_TYPE DST data type
1333 * @param[in] M0 Number of SRC rows
1334 * @param[in] N0 Number of SRC columns
1335 * @param[in] src LHS tile
1336 * @param[out] dst DST tile
1337 */
1338#define T_FLOOR(DST_DATA_TYPE, M0, N0, src, dst) \
1339 ({ \
1340 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1341 { \
1342 dst[_m0].v = floor(CONVERT(src[_m0].v, VEC_DATA_TYPE(DST_DATA_TYPE, N0))); \
Michalis Spyrou06adbc52022-05-06 17:06:21 +01001343 }) \
1344 })
1345
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001346/** Matrix multiplication
1347 *
1348 * @note Performs: LHS X RHS + DST = DST
1349 *
1350 * @param[in] LHS_DATA_TYPE LHS tile data type
1351 * @param[in] RHS_DATA_TYPE RHS tile data type
1352 * @param[in] DST_DATA_TYPE RHS tile data type
1353 * @param[in] M0 Number of LHS rows
1354 * @param[in] N0 Number of RHS columns
1355 * @param[in] K0 Number of LHS columns
1356 * @param[in] LHS_LAYOUT LHS layout (T= transposed, NT= not transposed)
1357 * @param[in] RHS_LAYOUT RHS layout (T= transposed, NT= not transposed)
1358 * @param[in] lhs LHS tile
1359 * @param[in] rhs RHS tile
1360 * @param[in, out] dst DST tile
Gunes Bayir9d0c4de2023-04-13 18:22:58 +01001361 *
1362 * @note For Int8/UInt8 multiplications, we only have T_MMUL_NT_T because we need
1363 * the multiply the rows of Lhs and Rhs tensors to utilize dot product extension.
1364 * Addition of other versions requires dealing with on the fly transposition of
1365 * these tile elements and therefore is not favored.
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001366 */
1367#define T_MMUL(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, LHS_LAYOUT, RHS_LAYOUT, lhs, rhs, dst) T_MMUL_##LHS_LAYOUT##_##RHS_LAYOUT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001368#define T_MMUL_NT_T(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_##LHS_DATA_TYPE##_##RHS_DATA_TYPE##_##DST_DATA_TYPE(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1369#define T_MMUL_NT_T_float_float_float(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
Giorgio Arena945ae9e2021-10-13 11:13:04 +01001370#define T_MMUL_NT_T_half_half_float(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001371#define T_MMUL_NT_T_half_half_half(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1372#define T_MMUL_NT_T_char_char_int(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1373#define T_MMUL_NT_T_uchar_uchar_uint(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1374#define T_MMUL_NT_T_uchar_uchar_int(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1375#define T_MMUL_NT_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001376 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001377 LOOP_UNROLLING(int, _m, 0, 1, M0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001378 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001379 LOOP_UNROLLING(int, _n, 0, 1, N0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001380 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001381 LOOP_UNROLLING(int, _k, 0, 1, K0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001382 { \
Giorgio Arena945ae9e2021-10-13 11:13:04 +01001383 dst[_m].s[_n] = fma((DST_DATA_TYPE)(lhs[_m].s[_k]), (DST_DATA_TYPE)(rhs[_n].s[_k]), dst[_m].s[_n]); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001384 }) \
1385 }) \
1386 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001387 }
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001388
Ramy Elgammal2b6ebfe2023-03-09 21:15:37 +00001389#define T_MMUL_NT_NT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_NT_##LHS_DATA_TYPE##_##RHS_DATA_TYPE##_##DST_DATA_TYPE(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1390#define T_MMUL_NT_NT_float_float_float(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_NT_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1391#define T_MMUL_NT_NT_half_half_float(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_NT_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1392#define T_MMUL_NT_NT_half_half_half(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_NT_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1393#define T_MMUL_NT_NT_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) \
1394 { \
1395 LOOP_UNROLLING(int, _m, 0, 1, M0, \
1396 { \
1397 LOOP_UNROLLING(int, _k, 0, 1, K0, \
1398 { \
1399 dst[_m].v = fma((DST_DATA_TYPE)(lhs[_m].s[_k]), (rhs[_k].v), dst[_m].v); \
1400 }) \
1401 }) \
1402 }
1403
Gunes Bayir8918b232023-03-17 13:52:21 +00001404#define T_MMUL_T_NT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_T_NT_##LHS_DATA_TYPE##_##RHS_DATA_TYPE##_##DST_DATA_TYPE(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1405#define T_MMUL_T_NT_float_float_float(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_T_NT_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1406#define T_MMUL_T_NT_half_half_float(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_T_NT_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1407#define T_MMUL_T_NT_half_half_half(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_T_NT_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1408#define T_MMUL_T_NT_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) \
1409 { \
1410 LOOP_UNROLLING(int, _m, 0, 1, M0, \
1411 { \
1412 LOOP_UNROLLING(int, _n, 0, 1, N0, \
1413 { \
1414 LOOP_UNROLLING(int, _k, 0, 1, K0, \
1415 { \
1416 dst[_m].s[_n] = fma((DST_DATA_TYPE)(lhs[_k].s[_m]), (DST_DATA_TYPE)(rhs[_k].s[_n]), dst[_m].s[_n]); \
1417 }) \
1418 }) \
1419 }) \
1420 }
1421
1422#define T_MMUL_T_T(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_T_T_##LHS_DATA_TYPE##_##RHS_DATA_TYPE##_##DST_DATA_TYPE(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1423#define T_MMUL_T_T_float_float_float(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_T_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1424#define T_MMUL_T_T_half_half_float(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_T_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1425#define T_MMUL_T_T_half_half_half(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_T_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1426#define T_MMUL_T_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) \
1427 { \
1428 LOOP_UNROLLING(int, _m, 0, 1, M0, \
1429 { \
1430 LOOP_UNROLLING(int, _n, 0, 1, N0, \
1431 { \
1432 LOOP_UNROLLING(int, _k, 0, 1, K0, \
1433 { \
1434 dst[_m].s[_n] = fma((DST_DATA_TYPE)(lhs[_k].s[_m]), (DST_DATA_TYPE)(rhs[_n].s[_k]), dst[_m].s[_n]); \
1435 }) \
1436 }) \
1437 }) \
1438 }
1439
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001440#define T_MMUL_NT_T_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) \
1441 ({ \
1442 LOOP_UNROLLING(int, _m, 0, 1, M0, \
1443 { \
1444 LOOP_UNROLLING(int, _n, 0, 1, N0, \
1445 { \
1446 DOT_PRODUCT_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, K0, (lhs[_m].v), (rhs[_n].v), dst[_m].s[_n]); \
1447 }) \
1448 }) \
Gian Marco Iodice561c1762021-04-16 15:08:59 +01001449 })
SiCong Lica364df2022-04-13 15:48:19 +01001450
Ramy Elgammal2b6ebfe2023-03-09 21:15:37 +00001451#endif /* ACL_SRC_CORE_CL_CL_KERNELS_TILE_HELPERS */