1 dnl AMD K6 mpn_mul_basecase -- multiply two mpn numbers.
3 dnl K6: approx 9.0 cycles per cross product on 30x30 limbs (with 16 limbs/loop
7 dnl Copyright (C) 1999, 2000 Free Software Foundation, Inc.
9 dnl This file is part of the GNU MP Library.
11 dnl The GNU MP Library is free software; you can redistribute it and/or
12 dnl modify it under the terms of the GNU Lesser General Public License as
13 dnl published by the Free Software Foundation; either version 2.1 of the
14 dnl License, or (at your option) any later version.
16 dnl The GNU MP Library is distributed in the hope that it will be useful,
17 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
18 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 dnl Lesser General Public License for more details.
21 dnl You should have received a copy of the GNU Lesser General Public
22 dnl License along with the GNU MP Library; see the file COPYING.LIB. If
23 dnl not, write to the Free Software Foundation, Inc., 59 Temple Place -
24 dnl Suite 330, Boston, MA 02111-1307, USA.
27 include(`../config.m4')
30 dnl K6: UNROLL_COUNT cycles/product (approx)
34 dnl Maximum possible with the current code is 32.
36 dnl With 16 the inner unrolled loop fits exactly in a 256 byte block, which
37 dnl might explain it's good performance.
39 deflit(UNROLL_COUNT, 16)
42 C void mpn_mul_basecase (mp_ptr wp,
43 C mp_srcptr xp, mp_size_t xsize,
44 C mp_srcptr yp, mp_size_t ysize);
46 C Calculate xp,xsize multiplied by yp,ysize, storing the result in
49 C This routine is essentially the same as mpn/generic/mul_basecase.c, but
50 C it's faster because it does most of the mpn_addmul_1() entry code only
51 C once. The saving is about 10-20% on typical sizes coming from the
52 C Karatsuba multiply code.
56 C The unrolled loop could be shared by mpn_addmul_1, with some extra stack
57 C setups and maybe 2 or 3 wasted cycles at the end. Code saving would be
61 deflit(UNROLL_THRESHOLD, 8)
63 deflit(UNROLL_THRESHOLD, 8)
66 defframe(PARAM_YSIZE,20)
67 defframe(PARAM_YP, 16)
68 defframe(PARAM_XSIZE,12)
74 PROLOGUE(mpn_mul_basecase)
77 movl PARAM_XSIZE, %ecx
81 movl (%eax), %eax C yp low limb
84 ja L(xsize_more_than_two_limbs)
85 je L(two_by_something)
88 C one limb by one limb
90 movl (%edx), %edx C xp low limb
100 C -----------------------------------------------------------------------------
110 movl %eax, %ecx C yp low limb
111 movl (%edx), %eax C xp low limb
117 C two limbs by one limb
124 movl %edx, %esi C carry
141 C -----------------------------------------------------------------------------
153 mull %ecx C xp[0] * yp[0]
160 movl %edx, %edi C carry, for wp[1]
162 mull %ecx C xp[1] * yp[0]
170 movl 4(%ecx), %ecx C yp[1]
172 movl 4(%esi), %eax C xp[1]
173 movl %edx, %edi C carry, for wp[2]
175 mull %ecx C xp[1] * yp[1]
181 movl (%esi), %eax C xp[0]
182 movl %edx, %esi C carry, for wp[3]
184 mull %ecx C xp[0] * yp[1]
200 C -----------------------------------------------------------------------------
202 L(xsize_more_than_two_limbs):
204 C The first limb of yp is processed with a simple mpn_mul_1 style loop
205 C inline. Unrolling this doesn't seem worthwhile since it's only run once
206 C (whereas the addmul below is run ysize-1 many times). A call to the
207 C actual mpn_mul_1 will be slowed down by the call and parameter pushing and
208 C popping, and doesn't seem likely to be worthwhile on the typical 10-20
209 C limb operations the Karatsuba code calls here with.
220 pushl %edi defframe_pushl(SAVE_EDI)
221 pushl %ebp defframe_pushl(SAVE_EBP)
224 pushl %esi defframe_pushl(SAVE_ESI)
227 pushl %ebx defframe_pushl(SAVE_EBX)
229 leal (%edx,%ecx,4), %ebx C xp end
232 leal (%edi,%ecx,4), %edi C wp end of mul1
239 C ecx counter, negative
245 movl (%ebx,%ecx,4), %eax
254 movl %eax, (%edi,%ecx,4)
260 movl PARAM_YSIZE, %edx
261 movl %esi, (%edi) C final carry
263 movl PARAM_XSIZE, %ecx
266 jnz L(ysize_more_than_one_limb)
275 L(ysize_more_than_one_limb):
276 cmpl $UNROLL_THRESHOLD, %ecx
282 C -----------------------------------------------------------------------------
283 C Simple addmul loop.
285 C Using ebx and edi pointing at the ends of their respective locations saves
286 C a couple of instructions in the outer loop. The inner loop is still 11
287 C cycles, the same as the simple loop in aorsmul_1.asm.
297 movl 4(%eax), %ebp C multiplier
300 movl %ecx, PARAM_XSIZE C -xsize
301 xorl %esi, %esi C initial carry
303 leal 4(%eax,%edx,4), %eax C yp end
307 movl %edx, PARAM_YSIZE
309 jmp L(simple_outer_entry)
312 C aligning here saves a couple of cycles
315 C edx ysize counter, negative
317 movl PARAM_YP, %eax C yp end
318 xorl %esi, %esi C carry
320 movl PARAM_XSIZE, %ecx C -xsize
321 movl %edx, PARAM_YSIZE
323 movl (%eax,%edx,4), %ebp C yp limb multiplier
324 L(simple_outer_entry):
331 C ecx counter, negative
334 C edi wp end of this addmul
337 movl (%ebx,%ecx,4), %eax
345 addl %eax, (%edi,%ecx,4)
352 movl PARAM_YSIZE, %edx
356 jnz L(simple_outer_top)
366 C -----------------------------------------------------------------------------
369 C The unrolled inner loop is the same as in aorsmul_1.asm, see that code for
372 C VAR_COUNTER is for the inner loop, running from VAR_COUNTER_INIT down to
375 C VAR_JMP is the computed jump into the unrolled loop.
377 C PARAM_XP and PARAM_WP get offset appropriately for where the unrolled loop
380 C VAR_XP_LOW is the least significant limb of xp, which is needed at the
381 C start of the unrolled loop. This can't just be fetched through the xp
382 C pointer because of the offset applied to it.
384 C PARAM_YSIZE is the outer loop counter, going from -(ysize-1) up to -1,
387 C PARAM_YP is offset appropriately so that the PARAM_YSIZE counter can be
388 C added to give the location of the next limb of yp, which is the multiplier
389 C in the unrolled loop.
391 C PARAM_WP is similarly offset so that the PARAM_YSIZE counter can be added
392 C to give the starting point in the destination for each unrolled loop (this
393 C point is one limb upwards for each limb of yp processed).
395 C Having PARAM_YSIZE count negative to zero means it's not necessary to
396 C store new values of PARAM_YP and PARAM_WP on each loop. Those values on
397 C the stack remain constant and on each loop an leal adjusts them with the
398 C PARAM_YSIZE counter value.
401 defframe(VAR_COUNTER, -20)
402 defframe(VAR_COUNTER_INIT, -24)
403 defframe(VAR_JMP, -28)
404 defframe(VAR_XP_LOW, -32)
405 deflit(VAR_STACK_SPACE, 16)
407 dnl For some strange reason using (%esp) instead of 0(%esp) is a touch
408 dnl slower in this code, hence the defframe empty-if-zero feature is
411 dnl If VAR_COUNTER is at (%esp), the effect is worse. In this case the
412 dnl unrolled loop is 255 instead of 256 bytes, but quite how this affects
413 dnl anything isn't clear.
415 define(`defframe_empty_if_zero_disabled',1)
419 C ebx xp end (not used)
423 C edi wp end of mul1 (not used)
427 leal -2(%ecx), %ebp C one limb processed at start,
428 decl %ecx C and ebp is one less
430 shrl $UNROLL_LOG2, %ebp
433 subl $VAR_STACK_SPACE, %esp
434 deflit(`FRAME', 16+VAR_STACK_SPACE)
435 andl $UNROLL_MASK, %ecx
440 movl %ebp, VAR_COUNTER_INIT
443 C 15 code bytes per limb
448 leal L(unroll_entry) (%ecx,%esi,1), %ecx
452 movl %ebp, VAR_COUNTER
458 leal 4(%edi,%esi,4), %edi C wp adjust for unrolling and mul1
460 leal (%ebx,%esi,4), %ebx C xp adjust for unrolling
462 movl %eax, VAR_XP_LOW
467 leal (%edi,%edx,4), %ecx C wp adjust for ysize indexing
468 movl 4(%ebx), %ebp C multiplier (yp second limb)
470 leal 4(%ebx,%edx,4), %ebx C yp adjust for ysize indexing
474 leal 1(%esi), %ecx C adjust parity for decl %ecx above
479 movl %edx, PARAM_YSIZE
480 jmp L(unroll_outer_entry)
485 C See README.family about old gas bugs
486 leal (%ecx,%esi,1), %ecx
487 addl $L(unroll_entry)-L(unroll_here), %ecx
493 C -----------------------------------------------------------------------------
494 C Aligning here saves a couple of cycles per loop. Using 32 doesn't
495 C cost any extra space, since the inner unrolled loop below is
502 movl %edx, PARAM_YSIZE C incremented ysize counter
506 movl VAR_COUNTER_INIT, %ebx
507 movl (%eax,%edx,4), %ebp C next multiplier
509 movl PARAM_XSIZE, %ecx
510 leal (%edi,%edx,4), %edi C adjust wp for where we are in yp
512 movl VAR_XP_LOW, %eax
513 movl %ebx, VAR_COUNTER
515 L(unroll_outer_entry):
518 C using testb is a tiny bit faster than testl
521 movl %eax, %ecx C low carry
524 movl %edx, %esi C high carry
528 movl %ecx, %esi C high,low carry other way around
537 C -----------------------------------------------------------------------------
547 C VAR_COUNTER loop counter
549 C 15 code bytes each limb
551 leal UNROLL_BYTES(%edi), %edi
554 deflit(CHUNK_COUNT,2)
555 forloop(`i', 0, UNROLL_COUNT/CHUNK_COUNT-1, `
556 deflit(`disp0', eval(i*CHUNK_COUNT*4))
557 deflit(`disp1', eval(disp0 + 4))
558 deflit(`disp2', eval(disp1 + 4))
560 movl disp1(%ebx), %eax
562 Zdisp( addl, %ecx, disp0,(%edi))
567 movl disp2(%ebx), %eax
569 addl %esi, disp1(%edi)
576 leal UNROLL_BYTES(%ebx), %ebx
581 movl PARAM_YSIZE, %edx
582 addl %ecx, UNROLL_BYTES(%edi)
587 movl %esi, UNROLL_BYTES+4(%edi)
589 jnz L(unroll_outer_top)