blob: f36f273e1dcd7a71883a16f8ec50889691ccdee4 [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 Iodice8155c022021-04-16 15:08:59 +010028#define TILE_VECTOR_SIZE1 1
29#define TILE_VECTOR_SIZE2 2
30#define TILE_VECTOR_SIZE3 3
31#define TILE_VECTOR_SIZE4 4
32#define TILE_VECTOR_SIZE5 8
33#define TILE_VECTOR_SIZE6 8
34#define TILE_VECTOR_SIZE7 8
35#define TILE_VECTOR_SIZE8 8
36#define TILE_VECTOR_SIZE9 16
37#define TILE_VECTOR_SIZE10 16
38#define TILE_VECTOR_SIZE11 16
39#define TILE_VECTOR_SIZE12 16
40#define TILE_VECTOR_SIZE13 16
41#define TILE_VECTOR_SIZE14 16
42#define TILE_VECTOR_SIZE15 16
43#define TILE_VECTOR_SIZE16 16
44
45#define TILE_VECTOR_TYPE1(DATA_TYPE) DATA_TYPE##1
46#define TILE_VECTOR_TYPE2(DATA_TYPE) DATA_TYPE##2
47#define TILE_VECTOR_TYPE3(DATA_TYPE) DATA_TYPE##3
48#define TILE_VECTOR_TYPE4(DATA_TYPE) DATA_TYPE##4
49#define TILE_VECTOR_TYPE5(DATA_TYPE) DATA_TYPE##8
50#define TILE_VECTOR_TYPE6(DATA_TYPE) DATA_TYPE##8
51#define TILE_VECTOR_TYPE7(DATA_TYPE) DATA_TYPE##8
52#define TILE_VECTOR_TYPE8(DATA_TYPE) DATA_TYPE##8
53#define TILE_VECTOR_TYPE9(DATA_TYPE) DATA_TYPE##16
54#define TILE_VECTOR_TYPE10(DATA_TYPE) DATA_TYPE##16
55#define TILE_VECTOR_TYPE11(DATA_TYPE) DATA_TYPE##16
56#define TILE_VECTOR_TYPE12(DATA_TYPE) DATA_TYPE##16
57#define TILE_VECTOR_TYPE13(DATA_TYPE) DATA_TYPE##16
58#define TILE_VECTOR_TYPE14(DATA_TYPE) DATA_TYPE##16
59#define TILE_VECTOR_TYPE15(DATA_TYPE) DATA_TYPE##16
60#define TILE_VECTOR_TYPE16(DATA_TYPE) DATA_TYPE##16
61
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000062/** Tile object
63 * A tile object is a 2D memory block and can be accessed using the following syntax:
64 * -# a[m0].v = access the the vector at row "m0" (OpenCL vector)
65 * -# a[m0].s[x] = access the scalar element at row "m0" and column "n0" (scalar access)
66 *
67 * @param[in] DATA_TYPE Data type of the tile
68 * @param[in] H Number of tile rows
69 * @param[in] W Number of tile colums
70 * @param[in] BASENAME Tile's name
71 */
72#define TILE(DATA_TYPE, H, W, BASENAME) TILE_STR(DATA_TYPE, H, W, BASENAME)
73#define TILE_STR(DATA_TYPE, H, W, BASENAME) \
74 union { \
Gian Marco Iodice8155c022021-04-16 15:08:59 +010075 DATA_TYPE s[TILE_VECTOR_SIZE##W]; \
76 TILE_VECTOR_TYPE##W(DATA_TYPE) v; \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000077 } BASENAME[H]
78
Giorgio Arenabdd16d12021-05-13 16:58:51 +010079#define TENSOR4D_IMAGE(name) \
80 __read_only image2d_t name##_img, \
81 __global uchar *name##_ptr, \
82 uint name##_stride_x, \
83 uint name##_step_x, \
84 uint name##_stride_y, \
85 uint name##_step_y, \
86 uint name##_stride_z, \
87 uint name##_step_z, \
88 uint name##_stride_w, \
89 uint name##_step_w, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000090 uint name##_offset_first_element_in_bytes
91
Giorgio Arenabdd16d12021-05-13 16:58:51 +010092#define TENSOR4D_BUFFER(name) \
93 __global uchar *name##_ptr, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000094 uint name##_stride_x, \
95 uint name##_step_x, \
96 uint name##_stride_y, \
97 uint name##_step_y, \
98 uint name##_stride_z, \
99 uint name##_step_z, \
100 uint name##_stride_w, \
101 uint name##_step_w, \
102 uint name##_offset_first_element_in_bytes
103
104#define TENSOR4D_STR(name, type) TENSOR4D_##type(name)
105#define TENSOR4D(name, type) TENSOR4D_STR(name, type)
106
Giorgio Arenaea8d2662021-05-20 11:36:56 +0100107#if !defined(UNROLL_WITH_PRAGMA)
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100108#define UNROLL_INCR(idx, step, macro) idx += (step); (macro)
109
110#define LOOP_UNROLLING_1(idx, step, macro) (macro)
111#define LOOP_UNROLLING_2(idx, step, macro) LOOP_UNROLLING_1(idx, step, macro); UNROLL_INCR(idx, step, macro)
112#define LOOP_UNROLLING_3(idx, step, macro) LOOP_UNROLLING_2(idx, step, macro); UNROLL_INCR(idx, step, macro)
113#define LOOP_UNROLLING_4(idx, step, macro) LOOP_UNROLLING_3(idx, step, macro); UNROLL_INCR(idx, step, macro)
114#define LOOP_UNROLLING_5(idx, step, macro) LOOP_UNROLLING_4(idx, step, macro); UNROLL_INCR(idx, step, macro)
115#define LOOP_UNROLLING_6(idx, step, macro) LOOP_UNROLLING_5(idx, step, macro); UNROLL_INCR(idx, step, macro)
116#define LOOP_UNROLLING_7(idx, step, macro) LOOP_UNROLLING_6(idx, step, macro); UNROLL_INCR(idx, step, macro)
117#define LOOP_UNROLLING_8(idx, step, macro) LOOP_UNROLLING_7(idx, step, macro); UNROLL_INCR(idx, step, macro)
118#define LOOP_UNROLLING_9(idx, step, macro) LOOP_UNROLLING_8(idx, step, macro); UNROLL_INCR(idx, step, macro)
119#define LOOP_UNROLLING_10(idx, step, macro) LOOP_UNROLLING_9(idx, step, macro); UNROLL_INCR(idx, step, macro)
120#define LOOP_UNROLLING_11(idx, step, macro) LOOP_UNROLLING_10(idx, step, macro); UNROLL_INCR(idx, step, macro)
121#define LOOP_UNROLLING_12(idx, step, macro) LOOP_UNROLLING_11(idx, step, macro); UNROLL_INCR(idx, step, macro)
122#define LOOP_UNROLLING_13(idx, step, macro) LOOP_UNROLLING_12(idx, step, macro); UNROLL_INCR(idx, step, macro)
123#define LOOP_UNROLLING_14(idx, step, macro) LOOP_UNROLLING_13(idx, step, macro); UNROLL_INCR(idx, step, macro)
124#define LOOP_UNROLLING_15(idx, step, macro) LOOP_UNROLLING_14(idx, step, macro); UNROLL_INCR(idx, step, macro)
125#define LOOP_UNROLLING_16(idx, step, macro) LOOP_UNROLLING_15(idx, step, macro); UNROLL_INCR(idx, step, macro)
126#define LOOP_UNROLLING_17(idx, step, macro) LOOP_UNROLLING_16(idx, step, macro); UNROLL_INCR(idx, step, macro)
127#define LOOP_UNROLLING_18(idx, step, macro) LOOP_UNROLLING_17(idx, step, macro); UNROLL_INCR(idx, step, macro)
128#define LOOP_UNROLLING_19(idx, step, macro) LOOP_UNROLLING_18(idx, step, macro); UNROLL_INCR(idx, step, macro)
129#define LOOP_UNROLLING_20(idx, step, macro) LOOP_UNROLLING_19(idx, step, macro); UNROLL_INCR(idx, step, macro)
130#define LOOP_UNROLLING_21(idx, step, macro) LOOP_UNROLLING_20(idx, step, macro); UNROLL_INCR(idx, step, macro)
131#define LOOP_UNROLLING_22(idx, step, macro) LOOP_UNROLLING_21(idx, step, macro); UNROLL_INCR(idx, step, macro)
132#define LOOP_UNROLLING_23(idx, step, macro) LOOP_UNROLLING_22(idx, step, macro); UNROLL_INCR(idx, step, macro)
133#define LOOP_UNROLLING_24(idx, step, macro) LOOP_UNROLLING_23(idx, step, macro); UNROLL_INCR(idx, step, macro)
134#define LOOP_UNROLLING_25(idx, step, macro) LOOP_UNROLLING_24(idx, step, macro); UNROLL_INCR(idx, step, macro)
135#define LOOP_UNROLLING_26(idx, step, macro) LOOP_UNROLLING_25(idx, step, macro); UNROLL_INCR(idx, step, macro)
136#define LOOP_UNROLLING_27(idx, step, macro) LOOP_UNROLLING_26(idx, step, macro); UNROLL_INCR(idx, step, macro)
137#define LOOP_UNROLLING_28(idx, step, macro) LOOP_UNROLLING_27(idx, step, macro); UNROLL_INCR(idx, step, macro)
138#define LOOP_UNROLLING_29(idx, step, macro) LOOP_UNROLLING_28(idx, step, macro); UNROLL_INCR(idx, step, macro)
139#define LOOP_UNROLLING_30(idx, step, macro) LOOP_UNROLLING_29(idx, step, macro); UNROLL_INCR(idx, step, macro)
140#define LOOP_UNROLLING_31(idx, step, macro) LOOP_UNROLLING_30(idx, step, macro); UNROLL_INCR(idx, step, macro)
141#define LOOP_UNROLLING_32(idx, step, macro) LOOP_UNROLLING_31(idx, step, macro); UNROLL_INCR(idx, step, macro)
142#define LOOP_UNROLLING_33(idx, step, macro) LOOP_UNROLLING_32(idx, step, macro); UNROLL_INCR(idx, step, macro)
143#define LOOP_UNROLLING_34(idx, step, macro) LOOP_UNROLLING_33(idx, step, macro); UNROLL_INCR(idx, step, macro)
144#define LOOP_UNROLLING_35(idx, step, macro) LOOP_UNROLLING_34(idx, step, macro); UNROLL_INCR(idx, step, macro)
145#define LOOP_UNROLLING_36(idx, step, macro) LOOP_UNROLLING_35(idx, step, macro); UNROLL_INCR(idx, step, macro)
146#define LOOP_UNROLLING_37(idx, step, macro) LOOP_UNROLLING_36(idx, step, macro); UNROLL_INCR(idx, step, macro)
147#define LOOP_UNROLLING_38(idx, step, macro) LOOP_UNROLLING_37(idx, step, macro); UNROLL_INCR(idx, step, macro)
148#define LOOP_UNROLLING_39(idx, step, macro) LOOP_UNROLLING_38(idx, step, macro); UNROLL_INCR(idx, step, macro)
149#define LOOP_UNROLLING_40(idx, step, macro) LOOP_UNROLLING_39(idx, step, macro); UNROLL_INCR(idx, step, macro)
150#define LOOP_UNROLLING_41(idx, step, macro) LOOP_UNROLLING_40(idx, step, macro); UNROLL_INCR(idx, step, macro)
151#define LOOP_UNROLLING_42(idx, step, macro) LOOP_UNROLLING_41(idx, step, macro); UNROLL_INCR(idx, step, macro)
152#define LOOP_UNROLLING_43(idx, step, macro) LOOP_UNROLLING_42(idx, step, macro); UNROLL_INCR(idx, step, macro)
153#define LOOP_UNROLLING_44(idx, step, macro) LOOP_UNROLLING_43(idx, step, macro); UNROLL_INCR(idx, step, macro)
154#define LOOP_UNROLLING_45(idx, step, macro) LOOP_UNROLLING_44(idx, step, macro); UNROLL_INCR(idx, step, macro)
155#define LOOP_UNROLLING_46(idx, step, macro) LOOP_UNROLLING_45(idx, step, macro); UNROLL_INCR(idx, step, macro)
156#define LOOP_UNROLLING_47(idx, step, macro) LOOP_UNROLLING_46(idx, step, macro); UNROLL_INCR(idx, step, macro)
157#define LOOP_UNROLLING_48(idx, step, macro) LOOP_UNROLLING_47(idx, step, macro); UNROLL_INCR(idx, step, macro)
158#define LOOP_UNROLLING_49(idx, step, macro) LOOP_UNROLLING_48(idx, step, macro); UNROLL_INCR(idx, step, macro)
159#define LOOP_UNROLLING_50(idx, step, macro) LOOP_UNROLLING_49(idx, step, macro); UNROLL_INCR(idx, step, macro)
160#define LOOP_UNROLLING_51(idx, step, macro) LOOP_UNROLLING_50(idx, step, macro); UNROLL_INCR(idx, step, macro)
161#define LOOP_UNROLLING_52(idx, step, macro) LOOP_UNROLLING_51(idx, step, macro); UNROLL_INCR(idx, step, macro)
162#define LOOP_UNROLLING_53(idx, step, macro) LOOP_UNROLLING_52(idx, step, macro); UNROLL_INCR(idx, step, macro)
163#define LOOP_UNROLLING_54(idx, step, macro) LOOP_UNROLLING_53(idx, step, macro); UNROLL_INCR(idx, step, macro)
164#define LOOP_UNROLLING_55(idx, step, macro) LOOP_UNROLLING_54(idx, step, macro); UNROLL_INCR(idx, step, macro)
165#define LOOP_UNROLLING_56(idx, step, macro) LOOP_UNROLLING_55(idx, step, macro); UNROLL_INCR(idx, step, macro)
166#define LOOP_UNROLLING_57(idx, step, macro) LOOP_UNROLLING_56(idx, step, macro); UNROLL_INCR(idx, step, macro)
167#define LOOP_UNROLLING_58(idx, step, macro) LOOP_UNROLLING_57(idx, step, macro); UNROLL_INCR(idx, step, macro)
168#define LOOP_UNROLLING_59(idx, step, macro) LOOP_UNROLLING_58(idx, step, macro); UNROLL_INCR(idx, step, macro)
169#define LOOP_UNROLLING_60(idx, step, macro) LOOP_UNROLLING_59(idx, step, macro); UNROLL_INCR(idx, step, macro)
170#define LOOP_UNROLLING_61(idx, step, macro) LOOP_UNROLLING_60(idx, step, macro); UNROLL_INCR(idx, step, macro)
171#define LOOP_UNROLLING_62(idx, step, macro) LOOP_UNROLLING_61(idx, step, macro); UNROLL_INCR(idx, step, macro)
172#define LOOP_UNROLLING_63(idx, step, macro) LOOP_UNROLLING_62(idx, step, macro); UNROLL_INCR(idx, step, macro)
173#define LOOP_UNROLLING_64(idx, step, macro) LOOP_UNROLLING_63(idx, step, macro); UNROLL_INCR(idx, step, macro)
174#define LOOP_UNROLLING_65(idx, step, macro) LOOP_UNROLLING_64(idx, step, macro); UNROLL_INCR(idx, step, macro)
175#define LOOP_UNROLLING_66(idx, step, macro) LOOP_UNROLLING_65(idx, step, macro); UNROLL_INCR(idx, step, macro)
176#define LOOP_UNROLLING_67(idx, step, macro) LOOP_UNROLLING_66(idx, step, macro); UNROLL_INCR(idx, step, macro)
177#define LOOP_UNROLLING_68(idx, step, macro) LOOP_UNROLLING_67(idx, step, macro); UNROLL_INCR(idx, step, macro)
178#define LOOP_UNROLLING_69(idx, step, macro) LOOP_UNROLLING_68(idx, step, macro); UNROLL_INCR(idx, step, macro)
179#define LOOP_UNROLLING_70(idx, step, macro) LOOP_UNROLLING_69(idx, step, macro); UNROLL_INCR(idx, step, macro)
180#define LOOP_UNROLLING_71(idx, step, macro) LOOP_UNROLLING_70(idx, step, macro); UNROLL_INCR(idx, step, macro)
181#define LOOP_UNROLLING_72(idx, step, macro) LOOP_UNROLLING_71(idx, step, macro); UNROLL_INCR(idx, step, macro)
182#define LOOP_UNROLLING_73(idx, step, macro) LOOP_UNROLLING_72(idx, step, macro); UNROLL_INCR(idx, step, macro)
183#define LOOP_UNROLLING_74(idx, step, macro) LOOP_UNROLLING_73(idx, step, macro); UNROLL_INCR(idx, step, macro)
184#define LOOP_UNROLLING_75(idx, step, macro) LOOP_UNROLLING_74(idx, step, macro); UNROLL_INCR(idx, step, macro)
185#define LOOP_UNROLLING_76(idx, step, macro) LOOP_UNROLLING_75(idx, step, macro); UNROLL_INCR(idx, step, macro)
186#define LOOP_UNROLLING_77(idx, step, macro) LOOP_UNROLLING_76(idx, step, macro); UNROLL_INCR(idx, step, macro)
187#define LOOP_UNROLLING_78(idx, step, macro) LOOP_UNROLLING_77(idx, step, macro); UNROLL_INCR(idx, step, macro)
188#define LOOP_UNROLLING_79(idx, step, macro) LOOP_UNROLLING_78(idx, step, macro); UNROLL_INCR(idx, step, macro)
189#define LOOP_UNROLLING_80(idx, step, macro) LOOP_UNROLLING_79(idx, step, macro); UNROLL_INCR(idx, step, macro)
190#define LOOP_UNROLLING_81(idx, step, macro) LOOP_UNROLLING_80(idx, step, macro); UNROLL_INCR(idx, step, macro)
191#define LOOP_UNROLLING_82(idx, step, macro) LOOP_UNROLLING_81(idx, step, macro); UNROLL_INCR(idx, step, macro)
192#define LOOP_UNROLLING_83(idx, step, macro) LOOP_UNROLLING_82(idx, step, macro); UNROLL_INCR(idx, step, macro)
193#define LOOP_UNROLLING_84(idx, step, macro) LOOP_UNROLLING_83(idx, step, macro); UNROLL_INCR(idx, step, macro)
194#define LOOP_UNROLLING_85(idx, step, macro) LOOP_UNROLLING_84(idx, step, macro); UNROLL_INCR(idx, step, macro)
195#define LOOP_UNROLLING_86(idx, step, macro) LOOP_UNROLLING_85(idx, step, macro); UNROLL_INCR(idx, step, macro)
196#define LOOP_UNROLLING_87(idx, step, macro) LOOP_UNROLLING_86(idx, step, macro); UNROLL_INCR(idx, step, macro)
197#define LOOP_UNROLLING_88(idx, step, macro) LOOP_UNROLLING_87(idx, step, macro); UNROLL_INCR(idx, step, macro)
198#define LOOP_UNROLLING_89(idx, step, macro) LOOP_UNROLLING_88(idx, step, macro); UNROLL_INCR(idx, step, macro)
199#define LOOP_UNROLLING_90(idx, step, macro) LOOP_UNROLLING_89(idx, step, macro); UNROLL_INCR(idx, step, macro)
200#define LOOP_UNROLLING_91(idx, step, macro) LOOP_UNROLLING_90(idx, step, macro); UNROLL_INCR(idx, step, macro)
201#define LOOP_UNROLLING_92(idx, step, macro) LOOP_UNROLLING_91(idx, step, macro); UNROLL_INCR(idx, step, macro)
202#define LOOP_UNROLLING_93(idx, step, macro) LOOP_UNROLLING_92(idx, step, macro); UNROLL_INCR(idx, step, macro)
203#define LOOP_UNROLLING_94(idx, step, macro) LOOP_UNROLLING_93(idx, step, macro); UNROLL_INCR(idx, step, macro)
204#define LOOP_UNROLLING_95(idx, step, macro) LOOP_UNROLLING_94(idx, step, macro); UNROLL_INCR(idx, step, macro)
205#define LOOP_UNROLLING_96(idx, step, macro) LOOP_UNROLLING_95(idx, step, macro); UNROLL_INCR(idx, step, macro)
206#define LOOP_UNROLLING_97(idx, step, macro) LOOP_UNROLLING_96(idx, step, macro); UNROLL_INCR(idx, step, macro)
207#define LOOP_UNROLLING_98(idx, step, macro) LOOP_UNROLLING_97(idx, step, macro); UNROLL_INCR(idx, step, macro)
208#define LOOP_UNROLLING_99(idx, step, macro) LOOP_UNROLLING_98(idx, step, macro); UNROLL_INCR(idx, step, macro)
209#define LOOP_UNROLLING_100(idx, step, macro) LOOP_UNROLLING_99(idx, step, macro); UNROLL_INCR(idx, step, macro)
210#define LOOP_UNROLLING_101(idx, step, macro) LOOP_UNROLLING_100(idx, step, macro); UNROLL_INCR(idx, step, macro)
211#define LOOP_UNROLLING_102(idx, step, macro) LOOP_UNROLLING_101(idx, step, macro); UNROLL_INCR(idx, step, macro)
212#define LOOP_UNROLLING_103(idx, step, macro) LOOP_UNROLLING_102(idx, step, macro); UNROLL_INCR(idx, step, macro)
213#define LOOP_UNROLLING_104(idx, step, macro) LOOP_UNROLLING_103(idx, step, macro); UNROLL_INCR(idx, step, macro)
214#define LOOP_UNROLLING_105(idx, step, macro) LOOP_UNROLLING_104(idx, step, macro); UNROLL_INCR(idx, step, macro)
215#define LOOP_UNROLLING_106(idx, step, macro) LOOP_UNROLLING_105(idx, step, macro); UNROLL_INCR(idx, step, macro)
216#define LOOP_UNROLLING_107(idx, step, macro) LOOP_UNROLLING_106(idx, step, macro); UNROLL_INCR(idx, step, macro)
217#define LOOP_UNROLLING_108(idx, step, macro) LOOP_UNROLLING_107(idx, step, macro); UNROLL_INCR(idx, step, macro)
218#define LOOP_UNROLLING_109(idx, step, macro) LOOP_UNROLLING_108(idx, step, macro); UNROLL_INCR(idx, step, macro)
219#define LOOP_UNROLLING_110(idx, step, macro) LOOP_UNROLLING_109(idx, step, macro); UNROLL_INCR(idx, step, macro)
220#define LOOP_UNROLLING_111(idx, step, macro) LOOP_UNROLLING_110(idx, step, macro); UNROLL_INCR(idx, step, macro)
221#define LOOP_UNROLLING_112(idx, step, macro) LOOP_UNROLLING_111(idx, step, macro); UNROLL_INCR(idx, step, macro)
222#define LOOP_UNROLLING_113(idx, step, macro) LOOP_UNROLLING_112(idx, step, macro); UNROLL_INCR(idx, step, macro)
223#define LOOP_UNROLLING_114(idx, step, macro) LOOP_UNROLLING_113(idx, step, macro); UNROLL_INCR(idx, step, macro)
224#define LOOP_UNROLLING_115(idx, step, macro) LOOP_UNROLLING_114(idx, step, macro); UNROLL_INCR(idx, step, macro)
225#define LOOP_UNROLLING_116(idx, step, macro) LOOP_UNROLLING_115(idx, step, macro); UNROLL_INCR(idx, step, macro)
226#define LOOP_UNROLLING_117(idx, step, macro) LOOP_UNROLLING_116(idx, step, macro); UNROLL_INCR(idx, step, macro)
227#define LOOP_UNROLLING_118(idx, step, macro) LOOP_UNROLLING_117(idx, step, macro); UNROLL_INCR(idx, step, macro)
228#define LOOP_UNROLLING_119(idx, step, macro) LOOP_UNROLLING_118(idx, step, macro); UNROLL_INCR(idx, step, macro)
229#define LOOP_UNROLLING_120(idx, step, macro) LOOP_UNROLLING_119(idx, step, macro); UNROLL_INCR(idx, step, macro)
230#define LOOP_UNROLLING_121(idx, step, macro) LOOP_UNROLLING_120(idx, step, macro); UNROLL_INCR(idx, step, macro)
231#define LOOP_UNROLLING_122(idx, step, macro) LOOP_UNROLLING_121(idx, step, macro); UNROLL_INCR(idx, step, macro)
232#define LOOP_UNROLLING_123(idx, step, macro) LOOP_UNROLLING_122(idx, step, macro); UNROLL_INCR(idx, step, macro)
233#define LOOP_UNROLLING_124(idx, step, macro) LOOP_UNROLLING_123(idx, step, macro); UNROLL_INCR(idx, step, macro)
234#define LOOP_UNROLLING_125(idx, step, macro) LOOP_UNROLLING_124(idx, step, macro); UNROLL_INCR(idx, step, macro)
235#define LOOP_UNROLLING_126(idx, step, macro) LOOP_UNROLLING_125(idx, step, macro); UNROLL_INCR(idx, step, macro)
236#define LOOP_UNROLLING_127(idx, step, macro) LOOP_UNROLLING_126(idx, step, macro); UNROLL_INCR(idx, step, macro)
237#define LOOP_UNROLLING_128(idx, step, macro) LOOP_UNROLLING_127(idx, step, macro); UNROLL_INCR(idx, step, macro)
238
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100239#define LOOP_UNROLLING_STR(type, idx, start, step, num, macro) \
240 { \
241 type idx = start; \
242 LOOP_UNROLLING_##num(idx, step, macro); \
243 }
Giorgio Arenaea8d2662021-05-20 11:36:56 +0100244#else // !defined(UNROLL_WITH_PRAGMA)
245#define LOOP_UNROLLING_STR(type, idx, start, step, num, macro) \
246 { \
247 _Pragma("unroll") \
248 for(type idx = start; idx < (num * step); idx += step) \
249 { \
250 (macro); \
251 } \
252 }
253#endif // !defined(UNROLL_WITH_PRAGMA)
254#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 +0000255
256/** 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
257 * to avoid out-of-bound read/write
258 *
259 * @note PARTIAL_N0 is used for get_global_id(n) = 0.
260 *
261 * @param[in] IDX get_global_id index (0,1 and 2 only)
262 * @param[in] N0 Number of elements read/written on the IDX direction
263 * @param[in] PARTIAL_N0 Number of elements read/written on the IDX direction for get_global_id(IDX) = 0. If zero,
264 * the Number of elements read/written on the IDX direction for get_global_id(IDX) = 0 is N0
265 */
266#define GET_SPATIAL_IDX(IDX, N0, PARTIAL_N0) (max((int)(get_global_id(IDX) * N0 - (N0 - PARTIAL_N0) % N0), 0))
267
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000268/** Dot product integet 8bit function
269 *
270 * @note Performs: c += dot(a, b)
271 *
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100272 * @param[in] A_DATA_TYPE A (lhs) data type
273 * @param[in] B_DATA_TYPE B (rhs) data type
274 * @param[in] C_DATA_TYPE C (accumulator) data type
275 * @param[in] K0 Number of accumulations
276 * @param[in] a OpenCL vector a
277 * @param[in] b OpenCL vector b
278 * @param[in] c Scalar variable c
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000279 */
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100280#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)
281#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)
282#define DOT_PRODUCT1_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000283 ({ \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100284 c += (C_DATA_TYPE)(a) * (C_DATA_TYPE)(b); \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000285 })
286#if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100287#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));
288#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));
289#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 +0000290#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 +0100291#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 +0100292#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));
293#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 +0000294#else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100295#define DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
296 ({ \
297 c += (C_DATA_TYPE)(a).s0 * (C_DATA_TYPE)(b).s0; \
298 c += (C_DATA_TYPE)(a).s1 * (C_DATA_TYPE)(b).s1; \
299 })
300#define DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
301 ({ \
302 DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c); \
303 c += (C_DATA_TYPE)(a).s2 * (C_DATA_TYPE)(b).s2; \
304 })
305#define DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, x, y, val) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000306 ({ \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100307 val += (C_DATA_TYPE)(x).s0 * (C_DATA_TYPE)(y).s0; \
308 val += (C_DATA_TYPE)(x).s1 * (C_DATA_TYPE)(y).s1; \
309 val += (C_DATA_TYPE)(x).s2 * (C_DATA_TYPE)(y).s2; \
310 val += (C_DATA_TYPE)(x).s3 * (C_DATA_TYPE)(y).s3; \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000311 })
312#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100313#define DOT_PRODUCT5_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
314 ({ \
315 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s0123), ((b).s0123), c); \
316 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 +0000317 })
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100318#define DOT_PRODUCT6_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
319 ({ \
320 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s0123), ((b).s0123), c); \
321 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 +0000322 })
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100323#define DOT_PRODUCT7_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
324 ({ \
325 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s0123), ((b).s0123), c); \
326 DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s456), ((b).s456), c); \
327 })
328#define DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
329 ({ \
330 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).lo), ((b).lo), c); \
331 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).hi), ((b).hi), c); \
332 })
333#define DOT_PRODUCT9_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
334 ({ \
335 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
336 DOT_PRODUCT1_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s8), ((b).s8), c); \
337 })
338#define DOT_PRODUCT10_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
339 ({ \
340 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
341 DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89), ((b).s89), c); \
342 })
343#define DOT_PRODUCT11_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
344 ({ \
345 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
346 DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89A), ((b).s89A), c); \
347 })
348#define DOT_PRODUCT12_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
349 ({ \
350 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
351 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89AB), ((b).s89AB), c); \
352 })
353#define DOT_PRODUCT13_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
354 ({ \
355 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
356 DOT_PRODUCT5_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89ABC), ((b).s89ABC), c); \
357 })
358#define DOT_PRODUCT14_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
359 ({ \
360 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
361 DOT_PRODUCT6_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89ABCD), ((b).s89ABCD), c); \
362 })
363#define DOT_PRODUCT15_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
364 ({ \
365 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
366 DOT_PRODUCT7_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89ABCDE), ((b).s89ABCDE), c); \
367 })
368#define DOT_PRODUCT16_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
369 ({ \
370 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).lo), ((b).lo), c); \
371 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).hi), ((b).hi), c); \
372 })
373
374/** Dot product integet 8bit function
375 *
376 * @note Performs: c += dot(a, b)
377 *
378 * @param[in] A_DATA_TYPE A (lhs) data type
379 * @param[in] B_DATA_TYPE B (rhs) data type
380 * @param[in] C_DATA_TYPE C (accumulator) data type
381 * @param[in] K0 Number of accumulations
382 * @param[in] a OpenCL vector a
383 * @param[in] c Scalar variable c
384 */
385#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)
386#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 +0000387
388/** Load a vector from global memory (tensor)
389 *
390 * @param[in] DATA_TYPE Data type
391 * @param[in] WIDTH Number of dst columns
392 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
393 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
394 * @param[in] TENSOR Tensor basename
395 * @param[in] X Starting X position
396 * @param[in] Y Starting Y position
397 * @param[in] STRIDE_Y Stride Y (in bytes)
398 */
399#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)
400#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)
401#define V_LOAD_BUFFER(DATA_TYPE, WIDTH, TENSOR, X, Y, STRIDE_Y) \
402 VLOAD(WIDTH) \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100403 (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 +0000404#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))
405
406/** Load a tile from global memory (tensor)
407 *
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100408 * @param[in] DATA_TYPE Data type
409 * @param[in] HEIGHT Number of dst rows
410 * @param[in] WIDTH Number of dst columns
411 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
412 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
413 * @param[in] TENSOR Tensor basename
414 * @param[in] X Starting X position
415 * @param[in] Y Starting Y position
416 * @param[in] YI_MULTIPLIER Parameter used to multiply the internal row increment (_i).
417 * 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).
418 * In this case the address calculation is performed as: (Y + _i * Y_MULTIPLIER) * STRIDE_Y
419 * @param[in] STRIDE_Y Stride Y (in bytes) used to load each row.
420 * @param[out] dst Output tile
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000421 */
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100422#define T_LOAD(DATA_TYPE, HEIGHT, WIDTH, TENSOR_TYPE, TENSOR, X, Y, YI_MULTIPLIER, STRIDE_Y, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100423 ({ \
424 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
425 { \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100426 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 +0100427 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000428 })
429
430/** Load a tile from global memory (tensor) using an indirect Y index tile
431 *
432 * @param[in] DATA_TYPE Data type
433 * @param[in] HEIGHT Number of dst rows
434 * @param[in] WIDTH Number of dst columns
435 * @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
436 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
437 * @param[in] TENSOR Tensor basename
438 * @param[in] X Starting X position
439 * @param[in] STRIDE_Y Stride Y (in bytes)
440 * @param[in] indirect_y Indirect Y index tile
441 * @param[out] dst Output tile
442 */
443#define T_LOAD_INDIRECT(DATA_TYPE, HEIGHT, WIDTH, TENSOR_TYPE, TENSOR, X, STRIDE_Y, indirect_y, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100444 ({ \
445 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
446 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000447 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 +0100448 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000449 })
450
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100451/** Load a tile from global memory (tensor) when the tensor is stored using a NHWC layout
452 *
453 * @param[in] DATA_TYPE Data type
454 * @param[in] TILE_HEIGHT Number of elements to load from Y (height) dimension
455 * @param[in] TILE_WIDTH Number of elements to load from X (width) dimension
456 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
457 * @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
458 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
459 * @param[in] TENSOR Tensor basename
460 * @param[in] B Starting batch index
461 * @param[in] Y Starting Y index
462 * @param[in] X Starting X index
463 * @param[in] C Starting C index
464 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
465 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
466 * @param[in] STRIDE_Y Stride Y (in bytes)
467 * @param[out] dst Output tile
468 */
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100469#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 +0100470 ({ \
471 LOOP_UNROLLING(int, _yk, 0, 1, TILE_HEIGHT, \
472 { \
473 LOOP_UNROLLING(int, _xk, 0, 1, TILE_WIDTH, \
474 { \
475 int _src_y = (X) + _xk + ((Y) + _yk) * (TENSOR_WIDTH); \
476 _src_y += (B) * (int)(TENSOR_WIDTH) * (int)(TENSOR_HEIGHT); \
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100477 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 +0100478 if(_src_valid_y != 0) \
479 { \
480 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 +0100481 } \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100482 }) \
483 }) \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100484 })
485
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100486/** Load a tile from global memory (tensor) when the tensor is stored using a NHWC layout with dilation for the X and Y increments
487 *
488 * @param[in] DATA_TYPE Data type
489 * @param[in] TILE_HEIGHT Number of elements to load from Y (height) dimension
490 * @param[in] TILE_WIDTH Number of elements to load from X (width) dimension
491 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
492 * @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
493 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
494 * @param[in] TENSOR Tensor basename
495 * @param[in] B Starting batch index
496 * @param[in] Y Starting Y index
497 * @param[in] X Starting X index
498 * @param[in] C Starting C index
499 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
500 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
501 * @param[in] DILATION_X Dilation for the X increment
502 * @param[in] DILATION_Y Dilation for the Y increment
503 * @param[in] BOUNDARY_CHECK Boundary check flag. If true, it checks for any out-of-bound reads
504 * @param[out] dst Output tile
505 */
506#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) \
507 ({ \
508 LOOP_UNROLLING(int, _yk, 0, 1, TILE_HEIGHT, \
509 { \
510 LOOP_UNROLLING(int, _xk, 0, 1, TILE_WIDTH, \
511 { \
512 int _src_y = (X) + _xk * (DILATION_X); \
513 int _src_z = ((Y) + _yk * (DILATION_Y)); \
514 int _src_w = (B); \
515 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)); \
516 if(!(BOUNDARY_CHECK)) \
517 { \
518 dst[_xk + _yk * (TILE_WIDTH)].v = VLOAD(TILE_CHANNELS) \
519 (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))); \
520 } \
521 else \
522 { \
523 if(_src_valid_y) \
524 { \
525 dst[_xk + _yk * (TILE_WIDTH)].v = VLOAD(TILE_CHANNELS) \
526 (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))); \
527 } \
528 } \
529 }) \
530 }) \
531 })
532
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100533/** Load a tile from global memory (tensor) when the tensor is stored using a NHWC layout using indirect X and Y coordinates
534 *
535 * @param[in] DATA_TYPE Data type
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100536 * @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 +0100537 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
538 * @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
539 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
540 * @param[in] TENSOR Tensor basename
541 * @param[in] B Starting batch index
542 * @param[in] Y Starting Y index
543 * @param[in] X Starting X index
544 * @param[in] C Starting C index
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100545 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100546 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100547 * @param[in] STRIDE_Y Stride Y (in bytes)
548 * @param[out] xi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect X coordinate
549 * @param[out] yi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect Y coordinate
550 * @param[out] dst Output tile
551 */
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100552#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) \
553 ({ \
554 LOOP_UNROLLING(int, _i, 0, 1, TILE_AREA, \
555 { \
556 int _src_y = (X) + xi[_i].v + ((Y) + yi[_i].v) * (TENSOR_WIDTH); \
557 _src_y += (B) * (int)(TENSOR_WIDTH) * (int)(TENSOR_HEIGHT); \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100558 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 +0100559 if(_src_valid_y != 0) \
560 { \
561 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 +0100562 } \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100563 }) \
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100564 })
565
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100566/** Load a tile from global memory (tensor) when the tensor is stored using a NDHWC layout using indirect X, Y and Z coordinates
567 *
568 * @param[in] DATA_TYPE Data type
569 * @param[in] TILE_AREA Number of elements to load from Y (height) dimension * Number of elements to load from X (width) dimension
570 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
571 * @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
572 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
573 * @param[in] TENSOR Tensor basename
574 * @param[in] B Starting batch index
575 * @param[in] Z Starting Z index
576 * @param[in] Y Starting Y index
577 * @param[in] X Starting X index
578 * @param[in] C Starting C index
579 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
580 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
581 * @param[in] TENSOR_DEPTH Number of elements to load from Z (depth) dimension
582 * @param[in] STRIDE_Y Stride Y (in bytes)
583 * @param[out] xi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect X coordinate
584 * @param[out] yi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect Y coordinate
585 * @param[out] zi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect Z coordinate
586 * @param[out] dst Output tile
587 */
588#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) \
589 ({ \
590 LOOP_UNROLLING(int, _i, 0, 1, TILE_AREA, \
591 { \
592 int _src_y = (X) + xi[_i].v + ((Y) + yi[_i].v) * (TENSOR_WIDTH) + ((Z) + zi[_i].v) * (TENSOR_WIDTH * TENSOR_HEIGHT); \
593 _src_y += (B) * (int)(TENSOR_WIDTH) * (int)(TENSOR_HEIGHT) * (int)(TENSOR_DEPTH); \
594 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) \
595 && ((Z) + zi[_i].v) >= 0 && ((Z) + zi[_i].v) < (int)(TENSOR_DEPTH)); \
596 if(_src_valid_y != 0) \
597 { \
598 dst[_i].v = V_LOAD(DATA_TYPE, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, _src_y, STRIDE_Y); \
599 } \
600 }) \
601 })
602
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000603/** Store a tile to global memory (tensor) using an indirect Y index tile and conditionally use a different length for the store
604 *
605 * @note If WIDTH1_CONDITION is true, the store will use the WIDTH1 length for the store
606 * @note The vectors are stored in reverse order so the invalid rows are overwritten by the valid ones
607 *
608 * @param[in] DATA_TYPE Data type
609 * @param[in] HEIGHT Number of src rows
610 * @param[in] WIDTH0 Store width to use if WIDTH1_CONDITION = false
611 * @param[in] WIDTH1 Store width to use if WIDTH1_CONDITION = true
612 * @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
613 * cl_image is not supported.
614 * @param[in] TENSOR Tensor basename
615 * @param[in] X Starting X position
616 * @param[in] STRIDE_Y Stride Y (in bytes)
617 * @param[in] WIDTH1_CONDITION Condition to select the WIDTH1 store
618 * @param[in] src Input tile
619 * @param[in] indirect_y Indirect Y index tile
620 */
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000621#define T_STORE_INDIRECT_WIDTH_SELECT(DATA_TYPE, HEIGHT, WIDTH0, WIDTH1, TENSOR_TYPE, TENSOR, X, STRIDE_Y, WIDTH1_CONDITION, src, indirect_y) \
622 ({ \
623 if(WIDTH1_CONDITION) \
624 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100625 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000626 { \
627 VSTORE_PARTIAL(WIDTH0, WIDTH1) \
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100628 (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 +0100629 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000630 } \
631 else \
632 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100633 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000634 { \
635 VSTORE(WIDTH0) \
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100636 (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 +0100637 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000638 } \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000639 })
640
641/** Offset correction for the QASYMM8 computation
642 *
643 * @param[in] ACC_DATA_TYPE Accumulator data type
644 * @param[in] M0 Number of src/dst rows
645 * @param[in] N0 Number of src/dst columns
646 * @param[in] K0 Number of src columns
647 * @param[in] SRC_OFFSET Source quantization offset
648 * @param[in] WEI_OFFSET Weights quantization shift
649 * @param[in] lhs LHS tile
650 * @param[in] rhs RHS tile
651 * @param[out] dst DST tile
652 */
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100653#define T_OFFSET_CORRECTION(ACC_DATA_TYPE, M0, N0, K0, SRC_OFFSET, WEI_OFFSET, lhs, rhs, dst) \
654 ({ \
655 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
656 { \
657 ACC_DATA_TYPE _tm = 0; \
658 LOOP_UNROLLING(int, _k0, 0, 1, K0, \
659 { \
660 _tm += ((ACC_DATA_TYPE)lhs[_m0].s[_k0] * (ACC_DATA_TYPE)WEI_OFFSET); \
661 }) \
662 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
663 { \
664 dst[_m0].s[_n0] += _tm; \
665 LOOP_UNROLLING(int, _k0, 0, 1, K0, \
666 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000667 dst[_m0].s[_n0] += ((ACC_DATA_TYPE)rhs[_n0].s[_k0] * (ACC_DATA_TYPE)SRC_OFFSET); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100668 }) \
669 }) \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100670 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000671 })
672
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100673/** 8-bit quantization with fixed-point scale
674 *
675 * @param[in] SRC_DATA_TYPE SRC data type
676 * @param[in] DST_DATA_TYPE DST data type
677 * @param[in] QUANTIZATION_TYPE Quantization type (PER_TENSOR or PER_CHANNEL)
678 * @param[in] M0 Number of src/dst rows
679 * @param[in] N0 Number of src/dst columns
680 * @param[in] DST_OFFSET Quantization offset used for both the per-tensor and per-channel quantization
681 * @param[in] DST_SHIFT Quantization shift for the per-tensor quantization
682 * @param[in] DST_MULTIPLIER Quantization multiplier for the per-tensor quantization
683 * @param[in] src Input tile
684 * @param[in] dst_multipliers Output multipliers tile for the per-channel quantization
685 * @param[in] dst_shifts Output shift tile for the per-channel quantization
686 * @param[out] dst Output tile
687 */
688#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)
689#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)
690
691/** 8-bit per-tensor quantization with fixed-point scale
692 *
693 * @param[in] SRC_DATA_TYPE SRC data type
694 * @param[in] DST_DATA_TYPE DST data type
695 * @param[in] M0 Number of src/dst rows
696 * @param[in] N0 Number of src/dst columns
697 * @param[in] DST_OFFSET Quantization offset
698 * @param[in] DST_SHIFT Quantization shift for the per-tensor quantization
699 * @param[in] DST_MULTIPLIER Quantization multiplier for the per-tensor quantization
700 * @param[in] src Input tile
701 * @param[in] dst_multipliers (unused)
702 * @param[in] dst_shifts (unused)
703 * @param[out] dst Output tile
704 */
705#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) \
706 ({ \
707 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
708 { \
709 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
710 { \
711 SRC_DATA_TYPE _tmp = 0; \
712 SRC_DATA_TYPE _src = src[_m0].s[_n0]; \
713 _src *= select((SRC_DATA_TYPE)1, ((SRC_DATA_TYPE)1 << (SRC_DATA_TYPE)(-DST_SHIFT)), ((SRC_DATA_TYPE)DST_SHIFT < (SRC_DATA_TYPE)0)); \
714 SRC_DATA_TYPE overflow = _src == DST_MULTIPLIER && _src == INT_MIN; \
715 long a_64 = (long)(_src); \
716 long b_64 = (long)(DST_MULTIPLIER); \
717 long ab_64 = a_64 * b_64; \
718 long mask1 = 1 << 30; \
719 long mask2 = 1 - (1 << 30); \
720 long is_positive_or_zero = ab_64 >= 0; \
721 long nudge = select(mask2, mask1, is_positive_or_zero); \
722 SRC_DATA_TYPE ab_x2_high32 = CONVERT((ab_64 + nudge) / (long)(1ll << 31), SRC_DATA_TYPE); \
723 _tmp = select(ab_x2_high32, (SRC_DATA_TYPE)INT_MAX, overflow); \
724 if(DST_SHIFT >= 0) \
725 { \
Freddie Liardet767dbf92021-07-21 16:20:41 +0100726 long mask = ((((int)1) << DST_SHIFT) - (long)1); \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100727 long threshold = _tmp < (int)0 ? (mask >> 1) + (long)1 : (mask >> 1) + 0; \
728 _tmp = (_tmp & mask) > threshold ? (_tmp >> DST_SHIFT) + (int)1 : (_tmp >> DST_SHIFT); \
729 } \
730 _tmp += DST_OFFSET; \
731 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
732 }) \
733 }) \
734 })
735
736/** 8-bit per-channel quantization with fixed-point scale
737 *
738 * @param[in] SRC_DATA_TYPE SRC data type
739 * @param[in] DST_DATA_TYPE DST data type
740 * @param[in] M0 Number of src/dst rows
741 * @param[in] N0 Number of src/dst columns
742 * @param[in] DST_OFFSET Quantization offset
743 * @param[in] DST_SHIFT (unused)
744 * @param[in] DST_MULTIPLIER (unused)
745 * @param[in] src Input tile
746 * @param[in] dst_multipliers Output multipliers tile for the per-channel quantization
747 * @param[in] dst_shifts Output shift tile for the per-channel quantization
748 * @param[out] dst Output tile
749 */
750#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) \
751 ({ \
752 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
753 { \
754 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
755 { \
756 SRC_DATA_TYPE _tmp = 0; \
757 SRC_DATA_TYPE _src = src[_m0].s[_n0]; \
758 SRC_DATA_TYPE _dst_multiplier = dst_multipliers[0].s[_n0]; \
759 SRC_DATA_TYPE _dst_shift = dst_shifts[0].s[_n0]; \
760 _src *= select((SRC_DATA_TYPE)1, ((SRC_DATA_TYPE)1 << (SRC_DATA_TYPE)(-_dst_shift)), ((SRC_DATA_TYPE)_dst_shift < (SRC_DATA_TYPE)0)); \
761 SRC_DATA_TYPE overflow = _src == _dst_multiplier && _src == INT_MIN; \
762 long a_64 = (long)(_src); \
763 long b_64 = (long)(_dst_multiplier); \
764 long ab_64 = a_64 * b_64; \
765 long mask1 = 1 << 30; \
766 long mask2 = 1 - (1 << 30); \
767 long is_positive_or_zero = ab_64 >= 0; \
768 long nudge = select(mask2, mask1, is_positive_or_zero); \
769 SRC_DATA_TYPE ab_x2_high32 = CONVERT((ab_64 + nudge) / (long)(1ll << 31), SRC_DATA_TYPE); \
770 _tmp = select(ab_x2_high32, (SRC_DATA_TYPE)INT_MAX, overflow); \
771 if(_dst_shift >= 0) \
772 { \
773 long mask = ((((int)1) << _dst_shift) - (int)1); \
774 long threshold = _tmp < (int)0 ? (mask >> 1) + (long)1 : (mask >> 1) + 0; \
775 _tmp = (_tmp & mask) > threshold ? (_tmp >> _dst_shift) + (int)1 : (_tmp >> _dst_shift); \
776 } \
777 _tmp += DST_OFFSET; \
778 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
779 }) \
780 }) \
781 })
782
783/** Quantized the 8-bit tile with fixed-point scale for asymmetric
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000784 *
785 * @param[in] SRC_DATA_TYPE SRC data type
786 * @param[in] DST_DATA_TYPE DST data type
787 * @param[in] M0 Number of src/dst rows
788 * @param[in] N0 Number of src/dst columns
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100789 * @param[in] DST_OFFSET Quantization offset used for both the per-tensor and per-channel quantization
790 * @param[in] DST_SHIFT Quantization shift for the per-tensor quantization
791 * @param[in] DST_MULTIPLIER Quantization multiplier for the per-tensor quantization
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000792 * @param[in] src Input tile
793 * @param[out] dst Output tile
794 */
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100795#define T_QUANTIZE8_ASYMMETRIC(SRC_DATA_TYPE, DST_DATA_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst) \
796 ({ \
797 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
798 { \
799 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
800 { \
801 SRC_DATA_TYPE _tmp = 0; \
802 SRC_DATA_TYPE _src = src[_m0].s[_n0]; \
803 _src *= select((SRC_DATA_TYPE)1, ((SRC_DATA_TYPE)1 << (SRC_DATA_TYPE)(-DST_SHIFT)), ((SRC_DATA_TYPE)DST_SHIFT < (SRC_DATA_TYPE)0)); \
804 SRC_DATA_TYPE overflow = _src == DST_MULTIPLIER && _src == INT_MIN; \
805 long a_64 = (long)(_src); \
806 long b_64 = (long)(DST_MULTIPLIER); \
807 long ab_64 = a_64 * b_64; \
808 long mask1 = 1 << 30; \
809 long mask2 = 1 - (1 << 30); \
810 long is_positive_or_zero = ab_64 >= 0; \
811 long nudge = select(mask2, mask1, is_positive_or_zero); \
812 SRC_DATA_TYPE ab_x2_high32 = CONVERT((ab_64 + nudge) / (long)(1ll << 31), SRC_DATA_TYPE); \
813 _tmp = select(ab_x2_high32, (SRC_DATA_TYPE)INT_MAX, overflow); \
814 if(DST_SHIFT >= 0) \
815 { \
816 long mask = ((((int)1) << DST_SHIFT) - (int)1); \
817 long threshold = _tmp < (int)0 ? (mask >> 1) + (long)1 : (mask >> 1) + 0; \
818 _tmp = (_tmp & mask) > threshold ? (_tmp >> DST_SHIFT) + (int)1 : (_tmp >> DST_SHIFT); \
819 } \
820 _tmp += DST_OFFSET; \
821 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
822 }) \
823 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000824 })
825
826/** Conditional rowset (memset by row)
827 *
828 * @note Set the row to VALUE_TO_SET if the corresponding mask == 0
829 *
830 * @param[in] DATA_TYPE Data type
831 * @param[in] M0 Number of LHS rows
832 * @param[in] N0 Number of LHS columns
833 * @param[in] VALUE_TO_SET Value to set the row
834 * @param[in, out] a Input/output tile
835 * @param[out] mask Mask to check for setting the row to VALUE_TO_SET
836 */
837#define T_ROWSET_MASK(DATA_TYPE, M0, N0, VALUE_TO_SET, a, mask) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100838 ({ \
839 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
840 { \
841 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
842 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000843 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 +0100844 }) \
845 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000846 })
847
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100848/** Element-wise activation for floating point types
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000849 *
850 * @note Performs: activation(LHS) = DST
851 *
852 * @param[in] DATA_TYPE SRC/DST data type
853 * @param[in] M0 Number of SRC/DST rows
854 * @param[in] N0 Number of SRC/DST columns
855 * @param[in] ACTIVATION_TYPE Activation type
856 * @param[in] A_VAL A value used for the activation (e.g. tanh_op, brelu,..)
857 * @param[in] B_VAL B value used for the activation (e.g. tanh_op, brelu,..)
858 * @param[out] src SRC tile
859 * @param[out] dst DST tile
860 */
861#define T_ACTIVATION(DATA_TYPE, M0, N0, ACTIVATION_TYPE, A_VAL, B_VAL, src, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100862 ({ \
863 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
864 { \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000865 dst[_m0].v = ACTIVATION(ACTIVATION_TYPE, DATA_TYPE, N0, src[_m0].v, A_VAL, B_VAL); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100866 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000867 })
868
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100869// RELU Activation
870#define relu_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) (max((DATA_TYPE)ZERO_VALUE, x))
871// Bounded RELU Activation
872#define brelu_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) (min((DATA_TYPE)A_VAL, max((DATA_TYPE)ZERO_VALUE, x)))
873// Lower Upper Bounded RELU Activation
874#define lu_brelu_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) (min(max(x, (DATA_TYPE)B_VAL), (DATA_TYPE)A_VAL))
875// Hard Swish Activation
876#define hard_swish_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, 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))
877// Identity Activation
878#define identity_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) (x)
879
880#define ACT_OP_QUANTIZED(op, DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) op##_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x)
881#define ACTIVATION_QUANTIZED(op, DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) ACT_OP_QUANTIZED(op, DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x)
882
883/** Element-wise activation for quantized types
884 *
885 * @note Performs: activation(LHS) = DST
886 *
887 * @param[in] DATA_TYPE SRC/DST data type
888 * @param[in] M0 Number of SRC/DST rows
889 * @param[in] N0 Number of SRC/DST columns
890 * @param[in] ACTIVATION_TYPE Activation type
891 * @param[in] ZERO_VALUE The zero value to consider in the computation
892 * @param[in] A_VAL A value used for the activation (e.g. tanh_op, brelu,..)
893 * @param[in] B_VAL B value used for the activation (e.g. tanh_op, brelu,..)
894 * @param[out] src SRC tile
895 * @param[out] dst DST tile
896 */
897#define T_ACTIVATION_QUANTIZED(DATA_TYPE, M0, N0, ACTIVATION_TYPE, ZERO_VALUE, A_VAL, B_VAL, src, dst) \
898 ({ \
899 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
900 { \
901 dst[_m0].v = ACTIVATION_QUANTIZED(ACTIVATION_TYPE, DATA_TYPE, N0, ZERO_VALUE, A_VAL, B_VAL, src[_m0].v); \
902 }) \
903 })
904
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000905/** Element-wise addition with a constant value
906 *
907 * @note Performs: LHS + constant = DST
908 *
909 * @param[in] DATA_TYPE LHS/RHS/DST data type
910 * @param[in] M0 Number of LHS rows
911 * @param[in] N0 Number of LHS columns
912 * @param[in] lhs LHS tile
913 * @param[in] rhs_constant Constant value
914 * @param[out] dst DST tile
915 */
916#define T_ADD_CONSTANT(DATA_TYPE, M0, N0, lhs, rhs_constant, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100917 ({ \
918 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
919 { \
920 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
921 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000922 dst[_m0].s[_n0] = lhs[_m0].s[_n0] + rhs_constant; \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100923 }) \
924 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000925 })
926
927/** Element-wise addition with RHS broadcasted (RHS has the X dimension only)
928 *
929 * @note Performs: LHS + RHS[broadcasted] = DST
930 * @note Both tiles must have same data type
931 *
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100932 * @param[in] DST_DATA_TYPE DST data type
933 * @param[in] M0 Number of LHS rows
934 * @param[in] N0 Number of LHS columns
935 * @param[in] lhs LHS tile
936 * @param[in] rhs RHS tile
937 * @param[out] dst DST tile
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000938 */
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100939#define T_ADD_BROADCAST_X(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100940 ({ \
941 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
942 { \
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100943 dst[_m0].v = 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 +0100944 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000945 })
946
947/** Matrix multiplication
948 *
949 * @note Performs: LHS X RHS + DST = DST
950 *
951 * @param[in] LHS_DATA_TYPE LHS tile data type
952 * @param[in] RHS_DATA_TYPE RHS tile data type
953 * @param[in] DST_DATA_TYPE RHS tile data type
954 * @param[in] M0 Number of LHS rows
955 * @param[in] N0 Number of RHS columns
956 * @param[in] K0 Number of LHS columns
957 * @param[in] LHS_LAYOUT LHS layout (T= transposed, NT= not transposed)
958 * @param[in] RHS_LAYOUT RHS layout (T= transposed, NT= not transposed)
959 * @param[in] lhs LHS tile
960 * @param[in] rhs RHS tile
961 * @param[in, out] dst DST tile
962 */
963#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 +0100964#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)
965#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 +0100966#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 +0100967#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)
968#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)
969#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)
970#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)
971#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 +0000972 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100973 LOOP_UNROLLING(int, _m, 0, 1, M0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000974 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100975 LOOP_UNROLLING(int, _n, 0, 1, N0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000976 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100977 LOOP_UNROLLING(int, _k, 0, 1, K0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000978 { \
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100979 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 +0100980 }) \
981 }) \
982 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000983 }
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100984
985#define T_MMUL_NT_T_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) \
986 ({ \
987 LOOP_UNROLLING(int, _m, 0, 1, M0, \
988 { \
989 LOOP_UNROLLING(int, _n, 0, 1, N0, \
990 { \
991 DOT_PRODUCT_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, K0, (lhs[_m].v), (rhs[_n].v), dst[_m].s[_n]); \
992 }) \
993 }) \
Gian Marco Iodice561c1762021-04-16 15:08:59 +0100994 })