blob: 7910b4ce0e6aed77f5ecfd325cdd0b40bfc29ab2 [file] [log] [blame]
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001/*
2 * Copyright (c) 2021 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
Giorgio Arenabdd16d12021-05-13 16:58:51 +010025// *INDENT-OFF*
26// clang-format off
27
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000028/** Tile object
29 * A tile object is a 2D memory block and can be accessed using the following syntax:
30 * -# a[m0].v = access the the vector at row "m0" (OpenCL vector)
31 * -# a[m0].s[x] = access the scalar element at row "m0" and column "n0" (scalar access)
32 *
33 * @param[in] DATA_TYPE Data type of the tile
34 * @param[in] H Number of tile rows
35 * @param[in] W Number of tile colums
36 * @param[in] BASENAME Tile's name
37 */
38#define TILE(DATA_TYPE, H, W, BASENAME) TILE_STR(DATA_TYPE, H, W, BASENAME)
39#define TILE_STR(DATA_TYPE, H, W, BASENAME) \
40 union { \
Gian Marco Iodicec63b7222021-06-30 08:39:44 +000041 DATA_TYPE s[W]; \
42 DATA_TYPE##W v; \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000043 } BASENAME[H]
44
Giorgio Arenabdd16d12021-05-13 16:58:51 +010045#define TENSOR4D_IMAGE(name) \
46 __read_only image2d_t name##_img, \
47 __global uchar *name##_ptr, \
48 uint name##_stride_x, \
49 uint name##_step_x, \
50 uint name##_stride_y, \
51 uint name##_step_y, \
52 uint name##_stride_z, \
53 uint name##_step_z, \
54 uint name##_stride_w, \
55 uint name##_step_w, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000056 uint name##_offset_first_element_in_bytes
57
Giorgio Arenabdd16d12021-05-13 16:58:51 +010058#define TENSOR4D_BUFFER(name) \
59 __global uchar *name##_ptr, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000060 uint name##_stride_x, \
61 uint name##_step_x, \
62 uint name##_stride_y, \
63 uint name##_step_y, \
64 uint name##_stride_z, \
65 uint name##_step_z, \
66 uint name##_stride_w, \
67 uint name##_step_w, \
68 uint name##_offset_first_element_in_bytes
69
70#define TENSOR4D_STR(name, type) TENSOR4D_##type(name)
71#define TENSOR4D(name, type) TENSOR4D_STR(name, type)
72
Giorgio Arenaea8d2662021-05-20 11:36:56 +010073#if !defined(UNROLL_WITH_PRAGMA)
Giorgio Arenabdd16d12021-05-13 16:58:51 +010074#define UNROLL_INCR(idx, step, macro) idx += (step); (macro)
75
76#define LOOP_UNROLLING_1(idx, step, macro) (macro)
77#define LOOP_UNROLLING_2(idx, step, macro) LOOP_UNROLLING_1(idx, step, macro); UNROLL_INCR(idx, step, macro)
78#define LOOP_UNROLLING_3(idx, step, macro) LOOP_UNROLLING_2(idx, step, macro); UNROLL_INCR(idx, step, macro)
79#define LOOP_UNROLLING_4(idx, step, macro) LOOP_UNROLLING_3(idx, step, macro); UNROLL_INCR(idx, step, macro)
80#define LOOP_UNROLLING_5(idx, step, macro) LOOP_UNROLLING_4(idx, step, macro); UNROLL_INCR(idx, step, macro)
81#define LOOP_UNROLLING_6(idx, step, macro) LOOP_UNROLLING_5(idx, step, macro); UNROLL_INCR(idx, step, macro)
82#define LOOP_UNROLLING_7(idx, step, macro) LOOP_UNROLLING_6(idx, step, macro); UNROLL_INCR(idx, step, macro)
83#define LOOP_UNROLLING_8(idx, step, macro) LOOP_UNROLLING_7(idx, step, macro); UNROLL_INCR(idx, step, macro)
84#define LOOP_UNROLLING_9(idx, step, macro) LOOP_UNROLLING_8(idx, step, macro); UNROLL_INCR(idx, step, macro)
85#define LOOP_UNROLLING_10(idx, step, macro) LOOP_UNROLLING_9(idx, step, macro); UNROLL_INCR(idx, step, macro)
86#define LOOP_UNROLLING_11(idx, step, macro) LOOP_UNROLLING_10(idx, step, macro); UNROLL_INCR(idx, step, macro)
87#define LOOP_UNROLLING_12(idx, step, macro) LOOP_UNROLLING_11(idx, step, macro); UNROLL_INCR(idx, step, macro)
88#define LOOP_UNROLLING_13(idx, step, macro) LOOP_UNROLLING_12(idx, step, macro); UNROLL_INCR(idx, step, macro)
89#define LOOP_UNROLLING_14(idx, step, macro) LOOP_UNROLLING_13(idx, step, macro); UNROLL_INCR(idx, step, macro)
90#define LOOP_UNROLLING_15(idx, step, macro) LOOP_UNROLLING_14(idx, step, macro); UNROLL_INCR(idx, step, macro)
91#define LOOP_UNROLLING_16(idx, step, macro) LOOP_UNROLLING_15(idx, step, macro); UNROLL_INCR(idx, step, macro)
92#define LOOP_UNROLLING_17(idx, step, macro) LOOP_UNROLLING_16(idx, step, macro); UNROLL_INCR(idx, step, macro)
93#define LOOP_UNROLLING_18(idx, step, macro) LOOP_UNROLLING_17(idx, step, macro); UNROLL_INCR(idx, step, macro)
94#define LOOP_UNROLLING_19(idx, step, macro) LOOP_UNROLLING_18(idx, step, macro); UNROLL_INCR(idx, step, macro)
95#define LOOP_UNROLLING_20(idx, step, macro) LOOP_UNROLLING_19(idx, step, macro); UNROLL_INCR(idx, step, macro)
96#define LOOP_UNROLLING_21(idx, step, macro) LOOP_UNROLLING_20(idx, step, macro); UNROLL_INCR(idx, step, macro)
97#define LOOP_UNROLLING_22(idx, step, macro) LOOP_UNROLLING_21(idx, step, macro); UNROLL_INCR(idx, step, macro)
98#define LOOP_UNROLLING_23(idx, step, macro) LOOP_UNROLLING_22(idx, step, macro); UNROLL_INCR(idx, step, macro)
99#define LOOP_UNROLLING_24(idx, step, macro) LOOP_UNROLLING_23(idx, step, macro); UNROLL_INCR(idx, step, macro)
100#define LOOP_UNROLLING_25(idx, step, macro) LOOP_UNROLLING_24(idx, step, macro); UNROLL_INCR(idx, step, macro)
101#define LOOP_UNROLLING_26(idx, step, macro) LOOP_UNROLLING_25(idx, step, macro); UNROLL_INCR(idx, step, macro)
102#define LOOP_UNROLLING_27(idx, step, macro) LOOP_UNROLLING_26(idx, step, macro); UNROLL_INCR(idx, step, macro)
103#define LOOP_UNROLLING_28(idx, step, macro) LOOP_UNROLLING_27(idx, step, macro); UNROLL_INCR(idx, step, macro)
104#define LOOP_UNROLLING_29(idx, step, macro) LOOP_UNROLLING_28(idx, step, macro); UNROLL_INCR(idx, step, macro)
105#define LOOP_UNROLLING_30(idx, step, macro) LOOP_UNROLLING_29(idx, step, macro); UNROLL_INCR(idx, step, macro)
106#define LOOP_UNROLLING_31(idx, step, macro) LOOP_UNROLLING_30(idx, step, macro); UNROLL_INCR(idx, step, macro)
107#define LOOP_UNROLLING_32(idx, step, macro) LOOP_UNROLLING_31(idx, step, macro); UNROLL_INCR(idx, step, macro)
108#define LOOP_UNROLLING_33(idx, step, macro) LOOP_UNROLLING_32(idx, step, macro); UNROLL_INCR(idx, step, macro)
109#define LOOP_UNROLLING_34(idx, step, macro) LOOP_UNROLLING_33(idx, step, macro); UNROLL_INCR(idx, step, macro)
110#define LOOP_UNROLLING_35(idx, step, macro) LOOP_UNROLLING_34(idx, step, macro); UNROLL_INCR(idx, step, macro)
111#define LOOP_UNROLLING_36(idx, step, macro) LOOP_UNROLLING_35(idx, step, macro); UNROLL_INCR(idx, step, macro)
112#define LOOP_UNROLLING_37(idx, step, macro) LOOP_UNROLLING_36(idx, step, macro); UNROLL_INCR(idx, step, macro)
113#define LOOP_UNROLLING_38(idx, step, macro) LOOP_UNROLLING_37(idx, step, macro); UNROLL_INCR(idx, step, macro)
114#define LOOP_UNROLLING_39(idx, step, macro) LOOP_UNROLLING_38(idx, step, macro); UNROLL_INCR(idx, step, macro)
115#define LOOP_UNROLLING_40(idx, step, macro) LOOP_UNROLLING_39(idx, step, macro); UNROLL_INCR(idx, step, macro)
116#define LOOP_UNROLLING_41(idx, step, macro) LOOP_UNROLLING_40(idx, step, macro); UNROLL_INCR(idx, step, macro)
117#define LOOP_UNROLLING_42(idx, step, macro) LOOP_UNROLLING_41(idx, step, macro); UNROLL_INCR(idx, step, macro)
118#define LOOP_UNROLLING_43(idx, step, macro) LOOP_UNROLLING_42(idx, step, macro); UNROLL_INCR(idx, step, macro)
119#define LOOP_UNROLLING_44(idx, step, macro) LOOP_UNROLLING_43(idx, step, macro); UNROLL_INCR(idx, step, macro)
120#define LOOP_UNROLLING_45(idx, step, macro) LOOP_UNROLLING_44(idx, step, macro); UNROLL_INCR(idx, step, macro)
121#define LOOP_UNROLLING_46(idx, step, macro) LOOP_UNROLLING_45(idx, step, macro); UNROLL_INCR(idx, step, macro)
122#define LOOP_UNROLLING_47(idx, step, macro) LOOP_UNROLLING_46(idx, step, macro); UNROLL_INCR(idx, step, macro)
123#define LOOP_UNROLLING_48(idx, step, macro) LOOP_UNROLLING_47(idx, step, macro); UNROLL_INCR(idx, step, macro)
124#define LOOP_UNROLLING_49(idx, step, macro) LOOP_UNROLLING_48(idx, step, macro); UNROLL_INCR(idx, step, macro)
125#define LOOP_UNROLLING_50(idx, step, macro) LOOP_UNROLLING_49(idx, step, macro); UNROLL_INCR(idx, step, macro)
126#define LOOP_UNROLLING_51(idx, step, macro) LOOP_UNROLLING_50(idx, step, macro); UNROLL_INCR(idx, step, macro)
127#define LOOP_UNROLLING_52(idx, step, macro) LOOP_UNROLLING_51(idx, step, macro); UNROLL_INCR(idx, step, macro)
128#define LOOP_UNROLLING_53(idx, step, macro) LOOP_UNROLLING_52(idx, step, macro); UNROLL_INCR(idx, step, macro)
129#define LOOP_UNROLLING_54(idx, step, macro) LOOP_UNROLLING_53(idx, step, macro); UNROLL_INCR(idx, step, macro)
130#define LOOP_UNROLLING_55(idx, step, macro) LOOP_UNROLLING_54(idx, step, macro); UNROLL_INCR(idx, step, macro)
131#define LOOP_UNROLLING_56(idx, step, macro) LOOP_UNROLLING_55(idx, step, macro); UNROLL_INCR(idx, step, macro)
132#define LOOP_UNROLLING_57(idx, step, macro) LOOP_UNROLLING_56(idx, step, macro); UNROLL_INCR(idx, step, macro)
133#define LOOP_UNROLLING_58(idx, step, macro) LOOP_UNROLLING_57(idx, step, macro); UNROLL_INCR(idx, step, macro)
134#define LOOP_UNROLLING_59(idx, step, macro) LOOP_UNROLLING_58(idx, step, macro); UNROLL_INCR(idx, step, macro)
135#define LOOP_UNROLLING_60(idx, step, macro) LOOP_UNROLLING_59(idx, step, macro); UNROLL_INCR(idx, step, macro)
136#define LOOP_UNROLLING_61(idx, step, macro) LOOP_UNROLLING_60(idx, step, macro); UNROLL_INCR(idx, step, macro)
137#define LOOP_UNROLLING_62(idx, step, macro) LOOP_UNROLLING_61(idx, step, macro); UNROLL_INCR(idx, step, macro)
138#define LOOP_UNROLLING_63(idx, step, macro) LOOP_UNROLLING_62(idx, step, macro); UNROLL_INCR(idx, step, macro)
139#define LOOP_UNROLLING_64(idx, step, macro) LOOP_UNROLLING_63(idx, step, macro); UNROLL_INCR(idx, step, macro)
140#define LOOP_UNROLLING_65(idx, step, macro) LOOP_UNROLLING_64(idx, step, macro); UNROLL_INCR(idx, step, macro)
141#define LOOP_UNROLLING_66(idx, step, macro) LOOP_UNROLLING_65(idx, step, macro); UNROLL_INCR(idx, step, macro)
142#define LOOP_UNROLLING_67(idx, step, macro) LOOP_UNROLLING_66(idx, step, macro); UNROLL_INCR(idx, step, macro)
143#define LOOP_UNROLLING_68(idx, step, macro) LOOP_UNROLLING_67(idx, step, macro); UNROLL_INCR(idx, step, macro)
144#define LOOP_UNROLLING_69(idx, step, macro) LOOP_UNROLLING_68(idx, step, macro); UNROLL_INCR(idx, step, macro)
145#define LOOP_UNROLLING_70(idx, step, macro) LOOP_UNROLLING_69(idx, step, macro); UNROLL_INCR(idx, step, macro)
146#define LOOP_UNROLLING_71(idx, step, macro) LOOP_UNROLLING_70(idx, step, macro); UNROLL_INCR(idx, step, macro)
147#define LOOP_UNROLLING_72(idx, step, macro) LOOP_UNROLLING_71(idx, step, macro); UNROLL_INCR(idx, step, macro)
148#define LOOP_UNROLLING_73(idx, step, macro) LOOP_UNROLLING_72(idx, step, macro); UNROLL_INCR(idx, step, macro)
149#define LOOP_UNROLLING_74(idx, step, macro) LOOP_UNROLLING_73(idx, step, macro); UNROLL_INCR(idx, step, macro)
150#define LOOP_UNROLLING_75(idx, step, macro) LOOP_UNROLLING_74(idx, step, macro); UNROLL_INCR(idx, step, macro)
151#define LOOP_UNROLLING_76(idx, step, macro) LOOP_UNROLLING_75(idx, step, macro); UNROLL_INCR(idx, step, macro)
152#define LOOP_UNROLLING_77(idx, step, macro) LOOP_UNROLLING_76(idx, step, macro); UNROLL_INCR(idx, step, macro)
153#define LOOP_UNROLLING_78(idx, step, macro) LOOP_UNROLLING_77(idx, step, macro); UNROLL_INCR(idx, step, macro)
154#define LOOP_UNROLLING_79(idx, step, macro) LOOP_UNROLLING_78(idx, step, macro); UNROLL_INCR(idx, step, macro)
155#define LOOP_UNROLLING_80(idx, step, macro) LOOP_UNROLLING_79(idx, step, macro); UNROLL_INCR(idx, step, macro)
156#define LOOP_UNROLLING_81(idx, step, macro) LOOP_UNROLLING_80(idx, step, macro); UNROLL_INCR(idx, step, macro)
157#define LOOP_UNROLLING_82(idx, step, macro) LOOP_UNROLLING_81(idx, step, macro); UNROLL_INCR(idx, step, macro)
158#define LOOP_UNROLLING_83(idx, step, macro) LOOP_UNROLLING_82(idx, step, macro); UNROLL_INCR(idx, step, macro)
159#define LOOP_UNROLLING_84(idx, step, macro) LOOP_UNROLLING_83(idx, step, macro); UNROLL_INCR(idx, step, macro)
160#define LOOP_UNROLLING_85(idx, step, macro) LOOP_UNROLLING_84(idx, step, macro); UNROLL_INCR(idx, step, macro)
161#define LOOP_UNROLLING_86(idx, step, macro) LOOP_UNROLLING_85(idx, step, macro); UNROLL_INCR(idx, step, macro)
162#define LOOP_UNROLLING_87(idx, step, macro) LOOP_UNROLLING_86(idx, step, macro); UNROLL_INCR(idx, step, macro)
163#define LOOP_UNROLLING_88(idx, step, macro) LOOP_UNROLLING_87(idx, step, macro); UNROLL_INCR(idx, step, macro)
164#define LOOP_UNROLLING_89(idx, step, macro) LOOP_UNROLLING_88(idx, step, macro); UNROLL_INCR(idx, step, macro)
165#define LOOP_UNROLLING_90(idx, step, macro) LOOP_UNROLLING_89(idx, step, macro); UNROLL_INCR(idx, step, macro)
166#define LOOP_UNROLLING_91(idx, step, macro) LOOP_UNROLLING_90(idx, step, macro); UNROLL_INCR(idx, step, macro)
167#define LOOP_UNROLLING_92(idx, step, macro) LOOP_UNROLLING_91(idx, step, macro); UNROLL_INCR(idx, step, macro)
168#define LOOP_UNROLLING_93(idx, step, macro) LOOP_UNROLLING_92(idx, step, macro); UNROLL_INCR(idx, step, macro)
169#define LOOP_UNROLLING_94(idx, step, macro) LOOP_UNROLLING_93(idx, step, macro); UNROLL_INCR(idx, step, macro)
170#define LOOP_UNROLLING_95(idx, step, macro) LOOP_UNROLLING_94(idx, step, macro); UNROLL_INCR(idx, step, macro)
171#define LOOP_UNROLLING_96(idx, step, macro) LOOP_UNROLLING_95(idx, step, macro); UNROLL_INCR(idx, step, macro)
172#define LOOP_UNROLLING_97(idx, step, macro) LOOP_UNROLLING_96(idx, step, macro); UNROLL_INCR(idx, step, macro)
173#define LOOP_UNROLLING_98(idx, step, macro) LOOP_UNROLLING_97(idx, step, macro); UNROLL_INCR(idx, step, macro)
174#define LOOP_UNROLLING_99(idx, step, macro) LOOP_UNROLLING_98(idx, step, macro); UNROLL_INCR(idx, step, macro)
175#define LOOP_UNROLLING_100(idx, step, macro) LOOP_UNROLLING_99(idx, step, macro); UNROLL_INCR(idx, step, macro)
176#define LOOP_UNROLLING_101(idx, step, macro) LOOP_UNROLLING_100(idx, step, macro); UNROLL_INCR(idx, step, macro)
177#define LOOP_UNROLLING_102(idx, step, macro) LOOP_UNROLLING_101(idx, step, macro); UNROLL_INCR(idx, step, macro)
178#define LOOP_UNROLLING_103(idx, step, macro) LOOP_UNROLLING_102(idx, step, macro); UNROLL_INCR(idx, step, macro)
179#define LOOP_UNROLLING_104(idx, step, macro) LOOP_UNROLLING_103(idx, step, macro); UNROLL_INCR(idx, step, macro)
180#define LOOP_UNROLLING_105(idx, step, macro) LOOP_UNROLLING_104(idx, step, macro); UNROLL_INCR(idx, step, macro)
181#define LOOP_UNROLLING_106(idx, step, macro) LOOP_UNROLLING_105(idx, step, macro); UNROLL_INCR(idx, step, macro)
182#define LOOP_UNROLLING_107(idx, step, macro) LOOP_UNROLLING_106(idx, step, macro); UNROLL_INCR(idx, step, macro)
183#define LOOP_UNROLLING_108(idx, step, macro) LOOP_UNROLLING_107(idx, step, macro); UNROLL_INCR(idx, step, macro)
184#define LOOP_UNROLLING_109(idx, step, macro) LOOP_UNROLLING_108(idx, step, macro); UNROLL_INCR(idx, step, macro)
185#define LOOP_UNROLLING_110(idx, step, macro) LOOP_UNROLLING_109(idx, step, macro); UNROLL_INCR(idx, step, macro)
186#define LOOP_UNROLLING_111(idx, step, macro) LOOP_UNROLLING_110(idx, step, macro); UNROLL_INCR(idx, step, macro)
187#define LOOP_UNROLLING_112(idx, step, macro) LOOP_UNROLLING_111(idx, step, macro); UNROLL_INCR(idx, step, macro)
188#define LOOP_UNROLLING_113(idx, step, macro) LOOP_UNROLLING_112(idx, step, macro); UNROLL_INCR(idx, step, macro)
189#define LOOP_UNROLLING_114(idx, step, macro) LOOP_UNROLLING_113(idx, step, macro); UNROLL_INCR(idx, step, macro)
190#define LOOP_UNROLLING_115(idx, step, macro) LOOP_UNROLLING_114(idx, step, macro); UNROLL_INCR(idx, step, macro)
191#define LOOP_UNROLLING_116(idx, step, macro) LOOP_UNROLLING_115(idx, step, macro); UNROLL_INCR(idx, step, macro)
192#define LOOP_UNROLLING_117(idx, step, macro) LOOP_UNROLLING_116(idx, step, macro); UNROLL_INCR(idx, step, macro)
193#define LOOP_UNROLLING_118(idx, step, macro) LOOP_UNROLLING_117(idx, step, macro); UNROLL_INCR(idx, step, macro)
194#define LOOP_UNROLLING_119(idx, step, macro) LOOP_UNROLLING_118(idx, step, macro); UNROLL_INCR(idx, step, macro)
195#define LOOP_UNROLLING_120(idx, step, macro) LOOP_UNROLLING_119(idx, step, macro); UNROLL_INCR(idx, step, macro)
196#define LOOP_UNROLLING_121(idx, step, macro) LOOP_UNROLLING_120(idx, step, macro); UNROLL_INCR(idx, step, macro)
197#define LOOP_UNROLLING_122(idx, step, macro) LOOP_UNROLLING_121(idx, step, macro); UNROLL_INCR(idx, step, macro)
198#define LOOP_UNROLLING_123(idx, step, macro) LOOP_UNROLLING_122(idx, step, macro); UNROLL_INCR(idx, step, macro)
199#define LOOP_UNROLLING_124(idx, step, macro) LOOP_UNROLLING_123(idx, step, macro); UNROLL_INCR(idx, step, macro)
200#define LOOP_UNROLLING_125(idx, step, macro) LOOP_UNROLLING_124(idx, step, macro); UNROLL_INCR(idx, step, macro)
201#define LOOP_UNROLLING_126(idx, step, macro) LOOP_UNROLLING_125(idx, step, macro); UNROLL_INCR(idx, step, macro)
202#define LOOP_UNROLLING_127(idx, step, macro) LOOP_UNROLLING_126(idx, step, macro); UNROLL_INCR(idx, step, macro)
203#define LOOP_UNROLLING_128(idx, step, macro) LOOP_UNROLLING_127(idx, step, macro); UNROLL_INCR(idx, step, macro)
204
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100205#define LOOP_UNROLLING_STR(type, idx, start, step, num, macro) \
206 { \
207 type idx = start; \
208 LOOP_UNROLLING_##num(idx, step, macro); \
209 }
Giorgio Arenaea8d2662021-05-20 11:36:56 +0100210#else // !defined(UNROLL_WITH_PRAGMA)
211#define LOOP_UNROLLING_STR(type, idx, start, step, num, macro) \
212 { \
213 _Pragma("unroll") \
214 for(type idx = start; idx < (num * step); idx += step) \
215 { \
216 (macro); \
217 } \
218 }
219#endif // !defined(UNROLL_WITH_PRAGMA)
220#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 +0000221
222/** 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
223 * to avoid out-of-bound read/write
224 *
225 * @note PARTIAL_N0 is used for get_global_id(n) = 0.
226 *
227 * @param[in] IDX get_global_id index (0,1 and 2 only)
228 * @param[in] N0 Number of elements read/written on the IDX direction
229 * @param[in] PARTIAL_N0 Number of elements read/written on the IDX direction for get_global_id(IDX) = 0. If zero,
230 * the Number of elements read/written on the IDX direction for get_global_id(IDX) = 0 is N0
231 */
232#define GET_SPATIAL_IDX(IDX, N0, PARTIAL_N0) (max((int)(get_global_id(IDX) * N0 - (N0 - PARTIAL_N0) % N0), 0))
233
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000234/** Dot product integet 8bit function
235 *
236 * @note Performs: c += dot(a, b)
237 *
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000238 * @param[in] DST_DATA_TYPE Accumulator data type
239 * @param[in] K0 Number of accumulations
240 * @param[in] a OpenCL vector a
241 * @param[in] b OpenCL vector b
242 * @param[in] c Scalar variable c
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000243 */
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000244#define DOT_PRODUCT_INTEGER8(DST_DATA_TYPE, K0, a, b, c) DOT_PRODUCT_INTEGER8_STR(DST_DATA_TYPE, K0, a, b, c)
245#define DOT_PRODUCT_INTEGER8_STR(DST_DATA_TYPE, K0, a, b, c) DOT_PRODUCT##K0##_INTEGER8(DST_DATA_TYPE, a, b, c)
246#define DOT_PRODUCT1_INTEGER8(DST_DATA_TYPE, a, b, c) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000247 ({ \
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000248 c += (DST_DATA_TYPE)a * (DST_DATA_TYPE)b; \
249 })
250#define DOT_PRODUCT2_INTEGER8(DST_DATA_TYPE, a, b, c) \
251 ({ \
252 c += (DST_DATA_TYPE)a.s0 * (DST_DATA_TYPE)b.s0; \
253 c += (DST_DATA_TYPE)a.s1 * (DST_DATA_TYPE)b.s1; \
254 })
255#define DOT_PRODUCT3_INTEGER8(DST_DATA_TYPE, a, b, c) \
256 ({ \
257 DOT_PRODUCT2_INTEGER8(DST_DATA_TYPE, a, b, c); \
258 c += (DST_DATA_TYPE)a.s2 * (DST_DATA_TYPE)b.s2; \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000259 })
260#if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000261#define DOT_PRODUCT4_INTEGER8(DST_DATA_TYPE, x, y, val) val = arm_dot_acc((x), (y), (val));
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000262#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)
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000263#define DOT_PRODUCT4_INTEGER8(DST_DATA_TYPE, x, y, val) val += arm_dot((x), (y));
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000264#else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000265#define DOT_PRODUCT4_INTEGER8(DST_DATA_TYPE, x, y, val) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000266 ({ \
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000267 val += (DST_DATA_TYPE)x.s0 * (DST_DATA_TYPE)y.s0; \
268 val += (DST_DATA_TYPE)x.s1 * (DST_DATA_TYPE)y.s1; \
269 val += (DST_DATA_TYPE)x.s2 * (DST_DATA_TYPE)y.s2; \
270 val += (DST_DATA_TYPE)x.s3 * (DST_DATA_TYPE)y.s3; \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000271 })
272#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000273#define DOT_PRODUCT8_INTEGER8(DST_DATA_TYPE, a, b, c) \
274 ({ \
275 DOT_PRODUCT4_INTEGER8(DST_DATA_TYPE, (a.lo), (b.lo), c); \
276 DOT_PRODUCT4_INTEGER8(DST_DATA_TYPE, (a.hi), (b.hi), c); \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000277 })
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000278#define DOT_PRODUCT16_INTEGER8(DST_DATA_TYPE, a, b, c) \
279 ({ \
280 DOT_PRODUCT8_INTEGER8(DST_DATA_TYPE, (a.lo), (b.lo), c); \
281 DOT_PRODUCT8_INTEGER8(DST_DATA_TYPE, (a.hi), (b.hi), c); \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000282 })
283
284/** Load a vector from global memory (tensor)
285 *
286 * @param[in] DATA_TYPE Data type
287 * @param[in] WIDTH Number of dst columns
288 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
289 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
290 * @param[in] TENSOR Tensor basename
291 * @param[in] X Starting X position
292 * @param[in] Y Starting Y position
293 * @param[in] STRIDE_Y Stride Y (in bytes)
294 */
295#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)
296#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)
297#define V_LOAD_BUFFER(DATA_TYPE, WIDTH, TENSOR, X, Y, STRIDE_Y) \
298 VLOAD(WIDTH) \
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000299 (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 +0000300#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))
301
302/** Load a tile from global memory (tensor)
303 *
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100304 * @param[in] DATA_TYPE Data type
305 * @param[in] HEIGHT Number of dst rows
306 * @param[in] WIDTH Number of dst columns
307 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
308 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
309 * @param[in] TENSOR Tensor basename
310 * @param[in] X Starting X position
311 * @param[in] Y Starting Y position
312 * @param[in] YI_MULTIPLIER Parameter used to multiply the internal row increment (_i).
313 * 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).
314 * In this case the address calculation is performed as: (Y + _i * Y_MULTIPLIER) * STRIDE_Y
315 * @param[in] STRIDE_Y Stride Y (in bytes) used to load each row.
316 * @param[out] dst Output tile
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000317 */
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100318#define T_LOAD(DATA_TYPE, HEIGHT, WIDTH, TENSOR_TYPE, TENSOR, X, Y, YI_MULTIPLIER, STRIDE_Y, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100319 ({ \
320 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
321 { \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100322 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 +0100323 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000324 })
325
326/** Load a tile from global memory (tensor) using an indirect Y index tile
327 *
328 * @param[in] DATA_TYPE Data type
329 * @param[in] HEIGHT Number of dst rows
330 * @param[in] WIDTH Number of dst columns
331 * @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
332 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
333 * @param[in] TENSOR Tensor basename
334 * @param[in] X Starting X position
335 * @param[in] STRIDE_Y Stride Y (in bytes)
336 * @param[in] indirect_y Indirect Y index tile
337 * @param[out] dst Output tile
338 */
339#define T_LOAD_INDIRECT(DATA_TYPE, HEIGHT, WIDTH, TENSOR_TYPE, TENSOR, X, STRIDE_Y, indirect_y, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100340 ({ \
341 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
342 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000343 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 +0100344 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000345 })
346
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100347/** Load a tile from global memory (tensor) when the tensor is stored using a NHWC layout
348 *
349 * @param[in] DATA_TYPE Data type
350 * @param[in] TILE_HEIGHT Number of elements to load from Y (height) dimension
351 * @param[in] TILE_WIDTH Number of elements to load from X (width) dimension
352 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
353 * @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
354 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
355 * @param[in] TENSOR Tensor basename
356 * @param[in] B Starting batch index
357 * @param[in] Y Starting Y index
358 * @param[in] X Starting X index
359 * @param[in] C Starting C index
360 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
361 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
362 * @param[in] STRIDE_Y Stride Y (in bytes)
363 * @param[out] dst Output tile
364 */
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100365#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 +0100366 ({ \
367 LOOP_UNROLLING(int, _yk, 0, 1, TILE_HEIGHT, \
368 { \
369 LOOP_UNROLLING(int, _xk, 0, 1, TILE_WIDTH, \
370 { \
371 int _src_y = (X) + _xk + ((Y) + _yk) * (TENSOR_WIDTH); \
372 _src_y += (B) * (int)(TENSOR_WIDTH) * (int)(TENSOR_HEIGHT); \
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100373 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 +0100374 if(_src_valid_y != 0) \
375 { \
376 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 +0100377 } \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100378 }) \
379 }) \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100380 })
381
382/** Load a tile from global memory (tensor) when the tensor is stored using a NHWC layout using indirect X and Y coordinates
383 *
384 * @param[in] DATA_TYPE Data type
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100385 * @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 +0100386 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
387 * @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
388 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
389 * @param[in] TENSOR Tensor basename
390 * @param[in] B Starting batch index
391 * @param[in] Y Starting Y index
392 * @param[in] X Starting X index
393 * @param[in] C Starting C index
394 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
395 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
396 * @param[in] STRIDE_Y Stride Y (in bytes)
397 * @param[out] xi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect X coordinate
398 * @param[out] yi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect Y coordinate
399 * @param[out] dst Output tile
400 */
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100401#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) \
402 ({ \
403 LOOP_UNROLLING(int, _i, 0, 1, TILE_AREA, \
404 { \
405 int _src_y = (X) + xi[_i].v + ((Y) + yi[_i].v) * (TENSOR_WIDTH); \
406 _src_y += (B) * (int)(TENSOR_WIDTH) * (int)(TENSOR_HEIGHT); \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100407 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 +0100408 if(_src_valid_y != 0) \
409 { \
410 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 +0100411 } \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100412 }) \
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100413 })
414
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000415/** Store a tile to global memory (tensor) using an indirect Y index tile and conditionally use a different length for the store
416 *
417 * @note If WIDTH1_CONDITION is true, the store will use the WIDTH1 length for the store
418 * @note The vectors are stored in reverse order so the invalid rows are overwritten by the valid ones
419 *
420 * @param[in] DATA_TYPE Data type
421 * @param[in] HEIGHT Number of src rows
422 * @param[in] WIDTH0 Store width to use if WIDTH1_CONDITION = false
423 * @param[in] WIDTH1 Store width to use if WIDTH1_CONDITION = true
424 * @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
425 * cl_image is not supported.
426 * @param[in] TENSOR Tensor basename
427 * @param[in] X Starting X position
428 * @param[in] STRIDE_Y Stride Y (in bytes)
429 * @param[in] WIDTH1_CONDITION Condition to select the WIDTH1 store
430 * @param[in] src Input tile
431 * @param[in] indirect_y Indirect Y index tile
432 */
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000433#define T_STORE_INDIRECT_WIDTH_SELECT(DATA_TYPE, HEIGHT, WIDTH0, WIDTH1, TENSOR_TYPE, TENSOR, X, STRIDE_Y, WIDTH1_CONDITION, src, indirect_y) \
434 ({ \
435 if(WIDTH1_CONDITION) \
436 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100437 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000438 { \
439 VSTORE_PARTIAL(WIDTH0, WIDTH1) \
440 (src[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)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100441 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000442 } \
443 else \
444 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100445 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000446 { \
447 VSTORE(WIDTH0) \
448 (src[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)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100449 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000450 } \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000451 })
452
453/** Offset correction for the QASYMM8 computation
454 *
455 * @param[in] ACC_DATA_TYPE Accumulator data type
456 * @param[in] M0 Number of src/dst rows
457 * @param[in] N0 Number of src/dst columns
458 * @param[in] K0 Number of src columns
459 * @param[in] SRC_OFFSET Source quantization offset
460 * @param[in] WEI_OFFSET Weights quantization shift
461 * @param[in] lhs LHS tile
462 * @param[in] rhs RHS tile
463 * @param[out] dst DST tile
464 */
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100465#define T_OFFSET_CORRECTION(ACC_DATA_TYPE, M0, N0, K0, SRC_OFFSET, WEI_OFFSET, lhs, rhs, dst) \
466 ({ \
467 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
468 { \
469 ACC_DATA_TYPE _tm = 0; \
470 LOOP_UNROLLING(int, _k0, 0, 1, K0, \
471 { \
472 _tm += ((ACC_DATA_TYPE)lhs[_m0].s[_k0] * (ACC_DATA_TYPE)WEI_OFFSET); \
473 }) \
474 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
475 { \
476 dst[_m0].s[_n0] += _tm; \
477 LOOP_UNROLLING(int, _k0, 0, 1, K0, \
478 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000479 dst[_m0].s[_n0] += ((ACC_DATA_TYPE)rhs[_n0].s[_k0] * (ACC_DATA_TYPE)SRC_OFFSET); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100480 }) \
481 }) \
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000482 }); \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000483 })
484
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000485/** Quantized the tile (ASYMMETRIC) with fixed-point scale
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000486 *
487 * @param[in] SRC_DATA_TYPE SRC data type
488 * @param[in] DST_DATA_TYPE DST data type
489 * @param[in] M0 Number of src/dst rows
490 * @param[in] N0 Number of src/dst columns
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000491 * @param[in] DST_OFFSET Quantization offset
492 * @param[in] DST_SHIFT Quantization shift
493 * @param[in] DST_MULTIPLIER Quantization multiplier
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000494 * @param[in] src Input tile
495 * @param[out] dst Output tile
496 */
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000497#define T_QUANTIZE8_ASYMMETRIC(SRC_DATA_TYPE, DST_DATA_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst) \
498 ({ \
499 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
500 { \
501 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
502 { \
503 SRC_DATA_TYPE _tmp = 0; \
504 if(DST_SHIFT < 0) \
505 { \
506 _tmp = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(src[_m0].s[_n0], DST_MULTIPLIER, DST_SHIFT, 1); \
507 } \
508 else \
509 { \
510 _tmp = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(src[_m0].s[_n0], DST_MULTIPLIER, DST_SHIFT, 1); \
511 } \
512 _tmp += DST_OFFSET; \
513 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
514 }) \
515 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000516 })
517
518/** Conditional rowset (memset by row)
519 *
520 * @note Set the row to VALUE_TO_SET if the corresponding mask == 0
521 *
522 * @param[in] DATA_TYPE Data type
523 * @param[in] M0 Number of LHS rows
524 * @param[in] N0 Number of LHS columns
525 * @param[in] VALUE_TO_SET Value to set the row
526 * @param[in, out] a Input/output tile
527 * @param[out] mask Mask to check for setting the row to VALUE_TO_SET
528 */
529#define T_ROWSET_MASK(DATA_TYPE, M0, N0, VALUE_TO_SET, a, mask) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100530 ({ \
531 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
532 { \
533 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
534 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000535 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 +0100536 }) \
537 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000538 })
539
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000540/** Element-wise activation
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000541 *
542 * @note Performs: activation(LHS) = DST
543 *
544 * @param[in] DATA_TYPE SRC/DST data type
545 * @param[in] M0 Number of SRC/DST rows
546 * @param[in] N0 Number of SRC/DST columns
547 * @param[in] ACTIVATION_TYPE Activation type
548 * @param[in] A_VAL A value used for the activation (e.g. tanh_op, brelu,..)
549 * @param[in] B_VAL B value used for the activation (e.g. tanh_op, brelu,..)
550 * @param[out] src SRC tile
551 * @param[out] dst DST tile
552 */
553#define T_ACTIVATION(DATA_TYPE, M0, N0, ACTIVATION_TYPE, A_VAL, B_VAL, src, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100554 ({ \
555 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
556 { \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000557 dst[_m0].v = ACTIVATION(ACTIVATION_TYPE, DATA_TYPE, N0, src[_m0].v, A_VAL, B_VAL); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100558 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000559 })
560
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000561/** Element-wise addition with a constant value
562 *
563 * @note Performs: LHS + constant = DST
564 *
565 * @param[in] DATA_TYPE LHS/RHS/DST data type
566 * @param[in] M0 Number of LHS rows
567 * @param[in] N0 Number of LHS columns
568 * @param[in] lhs LHS tile
569 * @param[in] rhs_constant Constant value
570 * @param[out] dst DST tile
571 */
572#define T_ADD_CONSTANT(DATA_TYPE, M0, N0, lhs, rhs_constant, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100573 ({ \
574 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
575 { \
576 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
577 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000578 dst[_m0].s[_n0] = lhs[_m0].s[_n0] + rhs_constant; \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100579 }) \
580 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000581 })
582
583/** Element-wise addition with RHS broadcasted (RHS has the X dimension only)
584 *
585 * @note Performs: LHS + RHS[broadcasted] = DST
586 * @note Both tiles must have same data type
587 *
588 * @param[in] DATA_TYPE LHS/RHS/DST data type
589 * @param[in] M0 Number of LHS rows
590 * @param[in] N0 Number of LHS columns
591 * @param[in] lhs LHS tile
592 * @param[in] rhs RHS tile
593 * @param[out] dst DST tile
594 */
595#define T_ADD_BROADCAST_X(DATA_TYPE, M0, N0, lhs, rhs, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100596 ({ \
597 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
598 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000599 dst[_m0].v = lhs[_m0].v + rhs[0].v; \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100600 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000601 })
602
603/** Matrix multiplication
604 *
605 * @note Performs: LHS X RHS + DST = DST
606 *
607 * @param[in] LHS_DATA_TYPE LHS tile data type
608 * @param[in] RHS_DATA_TYPE RHS tile data type
609 * @param[in] DST_DATA_TYPE RHS tile data type
610 * @param[in] M0 Number of LHS rows
611 * @param[in] N0 Number of RHS columns
612 * @param[in] K0 Number of LHS columns
613 * @param[in] LHS_LAYOUT LHS layout (T= transposed, NT= not transposed)
614 * @param[in] RHS_LAYOUT RHS layout (T= transposed, NT= not transposed)
615 * @param[in] lhs LHS tile
616 * @param[in] rhs RHS tile
617 * @param[in, out] dst DST tile
618 */
619#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 Iodicec63b7222021-06-30 08:39:44 +0000620#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(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
621#define T_MMUL_NT_T_float_float_float(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_FLOAT(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
622#define T_MMUL_NT_T_half_half_half(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_FLOAT(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
623#define T_MMUL_NT_T_char_char_int(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_INTEGER8(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
624#define T_MMUL_NT_T_uchar_uchar_uint(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_INTEGER8(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
625#define T_MMUL_NT_T_uchar_uchar_int(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_INTEGER8(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
626#define T_MMUL_NT_T_FLOAT(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000627 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100628 LOOP_UNROLLING(int, _m, 0, 1, M0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000629 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100630 LOOP_UNROLLING(int, _n, 0, 1, N0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000631 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100632 LOOP_UNROLLING(int, _k, 0, 1, K0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000633 { \
634 dst[_m].s[_n] = fma((lhs[_m].s[_k]), (rhs[_n].s[_k]), dst[_m].s[_n]); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100635 }) \
636 }) \
637 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000638 }
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000639#define T_MMUL_NT_T_INTEGER8(DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) \
640 ({ \
641 LOOP_UNROLLING(int, _m, 0, 1, M0, \
642 { \
643 LOOP_UNROLLING(int, _n, 0, 1, N0, \
644 { \
645 DOT_PRODUCT_INTEGER8(DST_DATA_TYPE, K0, (lhs[_m].v), (rhs[_n].v), dst[_m].s[_n]); \
646 }) \
647 }) \
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100648 })
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000649
Georgios Pinitas66831652021-07-01 09:14:07 +0100650/** 8-bit quantization with fixed-point scale
651 *
652 * @param[in] SRC_DATA_TYPE SRC data type
653 * @param[in] DST_DATA_TYPE DST data type
654 * @param[in] QUANTIZATION_TYPE Quantization type (PER_TENSOR or PER_CHANNEL)
655 * @param[in] M0 Number of src/dst rows
656 * @param[in] N0 Number of src/dst columns
657 * @param[in] DST_OFFSET Quantization offset used for both the per-tensor and per-channel quantization
658 * @param[in] DST_SHIFT Quantization shift for the per-tensor quantization
659 * @param[in] DST_MULTIPLIER Quantization multiplier for the per-tensor quantization
660 * @param[in] src Input tile
661 * @param[in] dst_multipliers Output multipliers tile for the per-channel quantization
662 * @param[in] dst_shifts Output shift tile for the per-channel quantization
663 * @param[out] dst Output tile
664 */
665#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)
666#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)
667
668/** 8-bit per-tensor quantization with fixed-point scale
669 *
670 * @param[in] SRC_DATA_TYPE SRC data type
671 * @param[in] DST_DATA_TYPE DST data type
672 * @param[in] M0 Number of src/dst rows
673 * @param[in] N0 Number of src/dst columns
674 * @param[in] DST_OFFSET Quantization offset
675 * @param[in] DST_SHIFT Quantization shift for the per-tensor quantization
676 * @param[in] DST_MULTIPLIER Quantization multiplier for the per-tensor quantization
677 * @param[in] src Input tile
678 * @param[in] dst_multipliers (unused)
679 * @param[in] dst_shifts (unused)
680 * @param[out] dst Output tile
681 */
682#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) \
683 ({ \
684 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
685 { \
686 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
687 { \
688 SRC_DATA_TYPE _tmp = 0; \
689 SRC_DATA_TYPE _src = src[_m0].s[_n0]; \
690 _src *= select((SRC_DATA_TYPE)1, ((SRC_DATA_TYPE)1 << (SRC_DATA_TYPE)(-DST_SHIFT)), ((SRC_DATA_TYPE)DST_SHIFT < (SRC_DATA_TYPE)0)); \
691 SRC_DATA_TYPE overflow = _src == DST_MULTIPLIER && _src == INT_MIN; \
692 long a_64 = (long)(_src); \
693 long b_64 = (long)(DST_MULTIPLIER); \
694 long ab_64 = a_64 * b_64; \
695 long mask1 = 1 << 30; \
696 long mask2 = 1 - (1 << 30); \
697 long is_positive_or_zero = ab_64 >= 0; \
698 long nudge = select(mask2, mask1, is_positive_or_zero); \
699 SRC_DATA_TYPE ab_x2_high32 = CONVERT((ab_64 + nudge) / (long)(1ll << 31), SRC_DATA_TYPE); \
700 _tmp = select(ab_x2_high32, (SRC_DATA_TYPE)INT_MAX, overflow); \
701 if(DST_SHIFT >= 0) \
702 { \
703 long mask = ((((int)1) << DST_SHIFT) - (int)1); \
704 long threshold = _tmp < (int)0 ? (mask >> 1) + (long)1 : (mask >> 1) + 0; \
705 _tmp = (_tmp & mask) > threshold ? (_tmp >> DST_SHIFT) + (int)1 : (_tmp >> DST_SHIFT); \
706 } \
707 _tmp += DST_OFFSET; \
708 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
709 }) \
710 }) \
711 })
712
713/** 8-bit per-channel quantization with fixed-point scale
714 *
715 * @param[in] SRC_DATA_TYPE SRC data type
716 * @param[in] DST_DATA_TYPE DST data type
717 * @param[in] M0 Number of src/dst rows
718 * @param[in] N0 Number of src/dst columns
719 * @param[in] DST_OFFSET Quantization offset
720 * @param[in] DST_SHIFT (unused)
721 * @param[in] DST_MULTIPLIER (unused)
722 * @param[in] src Input tile
723 * @param[in] dst_multipliers Output multipliers tile for the per-channel quantization
724 * @param[in] dst_shifts Output shift tile for the per-channel quantization
725 * @param[out] dst Output tile
726 */
727#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) \
728 ({ \
729 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
730 { \
731 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
732 { \
733 SRC_DATA_TYPE _tmp = 0; \
734 SRC_DATA_TYPE _src = src[_m0].s[_n0]; \
735 SRC_DATA_TYPE _dst_multiplier = dst_multipliers[0].s[_n0]; \
736 SRC_DATA_TYPE _dst_shift = dst_shifts[0].s[_n0]; \
737 _src *= select((SRC_DATA_TYPE)1, ((SRC_DATA_TYPE)1 << (SRC_DATA_TYPE)(-_dst_shift)), ((SRC_DATA_TYPE)_dst_shift < (SRC_DATA_TYPE)0)); \
738 SRC_DATA_TYPE overflow = _src == _dst_multiplier && _src == INT_MIN; \
739 long a_64 = (long)(_src); \
740 long b_64 = (long)(_dst_multiplier); \
741 long ab_64 = a_64 * b_64; \
742 long mask1 = 1 << 30; \
743 long mask2 = 1 - (1 << 30); \
744 long is_positive_or_zero = ab_64 >= 0; \
745 long nudge = select(mask2, mask1, is_positive_or_zero); \
746 SRC_DATA_TYPE ab_x2_high32 = CONVERT((ab_64 + nudge) / (long)(1ll << 31), SRC_DATA_TYPE); \
747 _tmp = select(ab_x2_high32, (SRC_DATA_TYPE)INT_MAX, overflow); \
748 if(_dst_shift >= 0) \
749 { \
750 long mask = ((((int)1) << _dst_shift) - (int)1); \
751 long threshold = _tmp < (int)0 ? (mask >> 1) + (long)1 : (mask >> 1) + 0; \
752 _tmp = (_tmp & mask) > threshold ? (_tmp >> _dst_shift) + (int)1 : (_tmp >> _dst_shift); \
753 } \
754 _tmp += DST_OFFSET; \
755 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
756 }) \
757 }) \
758 })
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000759// clang-format on
760// *INDENT-ON*