aboutsummaryrefslogtreecommitdiff
path: root/gmp-6.3.0/mpn/x86_64/core2/divrem_1.asm
blob: 1b3f1394ec5cd540c430e9ae85c42390b974c4ee (plain)
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
dnl  x86-64 mpn_divrem_1 -- mpn by limb division.

dnl  Copyright 2004, 2005, 2007-2010, 2012, 2014 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		norm	unorm	frac
C AMD K8,K9	15	15	12
C AMD K10	15	15	12
C Intel P4	44	44	43
C Intel core2	24	24	19.5
C Intel corei	19	19	18
C Intel atom	51	51	36
C VIA nano	46	44	22.5

C mp_limb_t
C mpn_divrem_1 (mp_ptr qp, mp_size_t fn,
C               mp_srcptr np, mp_size_t nn, mp_limb_t d)

C mp_limb_t
C mpn_preinv_divrem_1 (mp_ptr qp, mp_size_t fn,
C                      mp_srcptr np, mp_size_t nn, mp_limb_t d,
C                      mp_limb_t dinv, int cnt)

C INPUT PARAMETERS
define(`qp',		`%rdi')
define(`fn_param',	`%rsi')
define(`up_param',	`%rdx')
define(`un_param',	`%rcx')
define(`d',		`%r8')
define(`dinv',		`%r9')		C only for mpn_preinv_divrem_1
C       shift passed on stack		C only for mpn_preinv_divrem_1

define(`cnt',		`%rcx')
define(`up',		`%rsi')
define(`fn',		`%r12')
define(`un',		`%rbx')


C rax rbx rcx rdx rsi rdi rbp r8  r9  r10 r11 r12 r13 r14 r15
C         cnt         qp      d  dinv

ABI_SUPPORT(DOS64)
ABI_SUPPORT(STD64)

IFSTD(`define(`CNTOFF',		`40($1)')')
IFDOS(`define(`CNTOFF',		`104($1)')')

ASM_START()
	TEXT
	ALIGN(16)
PROLOGUE(mpn_preinv_divrem_1)
	FUNC_ENTRY(4)
IFDOS(`	mov	56(%rsp), %r8	')
IFDOS(`	mov	64(%rsp), %r9	')
	xor	R32(%rax), R32(%rax)
	push	%r13
	push	%r12
	push	%rbp
	push	%rbx

	mov	fn_param, fn
	mov	un_param, un
	add	fn_param, un_param
	mov	up_param, up

	lea	-8(qp,un_param,8), qp

	mov	CNTOFF(%rsp), R8(cnt)
	shl	R8(cnt), d
	jmp	L(ent)
EPILOGUE()

	ALIGN(16)
PROLOGUE(mpn_divrem_1)
	FUNC_ENTRY(4)
IFDOS(`	mov	56(%rsp), %r8	')
	xor	R32(%rax), R32(%rax)
	push	%r13
	push	%r12
	push	%rbp
	push	%rbx

	mov	fn_param, fn
	mov	un_param, un
	add	fn_param, un_param
	mov	up_param, up
	je	L(ret)

	lea	-8(qp,un_param,8), qp
	xor	R32(%rbp), R32(%rbp)

L(unnormalized):
	test	un, un
	je	L(44)
	mov	-8(up,un,8), %rax
	cmp	d, %rax
	jae	L(44)
	mov	%rbp, (qp)
	mov	%rax, %rbp
	lea	-8(qp), qp
	je	L(ret)
	dec	un
L(44):
	bsr	d, %rcx
	not	R32(%rcx)
	sal	R8(%rcx), d
	sal	R8(%rcx), %rbp

	push	%rcx
IFSTD(`	push	%rdi		')
IFSTD(`	push	%rsi		')
	push	%r8
IFSTD(`	sub	$8, %rsp	')
IFSTD(`	mov	d, %rdi		')
IFDOS(`	sub	$40, %rsp	')
IFDOS(`	mov	d, %rcx		')
	ASSERT(nz, `test $15, %rsp')
	CALL(	mpn_invert_limb)
IFSTD(`	add	$8, %rsp	')
IFDOS(`	add	$40, %rsp	')
	pop	%r8
IFSTD(`	pop	%rsi		')
IFSTD(`	pop	%rdi		')
	pop	%rcx

	mov	%rax, dinv
	mov	%rbp, %rax
	test	un, un
	je	L(frac)

L(ent):	mov	-8(up,un,8), %rbp
	shr	R8(%rcx), %rax
	shld	R8(%rcx), %rbp, %rax
	sub	$2, un
	js	L(end)

	ALIGN(16)
L(top):	lea	1(%rax), %r11
	mul	dinv
	mov	(up,un,8), %r10
	shld	R8(%rcx), %r10, %rbp
	mov	%rbp, %r13
	add	%rax, %r13
	adc	%r11, %rdx
	mov	%rdx, %r11
	imul	d, %rdx
	sub	%rdx, %rbp
	lea	(d,%rbp), %rax
	sub	$8, qp
	cmp	%r13, %rbp
	cmovc	%rbp, %rax
	adc	$-1, %r11
	cmp	d, %rax
	jae	L(ufx)
L(uok):	dec	un
	mov	%r11, 8(qp)
	mov	%r10, %rbp
	jns	L(top)

L(end):	lea	1(%rax), %r11
	sal	R8(%rcx), %rbp
	mul	dinv
	add	%rbp, %rax
	adc	%r11, %rdx
	mov	%rax, %r11
	mov	%rdx, %r13
	imul	d, %rdx
	sub	%rdx, %rbp
	mov	d, %rax
	add	%rbp, %rax
	cmp	%r11, %rbp
	cmovc	%rbp, %rax
	adc	$-1, %r13
	cmp	d, %rax
	jae	L(efx)
L(eok):	mov	%r13, (qp)
	sub	$8, qp
	jmp	L(frac)

L(ufx):	sub	d, %rax
	inc	%r11
	jmp	L(uok)
L(efx):	sub	d, %rax
	inc	%r13
	jmp	L(eok)

L(frac):mov	d, %rbp
	neg	%rbp
	jmp	L(fent)

	ALIGN(16)			C	    K8-K10  P6-CNR P6-NHM  P4
L(ftop):mul	dinv			C	      0,12   0,17   0,17
	add	%r11, %rdx		C	      5      8     10
	mov	%rax, %r11		C	      4      8      3
	mov	%rdx, %r13		C	      6      9     11
	imul	%rbp, %rdx		C	      6      9     11
	mov	d, %rax			C
	add	%rdx, %rax		C	     10     14     14
	cmp	%r11, %rdx		C	     10     14     14
	cmovc	%rdx, %rax		C	     11     15     15
	adc	$-1, %r13		C
	mov	%r13, (qp)		C
	sub	$8, qp			C
L(fent):lea	1(%rax), %r11		C
	dec	fn			C
	jns	L(ftop)			C

	shr	R8(%rcx), %rax
L(ret):	pop	%rbx
	pop	%rbp
	pop	%r12
	pop	%r13
	FUNC_EXIT()
	ret
EPILOGUE()