Debian Codegraph @

mpn/x86_64/zen/sbpi1_bdiv_r.asm

raw
1 dnl AMD64 mpn_sbpi1_bdiv_r optimised for AMD Zen
2
3 dnl Copyright 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
34 define(`up', `%rdi')
35 define(`un_param', `%rsi')
36 define(`dp_param', `%rdx')
37 define(`dn_param', `%rcx')
38 define(`dinv', `%r8')
39
40 define(`i', `%rcx')
41 define(`dn', `%r14')
42
43 define(`dp', `%rsi')
44 define(`un', `%r15')
45
46 C TODO
47 C * The o1...o8 loops for special dn counts were naively hand-optimised by
48 C folding the generic loops. They can probably be tuned. The speculative
49 C quotient limb generation might not be in the optimal spot.
50 C * Perhaps avoid late-in-loop jumps, e.g., lo0.
51 C * Improve regalloc wrt dn_param/dn and un_param/un to save some moves.
52
53 C ABI_SUPPORT(DOS64)
54 C ABI_SUPPORT(STD64)
55
56 ASM_START()
57 TEXT
58 ALIGN(16)
59 PROLOGUE(mpn_sbpi1_bdiv_r)
60 FUNC_ENTRY(4)
61 IFDOS(` mov 56(%rsp), dinv ')
62 push %r15
63 push %r14
64 push %r13
65 push %r12
66 push %rbp
67 push %rbx
68
69 sub dn_param, un_param C outer loop count
70 mov dn_param, dn C FIXME: Suppress by reg re-alloc
71 push dinv C keep dinv on stack
72 mov un_param, un C FIXME: Suppress by reg re-alloc
73 xor R32(%rbp), R32(%rbp)
74
75 lea (dp_param,dn_param,8), dp
76
77 mov (up), %rdx
78 imul dinv, %rdx C first quotient limb
79
80 neg dn
81 lea -32(up,dn_param,8), up
82
83 test $1, R8(dn_param)
84 jnz L(cx1)
85
86 L(cx0): test $2, R8(dn_param)
87 jnz L(b2)
88
89
90 C =============================================================================
91 L(b0): cmp $-4, dn
92 jnz L(gt4)
93
94 L(o4): mulx( -32,(dp), %r9, %r14)
95 mulx( -24,(dp), %r11, %r10)
96 mulx( -16,(dp), %r13, %r12)
97 mulx( -8,(dp), %rbx, %rax)
98 add %r14, %r11
99 adc %r10, %r13
100 adc %r12, %rbx
101 adc $0, %rax
102 add (up), %r9
103 adc 8(up), %r11
104 mov %r8, %rdx C dinv
105 mov %r11, 8(up)
106 mulx( %r11, %rdx, %r12) C next quotient
107 adc %r13, 16(up)
108 adc %rbx, 24(up)
109 adc %rbp, %rax
110 setc R8(%rbp)
111 add %rax, 32(up)
112 adc $0, R32(%rbp)
113 lea 8(up), up
114 dec un
115 jne L(o4)
116 jmp L(ret)
117
118 L(gt4): cmp $-8, dn
119 jnz L(out0)
120
121 L(o8): mulx( -64,(dp), %r9, %r14)
122 mulx( -56,(dp), %rcx, %r10)
123 mulx( -48,(dp), %r13, %r12)
124 mulx( -40,(dp), %rbx, %rax)
125 add %r14, %rcx
126 adc %r10, %r13
127 adc %r12, %rbx
128 adc $0, %rax
129 add -32(up), %r9
130 mulx( -32,(dp), %r9, %r14)
131 adc -24(up), %rcx
132 mov %rcx, -24(up)
133 mulx( -24,(dp), %r11, %r10)
134 adc %r13, -16(up)
135 mulx( -16,(dp), %r13, %r12)
136 adc %rbx, -8(up)
137 adc %rax, %r9
138 mulx( -8,(dp), %rbx, %rax)
139 adc %r14, %r11
140 adc %r10, %r13
141 adc %r12, %rbx
142 adc $0, %rax
143 mov %r8, %rdx C dinv
144 mulx( %rcx, %rdx, %r12) C next quotient
145 add %r9, (up)
146 adc %r11, 8(up)
147 adc %r13, 16(up)
148 adc %rbx, 24(up)
149 adc %rbp, %rax
150 setc R8(%rbp)
151 add %rax, 32(up)
152 adc $0, R32(%rbp)
153 lea 8(up), up
154 dec un
155 jne L(o8)
156 jmp L(ret)
157
158 L(out0):mov dn, i
159 .byte 0xc4,0x22,0xb3,0xf6,0x04,0xf6 C mulx (dp,dn,8),%r9,%r8
160 .byte 0xc4,0x22,0xa3,0xf6,0x54,0xf6,0x08 C mulx 8(dp,dn,8),%r11,%r10
161 .byte 0xc4,0x22,0x93,0xf6,0x64,0xf6,0x10 C mulx 16(dp,dn,8),%r13,%r12
162 clc
163 jmp L(lo0)
164
165 ALIGN(16)
166 L(top0):add %r9, (up,i,8)
167 .byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (dp,i,8), %r9, %r8
168 adc %r11, 8(up,i,8)
169 .byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(dp,i,8), %r11, %r10
170 adc %r13, 16(up,i,8)
171 .byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(dp,i,8), %r13, %r12
172 adc %rbx, 24(up,i,8)
173 adc %rax, %r9
174 L(lo0): .byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(dp,i,8), %rbx, %rax
175 adc %r8, %r11
176 adc %r10, %r13
177 adc %r12, %rbx
178 adc $0, %rax
179 add $4, i
180 js L(top0)
181
182 mov (%rsp), %rdx C dinv
183 .byte 0xc4,0x22,0xeb,0xf6,0x64,0xf7,0x28 C mulx 40(%rdi,%r14,8),%rdx,%r12
184 add %r9, (up)
185 adc %r11, 8(up)
186 adc %r13, 16(up)
187 adc %rbx, 24(up)
188 adc %rbp, %rax
189 setc R8(%rbp)
190 add %rax, 32(up)
191 adc $0, R32(%rbp)
192 lea 8(up), up
193 dec un
194 jne L(out0)
195 jmp L(ret)
196
197 L(cx1): test $2, R8(dn_param)
198 jnz L(b3)
199
200 C =============================================================================
201 L(b1): cmp $-1, dn
202 jnz L(gt1)
203
204 mov 24(up), %r9
205 L(o1): mulx( -8,(dp), %rbx, %rdx)
206 add %r9, %rbx
207 adc %rbp, %rdx
208 add 32(up), %rdx
209 setc R8(%rbp)
210 mov %rdx, %r9
211 mulx( %r8, %rdx, %r12) C next quotient
212 lea 8(up), up
213 dec un
214 jne L(o1)
215 mov %r9, 24(up)
216 jmp L(ret)
217
218 L(gt1): cmp $-5, dn
219 jnz L(out1)
220
221 L(o5): mulx( -40,(dp), %rbx, %rax)
222 mulx( -32,(dp), %r9, %r14)
223 mulx( -24,(dp), %r11, %r10)
224 mulx( -16,(dp), %r13, %r12)
225 add -8(up), %rbx
226 adc %rax, %r9
227 mulx( -8,(dp), %rbx, %rax)
228 adc %r14, %r11
229 adc %r10, %r13
230 adc %r12, %rbx
231 adc $0, %rax
232 add (up), %r9
233 mov %r9, (up)
234 mov %r8, %rdx C dinv
235 mulx( %r9, %rdx, %r12) C next quotient
236 adc %r11, 8(up)
237 adc %r13, 16(up)
238 adc %rbx, 24(up)
239 adc %rbp, %rax
240 setc R8(%rbp)
241 add %rax, 32(up)
242 adc $0, R32(%rbp)
243 lea 8(up), up
244 dec un
245 jne L(o5)
246 jmp L(ret)
247
248 L(out1):lea 1(dn), i
249 .byte 0xc4,0xa2,0xe3,0xf6,0x04,0xf6 C mulx (dp,dn,8),%rbx,%rax
250 .byte 0xc4,0x22,0xb3,0xf6,0x44,0xf6,0x08 C mulx 8(dp,dn,8),%r9,%r8
251 .byte 0xc4,0x22,0xa3,0xf6,0x54,0xf6,0x10 C mulx 16(dp,dn,8),%r11,%r10
252 clc
253 jmp L(lo1)
254
255 ALIGN(16)
256 L(top1):add %r9, (up,i,8)
257 .byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (dp,i,8), %r9, %r8
258 adc %r11, 8(up,i,8)
259 .byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(dp,i,8), %r11, %r10
260 adc %r13, 16(up,i,8)
261 L(lo1): .byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(dp,i,8), %r13, %r12
262 adc %rbx, 24(up,i,8)
263 adc %rax, %r9
264 .byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(dp,i,8), %rbx, %rax
265 adc %r8, %r11
266 adc %r10, %r13
267 adc %r12, %rbx
268 adc $0, %rax
269 add $4, i
270 js L(top1)
271
272 mov (%rsp), %rdx C dinv
273 .byte 0xc4,0x22,0xeb,0xf6,0x64,0xf7,0x28 C mulx 40(up,dn,8), %rdx, %r12
274 add %r9, (up)
275 adc %r11, 8(up)
276 adc %r13, 16(up)
277 adc %rbx, 24(up)
278 adc %rbp, %rax
279 setc R8(%rbp)
280 add %rax, 32(up)
281 adc $0, R32(%rbp)
282 lea 8(up), up
283 dec un
284 jne L(out1)
285 jmp L(ret)
286
287 C =============================================================================
288 L(b2): cmp $-2, dn
289 jnz L(gt2)
290
291 mov 16(up), %r10
292 mov 24(up), %r9
293 L(o2): mulx( -16,(dp), %r13, %r12)
294 mulx( -8,(dp), %rbx, %rax)
295 add %r12, %rbx
296 adc $0, %rax
297 add %r10, %r13 C 0 add just to produce carry
298 mov %r9, %r10 C 1
299 adc %rbx, %r10 C 1
300 mov %r8, %rdx
301 mulx( %r10, %rdx, %r12) C next quotient
302 adc %rbp, %rax C 2
303 setc R8(%rbp) C 3
304 mov 32(up), %r9 C 2
305 add %rax, %r9 C 2
306 adc $0, R32(%rbp) C 3
307 lea 8(up), up
308 dec un
309 jne L(o2)
310 mov %r10, 16(up)
311 mov %r9, 24(up)
312 jmp L(ret)
313
314 L(gt2): cmp $-6, dn
315 jnz L(out2)
316
317 L(o6): mulx( -48,(dp), %r13, %r12)
318 mulx( -40,(dp), %rcx, %rax)
319 add %r12, %rcx
320 adc $0, %rax
321 mulx( -32,(dp), %r9, %r14)
322 mulx( -24,(dp), %r11, %r10)
323 add -16(up), %r13
324 mulx( -16,(dp), %r13, %r12)
325 adc -8(up), %rcx
326 mov %rcx, -8(up)
327 adc %rax, %r9
328 mulx( -8,(dp), %rbx, %rax)
329 adc %r14, %r11
330 adc %r10, %r13
331 adc %r12, %rbx
332 adc $0, %rax
333 mov %r8, %rdx C dinv
334 mulx( %rcx, %rdx, %r12) C next quotient
335 add %r9, (up)
336 adc %r11, 8(up)
337 adc %r13, 16(up)
338 adc %rbx, 24(up)
339 adc %rbp, %rax
340 setc R8(%rbp)
341 add %rax, 32(up)
342 adc $0, R32(%rbp)
343 lea 8(up), up
344 dec un
345 jne L(o6)
346 jmp L(ret)
347
348 L(out2):lea 2(dn), i
349 .byte 0xc4,0x22,0x93,0xf6,0x24,0xf6 C mulx (dp,dn,8),%r13,%r12
350 .byte 0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x08 C mulx 8(dp,dn,8),%rbx,%rax
351 add %r12, %rbx
352 adc $0, %rax
353 .byte 0xc4,0x22,0xb3,0xf6,0x44,0xf6,0x10 C mulx 16(dp,dn,8),%r9,%r8
354 jmp L(lo2)
355
356 ALIGN(16)
357 L(top2):add %r9, (up,i,8)
358 .byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (dp,i,8), %r9, %r8
359 adc %r11, 8(up,i,8)
360 L(lo2): .byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(dp,i,8), %r11, %r10
361 adc %r13, 16(up,i,8)
362 .byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(dp,i,8), %r13, %r12
363 adc %rbx, 24(up,i,8)
364 adc %rax, %r9
365 .byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(dp,i,8), %rbx, %rax
366 adc %r8, %r11
367 adc %r10, %r13
368 adc %r12, %rbx
369 adc $0, %rax
370 add $4, i
371 js L(top2)
372
373 mov (%rsp), %rdx C dinv
374 .byte 0xc4,0x22,0xeb,0xf6,0x64,0xf7,0x28 C mulx 40(up,dn,8), %rdx, %r12
375 add %r9, (up)
376 adc %r11, 8(up)
377 adc %r13, 16(up)
378 adc %rbx, 24(up)
379 adc %rbp, %rax
380 setc R8(%rbp)
381 add %rax, 32(up)
382 adc $0, R32(%rbp)
383 lea 8(up), up
384 dec un
385 jne L(out2)
386 jmp L(ret)
387
388 C =============================================================================
389 L(b3): cmp $-3, dn
390 jnz L(gt3)
391
392 mov 8(up), %r14
393 mov 16(up), %r9
394 mov 24(up), %rcx
395 L(o3): mulx( -24,(dp), %r11, %r10)
396 mulx( -16,(dp), %r13, %r12)
397 mulx( -8,(dp), %rbx, %rax)
398 add %r10, %r13
399 adc %r12, %rbx
400 adc $0, %rax
401 add %r14, %r11
402 mov %r9, %r14
403 adc %r13, %r14
404 mov %rcx, %r9
405 mov %r8, %rdx C dinv
406 mulx( %r14, %rdx, %r12) C next quotient
407 adc %rbx, %r9
408 adc %rbp, %rax
409 setc R8(%rbp)
410 mov 32(up), %rcx
411 add %rax, %rcx
412 adc $0, R32(%rbp)
413 lea 8(up), up
414 dec un
415 jne L(o3)
416 mov %r14, 8(up)
417 mov %r9, 16(up)
418 mov %rcx, 24(up)
419 jmp L(ret)
420
421 L(gt3): cmp $-7, dn
422 jnz L(out3)
423
424 L(o7): mulx( -56,(dp), %r11, %r10)
425 mulx( -48,(dp), %rcx, %r12)
426 mulx( -40,(dp), %rbx, %rax)
427 add %r10, %rcx
428 adc %r12, %rbx
429 adc $0, %rax
430 mulx( -32,(dp), %r9, %r14)
431 add -24(up), %r11
432 mulx( -24,(dp), %r11, %r10)
433 adc -16(up), %rcx
434 mov %rcx, -16(up)
435 mulx( -16,(dp), %r13, %r12)
436 adc %rbx, -8(up)
437 adc %rax, %r9
438 mulx( -8,(dp), %rbx, %rax)
439 adc %r14, %r11
440 adc %r10, %r13
441 adc %r12, %rbx
442 adc $0, %rax
443 mov %r8, %rdx C dinv
444 mulx( %rcx, %rdx, %r12) C next quotient
445 add %r9, (up)
446 adc %r11, 8(up)
447 adc %r13, 16(up)
448 adc %rbx, 24(up)
449 adc %rbp, %rax
450 setc R8(%rbp)
451 add %rax, 32(up)
452 adc $0, R32(%rbp)
453 lea 8(up), up
454 dec un
455 jne L(o7)
456 jmp L(ret)
457
458 L(out3):lea 3(dn), i
459 .byte 0xc4,0x22,0xa3,0xf6,0x14,0xf6 C mulx (dp,dn,8),%r11,%r10
460 .byte 0xc4,0x22,0x93,0xf6,0x64,0xf6,0x08 C mulx 8(dp,dn,8),%r13,%r12
461 .byte 0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x10 C mulx 16(dp,dn,8),%rbx,%rax
462 add %r10, %r13
463 adc %r12, %rbx
464 adc $0, %rax
465 jmp L(lo3)
466
467 ALIGN(16)
468 L(top3):add %r9, (up,i,8)
469 L(lo3): .byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (dp,i,8), %r9, %r8
470 adc %r11, 8(up,i,8)
471 .byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(dp,i,8), %r11, %r10
472 adc %r13, 16(up,i,8)
473 .byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(dp,i,8), %r13, %r12
474 adc %rbx, 24(up,i,8)
475 adc %rax, %r9
476 .byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(dp,i,8), %rbx, %rax
477 adc %r8, %r11
478 adc %r10, %r13
479 adc %r12, %rbx
480 adc $0, %rax
481 add $4, i
482 js L(top3)
483
484 mov (%rsp), %rdx C dinv
485 .byte 0xc4,0x22,0xeb,0xf6,0x64,0xf7,0x28 C mulx 40(up,dn,8), %rdx, %r12
486 add %r9, (up)
487 adc %r11, 8(up)
488 adc %r13, 16(up)
489 adc %rbx, 24(up)
490 adc %rbp, %rax
491 setc R8(%rbp)
492 add %rax, 32(up)
493 adc $0, R32(%rbp)
494 lea 8(up), up
495 dec un
496 jne L(out3)
497
498 L(ret): mov %rbp, %rax
499 pop %rsi C dummy dealloc
500 pop %rbx
501 pop %rbp
502 pop %r12
503 pop %r13
504 pop %r14
505 pop %r15
506 ret
507 EPILOGUE()