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
|
dnl AMD K7 mpn_addmul_1/mpn_submul_1 -- add or subtract mpn multiple.
dnl Copyright 1999, 2000, 2001, 2002, 2005 Free Software Foundation, Inc.
dnl
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
dnl modify it under the terms of the GNU Lesser General Public License as
dnl published by the Free Software Foundation; either version 2.1 of the
dnl License, or (at your option) any later version.
dnl
dnl The GNU MP Library is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
dnl Lesser General Public License for more details.
dnl
dnl You should have received a copy of the GNU Lesser General Public
dnl License along with the GNU MP Library; see the file COPYING.LIB. If
dnl not, write to the Free Software Foundation, Inc., 51 Franklin Street,
dnl Fifth Floor, Boston, MA 02110-1301, USA.
include(`../config.m4')
C cycles/limb
C P5:
C P6 model 0-8,10-12) 6.35
C P6 model 9 (Banias)
C P6 model 13 (Dothan) 6.25
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 K6:
C K7: 3.9
C K8:
dnl K7: UNROLL_COUNT cycles/limb
dnl 4 4.42
dnl 8 4.16
dnl 16 3.9
dnl 32 3.9
dnl 64 3.87
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.
ifdef(`PIC',`
deflit(UNROLL_THRESHOLD, 9)
',`
deflit(UNROLL_THRESHOLD, 6)
')
defframe(PARAM_CARRY, 20)
defframe(PARAM_MULTIPLIER,16)
defframe(PARAM_SIZE, 12)
defframe(PARAM_SRC, 8)
defframe(PARAM_DST, 4)
deflit(`FRAME',0)
defframe(SAVE_EBX, -4)
defframe(SAVE_ESI, -8)
defframe(SAVE_EDI, -12)
defframe(SAVE_EBP, -16)
deflit(SAVE_SIZE, 16)
TEXT
ALIGN(32)
PROLOGUE(M4_function_1)
movl PARAM_SIZE, %edx
movl PARAM_SRC, %eax
xorl %ecx, %ecx
decl %edx
jnz L(start_1)
movl (%eax), %eax
movl PARAM_DST, %ecx
mull PARAM_MULTIPLIER
M4_inst %eax, (%ecx)
adcl $0, %edx
movl %edx, %eax
ret
EPILOGUE()
ALIGN(16)
PROLOGUE(M4_function_1c)
movl PARAM_SIZE, %edx
movl PARAM_SRC, %eax
decl %edx
jnz L(more_than_one_limb)
movl (%eax), %eax
movl PARAM_DST, %ecx
mull PARAM_MULTIPLIER
addl PARAM_CARRY, %eax
adcl $0, %edx
M4_inst %eax, (%ecx)
adcl $0, %edx
movl %edx, %eax
ret
C offset 0x44 so close enough to aligned
L(more_than_one_limb):
movl PARAM_CARRY, %ecx
L(start_1):
C eax src
C ecx initial carry
C edx size-1
subl $SAVE_SIZE, %esp
deflit(`FRAME',16)
movl %ebx, SAVE_EBX
movl %esi, SAVE_ESI
movl %edx, %ebx C size-1
movl PARAM_SRC, %esi
movl %ebp, SAVE_EBP
cmpl $UNROLL_THRESHOLD, %edx
movl PARAM_MULTIPLIER, %ebp
movl %edi, SAVE_EDI
movl (%esi), %eax C src low limb
movl PARAM_DST, %edi
ja L(unroll)
C simple loop
leal 4(%esi,%ebx,4), %esi C point one limb past last
leal (%edi,%ebx,4), %edi C point at last limb
negl %ebx
C The movl to load the next source limb is done well ahead of the
C mul. This is necessary for full speed, and leads to one limb
C handled separately at the end.
L(simple):
C eax src limb
C ebx loop counter
C ecx carry limb
C edx scratch
C esi src
C edi dst
C ebp multiplier
mull %ebp
addl %eax, %ecx
adcl $0, %edx
M4_inst %ecx, (%edi,%ebx,4)
movl (%esi,%ebx,4), %eax
adcl $0, %edx
incl %ebx
movl %edx, %ecx
jnz L(simple)
mull %ebp
movl SAVE_EBX, %ebx
movl SAVE_ESI, %esi
movl SAVE_EBP, %ebp
addl %eax, %ecx
adcl $0, %edx
M4_inst %ecx, (%edi)
adcl $0, %edx
movl SAVE_EDI, %edi
addl $SAVE_SIZE, %esp
movl %edx, %eax
ret
C -----------------------------------------------------------------------------
ALIGN(16)
L(unroll):
C eax src low limb
C ebx size-1
C ecx carry
C edx size-1
C esi src
C edi dst
C ebp multiplier
dnl overlapping with parameters no longer needed
define(VAR_COUNTER,`PARAM_SIZE')
define(VAR_JUMP, `PARAM_MULTIPLIER')
subl $2, %ebx C (size-2)-1
decl %edx C size-2
shrl $UNROLL_LOG2, %ebx
negl %edx
movl %ebx, VAR_COUNTER
andl $UNROLL_MASK, %edx
movl %edx, %ebx
shll $4, %edx
ifdef(`PIC',`
call L(pic_calc)
L(here):
',`
leal L(entry) (%edx,%ebx,1), %edx
')
negl %ebx
movl %edx, VAR_JUMP
mull %ebp
addl %eax, %ecx C initial carry, becomes low carry
adcl $0, %edx
testb $1, %bl
movl 4(%esi), %eax C src second limb
leal ifelse(UNROLL_BYTES,256,128+) 8(%esi,%ebx,4), %esi
leal ifelse(UNROLL_BYTES,256,128) (%edi,%ebx,4), %edi
movl %edx, %ebx C high carry
cmovnz( %ecx, %ebx) C high,low carry other way around
cmovnz( %edx, %ecx)
jmp *VAR_JUMP
ifdef(`PIC',`
L(pic_calc):
C See mpn/x86/README about old gas bugs
leal (%edx,%ebx,1), %edx
addl $L(entry)-L(here), %edx
addl (%esp), %edx
ret_internal
')
C -----------------------------------------------------------------------------
C This code uses a "two carry limbs" scheme. At the top of the loop the
C carries are ebx=lo, ecx=hi, then they swap for each limb processed. For
C the computed jump an odd size means they start one way around, an even
C size the other. Either way one limb is handled separately at the start of
C the loop.
C
C The positioning of the movl to load the next source limb is important.
C Moving it after the adcl with a view to avoiding a separate mul at the end
C of the loop slows the code down.
ALIGN(32)
L(top):
C eax src limb
C ebx carry high
C ecx carry low
C edx scratch
C esi src+8
C edi dst
C ebp multiplier
C
C VAR_COUNTER loop counter
C
C 17 bytes each limb
L(entry):
deflit(CHUNK_COUNT,2)
forloop(`i', 0, UNROLL_COUNT/CHUNK_COUNT-1, `
deflit(`disp0', eval(i*CHUNK_COUNT*4 ifelse(UNROLL_BYTES,256,-128)))
deflit(`disp1', eval(disp0 + 4))
mull %ebp
Zdisp( M4_inst,%ecx, disp0,(%edi))
movl $0, %ecx
adcl %eax, %ebx
Zdisp( movl, disp0,(%esi), %eax)
adcl %edx, %ecx
mull %ebp
M4_inst %ebx, disp1(%edi)
movl $0, %ebx
adcl %eax, %ecx
movl disp1(%esi), %eax
adcl %edx, %ebx
')
decl VAR_COUNTER
leal UNROLL_BYTES(%esi), %esi
leal UNROLL_BYTES(%edi), %edi
jns L(top)
C eax src limb
C ebx carry high
C ecx carry low
C edx
C esi
C edi dst (points at second last limb)
C ebp multiplier
deflit(`disp0', ifelse(UNROLL_BYTES,256,-128))
deflit(`disp1', eval(disp0-0 + 4))
mull %ebp
M4_inst %ecx, disp0(%edi)
movl SAVE_EBP, %ebp
adcl %ebx, %eax
movl SAVE_EBX, %ebx
movl SAVE_ESI, %esi
adcl $0, %edx
M4_inst %eax, disp1(%edi)
movl SAVE_EDI, %edi
adcl $0, %edx
addl $SAVE_SIZE, %esp
movl %edx, %eax
ret
EPILOGUE()
|