1 dnl AMD K6 mpn_mul_basecase -- multiply two mpn numbers.
3 dnl Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 dnl This file is part of the GNU MP Library.
7 dnl The GNU MP Library is free software; you can redistribute it and/or
8 dnl modify it under the terms of the GNU Lesser General Public License as
9 dnl published by the Free Software Foundation; either version 3 of the
10 dnl License, or (at your option) any later version.
12 dnl The GNU MP Library is distributed in the hope that it will be useful,
13 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
14 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 dnl Lesser General Public License for more details.
17 dnl You should have received a copy of the GNU Lesser General Public License
18 dnl along with the GNU MP Library. If not, see http://www.gnu.org/licenses/.
20 include(`../config.m4')
23 C K6: approx 9.0 cycles per cross product on 30x30 limbs (with 16 limbs/loop
28 dnl K6: UNROLL_COUNT cycles/product (approx)
32 dnl Maximum possible with the current code is 32.
34 dnl With 16 the inner unrolled loop fits exactly in a 256 byte block, which
35 dnl might explain it's good performance.
37 deflit(UNROLL_COUNT, 16)
40 C void mpn_mul_basecase (mp_ptr wp,
41 C mp_srcptr xp, mp_size_t xsize,
42 C mp_srcptr yp, mp_size_t ysize);
44 C Calculate xp,xsize multiplied by yp,ysize, storing the result in
47 C This routine is essentially the same as mpn/generic/mul_basecase.c, but
48 C it's faster because it does most of the mpn_addmul_1() entry code only
49 C once. The saving is about 10-20% on typical sizes coming from the
50 C Karatsuba multiply code.
54 C The mul_1 loop is about 8.5 c/l, which is slower than mpn_mul_1 at 6.25
55 C c/l. Could call mpn_mul_1 when ysize is big enough to make it worthwhile.
57 C The main unrolled addmul loop could be shared by mpn_addmul_1, using some
58 C extra stack setups and maybe 2 or 3 wasted cycles at the end. Code saving
62 deflit(UNROLL_THRESHOLD, 8)
64 deflit(UNROLL_THRESHOLD, 8)
67 defframe(PARAM_YSIZE,20)
68 defframe(PARAM_YP, 16)
69 defframe(PARAM_XSIZE,12)
75 PROLOGUE(mpn_mul_basecase)
78 movl PARAM_XSIZE, %ecx
82 movl (%eax), %eax C yp low limb
85 ja L(xsize_more_than_two_limbs)
86 je L(two_by_something)
89 C one limb by one limb
91 movl (%edx), %edx C xp low limb
101 C -----------------------------------------------------------------------------
111 movl %eax, %ecx C yp low limb
112 movl (%edx), %eax C xp low limb
118 C two limbs by one limb
125 movl %edx, %esi C carry
142 C -----------------------------------------------------------------------------
154 mull %ecx C xp[0] * yp[0]
161 movl %edx, %edi C carry, for wp[1]
163 mull %ecx C xp[1] * yp[0]
171 movl 4(%ecx), %ecx C yp[1]
173 movl 4(%esi), %eax C xp[1]
174 movl %edx, %edi C carry, for wp[2]
176 mull %ecx C xp[1] * yp[1]
182 movl (%esi), %eax C xp[0]
183 movl %edx, %esi C carry, for wp[3]
185 mull %ecx C xp[0] * yp[1]
201 C -----------------------------------------------------------------------------
203 L(xsize_more_than_two_limbs):
205 C The first limb of yp is processed with a simple mpn_mul_1 style loop
206 C inline. Unrolling this doesn't seem worthwhile since it's only run once
207 C (whereas the addmul below is run ysize-1 many times). A call to the
208 C actual mpn_mul_1 will be slowed down by the call and parameter pushing and
209 C popping, and doesn't seem likely to be worthwhile on the typical 10-20
210 C limb operations the Karatsuba code calls here with.
221 pushl %edi defframe_pushl(SAVE_EDI)
222 pushl %ebp defframe_pushl(SAVE_EBP)
225 pushl %esi defframe_pushl(SAVE_ESI)
228 pushl %ebx defframe_pushl(SAVE_EBX)
230 leal (%edx,%ecx,4), %ebx C xp end
233 leal (%edi,%ecx,4), %edi C wp end of mul1
240 C ecx counter, negative
246 movl (%ebx,%ecx,4), %eax
255 movl %eax, (%edi,%ecx,4)
261 movl PARAM_YSIZE, %edx
262 movl %esi, (%edi) C final carry
264 movl PARAM_XSIZE, %ecx
267 jnz L(ysize_more_than_one_limb)
276 L(ysize_more_than_one_limb):
277 cmpl $UNROLL_THRESHOLD, %ecx
283 C -----------------------------------------------------------------------------
284 C Simple addmul loop.
286 C Using ebx and edi pointing at the ends of their respective locations saves
287 C a couple of instructions in the outer loop. The inner loop is still 11
288 C cycles, the same as the simple loop in aorsmul_1.asm.
298 movl 4(%eax), %ebp C multiplier
301 movl %ecx, PARAM_XSIZE C -xsize
302 xorl %esi, %esi C initial carry
304 leal 4(%eax,%edx,4), %eax C yp end
308 movl %edx, PARAM_YSIZE
310 jmp L(simple_outer_entry)
313 C aligning here saves a couple of cycles
316 C edx ysize counter, negative
318 movl PARAM_YP, %eax C yp end
319 xorl %esi, %esi C carry
321 movl PARAM_XSIZE, %ecx C -xsize
322 movl %edx, PARAM_YSIZE
324 movl (%eax,%edx,4), %ebp C yp limb multiplier
325 L(simple_outer_entry):
332 C ecx counter, negative
335 C edi wp end of this addmul
338 movl (%ebx,%ecx,4), %eax
346 addl %eax, (%edi,%ecx,4)
353 movl PARAM_YSIZE, %edx
357 jnz L(simple_outer_top)
367 C -----------------------------------------------------------------------------
370 C The unrolled inner loop is the same as in aorsmul_1.asm, see that code for
373 C VAR_COUNTER is for the inner loop, running from VAR_COUNTER_INIT down to
376 C VAR_JMP is the computed jump into the unrolled loop.
378 C PARAM_XP and PARAM_WP get offset appropriately for where the unrolled loop
381 C VAR_XP_LOW is the least significant limb of xp, which is needed at the
382 C start of the unrolled loop. This can't just be fetched through the xp
383 C pointer because of the offset applied to it.
385 C PARAM_YSIZE is the outer loop counter, going from -(ysize-1) up to -1,
388 C PARAM_YP is offset appropriately so that the PARAM_YSIZE counter can be
389 C added to give the location of the next limb of yp, which is the multiplier
390 C in the unrolled loop.
392 C PARAM_WP is similarly offset so that the PARAM_YSIZE counter can be added
393 C to give the starting point in the destination for each unrolled loop (this
394 C point is one limb upwards for each limb of yp processed).
396 C Having PARAM_YSIZE count negative to zero means it's not necessary to
397 C store new values of PARAM_YP and PARAM_WP on each loop. Those values on
398 C the stack remain constant and on each loop an leal adjusts them with the
399 C PARAM_YSIZE counter value.
402 defframe(VAR_COUNTER, -20)
403 defframe(VAR_COUNTER_INIT, -24)
404 defframe(VAR_JMP, -28)
405 defframe(VAR_XP_LOW, -32)
406 deflit(VAR_STACK_SPACE, 16)
408 dnl For some strange reason using (%esp) instead of 0(%esp) is a touch
409 dnl slower in this code, hence the defframe empty-if-zero feature is
412 dnl If VAR_COUNTER is at (%esp), the effect is worse. In this case the
413 dnl unrolled loop is 255 instead of 256 bytes, but quite how this affects
414 dnl anything isn't clear.
416 define(`defframe_empty_if_zero_disabled',1)
420 C ebx xp end (not used)
424 C edi wp end of mul1 (not used)
428 leal -2(%ecx), %ebp C one limb processed at start,
429 decl %ecx C and ebp is one less
431 shrl $UNROLL_LOG2, %ebp
434 subl $VAR_STACK_SPACE, %esp
435 deflit(`FRAME', 16+VAR_STACK_SPACE)
436 andl $UNROLL_MASK, %ecx
441 movl %ebp, VAR_COUNTER_INIT
444 C 15 code bytes per limb
449 leal L(unroll_entry) (%ecx,%esi,1), %ecx
453 movl %ebp, VAR_COUNTER
459 leal 4(%edi,%esi,4), %edi C wp adjust for unrolling and mul1
461 leal (%ebx,%esi,4), %ebx C xp adjust for unrolling
463 movl %eax, VAR_XP_LOW
468 leal (%edi,%edx,4), %ecx C wp adjust for ysize indexing
469 movl 4(%ebx), %ebp C multiplier (yp second limb)
471 leal 4(%ebx,%edx,4), %ebx C yp adjust for ysize indexing
475 leal 1(%esi), %ecx C adjust parity for decl %ecx above
480 movl %edx, PARAM_YSIZE
481 jmp L(unroll_outer_entry)
486 C See mpn/x86/README about old gas bugs
487 leal (%ecx,%esi,1), %ecx
488 addl $L(unroll_entry)-L(unroll_here), %ecx
494 C -----------------------------------------------------------------------------
495 C Aligning here saves a couple of cycles per loop. Using 32 doesn't
496 C cost any extra space, since the inner unrolled loop below is
503 movl %edx, PARAM_YSIZE C incremented ysize counter
507 movl VAR_COUNTER_INIT, %ebx
508 movl (%eax,%edx,4), %ebp C next multiplier
510 movl PARAM_XSIZE, %ecx
511 leal (%edi,%edx,4), %edi C adjust wp for where we are in yp
513 movl VAR_XP_LOW, %eax
514 movl %ebx, VAR_COUNTER
516 L(unroll_outer_entry):
519 C using testb is a tiny bit faster than testl
522 movl %eax, %ecx C low carry
525 movl %edx, %esi C high carry
529 movl %ecx, %esi C high,low carry other way around
538 C -----------------------------------------------------------------------------
548 C VAR_COUNTER loop counter
550 C 15 code bytes each limb
552 leal UNROLL_BYTES(%edi), %edi
555 deflit(CHUNK_COUNT,2)
556 forloop(`i', 0, UNROLL_COUNT/CHUNK_COUNT-1, `
557 deflit(`disp0', eval(i*CHUNK_COUNT*4))
558 deflit(`disp1', eval(disp0 + 4))
559 deflit(`disp2', eval(disp1 + 4))
561 movl disp1(%ebx), %eax
563 Zdisp( addl, %ecx, disp0,(%edi))
568 movl disp2(%ebx), %eax
570 addl %esi, disp1(%edi)
577 leal UNROLL_BYTES(%ebx), %ebx
582 movl PARAM_YSIZE, %edx
583 addl %ecx, UNROLL_BYTES(%edi)
588 movl %esi, UNROLL_BYTES+4(%edi)
590 jnz L(unroll_outer_top)