-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_avx_memcpy.php
246 lines (219 loc) · 5.42 KB
/
gen_avx_memcpy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
const RET_REG = "rax";
const DST_REG = "rdi";
const SRC_REG = "rsi";
const LEN_REG = "rdx";
const COUNTER_REG = "rcx";
const TMP_REG = ["r9", "r9d", "r9w", "r9b"];
const MAX_VEC_REGISTER_BIT = 512;
const MAX_VEC_REGISTER_NUM = 32;
const MAX_VEC_REGISTER_BYTES = MAX_VEC_REGISTER_NUM / 8;
const MAX_BRANCH_BYTES = MAX_VEC_REGISTER_BIT * MAX_VEC_REGISTER_BYTES;
const VEC_BYTES = [
"zmm" => 64,
"ymm" => 32,
"xmm" => 16
];
const APPROP_MOV = [
"zmm" => "vmovdqu64",
"ymm" => "vmovdqu",
"xmm" => "vmovdqu",
];
$j = MAX_BRANCH_BYTES;
$loop = "\n";
$branches = "\n";
$greatestVecReg = [
512 => "zmm",
256 => "ymm",
128 => "xmm",
][MAX_VEC_REGISTER_BIT];
$mm = MAX_VEC_REGISTER_NUM;
while ($j >= 16) {
$loop .= sprintf(" cmp %s, %d\n", COUNTER_REG, $j);
$loop .= sprintf(" jmp .jge_%d ", $j);
$loop .= sprintf("; If the length is greater than or equal to %d\n", $j);
$branches .= sprintf(".jge_%d:\n", $j);
generate_branches($branches, $j, $mm);
$branches .= sprintf(" add %s, %d\n", COUNTER_REG, $j);
$branches .= sprintf(" sub %s, %d\n", LEN_REG, $j);
$branches .= sprintf(" test rdx, rdx\n");
$branches .= sprintf(" jnz ._loop\n");
$branches .= sprintf(" jmp .epi\n");
$mm /= 2;
$j /= 2;
}
function generate_branches(string &$branches, int $j, int &$mm)
{
switch (MAX_VEC_REGISTER_BIT) {
case 512:
if ($j < 32) {
$mm = 1;
$curReg = "xmm";
} else
if ($j < 64) {
$mm = ($j <= 32) ? 1 : 2;
$curReg = "ymm";
} else {
$curReg = "zmm";
}
for ($i = 0; $i < $mm; $i++) {
$branches .= sprintf(
" prefetcht1 [%s + %s + (%d * %d)]\n",
SRC_REG, COUNTER_REG, VEC_BYTES[$curReg], $i
);
}
for ($i = 0; $i < $mm; $i++) {
$branches .= sprintf(
" prefetcht1 [%s + %s + (%d * %d)]\n",
DST_REG, COUNTER_REG, VEC_BYTES[$curReg], $i
);
}
for ($i = 0; $i < $mm; $i++) {
$branches .= sprintf(
" %s %s%d, [%s + %s + (%d * %d)]\n",
APPROP_MOV[$curReg],
$curReg, $i, SRC_REG, COUNTER_REG, VEC_BYTES[$curReg], $i
);
}
for ($i = 0; $i < $mm; $i++) {
$branches .= sprintf(
" %s [%s + %s + (%d * %d)], %s%d\n",
APPROP_MOV[$curReg],
DST_REG, COUNTER_REG, VEC_BYTES[$curReg], $i, $curReg, $i
);
}
break;
case 256:
if ($j < 32) {
$mm = 1;
$curReg = "xmm";
} else
if ($j < 64) {
$mm = ($j <= 32) ? 1 : 2;
$curReg = "ymm";
} else {
$curReg = "ymm";
}
for ($i = 0; $i < $mm; $i++) {
$branches .= sprintf(
" prefetcht1 [%s + %s + (%d * %d)]\n",
SRC_REG, COUNTER_REG, VEC_BYTES[$curReg], $i
);
}
for ($i = 0; $i < $mm; $i++) {
$branches .= sprintf(
" prefetcht1 [%s + %s + (%d * %d)]\n",
DST_REG, COUNTER_REG, VEC_BYTES[$curReg], $i
);
}
for ($i = 0; $i < $mm; $i++) {
$branches .= sprintf(
" %s %s%d, [%s + %s + (%d * %d)]\n",
APPROP_MOV[$curReg],
$curReg, $i, SRC_REG, COUNTER_REG, VEC_BYTES[$curReg], $i
);
}
for ($i = 0; $i < $mm; $i++) {
$branches .= sprintf(
" %s [%s + %s + (%d * %d)], %s%d\n",
APPROP_MOV[$curReg],
DST_REG, COUNTER_REG, VEC_BYTES[$curReg], $i, $curReg, $i
);
}
break;
}
}
$branches .= sprintf(<<<CODE
.jge_8:
mov %s, [%s + %s]
mov [%s + %s], %s
add %s, 8
sub %s, 8
test %s, %s
jnz ._loop
CODE,
TMP_REG[0], SRC_REG, COUNTER_REG,
DST_REG, COUNTER_REG, TMP_REG[0],
COUNTER_REG,
LEN_REG,
LEN_REG, LEN_REG
);
$branches .= sprintf(<<<CODE
.jge_4:
mov %s, [%s + %s]
mov [%s + %s], %s
add %s, 4
sub %s, 4
test %s, %s
jnz ._loop
CODE,
TMP_REG[1], SRC_REG, COUNTER_REG,
DST_REG, COUNTER_REG, TMP_REG[1],
COUNTER_REG,
LEN_REG,
LEN_REG, LEN_REG
);
$branches .= sprintf(<<<CODE
.jge_2:
mov %s, [%s + %s]
mov [%s + %s], %s
add %s, 2
sub %s, 2
test %s, %s
jnz ._loop
CODE,
TMP_REG[2], SRC_REG, COUNTER_REG,
DST_REG, COUNTER_REG, TMP_REG[2],
COUNTER_REG,
LEN_REG,
LEN_REG, LEN_REG
);
$branches .= sprintf(<<<CODE
.jge_1:
mov %s, [%s + %s]
mov [%s + %s], %s
CODE,
TMP_REG[3], SRC_REG, COUNTER_REG,
DST_REG, COUNTER_REG, TMP_REG[3],);
printf(<<<CODE
;
; @author Ammar Faizi <ammarfaizi2@gmail.com> https://www.facebook.com/ammarfaizi2
;
[section .text]
global memcpy_avx%d
;
; void *memcpy_avx%d(void *dst, const void *src, size_t len);
;
; %s = dst
; %s = src
; %s = len
;
memcpy_avx%d:
xor %s, %s
mov %s, %s
._loop:
CODE,
MAX_VEC_REGISTER_BIT,
MAX_VEC_REGISTER_BIT,
DST_REG,
SRC_REG,
LEN_REG,
MAX_VEC_REGISTER_BIT,
COUNTER_REG, COUNTER_REG, RET_REG, SRC_REG);
printf("%s", $loop);
printf(<<<CODE
cmp %s, 8
jge .jge_8 ; If the length is greater than or equal to 8
cmp %s, 4
jge .jge_4 ; If the length is greater than or equal to 4
cmp %s, 2
jge .jge_2 ; If the length is greater than or equal to 2
cmp %s, 1
jge .jge_1 ; If the length is greater than or equal to 1
jmp .epi
CODE, COUNTER_REG, COUNTER_REG, COUNTER_REG, COUNTER_REG);
printf("%s", $branches);
printf(<<<CODE
.epi:
ret
CODE);