1 dnl AMD K7 mpn_sqr_basecase -- square an mpn number.
3 dnl Copyright 1999, 2000, 2001, 2002 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 K7: approx 2.3 cycles/crossproduct, or 4.55 cycles/triangular product
24 C (measured on the speed difference between 25 and 50 limbs, which is
25 C roughly the Karatsuba recursing range).
28 dnl These are the same as mpn/x86/k6/sqr_basecase.asm, see that code for
31 deflit(SQR_TOOM2_THRESHOLD_MAX, 66)
33 ifdef(`SQR_TOOM2_THRESHOLD_OVERRIDE',
34 `define(`SQR_TOOM2_THRESHOLD',SQR_TOOM2_THRESHOLD_OVERRIDE)')
36 m4_config_gmp_mparam(`SQR_TOOM2_THRESHOLD')
37 deflit(UNROLL_COUNT, eval(SQR_TOOM2_THRESHOLD-3))
40 C void mpn_sqr_basecase (mp_ptr dst, mp_srcptr src, mp_size_t size);
42 C With a SQR_TOOM2_THRESHOLD around 50 this code is about 1500 bytes,
43 C which is quite a bit, but is considered good value since squares big
44 C enough to use most of the code will be spending quite a few cycles in it.
47 defframe(PARAM_SIZE,12)
48 defframe(PARAM_SRC, 8)
49 defframe(PARAM_DST, 4)
53 PROLOGUE(mpn_sqr_basecase)
65 C------------------------------------------------------------------------------
81 C------------------------------------------------------------------------------
83 C Using the read/modify/write "add"s seems to be faster than saving and
84 C restoring registers. Perhaps the loads for the first set hide under the
85 C mul latency and the second gets store to load forwarding.
95 pushl %ebx FRAME_pushl()
103 movl %eax, (%ecx) C dst[0]
106 movl %edx, 4(%ecx) C dst[1]
110 movl %eax, 8(%ecx) C dst[2]
113 movl %edx, 12(%ecx) C dst[3]
115 mull 4(%ebx) C src[0]*src[1]
132 C------------------------------------------------------------------------------
133 defframe(SAVE_EBX, -4)
134 defframe(SAVE_ESI, -8)
135 defframe(SAVE_EDI, -12)
136 defframe(SAVE_EBP, -16)
137 deflit(STACK_SPACE, 16)
140 subl $STACK_SPACE, %esp
143 deflit(`FRAME',STACK_SPACE)
146 C------------------------------------------------------------------------------
149 C Writing out the loads and stores separately at the end of this code comes
150 C out about 10 cycles faster than using adcls to memory.
157 movl %eax, %ebx C src
160 movl %edx, %ecx C dst
164 mull %eax C src[0] ^ 2
170 mull %eax C src[1] ^ 2
176 mull %eax C src[2] ^ 2
182 mull 4(%ebx) C src[0] * src[1]
188 mull 8(%ebx) C src[0] * src[2]
197 mull 8(%ebx) C src[1] * src[2]
205 C ebx zero, will be dst[5]
253 C------------------------------------------------------------------------------
256 C First multiply src[0]*src[1..size-1] and store at dst[1..size].
257 C Further products are added in rather than stored.
267 defframe(`VAR_COUNTER',-20)
268 defframe(`VAR_JMP', -24)
269 deflit(EXTRA_STACK_SPACE, 8)
273 leal (%edx,%ecx,4), %edi C &dst[size]
277 leal (%eax,%ecx,4), %esi C &src[size]
279 movl (%eax), %ebp C multiplier
284 subl $EXTRA_STACK_SPACE, %esp
285 FRAME_subl_esp(EXTRA_STACK_SPACE)
296 movl (%esi,%ecx,4), %eax
301 movl %eax, (%edi,%ecx,4)
309 C Add products src[n]*src[n+1..size-1] at dst[2*n-1...], for each n=1..size-2.
311 C The last two products, which are the bottom right corner of the product
312 C triangle, are left to the end. These are src[size-3]*src[size-2,size-1]
313 C and src[size-2]*src[size-1]. If size is 4 then it's only these corner
314 C cases that need to be done.
316 C The unrolled code is the same as in mpn_addmul_1, see that routine for
319 C VAR_COUNTER is the outer loop, running from -size+4 to -1, inclusive.
321 C VAR_JMP is the computed jump into the unrolled code, stepped by one code
322 C chunk each outer loop.
324 C K7 does branch prediction on indirect jumps, which is bad since it's a
325 C different target each time. There seems no way to avoid this.
327 dnl This value also hard coded in some shifts and adds
328 deflit(CODE_BYTES_PER_LIMB, 17)
330 dnl With the unmodified &src[size] and &dst[size] pointers, the
331 dnl displacements in the unrolled code fit in a byte for UNROLL_COUNT
332 dnl values up to 31, but above that an offset must be added to them.
335 ifelse(eval(UNROLL_COUNT>31),1,
336 eval((UNROLL_COUNT-31)*4),
339 dnl Because the last chunk of code is generated differently, a label placed
340 dnl at the end doesn't work. Instead calculate the implied end using the
341 dnl start and how many chunks of code there are.
343 deflit(UNROLL_INNER_END,
344 `L(unroll_inner_start)+eval(UNROLL_COUNT*CODE_BYTES_PER_LIMB)')
354 movl PARAM_SIZE, %ecx
361 ifelse(OFFSET,0,,`subl $OFFSET, %edi')
362 ifelse(OFFSET,0,,`subl $OFFSET, %esi')
371 leal UNROLL_INNER_END-eval(2*CODE_BYTES_PER_LIMB)(%ecx,%edx), %ecx
375 C The calculated jump mustn't come out to before the start of the
376 C code available. This is the limit UNROLL_COUNT puts on the src
377 C operand size, but checked here directly using the jump address.
379 `movl_text_address(L(unroll_inner_start), %eax)
383 C------------------------------------------------------------------------------
387 C ebx high limb to store
389 C edx VAR_COUNTER, limbs, negative
390 C esi &src[size], constant
391 C edi dst ptr, high of last addmul
394 movl -12+OFFSET(%esi,%edx,4), %ebp C next multiplier
395 movl -8+OFFSET(%esi,%edx,4), %eax C first of multiplicand
397 movl %edx, VAR_COUNTER
401 define(cmovX,`ifelse(eval(UNROLL_COUNT%2),0,`cmovz($@)',`cmovnz($@)')')
404 movl %edx, %ebx C high carry
405 movl %ecx, %edx C jump
407 movl %eax, %ecx C low carry
408 cmovX( %ebx, %ecx) C high carry reverse
409 cmovX( %eax, %ebx) C low carry reverse
411 leal CODE_BYTES_PER_LIMB(%edx), %eax
423 addl $UNROLL_INNER_END-eval(2*CODE_BYTES_PER_LIMB)-L(here), %ecx
429 C Must be an even address to preserve the significance of the low
430 C bit of the jump address indicating which way around ecx/ebx should
434 L(unroll_inner_start):
443 forloop(`i', UNROLL_COUNT, 1, `
444 deflit(`disp_src', eval(-i*4 + OFFSET))
445 deflit(`disp_dst', eval(disp_src - 4))
447 m4_assert(`disp_src>=-128 && disp_src<128')
448 m4_assert(`disp_dst>=-128 && disp_dst<128')
451 Zdisp( movl, disp_src,(%esi), %eax)
456 Zdisp( addl, %ecx, disp_dst,(%edi))
462 dnl this bit comes out last
463 Zdisp( movl, disp_src,(%esi), %eax)
468 Zdisp( addl, %ebx, disp_dst,(%edi))
470 ifelse(forloop_last,0,
486 addl %ecx, -4+OFFSET(%edi)
491 movl %edx, m4_empty_if_zero(OFFSET) (%edi)
492 movl VAR_COUNTER, %edx
495 jnz L(unroll_outer_top)
504 C------------------------------------------------------------------------------
543 C Left shift of dst[1..2*size-2], high bit shifted out becomes dst[2*size-1].
546 movl PARAM_SIZE, %eax
548 xorl %ecx, %ecx C clear carry
550 leal (%edi,%eax,8), %edi
551 notl %eax C -size-1, preserve carry
553 leal 2(%eax), %eax C -(size-1)
556 C eax counter, negative
561 C edi dst, pointing just after last limb
572 movl %eax, -4(%edi) C dst most significant limb
574 movl PARAM_SIZE, %ecx
577 C Now add in the squares on the diagonal, src[0]^2, src[1]^2, ...,
578 C src[size-1]^2. dst[0] hasn't yet been set at all yet, and just gets the
579 C low limb of src[0]^2.
581 movl (%esi), %eax C src[0]
585 leal (%esi,%ecx,4), %esi C src point just after last limb
588 movl %eax, (%edi,%ecx,8) C dst[0]
594 C ecx counter, negative
596 C esi src just after last limb
597 C edi dst just after last limb
600 movl (%esi,%ecx,4), %eax
605 addl %ebx, -4(%edi,%ecx,8)
606 adcl %eax, (%edi,%ecx,8)
616 addl %edx, -4(%edi) C dst most significant limb