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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
|
dnl AMD64 mpn_sbpi1_bdiv_r optimised for Intel Broadwell.
dnl Copyright 2015, 2021 Free Software Foundation, Inc.
dnl This file is part of the GNU MP Library.
dnl
dnl The GNU MP Library is free software; you can redistribute it and/or modify
dnl it under the terms of either:
dnl
dnl * the GNU Lesser General Public License as published by the Free
dnl Software Foundation; either version 3 of the License, or (at your
dnl option) any later version.
dnl
dnl or
dnl
dnl * the GNU General Public License as published by the Free Software
dnl Foundation; either version 2 of the License, or (at your option) any
dnl later version.
dnl
dnl or both in parallel, as here.
dnl
dnl The GNU MP Library is distributed in the hope that it will be useful, but
dnl WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
dnl or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
dnl for more details.
dnl
dnl You should have received copies of the GNU General Public License and the
dnl GNU Lesser General Public License along with the GNU MP Library. If not,
dnl see https://www.gnu.org/licenses/.
include(`../config.m4')
C cycles/limb mul_1 addmul_1
C AMD K8,K9 n/a n/a
C AMD K10 n/a n/a
C AMD bd1 n/a n/a
C AMD bd2 n/a n/a
C AMD bd3 n/a n/a
C AMD bd4 ? ?
C AMD zn1 ? ?
C AMD zn2 ? ?
C AMD zn3 ? ?
C AMD bt1 n/a n/a
C AMD bt2 n/a n/a
C Intel P4 n/a n/a
C Intel PNR n/a n/a
C Intel NHM n/a n/a
C Intel SBR n/a n/a
C Intel IBR n/a n/a
C Intel HWL 1.68 n/a
C Intel BWL 1.51 1.67-1.74
C Intel SKL 1.52 1.63-1.71
C Intel atom n/a n/a
C Intel SLM n/a n/a
C VIA nano n/a n/a
C The inner loops of this code are the result of running a code generation and
C optimisation tool suite written by David Harvey and Torbjorn Granlund.
C TODO
C * Do overlapped software pipelining.
C * Reduce register use, i.e., by combining n_neg and n_save.
C * Supporess initial store through up, it's always a zero.
C * Streamline up and dp setup.
C * When changing this, make sure the code which falls into the inner loops
C does not execute too many no-ops (for both PIC and non-PIC).
dnl mp_limb_t
dnl mpn_sbpi1_bdiv_r (mp_ptr up, mp_size_t un,
dnl mp_srcptr dp, mp_size_t dn, mp_limb_t dinv)
define(`up', `%rdi')
define(`un', `%rsi')
define(`dp_param',`%rdx')
define(`dn_param',`%rcx')
define(`dinv', `%r8')
define(`n', `%rcx')
define(`n_save', `%rbp')
define(`dp', `%r14')
define(`n_neg', `%rbx')
define(`q', `%rdx')
define(`jaddr', `%rax')
define(`w0', `%r12')
define(`w1', `%r9')
define(`w2', `%r10')
define(`w3', `%r11')
ifdef(`MAX_SPECIAL',,`
define(`MAX_SPECIAL', 8)')
ABI_SUPPORT(DOS64)
ABI_SUPPORT(STD64)
ASM_START()
TEXT
ALIGN(16)
PROLOGUE(mpn_sbpi1_bdiv_r)
FUNC_ENTRY(4)
IFDOS(` mov 56(%rsp), %r8 ')
lea L(atab)(%rip), %r10
cmp $MAX_SPECIAL, dn_param
jbe L(sma)
ifelse(MAX_SPECIAL,8,,`
forloop(i,eval(MAX_SPECIAL+1),9,`L(i):
')')
L(gen): push %rbx
push %rbp
push %r12
push %r13
push %r14
mov dp_param, dp C free up rdx
xor %r13, %r13
sub dn_param, un C outer loop count
lea -8(,dn_param,8), n_neg
neg n_neg
mov dn_param, n_save
mov R32(dn_param), R32(%rax)
shr $3, n_save C loop count
and $7, R32(%rax) C clear CF and OF as side-effect
ifdef(`PIC',
` movslq (%r10,%rax,4), %rax
lea (%rax,%r10), jaddr
',`
mov (%r10,%rax,8), jaddr
')
mov (up), q
imul dinv, q
jmp L(outer)
L(f0): mulx( (dp), w2, w3)
lea -1(n), n
mulx( 8,(dp), w0, w1)
lea -8(dp), dp
adcx( w3, w0)
adox( (up), w2)
lea -8(up), up
jmp L(b0x)
L(f3): mulx( (dp), w0, w1)
mulx( 8,(dp), w2, w3)
adox( (up), w0)
lea -48(up), up
lea 16(dp), dp
jmp L(b3x)
L(f4): mulx( (dp), w2, w3)
mulx( 8,(dp), w0, w1)
lea 24(dp), dp
adox( (up), w2)
lea -40(up), up
adcx( w3, w0)
jmp L(b4x)
L(f5): mulx( (dp), w0, w1)
mulx( 8,(dp), w2, w3)
lea 32(dp), dp
adcx( w1, w2)
adox( (up), w0)
lea -32(up), up
jmp L(b5x)
L(f6): mulx( (dp), w2, w3)
mulx( 8,(dp), w0, w1)
lea 40(dp), dp
adox( (up), w2)
lea -24(up), up
adcx( w3, w0)
jmp L(b6x)
L(f7): mulx( (dp), w0, w1)
mulx( 8,(dp), w2, w3)
lea 48(dp), dp
adcx( w1, w2)
adox( (up), w0)
lea -16(up), up
jmp L(b7x)
L(f1): mulx( (dp), w0, w1)
mulx( 8,(dp), w2, w3)
adox( (up), w0)
lea -1(n), n
jmp L(b1x)
L(f2): mulx( (dp), w2, w3)
mulx( 8,(dp), w0, w1)
lea 8(dp), dp
adox( (up), w2)
lea 8(up), up
adcx( w3, w0)
jmp L(b2x)
L(end): adox( (up), w0)
adox( %rcx, w1) C relies on rcx = 0
mov w0, (up)
adc %rcx, w1 C relies on rcx = 0
mov 8(up,n_neg), q C Compute next quotient early...
mulx( dinv, q, %r12) C ...(unused in last iteration)
bt $0, R32(%r13)
adc w1, 8(up)
setc R8(%r13)
dec un C clear OF as side-effect
jz L(done)
lea (dp,n_neg), dp C reset dp to D[]'s beginning
lea 8(up,n_neg), up C point up to U[]'s current beginning
L(outer):
mov n_save, n
test %eax, %eax C clear CF and OF
jmp *jaddr
ALIGN(16)
L(top): adox( -8,(up), w2)
adcx( w3, w0)
mov w2, -8(up)
jrcxz L(end)
L(b2x): mulx( 8,(dp), w2, w3)
adox( (up), w0)
lea -1(n), n
mov w0, (up)
L(b1x): adcx( w1, w2)
mulx( 16,(dp), w0, w1)
adcx( w3, w0)
adox( 8,(up), w2)
mov w2, 8(up)
L(b0x): mulx( 24,(dp), w2, w3)
lea 64(dp), dp
adcx( w1, w2)
adox( 16,(up), w0)
mov w0, 16(up)
L(b7x): mulx( -32,(dp), w0, w1)
adox( 24,(up), w2)
adcx( w3, w0)
mov w2, 24(up)
L(b6x): mulx( -24,(dp), w2, w3)
adcx( w1, w2)
adox( 32,(up), w0)
mov w0, 32(up)
L(b5x): mulx( -16,(dp), w0, w1)
adox( 40,(up), w2)
adcx( w3, w0)
mov w2, 40(up)
L(b4x): adox( 48,(up), w0)
mulx( -8,(dp), w2, w3)
mov w0, 48(up)
L(b3x): lea 64(up), up
adcx( w1, w2)
mulx( (dp), w0, w1)
jmp L(top)
L(done):mov %r13, %rax
pop %r14
pop %r13
pop %r12
pop %rbp
pop %rbx
FUNC_EXIT()
ret
L(sma):
ifdef(`PIC',
` movslq 28(%r10,dn_param,4), %rax
lea (%rax,%r10), jaddr
',`
mov 56(%r10,dn_param,8), jaddr
')
jmp *jaddr
L(1): mov (dp_param), %r10
xor R32(%rax), R32(%rax)
mov (up), %rdx
dec un
mov %rdx, %r9
L(o1): mulx( dinv, %rdx, %r11) C next quotient
lea 8(up), up
mulx( %r10, %rcx, %rdx) C 0 1
add %r9, %rcx C 0
adc %rax, %rdx C 1
add (up), %rdx C 1
setc R8(%rax) C 2
mov %rdx, %r9 C 1
dec un
jnz L(o1)
mov %r9, (up)
FUNC_EXIT()
ret
ifdef(`VER',,`define(`VER',1)')
L(2): push %r12
push %r14
mov dp_param, dp C free up rdx
sub dn_param, un C loop count
mov (up), q
imul dinv, q
ifelse(VER,0,`
xor R32(%rax), R32(%rax)
L(o2): test %eax, %eax C clear CF and OF
mulx( (dp), w2, w3) C 0 1
mulx( 8,(dp), %rdx, w1) C 1 2
add (up), w2 C 0
adc 8(up), %rdx C 1
adc $0, w1 C 2 cannot carry further
add w3, %rdx C 1
mov %rdx, 8(up) C 1
adc $0, w1 C 2
imul dinv, q C
bt $0, R32(%rax)
adc 16(up), w1 C 2
mov w1, 16(up)
setc R8(%rax)
lea 8(up), up
dec un
jnz L(o2)
')
ifelse(VER,1,`
push %rbx
push %r13
xor R32(%r13), R32(%r13)
mov (up), %rax
mov 8(up), %rbx
L(o2): xor R32(%rcx), R32(%rcx)
mulx( (dp), w2, w3) C 0 1
mulx( 8,(dp), %rdx, w1) C 1 2
adox( %rax, w2) C 0
adcx( w3, %rdx) C 1
adox( %rbx, %rdx) C 1
adox( %rcx, w1) C 2 cannot carry further
mov %rdx, %rax C 1
adc %rcx, w1 C 2
imul dinv, q C
bt $0, R32(%r13)
adc 16(up), w1 C 2
mov w1, %rbx
setc R8(%r13)
lea 8(up), up
dec un
jnz L(o2)
mov %rax, (up)
mov %rbx, 8(up)
mov %r13, %rax
pop %r13
pop %rbx
')
ifelse(VER,2,`
xor R32(%rax), R32(%rax)
mov (up), %r10
mov 8(up), %r9
L(o2): mulx( (dp), %r12, %r11)
mulx( 8,(dp), %rdx, %rcx)
add %r11, %rdx C 1
adc $0, %rcx C 2
add %r10, %r12 C 0 add just to produce carry
adc %r9, %rdx C 1
mov %rdx, %r10 C 1
mulx( dinv, %rdx, %r12) C next quotient
adc %rax, %rcx C 2
setc R8(%rax) C 3
mov 16(up), %r9 C 2
add %rcx, %r9 C 2
adc $0, R32(%rax) C 3
lea 8(up), up
dec un
jnz L(o2)
mov %r10, (up)
mov %r9, 8(up)
')
ifelse(VER,3,`
xor R32(%rax), R32(%rax)
mov (up), %r10
mov 8(up), %r9
L(o2): mulx( (dp), %r12, %r11)
add %r10, %r12 C 0 add just to produce carry
mulx( 8,(dp), %rdx, %rcx)
adc %r11, %rdx C 1
adc $0, %rcx C 2
add %r9, %rdx C 1
mov %rdx, %r10 C 1
mulx( dinv, %rdx, %r12) C next quotient
adc %rax, %rcx C 2
setc R8(%rax) C 3
mov 16(up), %r9 C 2
add %rcx, %r9 C 2
adc $0, R32(%rax) C 3
lea 8(up), up
dec un
jnz L(o2)
mov %r10, (up)
mov %r9, 8(up)
')
pop %r14
pop %r12
FUNC_EXIT()
ret
ifelse(eval(MAX_SPECIAL>=3),1,`
L(3): push %rbx
push %r12
push %r13
push %r14
mov dp_param, dp C free up rdx
xor %r13, %r13
sub dn_param, un C outer loop count
mov (up), %rax
mov 8(up), %rbx
mov %rax, q
imul dinv, q
L(o3): xor R32(%rcx), R32(%rcx) C clear rcx, CF, and OF
mulx( (dp), w0, w1) C 0 1
adox( %rax, w0) C 0
mulx( 8,(dp), %rax, w3) C 1 2
adcx( w1, %rax) C 1
adox( %rbx, %rax) C 1
mulx( 16,(dp), %rbx, w1) C 2 3
mov dinv, q C 1
mulx( %rax, q, w0)
adcx( w3, %rbx) C 2
adox( 16,(up), %rbx) C 2
adox( %rcx, w1) C 3
adc $0, w1 C 3
bt $0, R32(%r13)
adc w1, 24(up)
setc R8(%r13)
lea 8(up), up
dec un
jnz L(o3)
jmp L(esma)
')
ifelse(eval(MAX_SPECIAL>=4),1,`
L(4): push %rbx
push %r12
push %r13
push %r14
mov dp_param, dp C free up rdx
xor %r13, %r13
sub dn_param, un C outer loop count
mov (up), %rax
mov 8(up), %rbx
mov %rax, q
imul dinv, q
L(o4): xor R32(%rcx), R32(%rcx)
mulx( (dp), w2, w3)
adox( %rax, w2)
mulx( 8,(dp), %rax, w1)
adcx( w3, %rax)
adox( %rbx, %rax)
mulx( 16,(dp), %rbx, w3)
adcx( w1, %rbx)
mulx( 24,(dp), w0, w1)
mov dinv, q
mulx( %rax, q, w2)
adox( 16,(up), %rbx)
adcx( w3, w0)
adox( 24,(up), w0)
adox( %rcx, w1)
mov w0, 24(up)
adc %rcx, w1
bt $0, R32(%r13)
adc w1, 32(up)
setc R8(%r13)
lea 8(up), up
dec un
jnz L(o4)
jmp L(esma)
')
ifelse(eval(MAX_SPECIAL>=5),1,`
L(5): push %rbx
push %r12
push %r13
push %r14
mov dp_param, dp C free up rdx
xor %r13, %r13
sub dn_param, un C outer loop count
mov (up), %rax
mov 8(up), %rbx
mov %rax, q
imul dinv, q
L(o5): xor R32(%rcx), R32(%rcx)
mulx( (dp), w0, w1)
adox( %rax, w0)
mulx( 8,(dp), %rax, w3)
adcx( w1, %rax)
adox( %rbx, %rax)
mulx( 16,(dp), %rbx, w1)
adcx( w3, %rbx)
adox( 16,(up), %rbx)
mulx( 24,(dp), w2, w3)
adcx( w1, w2)
mulx( 32,(dp), w0, w1)
adox( 24,(up), w2)
adcx( w3, w0)
mov dinv, q
mulx( %rax, q, w3)
mov w2, 24(up)
adox( 32,(up), w0)
adox( %rcx, w1)
mov w0, 32(up)
adc %rcx, w1
bt $0, R32(%r13)
adc w1, 40(up)
setc R8(%r13)
lea 8(up), up
dec un
jnz L(o5)
jmp L(esma)
')
ifelse(eval(MAX_SPECIAL>=6),1,`
L(6): push %rbx
push %r12
push %r13
push %r14
mov dp_param, dp C free up rdx
xor %r13, %r13
sub dn_param, un C outer loop count
mov (up), %rax
mov 8(up), %rbx
mov %rax, q
imul dinv, q
L(o6): xor R32(%rcx), R32(%rcx)
mulx( (dp), w2, w3)
adox( %rax, w2)
mulx( 8,(dp), %rax, w1)
adcx( w3, %rax)
adox( %rbx, %rax)
mulx( 16,(dp), %rbx, w3)
adcx( w1, %rbx)
mulx( 24,(dp), w0, w1)
adox( 16,(up), %rbx)
adcx( w3, w0)
adox( 24,(up), w0)
mulx( 32,(dp), w2, w3)
mov w0, 24(up)
adcx( w1, w2)
mulx( 40,(dp), w0, w1)
adox( 32,(up), w2)
adcx( w3, w0)
mov dinv, q
mulx( %rax, q, w3)
mov w2, 32(up)
adox( 40,(up), w0)
adox( %rcx, w1)
mov w0, 40(up)
adc %rcx, w1
bt $0, R32(%r13)
adc w1, 48(up)
setc R8(%r13)
lea 8(up), up
dec un
jnz L(o6)
jmp L(esma)
')
ifelse(eval(MAX_SPECIAL>=7),1,`
L(7): push %rbx
push %r12
push %r13
push %r14
mov dp_param, dp
xor %r13, %r13
sub dn_param, un
mov (up), %rax
mov 8(up), %rbx
mov %rax, q
imul dinv, q
L(o7): xor R32(%rcx), R32(%rcx)
mulx( (dp), w0, w1)
adox( %rax, w0)
mulx( 8,(dp), %rax, w3)
adcx( w1, %rax)
adox( %rbx, %rax)
mulx( 16,(dp), %rbx, w1)
adcx( w3, %rbx)
mulx( 24,(dp), w2, w3)
adcx( w1, w2)
adox( 16,(up), %rbx)
mulx( 32,(dp), w0, w1)
adox( 24,(up), w2)
adcx( w3, w0)
mov w2, 24(up)
adox( 32,(up), w0)
mulx( 40,(dp), w2, w3)
mov w0, 32(up)
adcx( w1, w2)
mulx( 48,(dp), w0, w1)
adox( 40,(up), w2)
adcx( w3, w0)
mov w2, 40(up)
mov %rax, q
mulx( dinv, q, w2)
adox( 48,(up), w0)
adox( %rcx, w1)
mov w0, 48(up)
adc %rcx, w1
bt $0, R32(%r13)
adc w1, 56(up)
setc R8(%r13)
lea 8(up), up
dec un
jnz L(o7)
jmp L(esma)
')
ifelse(eval(MAX_SPECIAL>=8),1,`
L(8): push %rbx
push %r12
push %r13
push %r14
mov dp_param, dp
xor %r13, %r13
sub dn_param, un
mov (up), %rax
mov 8(up), %rbx
mov %rax, q
imul dinv, q
L(o8): xor R32(%rcx), R32(%rcx)
mulx( (dp), w2, w3)
adox( %rax, w2)
mulx( 8,(dp), %rax, w1)
adcx( w3, %rax)
adox( %rbx, %rax)
mulx( 16,(dp), %rbx, w3)
adcx( w1, %rbx)
mulx( 24,(dp), w0, w1)
adox( 16,(up), %rbx)
adcx( w3, w0)
mulx( 32,(dp), w2, w3)
adcx( w1, w2)
adox( 24,(up), w0)
mov w0, 24(up)
mulx( 40,(dp), w0, w1)
adox( 32,(up), w2)
adcx( w3, w0)
mov w2, 32(up)
adox( 40,(up), w0)
mulx( 48,(dp), w2, w3)
mov w0, 40(up)
adcx( w1, w2)
mulx( 56,(dp), w0, w1)
adox( 48,(up), w2)
adcx( w3, w0)
mov dinv, q
mulx( %rax, q, w3)
mov w2, 48(up)
adox( 56,(up), w0)
adox( %rcx, w1)
mov w0, 56(up)
adc %rcx, w1
bt $0, R32(%r13)
adc w1, 64(up)
setc R8(%r13)
lea 8(up), up
dec un
jnz L(o8)
jmp L(esma)
')
L(esma):mov %rax, (up)
mov %rbx, 8(up)
mov %r13, %rax
pop %r14
pop %r13
pop %r12
pop %rbx
FUNC_EXIT()
ret
JUMPTABSECT
ALIGN(8)
L(atab):JMPENT( L(f0), L(atab))
JMPENT( L(f1), L(atab))
JMPENT( L(f2), L(atab))
JMPENT( L(f3), L(atab))
JMPENT( L(f4), L(atab))
JMPENT( L(f5), L(atab))
JMPENT( L(f6), L(atab))
JMPENT( L(f7), L(atab))
JMPENT( L(1), L(atab))
JMPENT( L(2), L(atab))
JMPENT( L(3), L(atab))
JMPENT( L(4), L(atab))
JMPENT( L(5), L(atab))
JMPENT( L(6), L(atab))
JMPENT( L(7), L(atab))
JMPENT( L(8), L(atab))
TEXT
EPILOGUE()
|