Debian Codegraph @

mpn/x86_64/zen/mul_basecase.asm

raw
1 dnl AMD64 mpn_mul_basecase optimised for AMD Zen.
2
3 dnl Copyright 2012, 2013, 2017 Free Software Foundation, Inc.
4
5 dnl This file is part of the GNU MP Library.
6 dnl
7 dnl The GNU MP Library is free software; you can redistribute it and/or modify
8 dnl it under the terms of either:
9 dnl
10 dnl * the GNU Lesser General Public License as published by the Free
11 dnl Software Foundation; either version 3 of the License, or (at your
12 dnl option) any later version.
13 dnl
14 dnl or
15 dnl
16 dnl * the GNU General Public License as published by the Free Software
17 dnl Foundation; either version 2 of the License, or (at your option) any
18 dnl later version.
19 dnl
20 dnl or both in parallel, as here.
21 dnl
22 dnl The GNU MP Library is distributed in the hope that it will be useful, but
23 dnl WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 dnl or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 dnl for more details.
26 dnl
27 dnl You should have received copies of the GNU General Public License and the
28 dnl GNU Lesser General Public License along with the GNU MP Library. If not,
29 dnl see https://www.gnu.org/licenses/.
30
31 include(`../config.m4')
32
33 C TODO
34 C * Try 2x unrolling instead of current 4x, at least for mul_1. Else consider
35 C shallower sw pipelining of mul_1/addmul_1 loops, allowing 4 or 6 instead
36 C of 8 product registers.
37 C * Split up mul_1 into 4 loops in order to fall into the addmul_1 loops
38 C without branch tree.
39 C * Improve the overlapped software pipelining. The mulx in the osp block now
40 C suffers from write/read conflicts, in particular the 1 mod 4 case. Also,
41 C mul_1 could osp into addmul_1.
42 C * Let vn_param be vn to save a copy.
43 C * Re-allocate to benefit more from 32-bit encoding.
44 C * Poor performance for e.g. n = 12,16.
45
46 define(`rp', `%rdi')
47 define(`up', `%rsi')
48 define(`un_param', `%rdx')
49 define(`vp_param', `%rcx')
50 define(`vn_param', `%r8')
51
52 define(`un', `%r14')
53 define(`vp', `%rbp')
54 define(`v0', `%rdx')
55 define(`n', `%rcx')
56 define(`vn', `%r15')
57
58 ABI_SUPPORT(DOS64)
59 ABI_SUPPORT(STD64)
60
61 ASM_START()
62 TEXT
63 ALIGN(32)
64 PROLOGUE(mpn_mul_basecase)
65 FUNC_ENTRY(4)
66 IFDOS(` mov 56(%rsp), %r8d ')
67
68 cmp $2, un_param
69 ja L(gen)
70 mov (vp_param), %rdx
71 mulx( (up), %rax, %r9) C 0 1
72 je L(s2x)
73
74 L(s11): mov %rax, (rp)
75 mov %r9, 8(rp)
76 FUNC_EXIT()
77 ret
78
79 L(s2x): cmp $2, vn_param
80 mulx( 8,(up), %r8, %r10) C 1 2
81 je L(s22)
82
83 L(s21): add %r8, %r9
84 adc $0, %r10
85 mov %rax, (rp)
86 mov %r9, 8(rp)
87 mov %r10, 16(rp)
88 FUNC_EXIT()
89 ret
90
91 L(s22): add %r8, %r9 C 1
92 adc $0, %r10 C 2
93 mov 8(vp_param), %rdx
94 mov %rax, (rp)
95 mulx( (up), %r8, %r11) C 1 2
96 mulx( 8,(up), %rax, %rdx) C 2 3
97 add %r11, %rax C 2
98 adc $0, %rdx C 3
99 add %r8, %r9 C 1
100 adc %rax, %r10 C 2
101 adc $0, %rdx C 3
102 mov %r9, 8(rp)
103 mov %r10, 16(rp)
104 mov %rdx, 24(rp)
105 FUNC_EXIT()
106 ret
107
108
109 L(gen): push %r15
110 push %r14
111 push %r13
112 push %r12
113 push %rbp
114 push %rbx
115
116 mov un_param, un
117 mov vp_param, vp
118 mov vn_param, vn
119
120 mov (up), %r9
121 mov (vp), v0
122
123 lea (up,un,8), up
124 lea -32(rp,un,8), rp
125
126 neg un
127 mov un, n
128 test $1, R8(un)
129 jz L(mx0)
130 L(mx1): test $2, R8(un)
131 jz L(mb3)
132
133 L(mb1): mulx( %r9, %rbx, %rax)
134 inc n
135 .byte 0xc4,0x22,0xb3,0xf6,0x44,0xf6,0x08 C mulx 8(up,un,8), %r9, %r8
136 .byte 0xc4,0x22,0xa3,0xf6,0x54,0xf6,0x10 C mulx 16(up,un,8), %r11, %r10
137 jmp L(mlo1)
138
139 L(mb3): mulx( %r9, %r11, %r10)
140 .byte 0xc4,0x22,0x93,0xf6,0x64,0xf6,0x08 C mulx 8(up,un,8), %r13, %r12
141 .byte 0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x10 C mulx 16(up,un,8), %rbx, %rax
142 sub $-3, n
143 jz L(mwd3)
144 test R32(%rdx), R32(%rdx)
145 jmp L(mlo3)
146
147 L(mx0): test $2, R8(un)
148 jz L(mb0)
149
150 L(mb2): mulx( %r9, %r13, %r12)
151 .byte 0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x08 C mulx 8(up,un,8), %rbx, %rax
152 lea 2(n), n
153 .byte 0xc4,0x22,0xb3,0xf6,0x44,0xf6,0x10 C mulx 16(up,un,8), %r9, %r8
154 jmp L(mlo2)
155
156 L(mb0): mulx( %r9, %r9, %r8)
157 .byte 0xc4,0x22,0xa3,0xf6,0x54,0xf6,0x08 C mulx 8(up,un,8), %r11, %r10
158 .byte 0xc4,0x22,0x93,0xf6,0x64,0xf6,0x10 C mulx 16(up,un,8), %r13, %r12
159 jmp L(mlo0)
160
161 L(mtop):jrcxz L(mend)
162 adc %r8, %r11
163 mov %r9, (rp,n,8)
164 L(mlo3):.byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
165 adc %r10, %r13
166 mov %r11, 8(rp,n,8)
167 L(mlo2):.byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(up,n,8), %r11, %r10
168 adc %r12, %rbx
169 mov %r13, 16(rp,n,8)
170 L(mlo1):.byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(up,n,8), %r13, %r12
171 adc %rax, %r9
172 mov %rbx, 24(rp,n,8)
173 L(mlo0):.byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(up,n,8), %rbx, %rax
174 lea 4(n), n
175 jmp L(mtop)
176
177 L(mend):mov %r9, (rp)
178 adc %r8, %r11
179 L(mwd3):mov %r11, 8(rp)
180 adc %r10, %r13
181 mov %r13, 16(rp)
182 adc %r12, %rbx
183 adc $0, %rax
184 mov %rbx, 24(rp)
185 mov %rax, 32(rp)
186 add $8, vp
187 dec vn
188 jz L(end)
189
190 C The rest of the file are 4 osp loops around addmul_1
191
192 test $1, R8(un)
193 jnz L(0x1)
194
195 L(0x0): test $2, R8(un)
196 jnz L(oloop2_entry)
197
198 L(oloop0_entry):
199 C initial feed-in block
200 mov (vp), %rdx
201 add $8, vp
202 mov un, n
203 add $8, rp
204 .byte 0xc4,0x22,0xb3,0xf6,0x04,0xf6 C mulx (up,un,8), %r9, %r8
205 .byte 0xc4,0x22,0xa3,0xf6,0x54,0xf6,0x08 C mulx 8(up,un,8), %r11, %r10
206 .byte 0xc4,0x22,0x93,0xf6,0x64,0xf6,0x10 C mulx 16(up,un,8), %r13, %r12
207 .byte 0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x18 C mulx 24(up,un,8), %rbx, %rax
208 add %r8, %r11
209 jmp L(lo0)
210
211 L(oloop0):
212 C overlapped software pipelining block
213 mov (vp), %rdx C new
214 add $8, vp
215 add %r9, (rp) C prev
216 .byte 0xc4,0x22,0xb3,0xf6,0x04,0xf6 C mulx (%rsi,%r14,8),%r9,%r8
217 adc %r11, 8(rp) C prev
218 .byte 0xc4,0x22,0xa3,0xf6,0x54,0xf6,0x08 C mulx 0x8(%rsi,%r14,8),%r11,%r10
219 adc %r13, 16(rp) C prev
220 .byte 0xc4,0x22,0x93,0xf6,0x64,0xf6,0x10 C mulx 0x10(%rsi,%r14,8),%r13,%r12
221 adc %rbx, 24(rp) C prev
222 mov un, n
223 adc $0, %rax C prev
224 mov %rax, 32(rp) C prev
225 add $8, rp
226 .byte 0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x18 C mulx 0x18(%rsi,%r14,8),%rbx,%rax
227 add %r8, %r11 C new
228 jmp L(lo0)
229
230 ALIGN(16)
231 L(tp0): add %r9, (rp,n,8)
232 .byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
233 adc %r11, 8(rp,n,8)
234 .byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(up,n,8), %r11, %r10
235 adc %r13, 16(rp,n,8)
236 .byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(up,n,8), %r13, %r12
237 adc %rbx, 24(rp,n,8)
238 adc %rax, %r9
239 .byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(up,n,8), %rbx, %rax
240 adc %r8, %r11
241 L(lo0): adc %r10, %r13
242 adc %r12, %rbx
243 adc $0, %rax
244 add $4, n
245 jnz L(tp0)
246
247 dec vn
248 jne L(oloop0)
249
250 jmp L(final_wind_down)
251
252 L(oloop2_entry):
253 mov (vp), %rdx
254 add $8, vp
255 lea 2(un), n
256 add $8, rp
257 .byte 0xc4,0x22,0x93,0xf6,0x24,0xf6 C mulx (up,un,8), %r13, %r12
258 .byte 0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x08 C mulx 8(up,un,8), %rbx, %rax
259 add %r12, %rbx
260 adc $0, %rax
261 .byte 0xc4,0x22,0xb3,0xf6,0x44,0xf6,0x10 C mulx 16(up,un,8), %r9, %r8
262 .byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(up,n,8), %r11, %r10
263 add %r13, 16(rp,n,8)
264 jmp L(lo2)
265
266 L(oloop2):
267 mov (vp), %rdx
268 add $8, vp
269 add %r9, (rp)
270 adc %r11, 8(rp)
271 adc %r13, 16(rp)
272 .byte 0xc4,0x22,0x93,0xf6,0x24,0xf6 C mulx (up,un,8), %r13, %r12
273 adc %rbx, 24(rp)
274 adc $0, %rax
275 mov %rax, 32(rp)
276 .byte 0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x08 C mulx 8(up,un,8), %rbx, %rax
277 lea 2(un), n
278 add $8, rp
279 .byte 0xc4,0x22,0xb3,0xf6,0x44,0xf6,0x10 C mulx 16(up,un,8), %r9, %r8
280 add %r12, %rbx
281 adc $0, %rax
282 .byte 0xc4,0x22,0xa3,0xf6,0x54,0xf6,0x18 C mulx 0x18(%rsi,%r14,8),%r11,%r10
283 add %r13, 16(rp,n,8)
284 jmp L(lo2)
285
286 ALIGN(16)
287 L(tp2): add %r9, (rp,n,8)
288 .byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
289 adc %r11, 8(rp,n,8)
290 .byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(up,n,8), %r11, %r10
291 adc %r13, 16(rp,n,8)
292 L(lo2): .byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(up,n,8), %r13, %r12
293 adc %rbx, 24(rp,n,8)
294 adc %rax, %r9
295 .byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(up,n,8), %rbx, %rax
296 adc %r8, %r11
297 adc %r10, %r13
298 adc %r12, %rbx
299 adc $0, %rax
300 add $4, n
301 jnz L(tp2)
302
303 dec vn
304 jne L(oloop2)
305
306 jmp L(final_wind_down)
307
308 L(0x1): test $2, R8(un)
309 jz L(oloop3_entry)
310
311 L(oloop1_entry):
312 mov (vp), %rdx
313 add $8, vp
314 lea 1(un), n
315 add $8, rp
316 .byte 0xc4,0xa2,0xe3,0xf6,0x04,0xf6 C mulx (up,un,8), %rbx, %rax
317 .byte 0xc4,0x22,0xb3,0xf6,0x44,0xf6,0x08 C mulx 8(up,un,8), %r9, %r8
318 .byte 0xc4,0x22,0xa3,0xf6,0x54,0xf6,0x10 C mulx 16(up,un,8), %r11, %r10
319 .byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(up,n,8), %r13, %r12
320 add %rbx, 24(rp,n,8)
321 jmp L(lo1)
322
323 L(oloop1):
324 mov (vp), %rdx
325 add $8, vp
326 add %r9, (rp)
327 .byte 0xc4,0x22,0xb3,0xf6,0x44,0xf6,0x08 C mulx 8(up,un,8), %r9, %r8
328 adc %r11, 8(rp)
329 .byte 0xc4,0x22,0xa3,0xf6,0x54,0xf6,0x10 C mulx 16(up,un,8), %r11, %r10
330 adc %r13, 16(rp)
331 .byte 0xc4,0x22,0x93,0xf6,0x64,0xf6,0x18 C mulx 0x18(%rsi,%r14,8),%r13,%r12
332 adc %rbx, 24(rp)
333 adc $0, %rax
334 mov %rax, 32(rp)
335 .byte 0xc4,0xa2,0xe3,0xf6,0x04,0xf6 C mulx (up,un,8), %rbx, %rax
336 lea 1(un), n
337 add $8, rp
338 add %rbx, 24(rp,n,8)
339 jmp L(lo1)
340
341 ALIGN(16)
342 L(tp1): add %r9, (rp,n,8)
343 .byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
344 adc %r11, 8(rp,n,8)
345 .byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(up,n,8), %r11, %r10
346 adc %r13, 16(rp,n,8)
347 .byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(up,n,8), %r13, %r12
348 adc %rbx, 24(rp,n,8)
349 L(lo1): adc %rax, %r9
350 .byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(up,n,8), %rbx, %rax
351 adc %r8, %r11
352 adc %r10, %r13
353 adc %r12, %rbx
354 adc $0, %rax
355 add $4, n
356 jnz L(tp1)
357
358 dec vn
359 jne L(oloop1)
360
361 jmp L(final_wind_down)
362
363 L(oloop3_entry):
364 mov (vp), %rdx
365 add $8, vp
366 lea 3(un), n
367 add $8, rp
368 .byte 0xc4,0x22,0xa3,0xf6,0x14,0xf6 C mulx (up,un,8), %r11, %r10
369 .byte 0xc4,0x22,0x93,0xf6,0x64,0xf6,0x08 C mulx 8(up,un,8), %r13, %r12
370 .byte 0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x10 C mulx 16(up,un,8), %rbx, %rax
371 add %r10, %r13
372 adc %r12, %rbx
373 adc $0, %rax
374 test n, n
375 jz L(wd3)
376 .byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
377 add %r11, 8(rp,n,8)
378 jmp L(lo3)
379
380 L(oloop3):
381 mov (vp), %rdx
382 add $8, vp
383 add %r9, (rp)
384 adc %r11, 8(rp)
385 .byte 0xc4,0x22,0xa3,0xf6,0x14,0xf6 C mulx (up,un,8), %r11, %r10
386 adc %r13, 16(rp)
387 .byte 0xc4,0x22,0x93,0xf6,0x64,0xf6,0x08 C mulx 8(up,un,8), %r13, %r12
388 adc %rbx, 24(rp)
389 adc $0, %rax
390 mov %rax, 32(rp)
391 .byte 0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x10 C mulx 16(up,un,8), %rbx, %rax
392 lea 3(un), n
393 add $8, rp
394 add %r10, %r13
395 .byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
396 adc %r12, %rbx
397 adc $0, %rax
398 add %r11, 8(rp,n,8)
399 jmp L(lo3)
400
401 ALIGN(16)
402 L(tp3): add %r9, (rp,n,8)
403 .byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
404 adc %r11, 8(rp,n,8)
405 L(lo3): .byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(up,n,8), %r11, %r10
406 adc %r13, 16(rp,n,8)
407 .byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(up,n,8), %r13, %r12
408 adc %rbx, 24(rp,n,8)
409 adc %rax, %r9
410 .byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(up,n,8), %rbx, %rax
411 adc %r8, %r11
412 adc %r10, %r13
413 adc %r12, %rbx
414 adc $0, %rax
415 add $4, n
416 jnz L(tp3)
417
418 dec vn
419 jne L(oloop3)
420
421 L(final_wind_down):
422 add %r9, (rp)
423 adc %r11, 8(rp)
424 adc %r13, 16(rp)
425 adc %rbx, 24(rp)
426 adc $0, %rax
427 mov %rax, 32(rp)
428
429 L(end): pop %rbx
430 pop %rbp
431 pop %r12
432 pop %r13
433 pop %r14
434 pop %r15
435 FUNC_EXIT()
436 ret
437
438 L(3): mov (vp), %rdx
439 add $8, vp
440 add $8, rp
441 .byte 0xc4,0x22,0xa3,0xf6,0x14,0xf6 C mulx (up,un,8), %r11, %r10
442 .byte 0xc4,0x22,0x93,0xf6,0x64,0xf6,0x08 C mulx 8(up,un,8), %r13, %r12
443 .byte 0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x10 C mulx 16(up,un,8), %rbx, %rax
444 add %r10, %r13
445 adc %r12, %rbx
446 adc $0, %rax
447 L(wd3): adc %r11, 8(rp)
448 adc %r13, 16(rp)
449 adc %rbx, 24(rp)
450 adc $0, %rax
451 mov %rax, 32(rp)
452 dec vn
453 jne L(3)
454 jmp L(end)
455 EPILOGUE()