Skip to content

Commit 025741f

Browse files
authored
Fast Haswell CGEMM kernel
1 parent 105e26e commit 025741f

File tree

1 file changed

+287
-0
lines changed

1 file changed

+287
-0
lines changed
Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
#include "common.h"
2+
#include <stdint.h>
3+
4+
/* recommended settings: GEMM_P = 256, GEMM_Q = 256 */
5+
6+
#if defined(NN) || defined(NT) || defined(TN) || defined(TT)
7+
#define A_CONJ 0
8+
#define B_CONJ 0
9+
#endif
10+
#if defined(RN) || defined(RT) || defined(CN) || defined(CT)
11+
#define A_CONJ 1
12+
#define B_CONJ 0
13+
#endif
14+
#if defined(NR) || defined(NC) || defined(TR) || defined(TC)
15+
#define A_CONJ 0
16+
#define B_CONJ 1
17+
#endif
18+
#if defined(RR) || defined(RC) || defined(CR) || defined(CC)
19+
#define A_CONJ 1
20+
#define B_CONJ 1
21+
#endif
22+
23+
/* %0 = a_ptr, %1 = b_ptr, %2 = c_ptr, %3 = c_tmp, %4 = ldc(bytes), %5 = k_counter, %6 = &alpha, %7 = m_counter, %8 = b_pref */
24+
/* r11 = m, r12 = k << 4, r13 = k, r14 = b_head, r15 = temp */
25+
26+
/* m=8, ymm 0-3 temp, ymm 4-15 acc */
27+
#if A_CONJ == B_CONJ
28+
#define acc_m4n1_exp(ar,ai,b2,cl,cr) "vfmadd231ps %%ymm"#ar",%%ymm"#b2",%%ymm"#cl"; vfmadd231ps %%ymm"#ai",%%ymm"#b2",%%ymm"#cr";"
29+
#define acc_m8n1_con(ua,la,b1,uc,lc) "vfmaddsub231ps %%ymm"#ua",%%ymm"#b1",%%ymm"#uc"; vfmaddsub231ps %%ymm"#la",%%ymm"#b1",%%ymm"#lc";"
30+
#else
31+
#define acc_m4n1_exp(ar,ai,b2,cl,cr) "vfmadd231ps %%ymm"#ar",%%ymm"#b2",%%ymm"#cl"; vfnmadd231ps %%ymm"#ai",%%ymm"#b2",%%ymm"#cr";"
32+
#define acc_m8n1_con(ua,la,b1,uc,lc) "vfmsubadd231ps %%ymm"#ua",%%ymm"#b1",%%ymm"#uc"; vfmsubadd231ps %%ymm"#la",%%ymm"#b1",%%ymm"#lc";"
33+
#endif
34+
/* expanded accumulators for m8n1 and m8n2 */
35+
#define KERNEL_k1m8n1 \
36+
"vbroadcastsd (%1),%%ymm0; addq $8,%1;"\
37+
"vmovsldup (%0),%%ymm1; vmovshdup (%0),%%ymm2;" acc_m4n1_exp(1,2,0,4,5)\
38+
"vmovsldup 32(%0),%%ymm1; vmovshdup 32(%0),%%ymm2;" acc_m4n1_exp(1,2,0,6,7)\
39+
"addq $64,%0;"
40+
#define KERNEL_k1m8n2 \
41+
"vbroadcastsd (%1),%%ymm0; vbroadcastsd 8(%1),%%ymm1; addq $16,%1;"\
42+
"vmovsldup (%0),%%ymm2; vmovshdup (%0),%%ymm3;" acc_m4n1_exp(2,3,0,4,5) acc_m4n1_exp(2,3,1,8,9)\
43+
"vmovsldup 32(%0),%%ymm2; vmovshdup 32(%0),%%ymm3;" acc_m4n1_exp(2,3,0,6,7) acc_m4n1_exp(2,3,1,10,11)\
44+
"addq $64,%0;"
45+
/* contracted accumulators for m8n4 and m8n6 */
46+
#define acc_m8n2_con(ua,la,luc,llc,ruc,rlc,lboff,rboff,...) \
47+
"vbroadcastss "#lboff"("#__VA_ARGS__"),%%ymm2;" acc_m8n1_con(ua,la,2,luc,llc)\
48+
"vbroadcastss "#rboff"("#__VA_ARGS__"),%%ymm3;" acc_m8n1_con(ua,la,3,ruc,rlc)
49+
#define KERNEL_1_k1m8n4 \
50+
"vmovups (%0),%%ymm0; vmovups 32(%0),%%ymm1; prefetcht0 512(%0); addq $64,%0;"\
51+
acc_m8n2_con(0,1,4,5,6,7,0,8,%1) acc_m8n2_con(0,1,8,9,10,11,0,8,%1,%%r12,1)
52+
#define KERNEL_2_k1m8n4 \
53+
"vpermilps $177,%%ymm0,%%ymm0; vpermilps $177,%%ymm1,%%ymm1;"\
54+
acc_m8n2_con(0,1,4,5,6,7,4,12,%1) acc_m8n2_con(0,1,8,9,10,11,4,12,%1,%%r12,1)
55+
#define KERNEL_1_k1m8n6 KERNEL_1_k1m8n4 acc_m8n2_con(0,1,12,13,14,15,0,8,%1,%%r12,2)
56+
#define KERNEL_2_k1m8n6 KERNEL_2_k1m8n4 acc_m8n2_con(0,1,12,13,14,15,4,12,%1,%%r12,2)
57+
#define KERNEL_k1m8n4 KERNEL_1_k1m8n4 KERNEL_2_k1m8n4 "addq $16,%1;"
58+
#define KERNEL_k1m8n6 KERNEL_1_k1m8n6 KERNEL_2_k1m8n6 "addq $16,%1;"
59+
#define zero_4ymm(no1,no2,no3,no4) \
60+
"vpxor %%ymm"#no1",%%ymm"#no1",%%ymm"#no1"; vpxor %%ymm"#no2",%%ymm"#no2",%%ymm"#no2";"\
61+
"vpxor %%ymm"#no3",%%ymm"#no3",%%ymm"#no3"; vpxor %%ymm"#no4",%%ymm"#no4",%%ymm"#no4";"
62+
/* initialization and storage macros */
63+
#define INIT_m8n1 zero_4ymm(4,5,6,7)
64+
#define INIT_m8n2 zero_4ymm(4,5,6,7) zero_4ymm(8,9,10,11)
65+
#define INIT_m8n4 zero_4ymm(4,5,6,7) zero_4ymm(8,9,10,11)
66+
#define INIT_m8n6 INIT_m8n4 zero_4ymm(12,13,14,15)
67+
#if A_CONJ == B_CONJ
68+
#define cont_expacc(cl,cr,dst) "vpermilps $177,%%ymm"#cr",%%ymm"#cr"; vaddsubps %%ymm"#cl",%%ymm"#cr",%%ymm"#dst";"
69+
#else
70+
#define cont_expacc(cl,cr,dst) "vpermilps $177,%%ymm"#cr",%%ymm"#cr"; vaddsubps %%ymm"#cr",%%ymm"#cl",%%ymm"#dst";"
71+
#endif
72+
#if A_CONJ == 0
73+
#define save_1ymm(c,tmp,off,alpr,alpi,...) \
74+
"vpermilps $177,%%ymm"#c",%%ymm"#tmp"; vfmsubadd213ps "#off"("#__VA_ARGS__"),%%ymm"#alpr",%%ymm"#c";"\
75+
"vfmsubadd231ps %%ymm"#tmp",%%ymm"#alpi",%%ymm"#c"; vmovups %%ymm"#c","#off"("#__VA_ARGS__");"
76+
#else
77+
#define save_1ymm(c,tmp,off,alpr,alpi,...) \
78+
"vpermilps $177,%%ymm"#c",%%ymm"#tmp"; vfmaddsub213ps "#off"("#__VA_ARGS__"),%%ymm"#alpi",%%ymm"#tmp";"\
79+
"vfmaddsub231ps %%ymm"#c",%%ymm"#alpr",%%ymm"#tmp"; vmovups %%ymm"#tmp","#off"("#__VA_ARGS__");"
80+
#endif
81+
#define save_init_m8 "movq %2,%3; addq $64,%2; vbroadcastss (%6),%%ymm0; vbroadcastss 4(%6),%%ymm1;"
82+
#define SAVE_m8n1 save_init_m8 cont_expacc(4,5,4) cont_expacc(6,7,6) save_1ymm(4,2,0,0,1,%3) save_1ymm(6,3,32,0,1,%3)
83+
#define SAVE_m8n2 SAVE_m8n1\
84+
cont_expacc(8,9,8) cont_expacc(10,11,10) save_1ymm(8,2,0,0,1,%3,%4,1) save_1ymm(10,3,32,0,1,%3,%4,1)
85+
#define SAVE_m8n4 save_init_m8\
86+
save_1ymm(4,2,0,0,1,%3) save_1ymm(5,3,32,0,1,%3) save_1ymm(6,2,0,0,1,%3,%4,1) save_1ymm(7,3,32,0,1,%3,%4,1) "leaq (%3,%4,2),%3;"\
87+
save_1ymm(8,2,0,0,1,%3) save_1ymm(9,3,32,0,1,%3) save_1ymm(10,2,0,0,1,%3,%4,1) save_1ymm(11,3,32,0,1,%3,%4,1)
88+
#define SAVE_m8n6 SAVE_m8n4 "leaq (%3,%4,2),%3;"\
89+
save_1ymm(12,2,0,0,1,%3) save_1ymm(13,3,32,0,1,%3) save_1ymm(14,2,0,0,1,%3,%4,1) save_1ymm(15,3,32,0,1,%3,%4,1)
90+
#define COMPUTE_m8(ndim) \
91+
"movq %%r14,%1;" INIT_m8n##ndim "movq %2,%3; movq %%r13,%5;"\
92+
"testq %5,%5; jz "#ndim"8883f; cmpq $10,%5; jb "#ndim"8882f;"\
93+
"movq $10,%5; movq $84,%%r15;"\
94+
#ndim"8881:\n\t"\
95+
"prefetcht1 (%3); subq $63,%3; addq %%r15,%3;"\
96+
"prefetcht0 64(%1); prefetcht0 64(%1,%%r12,1); prefetcht0 64(%1,%%r12,2);"\
97+
KERNEL_k1m8n##ndim KERNEL_k1m8n##ndim\
98+
"testq $12,%5; movq $84,%%r15; cmovz %4,%%r15; prefetcht1 (%8); addq $16,%8;"\
99+
KERNEL_k1m8n##ndim KERNEL_k1m8n##ndim\
100+
"addq $4,%5; cmpq %5,%%r13; jnb "#ndim"8881b;"\
101+
"movq %2,%3; negq %5; leaq 10(%%r13,%5,1),%5; prefetcht0 (%6); prefetcht0 7(%6);"\
102+
#ndim"8882:\n\t"\
103+
"prefetcht0 (%3); prefetcht0 63(%3); addq %4,%3;"\
104+
KERNEL_k1m8n##ndim "decq %5; jnz "#ndim"8882b;"\
105+
#ndim"8883:\n\t"\
106+
"prefetcht0 (%%r14); prefetcht0 64(%%r14);" SAVE_m8n##ndim
107+
/* m=4, ymm 0-3 temp, ymm 4-15 acc, expanded accumulators */
108+
#define KERNEL_k1m4n1 \
109+
"vmovsldup (%0),%%ymm1; vmovshdup (%0),%%ymm2; addq $32,%0;"\
110+
"vbroadcastsd (%1),%%ymm0;" acc_m4n1_exp(1,2,0,4,5) "addq $8,%1;"
111+
#define acc_m4n2_exp(c1l,c1r,c2l,c2r,...) \
112+
"vbroadcastsd ("#__VA_ARGS__"),%%ymm2;" acc_m4n1_exp(0,1,2,c1l,c1r)\
113+
"vbroadcastsd 8("#__VA_ARGS__"),%%ymm3;" acc_m4n1_exp(0,1,3,c2l,c2r)
114+
#define KERNEL_h_k1m4n2 \
115+
"vmovsldup (%0),%%ymm0; vmovshdup (%0),%%ymm1; addq $32,%0;" acc_m4n2_exp(4,5,6,7,%1)
116+
#define KERNEL_h_k1m4n4 KERNEL_h_k1m4n2 acc_m4n2_exp(8,9,10,11,%1,%%r12,1)
117+
#define KERNEL_h_k1m4n6 KERNEL_h_k1m4n4 acc_m4n2_exp(12,13,14,15,%1,%%r12,2)
118+
#define KERNEL_k1m4n2 KERNEL_h_k1m4n2 "addq $16,%1;"
119+
#define KERNEL_k1m4n4 KERNEL_h_k1m4n4 "addq $16,%1;"
120+
#define KERNEL_k1m4n6 KERNEL_h_k1m4n6 "addq $16,%1;"
121+
#define INIT_m4n1 "vpxor %%ymm4,%%ymm4,%%ymm4; vpxor %%ymm5,%%ymm5,%%ymm5;"
122+
#define INIT_m4n2 zero_4ymm(4,5,6,7)
123+
#define INIT_m4n4 INIT_m4n2 zero_4ymm(8,9,10,11)
124+
#define INIT_m4n6 INIT_m4n4 zero_4ymm(12,13,14,15)
125+
#define save_init_m4 "movq %2,%3; addq $32,%2; vbroadcastss (%6),%%ymm0; vbroadcastss 4(%6),%%ymm1;"
126+
#define SAVE_m4n1 save_init_m4 cont_expacc(4,5,4) save_1ymm(4,2,0,0,1,%3)
127+
#define SAVE_m4n2 SAVE_m4n1 cont_expacc(6,7,6) save_1ymm(6,3,0,0,1,%3,%4,1)
128+
#define SAVE_m4n4 SAVE_m4n2 "leaq (%3,%4,2),%3;"\
129+
cont_expacc(8,9,8) cont_expacc(10,11,10) save_1ymm(8,2,0,0,1,%3) save_1ymm(10,3,0,0,1,%3,%4,1)
130+
#define SAVE_m4n6 SAVE_m4n4 "leaq (%3,%4,2),%3;"\
131+
cont_expacc(12,13,12) cont_expacc(14,15,14) save_1ymm(12,2,0,0,1,%3) save_1ymm(14,3,0,0,1,%3,%4,1)
132+
#define COMPUTE_m4(ndim) \
133+
"movq %%r14,%1;" INIT_m4n##ndim "movq %%r13,%5;"\
134+
"testq %5,%5; jz "#ndim"4442f;"\
135+
#ndim"4441:\n\t"\
136+
KERNEL_k1m4n##ndim\
137+
"decq %5; jnz "#ndim"4441b;"\
138+
#ndim"4442:\n\t"\
139+
SAVE_m4n##ndim
140+
/* m=2, xmm 0-3 temp, xmm 4-15 acc, expanded accumulators */
141+
#if A_CONJ == B_CONJ
142+
#define acc_m2n1_exp(ar,ai,b2,cl,cr) "vfmadd231ps %%xmm"#ar",%%xmm"#b2",%%xmm"#cl"; vfmadd231ps %%xmm"#ai",%%xmm"#b2",%%xmm"#cr";"
143+
#else
144+
#define acc_m2n1_exp(ar,ai,b2,cl,cr) "vfmadd231ps %%xmm"#ar",%%xmm"#b2",%%xmm"#cl"; vfnmadd231ps %%xmm"#ai",%%xmm"#b2",%%xmm"#cr";"
145+
#endif
146+
#define KERNEL_h_k1m2n1 \
147+
"vmovsldup (%0),%%xmm0; vmovshdup (%0),%%xmm1; addq $16,%0;"\
148+
"vmovddup (%1),%%xmm2;" acc_m2n1_exp(0,1,2,4,5)
149+
#define KERNEL_h_k1m2n2 KERNEL_h_k1m2n1\
150+
"vmovddup 8(%1),%%xmm3;" acc_m2n1_exp(0,1,3,6,7)
151+
#define acc_m2n2_exp(c1,c2,c3,c4,...)\
152+
"vmovddup ("#__VA_ARGS__"),%%xmm2;" acc_m2n1_exp(0,1,2,c1,c2)\
153+
"vmovddup 8("#__VA_ARGS__"),%%xmm3;" acc_m2n1_exp(0,1,3,c3,c4)
154+
#define KERNEL_h_k1m2n4 KERNEL_h_k1m2n2 acc_m2n2_exp(8,9,10,11,%1,%%r12,1)
155+
#define KERNEL_h_k1m2n6 KERNEL_h_k1m2n4 acc_m2n2_exp(12,13,14,15,%1,%%r12,2)
156+
#define KERNEL_k1m2n1 KERNEL_h_k1m2n1 "addq $8,%1;"
157+
#define KERNEL_k1m2n2 KERNEL_h_k1m2n2 "addq $16,%1;"
158+
#define KERNEL_k1m2n4 KERNEL_h_k1m2n4 "addq $16,%1;"
159+
#define KERNEL_k1m2n6 KERNEL_h_k1m2n6 "addq $16,%1;"
160+
#define zero_2xmm(no1,no2) "vpxor %%xmm"#no1",%%xmm"#no1",%%xmm"#no1"; vpxor %%xmm"#no2",%%xmm"#no2",%%xmm"#no2";"
161+
#define INIT_m2n1 zero_2xmm(4,5)
162+
#define INIT_m2n2 INIT_m2n1 zero_2xmm(6,7)
163+
#define INIT_m2n4 INIT_m2n2 zero_2xmm(8,9) zero_2xmm(10,11)
164+
#define INIT_m2n6 INIT_m2n4 zero_2xmm(12,13) zero_2xmm(14,15)
165+
#if A_CONJ == B_CONJ
166+
#define cont_expxmmacc(cl,cr,dst) "vpermilps $177,%%xmm"#cr",%%xmm"#cr"; vaddsubps %%xmm"#cl",%%xmm"#cr",%%xmm"#dst";"
167+
#else
168+
#define cont_expxmmacc(cl,cr,dst) "vpermilps $177,%%xmm"#cr",%%xmm"#cr"; vaddsubps %%xmm"#cr",%%xmm"#cl",%%xmm"#dst";"
169+
#endif
170+
#if A_CONJ == 0
171+
#define save_1xmm(c,tmp,alpr,alpi) \
172+
"vpermilps $177,%%xmm"#c",%%xmm"#tmp"; vfmsubadd213ps (%3),%%xmm"#alpr",%%xmm"#c";"\
173+
"vfmsubadd231ps %%xmm"#tmp",%%xmm"#alpi",%%xmm"#c"; vmovups %%xmm"#c",(%3); addq %4,%3;"
174+
#else
175+
#define save_1xmm(c,tmp,alpr,alpi) \
176+
"vpermilps $177,%%xmm"#c",%%xmm"#tmp"; vfmaddsub213ps (%3),%%xmm"#alpi",%%xmm"#tmp";"\
177+
"vfmaddsub231ps %%xmm"#c",%%xmm"#alpr",%%xmm"#tmp"; vmovups %%xmm"#tmp",(%3); addq %4,%3;"
178+
#endif
179+
#define save_init_m2 "movq %2,%3; addq $16,%2; vbroadcastss (%6),%%xmm0; vbroadcastss 4(%6),%%xmm1;"
180+
#define SAVE_m2n1 save_init_m2 cont_expxmmacc(4,5,4) save_1xmm(4,2,0,1)
181+
#define SAVE_m2n2 SAVE_m2n1 cont_expacc(6,7,6) save_1xmm(6,3,0,1)
182+
#define SAVE_m2n4 SAVE_m2n2 cont_expacc(8,9,8) save_1xmm(8,2,0,1) cont_expacc(10,11,10) save_1xmm(10,3,0,1)
183+
#define SAVE_m2n6 SAVE_m2n4 cont_expacc(12,13,12) save_1xmm(12,2,0,1) cont_expacc(14,15,14) save_1xmm(14,3,0,1)
184+
#define COMPUTE_m2(ndim) \
185+
"movq %%r14,%1;" INIT_m2n##ndim "movq %%r13,%5;"\
186+
"testq %5,%5; jz "#ndim"2222f;"\
187+
#ndim"2221:\n\t"\
188+
KERNEL_k1m2n##ndim\
189+
"decq %5; jnz "#ndim"2221b;"\
190+
#ndim"2222:\n\t"\
191+
SAVE_m2n##ndim
192+
/* m=1, xmm 0-3 temp, xmm 4-9 acc, expanded accumulators */
193+
#if A_CONJ == B_CONJ
194+
#define acc_m1n1_exp(ar,ai,b2,cl,cr) "vfmadd231ps %%xmm"#ar",%%xmm"#b2",%%xmm"#cl"; vfmadd231ps %%xmm"#ai",%%xmm"#b2",%%xmm"#cr";"
195+
#define acc_m1n2_exp(arb,aib,b4,cl,cr) "vfmadd231ps %%xmm"#arb",%%xmm"#b4",%%xmm"#cl"; vfmadd231ps %%xmm"#aib",%%xmm"#b4",%%xmm"#cr";"
196+
#else
197+
#define acc_m1n1_exp(ar,ai,b2,cl,cr) "vfmadd231ps %%xmm"#ar",%%xmm"#b2",%%xmm"#cl"; vfnmadd231ps %%xmm"#ai",%%xmm"#b2",%%xmm"#cr";"
198+
#define acc_m1n2_exp(arb,aib,b4,cl,cr) "vfmadd231ps %%xmm"#arb",%%xmm"#b4",%%xmm"#cl"; vfnmadd231ps %%xmm"#aib",%%xmm"#b4",%%xmm"#cr";"
199+
#endif
200+
#define KERNEL_k1m1n1 \
201+
"vbroadcastss (%0),%%xmm0; vbroadcastss 4(%0),%%xmm1; addq $8,%0;"\
202+
"vmovsd (%1),%%xmm2; addq $8,%1;" acc_m1n1_exp(0,1,2,4,5)
203+
#define KERNEL_h_k1m1n2 \
204+
"vbroadcastss (%0),%%xmm0; vbroadcastss 4(%0),%%xmm1; addq $8,%0;"\
205+
"vmovups (%1),%%xmm2;" acc_m1n2_exp(0,1,2,4,5)
206+
#define KERNEL_h_k1m1n4 KERNEL_h_k1m1n2 "vmovups (%1,%%r12,1),%%xmm2;" acc_m1n2_exp(0,1,2,6,7)
207+
#define KERNEL_h_k1m1n6 KERNEL_h_k1m1n4 "vmovups (%1,%%r12,2),%%xmm2;" acc_m1n2_exp(0,1,2,8,9)
208+
#define KERNEL_k1m1n2 KERNEL_h_k1m1n2 "addq $16,%1;"
209+
#define KERNEL_k1m1n4 KERNEL_h_k1m1n4 "addq $16,%1;"
210+
#define KERNEL_k1m1n6 KERNEL_h_k1m1n6 "addq $16,%1;"
211+
#define INIT_m1n1 zero_2xmm(4,5)
212+
#define INIT_m1n2 zero_2xmm(4,5)
213+
#define INIT_m1n4 INIT_m1n2 zero_2xmm(6,7)
214+
#define INIT_m1n6 INIT_m1n4 zero_2xmm(8,9)
215+
#if A_CONJ == 0
216+
#define save_m1n1(c,tmp1,tmp2,alpr,alpi) \
217+
"vpermilps $177,%%xmm"#c",%%xmm"#tmp1"; vmovsd (%3),%%xmm"#tmp2"; vfmsubadd213ps %%xmm"#tmp2",%%xmm"#alpr",%%xmm"#c";"\
218+
"vfmsubadd231ps %%xmm"#tmp1",%%xmm"#alpi",%%xmm"#c"; vmovsd %%xmm"#c",(%3);"
219+
#define save_m1n2(c,tmp1,tmp2,alpr,alpi) \
220+
"vpermilps $177,%%xmm"#c",%%xmm"#tmp1"; vmovsd (%3),%%xmm"#tmp2"; vmovhpd (%3,%4,1),%%xmm"#tmp2",%%xmm"#tmp2";"\
221+
"vfmsubadd213ps %%xmm"#tmp2",%%xmm"#alpr",%%xmm"#c"; vfmsubadd231ps %%xmm"#tmp1",%%xmm"#alpi",%%xmm"#c";"\
222+
"vmovsd %%xmm"#c",(%3); vmovhpd %%xmm"#c",(%3,%4,1); leaq (%3,%4,2),%3;"
223+
#else
224+
#define save_m1n1(c,tmp1,tmp2,alpr,alpi) \
225+
"vpermilps $177,%%xmm"#c",%%xmm"#tmp1"; vmovsd (%3),%%xmm"#tmp2"; vfmaddsub213ps %%xmm"#tmp2",%%xmm"#alpi",%%xmm"#tmp1";"\
226+
"vfmaddsub231ps %%xmm"#c",%%xmm"#alpr",%%xmm"#tmp1"; vmovsd %%xmm"#tmp1",(%3);"
227+
#define save_m1n2(c,tmp1,tmp2,alpr,alpi) \
228+
"vpermilps $177,%%xmm"#c",%%xmm"#tmp1"; vmovsd (%3),%%xmm"#tmp2"; vmovhpd (%3,%4,1),%%xmm"#tmp2",%%xmm"#tmp2";"\
229+
"vfmaddsub213ps %%xmm"#tmp2",%%xmm"#alpi",%%xmm"#tmp1"; vfmaddsub231ps %%xmm"#c",%%xmm"#alpr",%%xmm"#tmp1";"\
230+
"vmovsd %%xmm"#tmp1",(%3); vmovhpd %%xmm"#tmp1",(%3,%4,1); leaq (%3,%4,2),%3;"
231+
#endif
232+
#define save_init_m1 "movq %2,%3; addq $8,%2; vbroadcastss (%6),%%xmm0; vbroadcastss 4(%6),%%xmm1;"
233+
#define SAVE_m1n1 save_init_m1 cont_expxmmacc(4,5,4) save_m1n1(4,2,3,0,1)
234+
#define SAVE_m1n2 save_init_m1 cont_expxmmacc(4,5,4) save_m1n2(4,2,3,0,1)
235+
#define SAVE_m1n4 SAVE_m1n2 cont_expxmmacc(6,7,6) save_m1n2(6,2,3,0,1)
236+
#define SAVE_m1n6 SAVE_m1n4 cont_expxmmacc(8,9,8) save_m1n2(8,2,3,0,1)
237+
#define COMPUTE_m1(ndim) \
238+
"movq %%r14,%1;" INIT_m1n##ndim "movq %%r13,%5;"\
239+
"testq %5,%5; jz "#ndim"1112f;"\
240+
#ndim"1111:\n\t"\
241+
KERNEL_k1m1n##ndim\
242+
"decq %5; jnz "#ndim"1111b;"\
243+
#ndim"1112:\n\t"\
244+
SAVE_m1n##ndim
245+
#define COMPUTE(ndim) {\
246+
b_pref = b_ptr + ndim * K *2;\
247+
__asm__ __volatile__ (\
248+
"movq %1,%%r14; movq %5,%%r13; movq %5,%%r12; salq $4,%%r12; movq %7,%%r11;"\
249+
"cmpq $8,%7; jb "#ndim"9992f;"\
250+
#ndim"9991:\n\t"\
251+
COMPUTE_m8(ndim)\
252+
"subq $8,%7; cmpq $8,%7; jnb "#ndim"9991b;"\
253+
#ndim"9992:\n\t"\
254+
"cmpq $4,%7; jb "#ndim"9993f;"\
255+
COMPUTE_m4(ndim) "subq $4,%7;"\
256+
#ndim"9993:\n\t"\
257+
"cmpq $2,%7; jb "#ndim"9994f;"\
258+
COMPUTE_m2(ndim) "subq $2,%7;"\
259+
#ndim"9994:\n\t"\
260+
"testq %7,%7; jz "#ndim"9995f;"\
261+
COMPUTE_m1(ndim)\
262+
#ndim"9995:\n\t"\
263+
"movq %%r14,%1; movq %%r13,%5; movq %%r11,%7; vzeroupper;"\
264+
:"+r"(a_ptr),"+r"(b_ptr),"+r"(c_ptr),"+r"(c_tmp),"+r"(ldc_in_bytes),"+r"(K),"+r"(alp),"+r"(M),"+r"(b_pref)\
265+
::"cc","memory","r11","r12","r13","r14","r15","xmm0","xmm1","xmm2","xmm3","xmm4","xmm5",\
266+
"xmm6","xmm7","xmm8","xmm9","xmm10","xmm11","xmm12","xmm13","xmm14","xmm15");\
267+
a_ptr -= M * K *2; b_ptr += ndim * K *2; c_ptr += (ndim * LDC - M) * 2;\
268+
}
269+
int __attribute__ ((noinline))
270+
CNAME(BLASLONG m, BLASLONG n, BLASLONG k, float alphar, float alphai, float * __restrict__ A, float * __restrict__ B, float * __restrict__ C, BLASLONG LDC)
271+
{
272+
if(m==0||n==0||k==0||(alphar==0.0 && alphai==0.0)) return 0;
273+
int64_t ldc_in_bytes = (int64_t)LDC * sizeof(float) * 2;
274+
#if A_CONJ == B_CONJ
275+
float const_val[2] = {-alphar, -alphai};
276+
#else
277+
float const_val[2] = {alphar, alphai};
278+
#endif
279+
int64_t M = (int64_t)m, K = (int64_t)k;
280+
BLASLONG n_count = n;
281+
float *a_ptr = A,*b_ptr = B,*c_ptr = C,*c_tmp = C,*alp = const_val,*b_pref = B;
282+
for(;n_count>5;n_count-=6) COMPUTE(6)
283+
for(;n_count>3;n_count-=4) COMPUTE(4)
284+
for(;n_count>1;n_count-=2) COMPUTE(2)
285+
if(n_count>0) COMPUTE(1)
286+
return 0;
287+
}

0 commit comments

Comments
 (0)