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
|
dnl AMD64 mpn_sqr_basecase optimised for AMD Zen.
dnl Copyright 2012, 2013, 2017 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 TODO
C * Do overlapped software pipelining. This should close the remaining gap to
C mul_basecase.
C
C * Update un just once in the outer loop.
C
C * Perhaps keep un and n pre-multiplied by 8, thus suppressing ",8" from
C loads and stores. At least in some cases, the non-scaled form is faster.
C
C * Optimise xit3 code, e.g., using shrx and sarx like in the main loop.
C
C * The mul_1 feed-in code has gotten little attention and could probably be
C improved. Perhaps even expand it to 4 separate loops to allow straight
C fall-through into the 4 addmul_1 loops.
C
C * Clean up ad-hoc scratch register usage in the addmul_1 feed-in code blocks.
define(`rp', `%rdi')
define(`up', `%rsi')
define(`un_param',`%rdx')
define(`un', `%rbp')
define(`n', `%rcx')
C these are used just for the small op code
define(`w0', `%r8')
define(`w1', `%r9')
define(`w2', `%r10')
define(`w3', `%r11')
ABI_SUPPORT(DOS64)
ABI_SUPPORT(STD64)
ASM_START()
TEXT
ALIGN(16)
PROLOGUE(mpn_sqr_basecase)
FUNC_ENTRY(3)
cmp $2, R32(un_param)
jae L(gt1)
mov (up), %rdx
mulx( %rdx, %rax, %rdx)
mov %rax, (rp)
mov %rdx, 8(rp)
FUNC_EXIT()
ret
L(gt1): jne L(gt2)
mov (up), %rdx
mov 8(up), %rcx
mulx( %rcx, %r9, %r10) C v0 * v1 W 1 2
mulx( %rdx, %rax, %r8) C v0 * v0 W 0 1
mov %rcx, %rdx
mulx( %rdx, %r11, %rdx) C v1 * v1 W 2 3
add %r9, %r9 C W 1
adc %r10, %r10 C W 2
adc $0, %rdx C W 3
add %r9, %r8 C W 1
adc %r11, %r10 C W 2
adc $0, %rdx C W 3
mov %rax, (rp)
mov %r8, 8(rp)
mov %r10, 16(rp)
mov %rdx, 24(rp)
FUNC_EXIT()
ret
L(gt2): cmp $4, R32(un_param)
jae L(gt3)
push %rbx
mov (up), %rdx
mulx( 8,(up), w2, w3)
mulx( 16,(up), w0, w1)
add w3, w0
mov 8(up), %rdx
mulx( 16,(up), %rax, w3)
adc %rax, w1
adc $0, w3
test R32(%rbx), R32(%rbx)
mov (up), %rdx
mulx( %rdx, %rbx, %rcx)
mov %rbx, (rp)
mov 8(up), %rdx
mulx( %rdx, %rax, %rbx)
mov 16(up), %rdx
mulx( %rdx, %rsi, %rdx)
adcx( w2, w2)
adcx( w0, w0)
adcx( w1, w1)
adcx( w3, w3)
adox( w2, %rcx)
adox( w0, %rax)
adox( w1, %rbx)
adox( w3, %rsi)
mov $0, R32(%r8)
adox( %r8, %rdx)
adcx( %r8, %rdx)
mov %rcx, 8(rp)
mov %rax, 16(rp)
mov %rbx, 24(rp)
mov %rsi, 32(rp)
mov %rdx, 40(rp)
pop %rbx
FUNC_EXIT()
ret
L(gt3): push %r15
C push %r14
push %r13
push %r12
push %rbp
push %rbx
mov R32(un_param), R32(un)
mov (up), %rdx C up[0]
mov 8(up), %r9 C up[1]
mulx( %rdx, %rax, %r15) C up[0]^2
mov %rax, (rp)
shl %rdx
lea (up,un,8), up
lea -32(rp,un,8), rp
neg un
lea 4(un), n
and $-4, n
test $1, R8(un)
jnz L(mx0)
L(mx1): test $2, R8(un)
jz L(mb3)
L(mb1): mulx( %r9, %rbx, %rax)
.byte 0xc4,0x62,0xb3,0xf6,0x44,0xee,0x10 C mulx 16(up,un,8), %r9, %r8
.byte 0xc4,0x62,0xa3,0xf6,0x54,0xee,0x18 C mulx 24(up,un,8), %r11, %r10
add %r15, %rbx
jmp L(mlo1)
L(mb3): mulx( %r9, %r11, %r10)
.byte 0xc4,0x62,0x93,0xf6,0x64,0xee,0x10 C mulx 16(up,un,8), %r13, %r12
.byte 0xc4,0xe2,0xe3,0xf6,0x44,0xee,0x18 C mulx 24(up,un,8), %rbx, %rax
add %r15, %r11
jrcxz L(n4)
jmp L(mlo3)
L(n4): mov %r11, 8(rp)
adc %r10, %r13
adc %r12, %rbx
jmp L(m)
L(mx0): test $2, R8(un)
jnz L(mb0)
L(mb2): mulx( %r9, %r13, %r12)
.byte 0xc4,0xe2,0xe3,0xf6,0x44,0xee,0x10 C mulx 16(up,un,8), %rbx, %rax
.byte 0xc4,0x62,0xb3,0xf6,0x44,0xee,0x18 C mulx 24(up,un,8), %r9, %r8
add %r15, %r13
jmp L(mlo2)
L(mb0): mulx( %r9, %r9, %r8)
.byte 0xc4,0x62,0xa3,0xf6,0x54,0xee,0x10 C mulx 16(up,un,8), %r11, %r10
.byte 0xc4,0x62,0x93,0xf6,0x64,0xee,0x18 C mulx 24(up,un,8), %r13, %r12
add %r15, %r9
jmp L(mlo0)
ALIGN(16)
L(mtop):jrcxz L(mend)
adc %r8, %r11
mov %r9, (rp,n,8)
L(mlo3):.byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
adc %r10, %r13
mov %r11, 8(rp,n,8)
L(mlo2):.byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(up,n,8), %r11, %r10
adc %r12, %rbx
mov %r13, 16(rp,n,8)
L(mlo1):.byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(up,n,8), %r13, %r12
adc %rax, %r9
mov %rbx, 24(rp,n,8)
L(mlo0):.byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(up,n,8), %rbx, %rax
lea 4(n), n
jmp L(mtop)
L(mend):mov %r9, (rp)
adc %r8, %r11
mov %r11, 8(rp)
adc %r10, %r13
mov %r13, 16(rp)
adc %r12, %rbx
adc $0, %rax
mov %rbx, 24(rp)
mov %rax, 32(rp)
lea 2(un), un
mov $63, R32(%r15) C keep at 63 for shrx/sarx.
test $1, R8(un)
jz L(x0)
L(x1): test $2, R8(un)
jz L(f3)
jmp L(f1)
L(x0): test $2, R8(un)
jz L(f0)
C jmp L(f2)
L(f2): mov -8(up,un,8), %rdx C up[0]
lea 2(un), n
lea 8(rp), rp
.byte 0xc4,0x62,0x82,0xf7,0x5c,0xee,0xf0 C sarx %r15, -16(up,un,8), %r11
.byte 0xc4,0x62,0x83,0xf7,0x6c,0xee,0xf0 C shrx %r15, -16(up,un,8), %r13
and %rdx, %r11 C "ci" in C code
mulx( %rdx, %rax, %r10) C up[0]^2
lea (%r13,%rdx,2), %rdx C "u0" arg in C code
add %rax, %r11
.byte 0xc4,0x62,0x93,0xf6,0x24,0xee C mulx (up,un,8), %r13, %r12
.byte 0xc4,0xe2,0xe3,0xf6,0x44,0xee,0x08 C mulx 8(up,un,8), %rbx, %rax
adc %r10, %r13
adc %r12, %rbx
adc $0, %rax
jmp L(b2)
ALIGN(16)
L(top2):add %r9, (rp,n,8)
L(b2): .byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
adc %r11, 8(rp,n,8)
.byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(up,n,8), %r11, %r10
adc %r13, 16(rp,n,8)
.byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(up,n,8), %r13, %r12
adc %rbx, 24(rp,n,8)
adc %rax, %r9
.byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(up,n,8), %rbx, %rax
adc %r8, %r11
adc %r10, %r13
adc %r12, %rbx
adc $0, %rax
add $4, n
jnz L(top2)
inc un
add %r9, (rp)
adc %r11, 8(rp)
adc %r13, 16(rp)
adc %rbx, 24(rp)
adc $0, %rax
mov %rax, 32(rp)
L(f1): mov -8(up,un,8), %rdx C up[0]
lea 1(un), n
lea 8(rp), rp
.byte 0xc4,0x62,0x82,0xf7,0x6c,0xee,0xf0 C sarx %r15, -16(up,un,8), %r13
.byte 0xc4,0xe2,0x83,0xf7,0x5c,0xee,0xf0 C shrx %r15, -16(up,un,8), %rbx
and %rdx, %r13 C "ci" in C code
mulx( %rdx, %rax, %r12) C up[0]^2
lea (%rbx,%rdx,2), %rdx C "u0" arg in C code
add %rax, %r13
.byte 0xc4,0xe2,0xe3,0xf6,0x04,0xee C mulx (up,un,8), %rbx, %rax
adc %r12, %rbx
adc $0, %rax
.byte 0xc4,0x62,0xb3,0xf6,0x44,0xee,0x08 C mulx 8(up,un,8), %r9, %r8
jmp L(b1)
ALIGN(16)
L(top1):add %r9, (rp,n,8)
.byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
adc %r11, 8(rp,n,8)
L(b1): .byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(up,n,8), %r11, %r10
adc %r13, 16(rp,n,8)
.byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(up,n,8), %r13, %r12
adc %rbx, 24(rp,n,8)
adc %rax, %r9
.byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(up,n,8), %rbx, %rax
adc %r8, %r11
adc %r10, %r13
adc %r12, %rbx
adc $0, %rax
add $4, n
jnz L(top1)
inc un
add %r9, (rp)
adc %r11, 8(rp)
adc %r13, 16(rp)
adc %rbx, 24(rp)
adc $0, %rax
mov %rax, 32(rp)
L(f0): mov -8(up,un,8), %rdx C up[0]
lea (un), n
lea 8(rp), rp
.byte 0xc4,0xe2,0x82,0xf7,0x5c,0xee,0xf0 C sarx %r15, -16(up,un,8), %rbx
.byte 0xc4,0x62,0x83,0xf7,0x4c,0xee,0xf0 C shrx %r15, -16(up,un,8), %r9
and %rdx, %rbx C "ci" in C code
mulx( %rdx, %r10, %rax) C up[0]^2
lea (%r9,%rdx,2), %rdx C "u0" arg in C code
add %r10, %rbx
adc $0, %rax C "cin" in C code
.byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,un,8), %r9, %r8
.byte 0xc4,0x62,0xa3,0xf6,0x54,0xee,0x08 C mulx 8(up,un,8), %r11, %r10
jmp L(b0)
ALIGN(16)
L(top0):add %r9, (rp,n,8)
.byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
adc %r11, 8(rp,n,8)
.byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(up,n,8), %r11, %r10
adc %r13, 16(rp,n,8)
L(b0): .byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(up,n,8), %r13, %r12
adc %rbx, 24(rp,n,8)
adc %rax, %r9
.byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(up,n,8), %rbx, %rax
adc %r8, %r11
adc %r10, %r13
adc %r12, %rbx
adc $0, %rax
add $4, n
jnz L(top0)
inc un
add %r9, (rp)
adc %r11, 8(rp)
adc %r13, 16(rp)
adc %rbx, 24(rp)
adc $0, %rax
mov %rax, 32(rp)
L(f3): mov -8(up,un,8), %rdx C up[0]
lea 3(un), n
lea 8(rp), rp
.byte 0xc4,0x62,0x82,0xf7,0x4c,0xee,0xf0 C sarx %r15, -16(up,un,8), %r9
.byte 0xc4,0x62,0x83,0xf7,0x5c,0xee,0xf0 C shrx %r15, -16(up,un,8), %r11
and %rdx, %r9 C "ci" in C code
mulx( %rdx, %rax, %r8) C up[0]^2
lea (%r11,%rdx,2), %rdx C "u0" arg in C code
add %rax, %r9
.byte 0xc4,0x62,0xa3,0xf6,0x14,0xee C mulx (%rsi,%rbp,8),%r11,%r10
.byte 0xc4,0x62,0x93,0xf6,0x64,0xee,0x08 C mulx 0x8(%rsi,%rbp,8),%r13,%r12
.byte 0xc4,0xe2,0xe3,0xf6,0x44,0xee,0x10 C mulx 0x10(%rsi,%rbp,8),%rbx,%rax
adc %r8, %r11
adc %r10, %r13
adc %r12, %rbx
adc $0, %rax
jrcxz L(xit3)
jmp L(top3) C FIXME perhaps fall through
ALIGN(16)
L(top3):add %r9, (rp,n,8)
.byte 0xc4,0x62,0xb3,0xf6,0x04,0xce C mulx (up,n,8), %r9, %r8
adc %r11, 8(rp,n,8)
.byte 0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08 C mulx 8(up,n,8), %r11, %r10
adc %r13, 16(rp,n,8)
.byte 0xc4,0x62,0x93,0xf6,0x64,0xce,0x10 C mulx 16(up,n,8), %r13, %r12
adc %rbx, 24(rp,n,8)
adc %rax, %r9
.byte 0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18 C mulx 24(up,n,8), %rbx, %rax
adc %r8, %r11
adc %r10, %r13
adc %r12, %rbx
adc $0, %rax
add $4, n
jnz L(top3)
inc un
add %r9, (rp)
adc %r11, 8(rp)
adc %r13, 16(rp)
adc %rbx, 24(rp)
adc $0, %rax
mov %rax, 32(rp)
jmp L(f2)
L(xit3):add %r9, (rp)
adc %r11, 8(rp)
adc 16(rp), %r13
adc 24(rp), %rbx
L(m): adc $0, %rax
mov %rax, 32(rp)
mov -24(up), %rdx C FIXME: CSE
mov -32(up), %r9 C FIXME: CSE
sar $63, %r9
and %rdx, %r9
add %r13, %r9
mulx( %rdx, %rax, %r10)
mov -16(up), %r8 C FIXME: CSE
adc $0, %r10
add %rax, %r9
adc $0, %r10
mov %r9, 16(rp)
mov -32(up), %rax
shl %rax
adc %rdx, %rdx
mulx( %r8, %r13, %r12)
mulx( -8,(up), %r11, %rax) C FIXME: CSE
add %r10, %r13
adc %r12, %r11
adc $0, %rax
add %rbx, %r13
mov %r13, 24(rp)
adc 32(rp), %r11
adc $0, %rax
mov -16(up), %rdx C FIXME: CSE
mov -8(up), %r8 C FIXME: CSE
mov -24(up), %r9
sar $63, %r9
and %rdx, %r9
add %r11, %r9
mulx( %rdx, %rbp, %r10)
adc $0, %r10
add %rbp, %r9
adc $0, %r10
mov %r9, 32(rp)
mov -24(up), %rbp
shl %rbp
adc %rdx, %rdx
mulx( %r8, %rbx, %rbp)
add %r10, %rbx
adc $0, %rbp
adc %rbx, %rax
mov %rax, 40(rp)
adc $0, %rbp
mov -8(up), %rdx C FIXME: CSE
mov -16(up), %r9 C FIXME: CSE
sar $63, %r9
and %rdx, %r9
add %rbp, %r9
mulx( %rdx, %rbp, %r10)
adc $0, %r10
add %rbp, %r9
adc $0, %r10
mov %r9, 48(rp)
mov %r10, 56(rp)
pop %rbx
pop %rbp
pop %r12
pop %r13
C pop %r14
pop %r15
FUNC_EXIT()
ret
EPILOGUE()
|