[BACK]Return to diveby3.asm CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpn / x86 / pentium

Annotation of OpenXM_contrib/gmp/mpn/x86/pentium/diveby3.asm, Revision 1.1.1.2

1.1       maekawa     1: dnl  Intel P5 mpn_divexact_by3 -- mpn division by 3, expecting no remainder.
                      2:
1.1.1.2 ! ohara       3: dnl  Copyright 2000, 2002 Free Software Foundation, Inc.
1.1       maekawa     4: dnl
                      5: dnl  This file is part of the GNU MP Library.
                      6: dnl
                      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 2.1 of the
                     10: dnl  License, or (at your option) any later version.
                     11: dnl
                     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.
                     16: dnl
                     17: dnl  You should have received a copy of the GNU Lesser General Public
                     18: dnl  License along with the GNU MP Library; see the file COPYING.LIB.  If
                     19: dnl  not, write to the Free Software Foundation, Inc., 59 Temple Place -
                     20: dnl  Suite 330, Boston, MA 02111-1307, USA.
                     21:
                     22: include(`../config.m4')
                     23:
                     24:
1.1.1.2 ! ohara      25: C P5: 15.0 cycles/limb
        !            26:
        !            27:
1.1       maekawa    28: C mp_limb_t mpn_divexact_by3c (mp_ptr dst, mp_srcptr src, mp_size_t size,
                     29: C                              mp_limb_t carry);
                     30:
                     31: defframe(PARAM_CARRY,16)
                     32: defframe(PARAM_SIZE, 12)
                     33: defframe(PARAM_SRC,   8)
                     34: defframe(PARAM_DST,   4)
                     35:
                     36: dnl  multiplicative inverse of 3, modulo 2^32
                     37: deflit(INVERSE_3,        0xAAAAAAAB)
                     38:
                     39: dnl  ceil(b/3), ceil(b*2/3) and floor(b*2/3) where b=2^32
                     40: deflit(ONE_THIRD_CEIL,   0x55555556)
                     41: deflit(TWO_THIRDS_CEIL,  0xAAAAAAAB)
                     42: deflit(TWO_THIRDS_FLOOR, 0xAAAAAAAA)
                     43:
1.1.1.2 ! ohara      44:        TEXT
1.1       maekawa    45:        ALIGN(8)
                     46:
                     47: PROLOGUE(mpn_divexact_by3c)
                     48: deflit(`FRAME',0)
                     49:
                     50:        movl    PARAM_SRC, %ecx
                     51:        movl    PARAM_SIZE, %edx
                     52:
                     53:        decl    %edx
                     54:        jnz     L(two_or_more)
                     55:
                     56:        movl    (%ecx), %edx
                     57:        movl    PARAM_CARRY, %eax       C risk of cache bank clash here
                     58:
                     59:        movl    PARAM_DST, %ecx
                     60:        subl    %eax, %edx
                     61:
                     62:        sbbl    %eax, %eax              C 0 or -1
                     63:
                     64:        imull   $INVERSE_3, %edx, %edx
                     65:
                     66:        negl    %eax                    C 0 or 1
                     67:        cmpl    $ONE_THIRD_CEIL, %edx
                     68:
                     69:        sbbl    $-1, %eax               C +1 if edx>=ceil(b/3)
                     70:        cmpl    $TWO_THIRDS_CEIL, %edx
                     71:
                     72:        sbbl    $-1, %eax               C +1 if edx>=ceil(b*2/3)
                     73:        movl    %edx, (%ecx)
                     74:
                     75:        ret
                     76:
                     77:
                     78: L(two_or_more):
                     79:        C eax
                     80:        C ebx
                     81:        C ecx   src
                     82:        C edx   size-1
                     83:        C esi
                     84:        C edi
                     85:        C ebp
                     86:
                     87:        pushl   %ebx    FRAME_pushl()
                     88:        pushl   %esi    FRAME_pushl()
                     89:
                     90:        pushl   %edi    FRAME_pushl()
                     91:        pushl   %ebp    FRAME_pushl()
                     92:
                     93:        movl    PARAM_DST, %edi
                     94:        movl    PARAM_CARRY, %esi
                     95:
                     96:        movl    (%ecx), %eax            C src low limb
                     97:        xorl    %ebx, %ebx
                     98:
                     99:        sub     %esi, %eax
                    100:        movl    $TWO_THIRDS_FLOOR, %esi
                    101:
                    102:        leal    (%ecx,%edx,4), %ecx     C &src[size-1]
                    103:        leal    (%edi,%edx,4), %edi     C &dst[size-1]
                    104:
                    105:        adcl    $0, %ebx                C carry, 0 or 1
                    106:        negl    %edx                    C -(size-1)
                    107:
                    108:
                    109: C The loop needs a source limb ready at the top, which leads to one limb
                    110: C handled separately at the end, and the special case above for size==1.
                    111: C There doesn't seem to be any scheduling that would keep the speed but move
                    112: C the source load and carry subtract up to the top.
                    113: C
                    114: C The destination cache line prefetching adds 1 cycle to the loop but is
                    115: C considered worthwhile.  The slowdown is a factor of 1.07, but will prevent
                    116: C repeated write-throughs if the destination isn't in L1.  A version using
                    117: C an outer loop to prefetch only every 8 limbs (a cache line) proved to be
                    118: C no faster, due to unavoidable branch mispreditions in the inner loop.
                    119: C
                    120: C setc is 2 cycles on P54, so an adcl is used instead.  If the movl $0,%ebx
                    121: C could be avoided then the src limb fetch could pair up and save a cycle.
                    122: C This would probably mean going to a two limb loop with the carry limb
                    123: C alternately positive or negative, since an sbbl %ebx,%ebx will leave a
                    124: C value which is in the opposite sense to the preceding sbbl/adcl %ebx,%eax.
                    125: C
                    126: C A register is used for TWO_THIRDS_FLOOR because a cmp can't be done as
                    127: C "cmpl %edx, $n" with the immediate as the second operand.
                    128: C
                    129: C The "4" source displacement is in the loop rather than the setup because
                    130: C this gets L(top) aligned to 8 bytes at no cost.
                    131:
                    132:        ALIGN(8)
                    133: L(top):
                    134:        C eax   source limb, carry subtracted
                    135:        C ebx   carry (0 or 1)
                    136:        C ecx   &src[size-1]
                    137:        C edx   counter, limbs, negative
                    138:        C esi   TWO_THIRDS_FLOOR
                    139:        C edi   &dst[size-1]
                    140:        C ebp   scratch (result limb)
                    141:
                    142:        imull   $INVERSE_3, %eax, %ebp
                    143:
                    144:        cmpl    $ONE_THIRD_CEIL, %ebp
                    145:        movl    (%edi,%edx,4), %eax     C dst cache line prefetch
                    146:
                    147:        sbbl    $-1, %ebx               C +1 if ebp>=ceil(b/3)
                    148:        cmpl    %ebp, %esi
                    149:
                    150:        movl    4(%ecx,%edx,4), %eax    C next src limb
                    151:
                    152:        sbbl    %ebx, %eax              C and further -1 if ebp>=ceil(b*2/3)
                    153:        movl    $0, %ebx
                    154:
                    155:        adcl    $0, %ebx                C new carry
                    156:        movl    %ebp, (%edi,%edx,4)
                    157:
                    158:        incl    %edx
                    159:        jnz     L(top)
                    160:
                    161:
                    162:
                    163:        imull   $INVERSE_3, %eax, %edx
                    164:
                    165:        cmpl    $ONE_THIRD_CEIL, %edx
                    166:        movl    %edx, (%edi)
                    167:
                    168:        sbbl    $-1, %ebx       C +1 if edx>=ceil(b/3)
                    169:        cmpl    $TWO_THIRDS_CEIL, %edx
                    170:
                    171:        sbbl    $-1, %ebx       C +1 if edx>=ceil(b*2/3)
                    172:        popl    %ebp
                    173:
                    174:        movl    %ebx, %eax
                    175:        popl    %edi
                    176:
                    177:        popl    %esi
                    178:        popl    %ebx
                    179:
                    180:        ret
                    181:
                    182: EPILOGUE()

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>