aboutsummaryrefslogtreecommitdiff
path: root/vendor/gmp-6.3.0/mpn/x86_64/zen/sbpi1_bdiv_r.asm
blob: 0c24de5c3e224ced4a8e52e593a0de0b9abefab3 (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
dnl  AMD64 mpn_sbpi1_bdiv_r optimised for AMD Zen

dnl  Copyright 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')


define(`up',       `%rdi')
define(`un_param', `%rsi')
define(`dp_param', `%rdx')
define(`dn_param', `%rcx')
define(`dinv',     `%r8')

define(`i',        `%rcx')
define(`dn',       `%r14')

define(`dp',       `%rsi')
define(`un',       `%r15')

C TODO
C  * The o1...o8  loops for special dn counts were naively hand-optimised by
C    folding the generic loops.  They can probably be tuned.  The speculative
C    quotient limb generation might not be in the optimal spot.
C  * Perhaps avoid late-in-loop jumps, e.g., lo0.
C  * Improve regalloc wrt dn_param/dn and un_param/un to save some moves.

C ABI_SUPPORT(DOS64)
C ABI_SUPPORT(STD64)

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

	sub	dn_param, un_param		C outer loop count
	mov	dn_param, dn		C FIXME: Suppress by reg re-alloc
	push	dinv				C keep dinv on stack
	mov	un_param, un		C FIXME: Suppress by reg re-alloc
	xor	R32(%rbp), R32(%rbp)

	lea	(dp_param,dn_param,8), dp

	mov	(up), %rdx
	imul	dinv, %rdx			C first quotient limb

	neg	dn
	lea	-32(up,dn_param,8), up

	test	$1, R8(dn_param)
	jnz	L(cx1)

L(cx0):	test	$2, R8(dn_param)
	jnz	L(b2)


C =============================================================================
L(b0):	cmp	$-4, dn
	jnz	L(gt4)

L(o4):	mulx(	-32,(dp), %r9, %r14)
	mulx(	-24,(dp), %r11, %r10)
	mulx(	-16,(dp), %r13, %r12)
	mulx(	-8,(dp), %rbx, %rax)
	add	%r14, %r11
	adc	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	add	(up), %r9
	adc	8(up), %r11
	mov	%r8, %rdx			C dinv
	mov	%r11, 8(up)
	mulx(	%r11, %rdx, %r12)		C next quotient
	adc	%r13, 16(up)
	adc	%rbx, 24(up)
	adc	%rbp, %rax
	setc	R8(%rbp)
	add	%rax, 32(up)
	adc	$0, R32(%rbp)
	lea	8(up), up
	dec	un
	jne	L(o4)
	jmp	L(ret)

L(gt4):	cmp	$-8, dn
	jnz	L(out0)

L(o8):	mulx(	-64,(dp), %r9, %r14)
	mulx(	-56,(dp), %rcx, %r10)
	mulx(	-48,(dp), %r13, %r12)
	mulx(	-40,(dp), %rbx, %rax)
	add	%r14, %rcx
	adc	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	add	-32(up), %r9
	mulx(	-32,(dp), %r9, %r14)
	adc	-24(up), %rcx
	mov	%rcx, -24(up)
	mulx(	-24,(dp), %r11, %r10)
	adc	%r13, -16(up)
	mulx(	-16,(dp), %r13, %r12)
	adc	%rbx, -8(up)
	adc	%rax, %r9
	mulx(	-8,(dp), %rbx, %rax)
	adc	%r14, %r11
	adc	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	mov	%r8, %rdx			C dinv
	mulx(	%rcx, %rdx, %r12)		C next quotient
	add	%r9, (up)
	adc	%r11, 8(up)
	adc	%r13, 16(up)
	adc	%rbx, 24(up)
	adc	%rbp, %rax
	setc	R8(%rbp)
	add	%rax, 32(up)
	adc	$0, R32(%rbp)
	lea	8(up), up
	dec	un
	jne	L(o8)
	jmp	L(ret)

L(out0):mov	dn, i
	.byte	0xc4,0x22,0xb3,0xf6,0x04,0xf6		C mulx (dp,dn,8),%r9,%r8
	.byte	0xc4,0x22,0xa3,0xf6,0x54,0xf6,0x08	C mulx 8(dp,dn,8),%r11,%r10
	.byte	0xc4,0x22,0x93,0xf6,0x64,0xf6,0x10	C mulx 16(dp,dn,8),%r13,%r12
	clc
	jmp	L(lo0)

	ALIGN(16)
L(top0):add	%r9, (up,i,8)
	.byte	0xc4,0x62,0xb3,0xf6,0x04,0xce		C mulx (dp,i,8), %r9, %r8
	adc	%r11, 8(up,i,8)
	.byte	0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08	C mulx 8(dp,i,8), %r11, %r10
	adc	%r13, 16(up,i,8)
	.byte	0xc4,0x62,0x93,0xf6,0x64,0xce,0x10	C mulx 16(dp,i,8), %r13, %r12
	adc	%rbx, 24(up,i,8)
	adc	%rax, %r9
L(lo0):	.byte	0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18	C mulx 24(dp,i,8), %rbx, %rax
	adc	%r8, %r11
	adc	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	add	$4, i
	js	L(top0)

	mov	(%rsp), %rdx			C dinv
	.byte	0xc4,0x22,0xeb,0xf6,0x64,0xf7,0x28	C mulx 40(%rdi,%r14,8),%rdx,%r12
	add	%r9, (up)
	adc	%r11, 8(up)
	adc	%r13, 16(up)
	adc	%rbx, 24(up)
	adc	%rbp, %rax
	setc	R8(%rbp)
	add	%rax, 32(up)
	adc	$0, R32(%rbp)
	lea	8(up), up
	dec	un
	jne	L(out0)
	jmp	L(ret)

L(cx1):	test	$2, R8(dn_param)
	jnz	L(b3)

C =============================================================================
L(b1):	cmp	$-1, dn
	jnz	L(gt1)

	mov	24(up), %r9
L(o1):	mulx(	-8,(dp), %rbx, %rdx)
	add	%r9, %rbx
	adc	%rbp, %rdx
	add	32(up), %rdx
	setc	R8(%rbp)
	mov	%rdx, %r9
	mulx(	%r8, %rdx, %r12)		C next quotient
	lea	8(up), up
	dec	un
	jne	L(o1)
	mov	%r9, 24(up)
	jmp	L(ret)

L(gt1):	cmp	$-5, dn
	jnz	L(out1)

L(o5):	mulx(	-40,(dp), %rbx, %rax)
	mulx(	-32,(dp), %r9, %r14)
	mulx(	-24,(dp), %r11, %r10)
	mulx(	-16,(dp), %r13, %r12)
	add	-8(up), %rbx
	adc	%rax, %r9
	mulx(	-8,(dp), %rbx, %rax)
	adc	%r14, %r11
	adc	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	add	(up), %r9
	mov	%r9, (up)
	mov	%r8, %rdx			C dinv
	mulx(	%r9, %rdx, %r12)		C next quotient
	adc	%r11, 8(up)
	adc	%r13, 16(up)
	adc	%rbx, 24(up)
	adc	%rbp, %rax
	setc	R8(%rbp)
	add	%rax, 32(up)
	adc	$0, R32(%rbp)
	lea	8(up), up
	dec	un
	jne	L(o5)
	jmp	L(ret)

L(out1):lea	1(dn), i
	.byte	0xc4,0xa2,0xe3,0xf6,0x04,0xf6		C mulx (dp,dn,8),%rbx,%rax
	.byte	0xc4,0x22,0xb3,0xf6,0x44,0xf6,0x08	C mulx 8(dp,dn,8),%r9,%r8
	.byte	0xc4,0x22,0xa3,0xf6,0x54,0xf6,0x10	C mulx 16(dp,dn,8),%r11,%r10
	clc
	jmp	L(lo1)

	ALIGN(16)
L(top1):add	%r9, (up,i,8)
	.byte	0xc4,0x62,0xb3,0xf6,0x04,0xce		C mulx (dp,i,8), %r9, %r8
	adc	%r11, 8(up,i,8)
	.byte	0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08	C mulx 8(dp,i,8), %r11, %r10
	adc	%r13, 16(up,i,8)
L(lo1):	.byte	0xc4,0x62,0x93,0xf6,0x64,0xce,0x10	C mulx 16(dp,i,8), %r13, %r12
	adc	%rbx, 24(up,i,8)
	adc	%rax, %r9
	.byte	0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18	C mulx 24(dp,i,8), %rbx, %rax
	adc	%r8, %r11
	adc	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	add	$4, i
	js	L(top1)

	mov	(%rsp), %rdx			C dinv
	.byte	0xc4,0x22,0xeb,0xf6,0x64,0xf7,0x28	C mulx 40(up,dn,8), %rdx, %r12
	add	%r9, (up)
	adc	%r11, 8(up)
	adc	%r13, 16(up)
	adc	%rbx, 24(up)
	adc	%rbp, %rax
	setc	R8(%rbp)
	add	%rax, 32(up)
	adc	$0, R32(%rbp)
	lea	8(up), up
	dec	un
	jne	L(out1)
	jmp	L(ret)

C =============================================================================
L(b2):	cmp	$-2, dn
	jnz	L(gt2)

	mov	16(up), %r10
	mov	24(up), %r9
L(o2):	mulx(	-16,(dp), %r13, %r12)
	mulx(	-8,(dp), %rbx, %rax)
	add	%r12, %rbx
	adc	$0, %rax
	add	%r10, %r13		C 0  add just to produce carry
	mov	%r9, %r10		C 1
	adc	%rbx, %r10		C 1
	mov	%r8, %rdx
	mulx(	%r10, %rdx, %r12)	C next quotient
	adc	%rbp, %rax		C 2
	setc	R8(%rbp)		C 3
	mov	32(up), %r9		C 2
	add	%rax, %r9		C 2
	adc	$0, R32(%rbp)		C 3
	lea	8(up), up
	dec	un
	jne	L(o2)
	mov	%r10, 16(up)
	mov	%r9, 24(up)
	jmp	L(ret)

L(gt2):	cmp	$-6, dn
	jnz	L(out2)

L(o6):	mulx(	-48,(dp), %r13, %r12)
	mulx(	-40,(dp), %rcx, %rax)
	add	%r12, %rcx
	adc	$0, %rax
	mulx(	-32,(dp), %r9, %r14)
	mulx(	-24,(dp), %r11, %r10)
	add	-16(up), %r13
	mulx(	-16,(dp), %r13, %r12)
	adc	-8(up), %rcx
	mov	%rcx, -8(up)
	adc	%rax, %r9
	mulx(	-8,(dp), %rbx, %rax)
	adc	%r14, %r11
	adc	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	mov	%r8, %rdx			C dinv
	mulx(	%rcx, %rdx, %r12)		C next quotient
	add	%r9, (up)
	adc	%r11, 8(up)
	adc	%r13, 16(up)
	adc	%rbx, 24(up)
	adc	%rbp, %rax
	setc	R8(%rbp)
	add	%rax, 32(up)
	adc	$0, R32(%rbp)
	lea	8(up), up
	dec	un
	jne	L(o6)
	jmp	L(ret)

L(out2):lea	2(dn), i
	.byte	0xc4,0x22,0x93,0xf6,0x24,0xf6		C mulx (dp,dn,8),%r13,%r12
	.byte	0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x08	C mulx 8(dp,dn,8),%rbx,%rax
	add	%r12, %rbx
	adc	$0, %rax
	.byte	0xc4,0x22,0xb3,0xf6,0x44,0xf6,0x10	C mulx 16(dp,dn,8),%r9,%r8
	jmp	L(lo2)

	ALIGN(16)
L(top2):add	%r9, (up,i,8)
	.byte	0xc4,0x62,0xb3,0xf6,0x04,0xce		C mulx (dp,i,8), %r9, %r8
	adc	%r11, 8(up,i,8)
L(lo2):	.byte	0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08	C mulx 8(dp,i,8), %r11, %r10
	adc	%r13, 16(up,i,8)
	.byte	0xc4,0x62,0x93,0xf6,0x64,0xce,0x10	C mulx 16(dp,i,8), %r13, %r12
	adc	%rbx, 24(up,i,8)
	adc	%rax, %r9
	.byte	0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18	C mulx 24(dp,i,8), %rbx, %rax
	adc	%r8, %r11
	adc	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	add	$4, i
	js	L(top2)

	mov	(%rsp), %rdx			C dinv
	.byte	0xc4,0x22,0xeb,0xf6,0x64,0xf7,0x28	C mulx 40(up,dn,8), %rdx, %r12
	add	%r9, (up)
	adc	%r11, 8(up)
	adc	%r13, 16(up)
	adc	%rbx, 24(up)
	adc	%rbp, %rax
	setc	R8(%rbp)
	add	%rax, 32(up)
	adc	$0, R32(%rbp)
	lea	8(up), up
	dec	un
	jne	L(out2)
	jmp	L(ret)

C =============================================================================
L(b3):	cmp	$-3, dn
	jnz	L(gt3)

	mov	8(up), %r14
	mov	16(up), %r9
	mov	24(up), %rcx
L(o3):	mulx(	-24,(dp), %r11, %r10)
	mulx(	-16,(dp), %r13, %r12)
	mulx(	-8,(dp), %rbx, %rax)
	add	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	add	%r14, %r11
	mov	%r9, %r14
	adc	%r13, %r14
	mov	%rcx, %r9
	mov	%r8, %rdx			C dinv
	mulx(	%r14, %rdx, %r12)		C next quotient
	adc	%rbx, %r9
	adc	%rbp, %rax
	setc	R8(%rbp)
	mov	32(up), %rcx
	add	%rax, %rcx
	adc	$0, R32(%rbp)
	lea	8(up), up
	dec	un
	jne	L(o3)
	mov	%r14, 8(up)
	mov	%r9, 16(up)
	mov	%rcx, 24(up)
	jmp	L(ret)

L(gt3):	cmp	$-7, dn
	jnz	L(out3)

L(o7):	mulx(	-56,(dp), %r11, %r10)
	mulx(	-48,(dp), %rcx, %r12)
	mulx(	-40,(dp), %rbx, %rax)
	add	%r10, %rcx
	adc	%r12, %rbx
	adc	$0, %rax
	mulx(	-32,(dp), %r9, %r14)
	add	-24(up), %r11
	mulx(	-24,(dp), %r11, %r10)
	adc	-16(up), %rcx
	mov	%rcx, -16(up)
	mulx(	-16,(dp), %r13, %r12)
	adc	%rbx, -8(up)
	adc	%rax, %r9
	mulx(	-8,(dp), %rbx, %rax)
	adc	%r14, %r11
	adc	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	mov	%r8, %rdx			C dinv
	mulx(	%rcx, %rdx, %r12)		C next quotient
	add	%r9, (up)
	adc	%r11, 8(up)
	adc	%r13, 16(up)
	adc	%rbx, 24(up)
	adc	%rbp, %rax
	setc	R8(%rbp)
	add	%rax, 32(up)
	adc	$0, R32(%rbp)
	lea	8(up), up
	dec	un
	jne	L(o7)
	jmp	L(ret)

L(out3):lea	3(dn), i
	.byte	0xc4,0x22,0xa3,0xf6,0x14,0xf6		C mulx (dp,dn,8),%r11,%r10
	.byte	0xc4,0x22,0x93,0xf6,0x64,0xf6,0x08	C mulx 8(dp,dn,8),%r13,%r12
	.byte	0xc4,0xa2,0xe3,0xf6,0x44,0xf6,0x10	C mulx 16(dp,dn,8),%rbx,%rax
	add	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	jmp	L(lo3)

	ALIGN(16)
L(top3):add	%r9, (up,i,8)
L(lo3):	.byte	0xc4,0x62,0xb3,0xf6,0x04,0xce		C mulx (dp,i,8), %r9, %r8
	adc	%r11, 8(up,i,8)
	.byte	0xc4,0x62,0xa3,0xf6,0x54,0xce,0x08	C mulx 8(dp,i,8), %r11, %r10
	adc	%r13, 16(up,i,8)
	.byte	0xc4,0x62,0x93,0xf6,0x64,0xce,0x10	C mulx 16(dp,i,8), %r13, %r12
	adc	%rbx, 24(up,i,8)
	adc	%rax, %r9
	.byte	0xc4,0xe2,0xe3,0xf6,0x44,0xce,0x18	C mulx 24(dp,i,8), %rbx, %rax
	adc	%r8, %r11
	adc	%r10, %r13
	adc	%r12, %rbx
	adc	$0, %rax
	add	$4, i
	js	L(top3)

	mov	(%rsp), %rdx			C dinv
	.byte	0xc4,0x22,0xeb,0xf6,0x64,0xf7,0x28	C mulx 40(up,dn,8), %rdx, %r12
	add	%r9, (up)
	adc	%r11, 8(up)
	adc	%r13, 16(up)
	adc	%rbx, 24(up)
	adc	%rbp, %rax
	setc	R8(%rbp)
	add	%rax, 32(up)
	adc	$0, R32(%rbp)
	lea	8(up), up
	dec	un
	jne	L(out3)

L(ret):	mov	%rbp, %rax
	pop	%rsi			C dummy dealloc
	pop	%rbx
	pop	%rbp
	pop	%r12
	pop	%r13
	pop	%r14
	pop	%r15
	ret
EPILOGUE()