aboutsummaryrefslogtreecommitdiff
path: root/vendor/gmp-6.3.0/mpn/x86_64/k8/mulmid_basecase.asm
blob: 86f1414ed850e92eff66aa30d2dfeeb67f3e221c (plain) (blame)
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
dnl  AMD64 mpn_mulmid_basecase

dnl  Contributed by David Harvey.

dnl  Copyright 2011, 2012 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
C K8,K9:	 2.375  (2.5 when un - vn is "small")
C K10:		 ?
C P4:		 ?
C P6-15:	 ?

C INPUT PARAMETERS
define(`rp',      `%rdi')
define(`up',      `%rsi')
define(`un_param',`%rdx')
define(`vp_param',`%rcx')
define(`vn',      `%r8')

define(`v0', `%r12')
define(`v1', `%r9')

define(`w0', `%rbx')
define(`w1', `%rcx')
define(`w2', `%rbp')
define(`w3', `%r10')

define(`n',  `%r11')
define(`outer_addr', `%r14')
define(`un',  `%r13')
define(`vp',  `%r15')

define(`vp_inner', `%r10')

ABI_SUPPORT(DOS64)
ABI_SUPPORT(STD64)

ASM_START()
	TEXT
	ALIGN(16)
PROLOGUE(mpn_mulmid_basecase)
	FUNC_ENTRY(4)
IFDOS(`	mov	56(%rsp), %r8d	')
	push	%rbx
	push	%rbp
	push	%r12
	push	%r13
	push	%r14
	push	%r15

	mov	vp_param, vp

	C use un for row length (= un_param - vn + 1)
	lea	1(un_param), un
	sub	vn, un

	lea	(rp,un,8), rp

	cmp	$4, un		C TODO: needs tuning
	jc	L(diagonal)

	lea	(up,un_param,8), up

	test	$1, vn
	jz	L(mul_2)

C ===========================================================
C     mul_1 for vp[0] if vn is odd

L(mul_1):
	mov	R32(un), R32(w0)

	neg	un
	mov	(up,un,8), %rax
	mov	(vp), v0
	mul	v0

	and	$-4, un		C round down to multiple of 4
	mov	un, n

	and	$3, R32(w0)
	jz	L(mul_1_prologue_0)
	cmp	$2, R32(w0)
	jc	L(mul_1_prologue_1)
	jz	L(mul_1_prologue_2)

L(mul_1_prologue_3):
	mov	%rax, w3
	mov	%rdx, w0
	lea	L(addmul_prologue_3)(%rip), outer_addr
	jmp	L(mul_1_entry_3)

	ALIGN(16)
L(mul_1_prologue_0):
	mov	%rax, w2
	mov	%rdx, w3		C note already w0 == 0
	lea	L(addmul_prologue_0)(%rip), outer_addr
	jmp	L(mul_1_entry_0)

	ALIGN(16)
L(mul_1_prologue_1):
	add	$4, n
	mov	%rax, w1
	mov	%rdx, w2
	mov	$0, R32(w3)
	mov	(up,n,8), %rax
	lea	L(addmul_prologue_1)(%rip), outer_addr
	jmp	L(mul_1_entry_1)

	ALIGN(16)
L(mul_1_prologue_2):
	mov	%rax, w0
	mov	%rdx, w1
	mov	24(up,n,8), %rax
	mov	$0, R32(w2)
	mov	$0, R32(w3)
	lea	L(addmul_prologue_2)(%rip), outer_addr
	jmp	L(mul_1_entry_2)


	C this loop is 10 c/loop = 2.5 c/l on K8

	ALIGN(16)
L(mul_1_top):
	mov	w0, -16(rp,n,8)
	add	%rax, w1
	mov	(up,n,8), %rax
	adc	%rdx, w2
L(mul_1_entry_1):
	mov	$0, R32(w0)
	mul	v0
	mov	w1, -8(rp,n,8)
	add	%rax, w2
	adc	%rdx, w3
L(mul_1_entry_0):
	mov	8(up,n,8), %rax
	mul	v0
	mov	w2, (rp,n,8)
	add	%rax, w3
	adc	%rdx, w0
L(mul_1_entry_3):
	mov	16(up,n,8), %rax
	mul	v0
	mov	w3, 8(rp,n,8)
	mov	$0, R32(w2)		C zero
	mov	w2, w3			C zero
	add	%rax, w0
	mov	24(up,n,8), %rax
	mov	w2, w1			C zero
	adc	%rdx, w1
L(mul_1_entry_2):
	mul	v0
	add	$4, n
	js	L(mul_1_top)

	mov	w0, -16(rp)
	add	%rax, w1
	mov	w1, -8(rp)
	mov	w2, 8(rp)		C zero last limb of output
	adc	%rdx, w2
	mov	w2, (rp)

	dec	vn
	jz	L(ret)

	lea	-8(up), up
	lea	8(vp), vp

	mov	un, n
	mov	(vp), v0
	mov	8(vp), v1

	jmp	*outer_addr

C ===========================================================
C     mul_2 for vp[0], vp[1] if vn is even

	ALIGN(16)
L(mul_2):
	mov	R32(un), R32(w0)

	neg	un
	mov	-8(up,un,8), %rax
	mov	(vp), v0
	mov	8(vp), v1
	mul	v1

	and	$-4, un		C round down to multiple of 4
	mov	un, n

	and	$3, R32(w0)
	jz	L(mul_2_prologue_0)
	cmp	$2, R32(w0)
	jc	L(mul_2_prologue_1)
	jz	L(mul_2_prologue_2)

L(mul_2_prologue_3):
	mov	%rax, w1
	mov	%rdx, w2
	lea	L(addmul_prologue_3)(%rip), outer_addr
	jmp	L(mul_2_entry_3)

	ALIGN(16)
L(mul_2_prologue_0):
	mov	%rax, w0
	mov	%rdx, w1
	lea	L(addmul_prologue_0)(%rip), outer_addr
	jmp	L(mul_2_entry_0)

	ALIGN(16)
L(mul_2_prologue_1):
	mov	%rax, w3
	mov	%rdx, w0
	mov	$0, R32(w1)
	lea	L(addmul_prologue_1)(%rip), outer_addr
	jmp	L(mul_2_entry_1)

	ALIGN(16)
L(mul_2_prologue_2):
	mov	%rax, w2
	mov	%rdx, w3
	mov	$0, R32(w0)
	mov	16(up,n,8), %rax
	lea	L(addmul_prologue_2)(%rip), outer_addr
	jmp	L(mul_2_entry_2)


	C this loop is 18 c/loop = 2.25 c/l on K8

	ALIGN(16)
L(mul_2_top):
	mov     -8(up,n,8), %rax
	mul     v1
	add     %rax, w0
	adc     %rdx, w1
L(mul_2_entry_0):
	mov     $0, R32(w2)
	mov     (up,n,8), %rax
	mul     v0
	add     %rax, w0
	mov     (up,n,8), %rax
	adc     %rdx, w1
	adc     $0, R32(w2)
	mul     v1
	add     %rax, w1
	mov     w0, (rp,n,8)
	adc     %rdx, w2
L(mul_2_entry_3):
	mov     8(up,n,8), %rax
	mul     v0
	mov     $0, R32(w3)
	add     %rax, w1
	adc     %rdx, w2
	mov     $0, R32(w0)
	adc     $0, R32(w3)
	mov     8(up,n,8), %rax
	mov     w1, 8(rp,n,8)
	mul     v1
	add     %rax, w2
	mov     16(up,n,8), %rax
	adc     %rdx, w3
L(mul_2_entry_2):
	mov     $0, R32(w1)
	mul     v0
	add     %rax, w2
	mov     16(up,n,8), %rax
	adc     %rdx, w3
	adc     $0, R32(w0)
	mul     v1
	add     %rax, w3
	mov     w2, 16(rp,n,8)
	adc     %rdx, w0
L(mul_2_entry_1):
	mov     24(up,n,8), %rax
	mul     v0
	add     %rax, w3
	adc     %rdx, w0
	adc     $0, R32(w1)
	add     $4, n
	mov     w3, -8(rp,n,8)
	jnz     L(mul_2_top)

	mov	w0, (rp)
	mov	w1, 8(rp)

	sub	$2, vn
	jz	L(ret)

	lea	16(vp), vp
	lea	-16(up), up

	mov	un, n
	mov	(vp), v0
	mov	8(vp), v1

	jmp	*outer_addr

C ===========================================================
C     addmul_2 for remaining vp's

	ALIGN(16)
L(addmul_prologue_0):
	mov	-8(up,n,8), %rax
	mul	v1
	mov	%rax, w1
	mov	%rdx, w2
	mov	$0, R32(w3)
	jmp	L(addmul_entry_0)

	ALIGN(16)
L(addmul_prologue_1):
	mov	16(up,n,8), %rax
	mul	v1
	mov	%rax, w0
	mov	%rdx, w1
	mov	$0, R32(w2)
	mov	24(up,n,8), %rax
	jmp	L(addmul_entry_1)

	ALIGN(16)
L(addmul_prologue_2):
	mov	8(up,n,8), %rax
	mul	v1
	mov	%rax, w3
	mov	%rdx, w0
	mov	$0, R32(w1)
	jmp	L(addmul_entry_2)

	ALIGN(16)
L(addmul_prologue_3):
	mov	(up,n,8), %rax
	mul	v1
	mov	%rax, w2
	mov	%rdx, w3
	mov	$0, R32(w0)
	mov	$0, R32(w1)
	jmp	L(addmul_entry_3)

	C this loop is 19 c/loop = 2.375 c/l on K8

	ALIGN(16)
L(addmul_top):
	mov	$0, R32(w3)
	add	%rax, w0
	mov	-8(up,n,8), %rax
	adc	%rdx, w1
	adc	$0, R32(w2)
	mul	v1
	add	w0, -8(rp,n,8)
	adc	%rax, w1
	adc	%rdx, w2
L(addmul_entry_0):
	mov	(up,n,8), %rax
	mul	v0
	add	%rax, w1
	mov	(up,n,8), %rax
	adc	%rdx, w2
	adc	$0, R32(w3)
	mul	v1
	add	w1, (rp,n,8)
	mov	$0, R32(w1)
	adc	%rax, w2
	mov	$0, R32(w0)
	adc	%rdx, w3
L(addmul_entry_3):
	mov	8(up,n,8), %rax
	mul	v0
	add	%rax, w2
	mov	8(up,n,8), %rax
	adc	%rdx, w3
	adc	$0, R32(w0)
	mul	v1
	add	w2, 8(rp,n,8)
	adc	%rax, w3
	adc	%rdx, w0
L(addmul_entry_2):
	mov	16(up,n,8), %rax
	mul	v0
	add	%rax, w3
	mov	16(up,n,8), %rax
	adc	%rdx, w0
	adc	$0, R32(w1)
	mul	v1
	add	w3, 16(rp,n,8)
	nop			C don't ask...
	adc	%rax, w0
	mov	$0, R32(w2)
	mov	24(up,n,8), %rax
	adc	%rdx, w1
L(addmul_entry_1):
	mul	v0
	add	$4, n
	jnz	L(addmul_top)

	add	%rax, w0
	adc	%rdx, w1
	adc	$0, R32(w2)

	add	w0, -8(rp)
	adc	w1, (rp)
	adc	w2, 8(rp)

	sub	$2, vn
	jz	L(ret)

	lea	16(vp), vp
	lea	-16(up), up

	mov	un, n
	mov	(vp), v0
	mov	8(vp), v1

	jmp	*outer_addr

C ===========================================================
C     accumulate along diagonals if un - vn is small

	ALIGN(16)
L(diagonal):
	xor	R32(w0), R32(w0)
	xor	R32(w1), R32(w1)
	xor	R32(w2), R32(w2)

	neg	un

	mov	R32(vn), %eax
	and	$3, %eax
	jz	L(diag_prologue_0)
	cmp	$2, %eax
	jc	L(diag_prologue_1)
	jz	L(diag_prologue_2)

L(diag_prologue_3):
	lea	-8(vp), vp
	mov	vp, vp_inner
	add	$1, vn
	mov	vn, n
	lea	L(diag_entry_3)(%rip), outer_addr
	jmp	L(diag_entry_3)

L(diag_prologue_0):
	mov	vp, vp_inner
	mov	vn, n
	lea	0(%rip), outer_addr
	mov     -8(up,n,8), %rax
	jmp	L(diag_entry_0)

L(diag_prologue_1):
	lea	8(vp), vp
	mov	vp, vp_inner
	add	$3, vn
	mov	vn, n
	lea	0(%rip), outer_addr
	mov     -8(vp_inner), %rax
	jmp	L(diag_entry_1)

L(diag_prologue_2):
	lea	-16(vp), vp
	mov	vp, vp_inner
	add	$2, vn
	mov	vn, n
	lea	0(%rip), outer_addr
	mov	16(vp_inner), %rax
	jmp	L(diag_entry_2)


	C this loop is 10 c/loop = 2.5 c/l on K8

	ALIGN(16)
L(diag_top):
	add     %rax, w0
	adc     %rdx, w1
	mov     -8(up,n,8), %rax
	adc     $0, w2
L(diag_entry_0):
	mulq    (vp_inner)
	add     %rax, w0
	adc     %rdx, w1
	adc     $0, w2
L(diag_entry_3):
	mov     -16(up,n,8), %rax
	mulq    8(vp_inner)
	add     %rax, w0
	mov     16(vp_inner), %rax
	adc     %rdx, w1
	adc     $0, w2
L(diag_entry_2):
	mulq    -24(up,n,8)
	add     %rax, w0
	mov     24(vp_inner), %rax
	adc     %rdx, w1
	lea     32(vp_inner), vp_inner
	adc     $0, w2
L(diag_entry_1):
	mulq    -32(up,n,8)
	sub     $4, n
	jnz	L(diag_top)

	add	%rax, w0
	adc	%rdx, w1
	adc	$0, w2

	mov	w0, (rp,un,8)

	inc	un
	jz	L(diag_end)

	mov	vn, n
	mov	vp, vp_inner

	lea	8(up), up
	mov	w1, w0
	mov	w2, w1
	xor	R32(w2), R32(w2)

	jmp	*outer_addr

L(diag_end):
	mov	w1, (rp)
	mov	w2, 8(rp)

L(ret):	pop	%r15
	pop	%r14
	pop	%r13
	pop	%r12
	pop	%rbp
	pop	%rbx
	FUNC_EXIT()
	ret
EPILOGUE()