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
|
dnl Intel P6 mpn_addmul_1/mpn_submul_1 -- add or subtract mpn multiple.
dnl Copyright 1999-2002, 2005 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 P5
C P6 model 0-8,10-12 6.44
C P6 model 9 (Banias) 6.15
C P6 model 13 (Dothan) 6.11
C P4 model 0 (Willamette)
C P4 model 1 (?)
C P4 model 2 (Northwood)
C P4 model 3 (Prescott)
C P4 model 4 (Nocona)
C AMD K6
C AMD K7
C AMD K8
dnl P6 UNROLL_COUNT cycles/limb
dnl 8 6.7
dnl 16 6.35
dnl 32 6.3
dnl 64 6.3
dnl Maximum possible with the current code is 64.
deflit(UNROLL_COUNT, 16)
ifdef(`OPERATION_addmul_1', `
define(M4_inst, addl)
define(M4_function_1, mpn_addmul_1)
define(M4_function_1c, mpn_addmul_1c)
define(M4_description, add it to)
define(M4_desc_retval, carry)
',`ifdef(`OPERATION_submul_1', `
define(M4_inst, subl)
define(M4_function_1, mpn_submul_1)
define(M4_function_1c, mpn_submul_1c)
define(M4_description, subtract it from)
define(M4_desc_retval, borrow)
',`m4_error(`Need OPERATION_addmul_1 or OPERATION_submul_1
')')')
MULFUNC_PROLOGUE(mpn_addmul_1 mpn_addmul_1c mpn_submul_1 mpn_submul_1c)
C mp_limb_t M4_function_1 (mp_ptr dst, mp_srcptr src, mp_size_t size,
C mp_limb_t mult);
C mp_limb_t M4_function_1c (mp_ptr dst, mp_srcptr src, mp_size_t size,
C mp_limb_t mult, mp_limb_t carry);
C
C Calculate src,size multiplied by mult and M4_description dst,size.
C Return the M4_desc_retval limb from the top of the result.
C
C This code is pretty much the same as the K6 code. The unrolled loop is
C the same, but there's just a few scheduling tweaks in the setups and the
C simple loop.
C
C A number of variations have been tried for the unrolled loop, with one or
C two carries, and with loads scheduled earlier, but nothing faster than 6
C cycles/limb has been found.
ifdef(`PIC',`
deflit(UNROLL_THRESHOLD, 5)
',`
deflit(UNROLL_THRESHOLD, 5)
')
defframe(PARAM_CARRY, 20)
defframe(PARAM_MULTIPLIER,16)
defframe(PARAM_SIZE, 12)
defframe(PARAM_SRC, 8)
defframe(PARAM_DST, 4)
TEXT
ALIGN(32)
PROLOGUE(M4_function_1c)
pushl %ebx
deflit(`FRAME',4)
movl PARAM_CARRY, %ebx
jmp L(start_nc)
EPILOGUE()
PROLOGUE(M4_function_1)
push %ebx
deflit(`FRAME',4)
xorl %ebx, %ebx C initial carry
L(start_nc):
movl PARAM_SIZE, %ecx
pushl %esi
deflit(`FRAME',8)
movl PARAM_SRC, %esi
pushl %edi
deflit(`FRAME',12)
movl PARAM_DST, %edi
pushl %ebp
deflit(`FRAME',16)
cmpl $UNROLL_THRESHOLD, %ecx
movl PARAM_MULTIPLIER, %ebp
jae L(unroll)
C simple loop
C this is offset 0x22, so close enough to aligned
L(simple):
C eax scratch
C ebx carry
C ecx counter
C edx scratch
C esi src
C edi dst
C ebp multiplier
movl (%esi), %eax
addl $4, %edi
mull %ebp
addl %ebx, %eax
adcl $0, %edx
M4_inst %eax, -4(%edi)
movl %edx, %ebx
adcl $0, %ebx
decl %ecx
leal 4(%esi), %esi
jnz L(simple)
popl %ebp
popl %edi
popl %esi
movl %ebx, %eax
popl %ebx
ret
C------------------------------------------------------------------------------
C VAR_JUMP holds the computed jump temporarily because there's not enough
C registers when doing the mul for the initial two carry limbs.
C
C The add/adc for the initial carry in %ebx is necessary only for the
C mpn_add/submul_1c entry points. Duplicating the startup code to
C eliminate this for the plain mpn_add/submul_1 doesn't seem like a good
C idea.
dnl overlapping with parameters already fetched
define(VAR_COUNTER,`PARAM_SIZE')
define(VAR_JUMP, `PARAM_DST')
C this is offset 0x43, so close enough to aligned
L(unroll):
C eax
C ebx initial carry
C ecx size
C edx
C esi src
C edi dst
C ebp
movl %ecx, %edx
decl %ecx
subl $2, %edx
negl %ecx
shrl $UNROLL_LOG2, %edx
andl $UNROLL_MASK, %ecx
movl %edx, VAR_COUNTER
movl %ecx, %edx
C 15 code bytes per limb
ifdef(`PIC',`
call L(pic_calc)
L(here):
',`
shll $4, %edx
negl %ecx
leal L(entry) (%edx,%ecx,1), %edx
')
movl (%esi), %eax C src low limb
movl %edx, VAR_JUMP
leal ifelse(UNROLL_BYTES,256,128+) 4(%esi,%ecx,4), %esi
mull %ebp
addl %ebx, %eax C initial carry (from _1c)
adcl $0, %edx
movl %edx, %ebx C high carry
leal ifelse(UNROLL_BYTES,256,128) (%edi,%ecx,4), %edi
movl VAR_JUMP, %edx
testl $1, %ecx
movl %eax, %ecx C low carry
cmovnz( %ebx, %ecx) C high,low carry other way around
cmovnz( %eax, %ebx)
jmp *%edx
ifdef(`PIC',`
L(pic_calc):
shll $4, %edx
negl %ecx
C See mpn/x86/README about old gas bugs
leal (%edx,%ecx,1), %edx
addl $L(entry)-L(here), %edx
addl (%esp), %edx
ret_internal
')
C -----------------------------------------------------------
ALIGN(32)
L(top):
deflit(`FRAME',16)
C eax scratch
C ebx carry hi
C ecx carry lo
C edx scratch
C esi src
C edi dst
C ebp multiplier
C
C VAR_COUNTER loop counter
C
C 15 code bytes per limb
addl $UNROLL_BYTES, %edi
L(entry):
deflit(CHUNK_COUNT,2)
forloop(`i', 0, UNROLL_COUNT/CHUNK_COUNT-1, `
deflit(`disp0', eval(i*4*CHUNK_COUNT ifelse(UNROLL_BYTES,256,-128)))
deflit(`disp1', eval(disp0 + 4))
Zdisp( movl, disp0,(%esi), %eax)
mull %ebp
Zdisp( M4_inst,%ecx, disp0,(%edi))
adcl %eax, %ebx
movl %edx, %ecx
adcl $0, %ecx
movl disp1(%esi), %eax
mull %ebp
M4_inst %ebx, disp1(%edi)
adcl %eax, %ecx
movl %edx, %ebx
adcl $0, %ebx
')
decl VAR_COUNTER
leal UNROLL_BYTES(%esi), %esi
jns L(top)
deflit(`disp0', eval(UNROLL_BYTES ifelse(UNROLL_BYTES,256,-128)))
M4_inst %ecx, disp0(%edi)
movl %ebx, %eax
popl %ebp
popl %edi
popl %esi
popl %ebx
adcl $0, %eax
ret
EPILOGUE()
|