aboutsummaryrefslogtreecommitdiff
path: root/gmp-6.3.0/mpn/x86/pentium/mode1o.asm
blob: a90abca5a5668bc51135d51f95df4cf9efe0bb56 (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
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
dnl  Intel Pentium mpn_modexact_1_odd -- exact division style remainder.

dnl  Copyright 2000-2002, 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 P5: 23.0 cycles/limb


C mp_limb_t mpn_modexact_1_odd (mp_srcptr src, mp_size_t size,
C                               mp_limb_t divisor);
C mp_limb_t mpn_modexact_1c_odd (mp_srcptr src, mp_size_t size,
C                                mp_limb_t divisor, mp_limb_t carry);
C
C There seems no way to pair up the two lone instructions in the main loop.
C
C The special case for size==1 saves about 20 cycles (non-PIC), making it
C the same as mpn_mod_1, and in fact making modexact faster than mod_1 at
C all sizes.
C
C Alternatives:
C
C Using mmx for the multiplies might be possible, with pmullw and pmulhw
C having just 3 cycle latencies, but carry bit handling would probably be
C complicated.

defframe(PARAM_CARRY,  16)
defframe(PARAM_DIVISOR,12)
defframe(PARAM_SIZE,   8)
defframe(PARAM_SRC,    4)

dnl  re-using parameter space
define(VAR_INVERSE,`PARAM_SIZE')

	TEXT

	ALIGN(16)
PROLOGUE(mpn_modexact_1c_odd)
deflit(`FRAME',0)

	movl	PARAM_DIVISOR, %eax
	movl	PARAM_CARRY, %edx

	jmp	L(start_1c)

EPILOGUE()

	ALIGN(16)
PROLOGUE(mpn_modexact_1_odd)
deflit(`FRAME',0)

	movl	PARAM_DIVISOR, %eax
	xorl	%edx, %edx		C carry

L(start_1c):

ifdef(`PIC',`
ifdef(`DARWIN',`
	shrl	%eax			C d/2
	LEA(	binvert_limb_table, %ecx)
	pushl	%ebx		FRAME_pushl()
	movl	PARAM_SIZE, %ebx

	andl	$127, %eax
	subl	$2, %ebx

	movb	(%eax,%ecx), %cl
	jc	L(one_limb)
',`
	call	L(here)		FRAME_pushl()
L(here):

	shrl	%eax			C d/2
	movl	(%esp), %ecx		C eip

	addl	$_GLOBAL_OFFSET_TABLE_+[.-L(here)], %ecx
	movl	%ebx, (%esp)		C push ebx

	andl	$127, %eax
	movl	PARAM_SIZE, %ebx

	movl	binvert_limb_table@GOT(%ecx), %ecx
	subl	$2, %ebx

	movb	(%eax,%ecx), %cl			C inv 8 bits
	jc	L(one_limb)
')
',`
dnl non-PIC
	shrl	%eax			C d/2
	pushl	%ebx		FRAME_pushl()

	movl	PARAM_SIZE, %ebx
	andl	$127, %eax

	subl	$2, %ebx
	jc	L(one_limb)

	movb	binvert_limb_table(%eax), %cl		C inv 8 bits
')

	movl	%ecx, %eax
	addl	%ecx, %ecx		C 2*inv

	imull	%eax, %eax		C inv*inv

	imull	PARAM_DIVISOR, %eax	C inv*inv*d

	subl	%eax, %ecx		C inv = 2*inv - inv*inv*d

	movl	%ecx, %eax
	addl	%ecx, %ecx		C 2*inv

	imull	%eax, %eax		C inv*inv

	imull	PARAM_DIVISOR, %eax	C inv*inv*d

	subl	%eax, %ecx		C inv = 2*inv - inv*inv*d
	pushl	%esi		FRAME_pushl()

	ASSERT(e,`	C d*inv == 1 mod 2^GMP_LIMB_BITS
	movl	%ecx, %eax
	imull	PARAM_DIVISOR, %eax
	cmpl	$1, %eax')

	movl	PARAM_SRC, %esi
	movl	%ecx, VAR_INVERSE

	movl	(%esi), %eax		C src[0]
	leal	4(%esi,%ebx,4), %esi	C &src[size-1]

	xorl	$-1, %ebx		C -(size-1)
	ASSERT(nz)
	jmp	L(entry)


C The use of VAR_INVERSE means only a store is needed for that value, rather
C than a push and pop of say %edi.

	ALIGN(16)
L(top):
	C eax	scratch, low product
	C ebx	counter, limbs, negative
	C ecx	carry bit
	C edx	scratch, high product
	C esi	&src[size-1]
	C edi
	C ebp

	mull	PARAM_DIVISOR		C h:dummy = q*d

	movl	(%esi,%ebx,4), %eax	C src[i]
	subl	%ecx, %edx		C h -= -c

L(entry):
	subl	%edx, %eax		C s = src[i] - h

	sbbl	%ecx, %ecx		C new -c (0 or -1)

	imull	VAR_INVERSE, %eax	C q = s*i

	incl	%ebx
	jnz	L(top)


	mull	PARAM_DIVISOR

	movl	(%esi), %eax		C src high
	subl	%ecx, %edx		C h -= -c

	cmpl	PARAM_DIVISOR, %eax

	jbe	L(skip_last)
deflit(FRAME_LAST,FRAME)


	subl	%edx, %eax		C s = src[i] - h
	popl	%esi		FRAME_popl()

	sbbl	%ecx, %ecx		C c (0 or -1)
	popl	%ebx		FRAME_popl()

	imull	VAR_INVERSE, %eax	C q = s*i

	mull	PARAM_DIVISOR		C h:dummy = q*d

	movl	%edx, %eax

	subl	%ecx, %eax

	ret


C When high<divisor can skip last step.

L(skip_last):
deflit(`FRAME',FRAME_LAST)
	C eax	src high
	C ebx
	C ecx
	C edx	r
	C esi

	subl	%eax, %edx	C r-s
	popl	%esi		FRAME_popl()

	sbbl	%eax, %eax	C -1 if underflow
	movl	PARAM_DIVISOR, %ebx

	andl	%ebx, %eax	C divisor if underflow
	popl	%ebx		FRAME_popl()

	addl	%edx, %eax	C addback if underflow

	ret


C Special case for size==1 using a division for r = c-a mod d.
C Could look for a-c<d and save a division sometimes, but that doesn't seem
C worth bothering about.

L(one_limb):
deflit(`FRAME',4)
	C eax
	C ebx	size-2 (==-1)
	C ecx
	C edx	carry
	C esi	src end
	C edi
	C ebp

	movl	%edx, %eax
	movl	PARAM_SRC, %edx

	movl	PARAM_DIVISOR, %ecx
	popl	%ebx		FRAME_popl()

	subl	(%edx), %eax		C c-a

	sbbl	%edx, %edx
	decl	%ecx			C d-1

	andl	%ecx, %edx		C b*d+c-a if c<a, or c-a if c>=a

	divl	PARAM_DIVISOR

	movl	%edx, %eax

	ret

EPILOGUE()
ASM_END()