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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
dnl AMD K7 mpn_sqr_basecase -- square an mpn number.
dnl
dnl K7: approx 2.3 cycles/crossproduct, or 4.55 cycles/triangular product
dnl (measured on the speed difference between 25 and 50 limbs, which is
dnl roughly the Karatsuba recursing range).
dnl Copyright 1999, 2000 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., 59 Temple Place -
dnl Suite 330, Boston, MA 02111-1307, USA.
include(`../config.m4')
dnl These are the same as mpn/x86/k6/sqr_basecase.asm, see that code for
dnl some comments.
deflit(KARATSUBA_SQR_THRESHOLD_MAX, 66)
ifdef(`KARATSUBA_SQR_THRESHOLD_OVERRIDE',
`define(`KARATSUBA_SQR_THRESHOLD',KARATSUBA_SQR_THRESHOLD_OVERRIDE)')
m4_config_gmp_mparam(`KARATSUBA_SQR_THRESHOLD')
deflit(UNROLL_COUNT, eval(KARATSUBA_SQR_THRESHOLD-3))
C void mpn_sqr_basecase (mp_ptr dst, mp_srcptr src, mp_size_t size);
C
C With a KARATSUBA_SQR_THRESHOLD around 50 this code is about 1500 bytes,
C which is quite a bit, but is considered good value since squares big
C enough to use most of the code will be spending quite a few cycles in it.
defframe(PARAM_SIZE,12)
defframe(PARAM_SRC, 8)
defframe(PARAM_DST, 4)
TEXT
ALIGN(32)
PROLOGUE(mpn_sqr_basecase)
deflit(`FRAME',0)
movl PARAM_SIZE, %ecx
movl PARAM_SRC, %eax
cmpl $2, %ecx
movl PARAM_DST, %edx
je L(two_limbs)
ja L(three_or_more)
C------------------------------------------------------------------------------
C one limb only
C eax src
C ecx size
C edx dst
movl (%eax), %eax
movl %edx, %ecx
mull %eax
movl %edx, 4(%ecx)
movl %eax, (%ecx)
ret
C------------------------------------------------------------------------------
C
C Using the read/modify/write "add"s seems to be faster than saving and
C restoring registers. Perhaps the loads for the first set hide under the
C mul latency and the second gets store to load forwarding.
ALIGN(16)
L(two_limbs):
C eax src
C ebx
C ecx size
C edx dst
deflit(`FRAME',0)
pushl %ebx FRAME_pushl()
movl %eax, %ebx C src
movl (%eax), %eax
movl %edx, %ecx C dst
mull %eax C src[0]^2
movl %eax, (%ecx) C dst[0]
movl 4(%ebx), %eax
movl %edx, 4(%ecx) C dst[1]
mull %eax C src[1]^2
movl %eax, 8(%ecx) C dst[2]
movl (%ebx), %eax
movl %edx, 12(%ecx) C dst[3]
mull 4(%ebx) C src[0]*src[1]
popl %ebx
addl %eax, 4(%ecx)
adcl %edx, 8(%ecx)
adcl $0, 12(%ecx)
ASSERT(nc)
addl %eax, 4(%ecx)
adcl %edx, 8(%ecx)
adcl $0, 12(%ecx)
ASSERT(nc)
ret
C------------------------------------------------------------------------------
defframe(SAVE_EBX, -4)
defframe(SAVE_ESI, -8)
defframe(SAVE_EDI, -12)
defframe(SAVE_EBP, -16)
deflit(STACK_SPACE, 16)
L(three_or_more):
subl $STACK_SPACE, %esp
cmpl $4, %ecx
jae L(four_or_more)
deflit(`FRAME',STACK_SPACE)
C------------------------------------------------------------------------------
C Three limbs
C
C Writing out the loads and stores separately at the end of this code comes
C out about 10 cycles faster than using adcls to memory.
C eax src
C ecx size
C edx dst
movl %ebx, SAVE_EBX
movl %eax, %ebx C src
movl (%eax), %eax
movl %edx, %ecx C dst
movl %esi, SAVE_ESI
movl %edi, SAVE_EDI
mull %eax C src[0] ^ 2
movl %eax, (%ecx)
movl 4(%ebx), %eax
movl %edx, 4(%ecx)
mull %eax C src[1] ^ 2
movl %eax, 8(%ecx)
movl 8(%ebx), %eax
movl %edx, 12(%ecx)
mull %eax C src[2] ^ 2
movl %eax, 16(%ecx)
movl (%ebx), %eax
movl %edx, 20(%ecx)
mull 4(%ebx) C src[0] * src[1]
movl %eax, %esi
movl (%ebx), %eax
movl %edx, %edi
mull 8(%ebx) C src[0] * src[2]
addl %eax, %edi
movl %ebp, SAVE_EBP
movl $0, %ebp
movl 4(%ebx), %eax
adcl %edx, %ebp
mull 8(%ebx) C src[1] * src[2]
xorl %ebx, %ebx
addl %eax, %ebp
adcl $0, %edx
C eax
C ebx zero, will be dst[5]
C ecx dst
C edx dst[4]
C esi dst[1]
C edi dst[2]
C ebp dst[3]
adcl $0, %edx
addl %esi, %esi
adcl %edi, %edi
movl 4(%ecx), %eax
adcl %ebp, %ebp
adcl %edx, %edx
adcl $0, %ebx
addl %eax, %esi
movl 8(%ecx), %eax
adcl %eax, %edi
movl 12(%ecx), %eax
movl %esi, 4(%ecx)
adcl %eax, %ebp
movl 16(%ecx), %eax
movl %edi, 8(%ecx)
movl SAVE_ESI, %esi
movl SAVE_EDI, %edi
adcl %eax, %edx
movl 20(%ecx), %eax
movl %ebp, 12(%ecx)
adcl %ebx, %eax
ASSERT(nc)
movl SAVE_EBX, %ebx
movl SAVE_EBP, %ebp
movl %edx, 16(%ecx)
movl %eax, 20(%ecx)
addl $FRAME, %esp
ret
C------------------------------------------------------------------------------
L(four_or_more):
C First multiply src[0]*src[1..size-1] and store at dst[1..size].
C Further products are added in rather than stored.
C eax src
C ebx
C ecx size
C edx dst
C esi
C edi
C ebp
defframe(`VAR_COUNTER',-20)
defframe(`VAR_JMP', -24)
deflit(EXTRA_STACK_SPACE, 8)
movl %ebx, SAVE_EBX
movl %edi, SAVE_EDI
leal (%edx,%ecx,4), %edi C &dst[size]
movl %esi, SAVE_ESI
movl %ebp, SAVE_EBP
leal (%eax,%ecx,4), %esi C &src[size]
movl (%eax), %ebp C multiplier
movl $0, %ebx
decl %ecx
negl %ecx
subl $EXTRA_STACK_SPACE, %esp
FRAME_subl_esp(EXTRA_STACK_SPACE)
L(mul_1):
C eax scratch
C ebx carry
C ecx counter
C edx scratch
C esi &src[size]
C edi &dst[size]
C ebp multiplier
movl (%esi,%ecx,4), %eax
mull %ebp
addl %ebx, %eax
movl %eax, (%edi,%ecx,4)
movl $0, %ebx
adcl %edx, %ebx
incl %ecx
jnz L(mul_1)
C Add products src[n]*src[n+1..size-1] at dst[2*n-1...], for each n=1..size-2.
C
C The last two products, which are the bottom right corner of the product
C triangle, are left to the end. These are src[size-3]*src[size-2,size-1]
C and src[size-2]*src[size-1]. If size is 4 then it's only these corner
C cases that need to be done.
C
C The unrolled code is the same as in mpn_addmul_1, see that routine for
C some comments.
C
C VAR_COUNTER is the outer loop, running from -size+4 to -1, inclusive.
C
C VAR_JMP is the computed jump into the unrolled code, stepped by one code
C chunk each outer loop.
C
C K7 does branch prediction on indirect jumps, which is bad since it's a
C different target each time. There seems no way to avoid this.
dnl This value also hard coded in some shifts and adds
deflit(CODE_BYTES_PER_LIMB, 17)
dnl With the unmodified &src[size] and &dst[size] pointers, the
dnl displacements in the unrolled code fit in a byte for UNROLL_COUNT
dnl values up to 31, but above that an offset must be added to them.
deflit(OFFSET,
ifelse(eval(UNROLL_COUNT>31),1,
eval((UNROLL_COUNT-31)*4),
0))
dnl Because the last chunk of code is generated differently, a label placed
dnl at the end doesn't work. Instead calculate the implied end using the
dnl start and how many chunks of code there are.
deflit(UNROLL_INNER_END,
`L(unroll_inner_start)+eval(UNROLL_COUNT*CODE_BYTES_PER_LIMB)')
C eax
C ebx carry
C ecx
C edx
C esi &src[size]
C edi &dst[size]
C ebp
movl PARAM_SIZE, %ecx
movl %ebx, (%edi)
subl $4, %ecx
jz L(corner)
negl %ecx
ifelse(OFFSET,0,,`subl $OFFSET, %edi')
ifelse(OFFSET,0,,`subl $OFFSET, %esi')
movl %ecx, %edx
shll $4, %ecx
ifdef(`PIC',`
call L(pic_calc)
L(here):
',`
leal UNROLL_INNER_END-eval(2*CODE_BYTES_PER_LIMB)(%ecx,%edx), %ecx
')
C The calculated jump mustn't come out to before the start of the
C code available. This is the limit UNROLL_COUNT puts on the src
C operand size, but checked here directly using the jump address.
ASSERT(ae,
`movl_text_address(L(unroll_inner_start), %eax)
cmpl %eax, %ecx')
C------------------------------------------------------------------------------
ALIGN(16)
L(unroll_outer_top):
C eax
C ebx high limb to store
C ecx VAR_JMP
C edx VAR_COUNTER, limbs, negative
C esi &src[size], constant
C edi dst ptr, high of last addmul
C ebp
movl -12+OFFSET(%esi,%edx,4), %ebp C next multiplier
movl -8+OFFSET(%esi,%edx,4), %eax C first of multiplicand
movl %edx, VAR_COUNTER
mull %ebp
define(cmovX,`ifelse(eval(UNROLL_COUNT%2),0,`cmovz($@)',`cmovnz($@)')')
testb $1, %cl
movl %edx, %ebx C high carry
movl %ecx, %edx C jump
movl %eax, %ecx C low carry
cmovX( %ebx, %ecx) C high carry reverse
cmovX( %eax, %ebx) C low carry reverse
leal CODE_BYTES_PER_LIMB(%edx), %eax
xorl %edx, %edx
leal 4(%edi), %edi
movl %eax, VAR_JMP
jmp *%eax
ifdef(`PIC',`
L(pic_calc):
addl (%esp), %ecx
addl $UNROLL_INNER_END-eval(2*CODE_BYTES_PER_LIMB)-L(here), %ecx
addl %edx, %ecx
ret
')
C Must be an even address to preserve the significance of the low
C bit of the jump address indicating which way around ecx/ebx should
C start.
ALIGN(2)
L(unroll_inner_start):
C eax next limb
C ebx carry high
C ecx carry low
C edx scratch
C esi src
C edi dst
C ebp multiplier
forloop(`i', UNROLL_COUNT, 1, `
deflit(`disp_src', eval(-i*4 + OFFSET))
deflit(`disp_dst', eval(disp_src - 4))
m4_assert(`disp_src>=-128 && disp_src<128')
m4_assert(`disp_dst>=-128 && disp_dst<128')
ifelse(eval(i%2),0,`
Zdisp( movl, disp_src,(%esi), %eax)
adcl %edx, %ebx
mull %ebp
Zdisp( addl, %ecx, disp_dst,(%edi))
movl $0, %ecx
adcl %eax, %ebx
',`
dnl this bit comes out last
Zdisp( movl, disp_src,(%esi), %eax)
adcl %edx, %ecx
mull %ebp
dnl Zdisp( addl %ebx, disp_src,(%edi))
addl %ebx, disp_dst(%edi)
ifelse(forloop_last,0,
` movl $0, %ebx')
adcl %eax, %ecx
')
')
C eax next limb
C ebx carry high
C ecx carry low
C edx scratch
C esi src
C edi dst
C ebp multiplier
adcl $0, %edx
addl %ecx, -4+OFFSET(%edi)
movl VAR_JMP, %ecx
adcl $0, %edx
movl %edx, m4_empty_if_zero(OFFSET) (%edi)
movl VAR_COUNTER, %edx
incl %edx
jnz L(unroll_outer_top)
ifelse(OFFSET,0,,`
addl $OFFSET, %esi
addl $OFFSET, %edi
')
C------------------------------------------------------------------------------
L(corner):
C esi &src[size]
C edi &dst[2*size-5]
movl -12(%esi), %ebp
movl -8(%esi), %eax
movl %eax, %ecx
mull %ebp
addl %eax, -4(%edi)
movl -4(%esi), %eax
adcl $0, %edx
movl %edx, %ebx
movl %eax, %esi
mull %ebp
addl %ebx, %eax
adcl $0, %edx
addl %eax, (%edi)
movl %esi, %eax
adcl $0, %edx
movl %edx, %ebx
mull %ecx
addl %ebx, %eax
movl %eax, 4(%edi)
adcl $0, %edx
movl %edx, 8(%edi)
C Left shift of dst[1..2*size-2], high bit shifted out becomes dst[2*size-1].
L(lshift_start):
movl PARAM_SIZE, %eax
movl PARAM_DST, %edi
xorl %ecx, %ecx C clear carry
leal (%edi,%eax,8), %edi
notl %eax C -size-1, preserve carry
leal 2(%eax), %eax C -(size-1)
L(lshift):
C eax counter, negative
C ebx
C ecx
C edx
C esi
C edi dst, pointing just after last limb
C ebp
rcll -4(%edi,%eax,8)
rcll (%edi,%eax,8)
incl %eax
jnz L(lshift)
setc %al
movl PARAM_SRC, %esi
movl %eax, -4(%edi) C dst most significant limb
movl PARAM_SIZE, %ecx
C Now add in the squares on the diagonal, src[0]^2, src[1]^2, ...,
C src[size-1]^2. dst[0] hasn't yet been set at all yet, and just gets the
C low limb of src[0]^2.
movl (%esi), %eax C src[0]
mull %eax
leal (%esi,%ecx,4), %esi C src point just after last limb
negl %ecx
movl %eax, (%edi,%ecx,8) C dst[0]
incl %ecx
L(diag):
C eax scratch
C ebx scratch
C ecx counter, negative
C edx carry
C esi src just after last limb
C edi dst just after last limb
C ebp
movl (%esi,%ecx,4), %eax
movl %edx, %ebx
mull %eax
addl %ebx, -4(%edi,%ecx,8)
adcl %eax, (%edi,%ecx,8)
adcl $0, %edx
incl %ecx
jnz L(diag)
movl SAVE_ESI, %esi
movl SAVE_EBX, %ebx
addl %edx, -4(%edi) C dst most significant limb
movl SAVE_EDI, %edi
movl SAVE_EBP, %ebp
addl $FRAME, %esp
ret
EPILOGUE()
|