dnl AMD K7 mpn_mod_1 -- mpn by limb remainder. dnl Copyright 1999, 2000, 2001, 2002 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') C K7: 17.0 cycles/limb. C mp_limb_t mpn_mod_1 (mp_srcptr src, mp_size_t size, mp_limb_t divisor); C mp_limb_t mpn_mod_1c (mp_srcptr src, mp_size_t size, mp_limb_t divisor, C mp_limb_t carry); C mp_limb_t mpn_preinv_mod_1 (mp_srcptr src, mp_size_t size, mp_limb_t divisor, C mp_limb_t inverse); C C The code here is the same as mpn_divrem_1, but with the quotient C discarded. See mpn/x86/k7/mmx/divrem_1.c for some comments. dnl MUL_THRESHOLD is the size at which the multiply by inverse method is dnl used, rather than plain "divl"s. Minimum value 2. dnl dnl The inverse takes about 50 cycles to calculate, but after that the dnl multiply is 17 c/l versus division at 41 c/l. dnl dnl Using mul or div is about the same speed at 3 limbs, so the threshold dnl is set to 4 to get the smaller div code used at 3. deflit(MUL_THRESHOLD, 4) defframe(PARAM_INVERSE,16) dnl mpn_preinv_mod_1 defframe(PARAM_CARRY, 16) dnl mpn_mod_1c defframe(PARAM_DIVISOR,12) defframe(PARAM_SIZE, 8) defframe(PARAM_SRC, 4) defframe(SAVE_EBX, -4) defframe(SAVE_ESI, -8) defframe(SAVE_EDI, -12) defframe(SAVE_EBP, -16) defframe(VAR_NORM, -20) defframe(VAR_INVERSE, -24) defframe(VAR_SRC_STOP,-28) deflit(STACK_SPACE, 28) TEXT ALIGN(32) PROLOGUE(mpn_preinv_mod_1) deflit(`FRAME',0) movl PARAM_SRC, %ecx movl PARAM_SIZE, %eax subl $STACK_SPACE, %esp FRAME_subl_esp(STACK_SPACE) movl %ebp, SAVE_EBP movl PARAM_DIVISOR, %ebp movl %edi, SAVE_EDI movl PARAM_INVERSE, %edx movl %esi, SAVE_ESI movl -4(%ecx,%eax,4), %edi C src high limb leal -16(%ecx,%eax,4), %ecx C &src[size-4] movl %ebx, SAVE_EBX movl PARAM_INVERSE, %edx movl $0, VAR_NORM C l==0 movl %edi, %esi subl %ebp, %edi C high-divisor cmovc( %esi, %edi) C restore if underflow decl %eax jz L(done_edi) C size==1, high-divisor only movl 8(%ecx), %esi C src second high limb movl %edx, VAR_INVERSE movl $32, %ebx C 32-l decl %eax jz L(inverse_one_left) C size==2, one divide movd %ebx, %mm7 C 32-l decl %eax jz L(inverse_two_left) C size==3, two divides jmp L(inverse_top) C size>=4 L(done_edi): movl SAVE_ESI, %esi movl SAVE_EBP, %ebp movl %edi, %eax movl SAVE_EDI, %edi addl $STACK_SPACE, %esp ret EPILOGUE() ALIGN(32) PROLOGUE(mpn_mod_1c) deflit(`FRAME',0) movl PARAM_CARRY, %edx movl PARAM_SIZE, %ecx subl $STACK_SPACE, %esp deflit(`FRAME',STACK_SPACE) movl %ebp, SAVE_EBP movl PARAM_DIVISOR, %ebp movl %esi, SAVE_ESI movl PARAM_SRC, %esi jmp L(start_1c) EPILOGUE() ALIGN(32) PROLOGUE(mpn_mod_1) deflit(`FRAME',0) movl PARAM_SIZE, %ecx movl $0, %edx C initial carry (if can't skip a div) subl $STACK_SPACE, %esp deflit(`FRAME',STACK_SPACE) movl %esi, SAVE_ESI movl PARAM_SRC, %esi movl %ebp, SAVE_EBP movl PARAM_DIVISOR, %ebp orl %ecx, %ecx jz L(divide_done) movl -4(%esi,%ecx,4), %eax C src high limb cmpl %ebp, %eax C carry flag if high n2 leal (%ebp,%esi), %edx movd %mm0, %esi cmovc( %edx, %edi) C n - q1*d if underflow from using q1+1 cmpl %eax, %ecx jae L(inverse_top) L(inverse_loop_done): C ----------------------------------------------------------------------------- L(inverse_two_left): C eax scratch C ebx scratch (nadj, q1) C ecx &src[-1] C edx scratch C esi n10 C edi n2 C ebp divisor C C mm0 scratch (src dword) C mm7 rshift cmpl $0x80000000, %esi C n1 as 0=c, 1=nc movl %edi, %eax C n2 leal (%ebp,%esi), %ebx cmovc( %esi, %ebx) C nadj = n10 + (-n1 & d), ignoring overflow sbbl $-1, %eax C n2+n1 mull VAR_INVERSE C m*(n2+n1) movd 4(%ecx), %mm0 C src low limb C C addl %ebx, %eax C m*(n2+n1) + nadj, low giving carry flag leal 1(%edi), %ebx C n2+1 movl %ebp, %eax C d adcl %edx, %ebx C 1 + high(n2<<32 + m*(n2+n1) + nadj) = q1+1 sbbl $0, %ebx mull %ebx C (q1+1)*d psllq $32, %mm0 psrlq %mm7, %mm0 C subl %eax, %esi C sbbl %edx, %edi C n - (q1+1)*d movl %esi, %edi C remainder -> n2 leal (%ebp,%esi), %edx movd %mm0, %esi cmovc( %edx, %edi) C n - q1*d if underflow from using q1+1 L(inverse_one_left): C eax scratch C ebx scratch (nadj, q1) C ecx C edx scratch C esi n10 C edi n2 C ebp divisor C C mm0 src limb, shifted C mm7 rshift cmpl $0x80000000, %esi C n1 as 0=c, 1=nc movl %edi, %eax C n2 leal (%ebp,%esi), %ebx cmovc( %esi, %ebx) C nadj = n10 + (-n1 & d), ignoring overflow sbbl $-1, %eax C n2+n1 mull VAR_INVERSE C m*(n2+n1) movl VAR_NORM, %ecx C for final denorm C C addl %ebx, %eax C m*(n2+n1) + nadj, low giving carry flag leal 1(%edi), %ebx C n2+1 movl %ebp, %eax C d C adcl %edx, %ebx C 1 + high(n2<<32 + m*(n2+n1) + nadj) = q1+1 sbbl $0, %ebx mull %ebx C (q1+1)*d movl SAVE_EBX, %ebx C C subl %eax, %esi movl %esi, %eax C remainder movl SAVE_ESI, %esi sbbl %edx, %edi C n - (q1+1)*d leal (%ebp,%eax), %edx movl SAVE_EBP, %ebp cmovc( %edx, %eax) C n - q1*d if underflow from using q1+1 movl SAVE_EDI, %edi shrl %cl, %eax C denorm remainder addl $STACK_SPACE, %esp emms ret C ----------------------------------------------------------------------------- C C Special case for q1=0xFFFFFFFF, giving q=0xFFFFFFFF meaning the low dword C of q*d is simply -d and the remainder n-q*d = n10+d L(q1_ff): C eax (divisor) C ebx (q1+1 == 0) C ecx src pointer C edx C esi n10 C edi (n2) C ebp divisor movl PARAM_SRC, %edx leal (%ebp,%esi), %edi C n-q*d remainder -> next n2 psrlq %mm7, %mm0 movd %mm0, %esi C next n10 cmpl %edx, %ecx jae L(inverse_top) jmp L(inverse_loop_done) EPILOGUE()