1 dnl AMD K6 mpn_divrem_1 -- mpn by limb division.
3 dnl Copyright 1999, 2000, 2001, 2002, 2003, 2007 Free Software Foundation,
6 dnl This file is part of the GNU MP Library.
8 dnl The GNU MP Library is free software; you can redistribute it and/or
9 dnl modify it under the terms of the GNU Lesser General Public License as
10 dnl published by the Free Software Foundation; either version 3 of the
11 dnl License, or (at your option) any later version.
13 dnl The GNU MP Library is distributed in the hope that it will be useful,
14 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
15 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 dnl Lesser General Public License for more details.
18 dnl You should have received a copy of the GNU Lesser General Public License
19 dnl along with the GNU MP Library. If not, see http://www.gnu.org/licenses/.
21 include(`../config.m4')
27 C mp_limb_t mpn_divrem_1 (mp_ptr dst, mp_size_t xsize,
28 C mp_srcptr src, mp_size_t size, mp_limb_t divisor);
29 C mp_limb_t mpn_divrem_1c (mp_ptr dst, mp_size_t xsize,
30 C mp_srcptr src, mp_size_t size, mp_limb_t divisor,
33 C The code here is basically the same as mpn/x86/divrem_1.asm, but uses loop
34 C instead of decl+jnz, since it comes out 2 cycles/limb faster.
36 C A test is done to see if the high limb is less than the divisor, and if so
37 C one less div is done. A div is 20 cycles, so assuming high<divisor about
38 C half the time, then this test saves half that amount. The branch
39 C misprediction penalty is less than that.
41 C Back-to-back div instructions run at 20 cycles, the same as the loop here,
42 C so it seems there's nothing to gain by rearranging the loop. Pairing the
43 C mov and loop instructions was found to gain nothing.
47 C The low-latency K6 multiply might be thought to suit a mul-by-inverse, but
48 C that algorithm has been found to suffer from the relatively poor carry
49 C handling on K6 and too many auxiliary instructions. The fractional part
50 C however could be done at about 13 c/l, if it mattered enough.
52 defframe(PARAM_CARRY, 24)
53 defframe(PARAM_DIVISOR,20)
54 defframe(PARAM_SIZE, 16)
55 defframe(PARAM_SRC, 12)
56 defframe(PARAM_XSIZE, 8)
57 defframe(PARAM_DST, 4)
62 PROLOGUE(mpn_divrem_1c)
66 pushl %edi FRAME_pushl()
69 pushl %esi FRAME_pushl()
71 movl PARAM_DIVISOR, %esi
72 pushl %ebx FRAME_pushl()
75 pushl %ebp FRAME_pushl()
77 movl PARAM_XSIZE, %ebp
80 movl PARAM_CARRY, %edx
81 jz L(fraction) C if size==0
83 leal -4(%ebx,%ebp,4), %ebx C dst one limb below integer part
90 PROLOGUE(mpn_divrem_1)
94 pushl %edi FRAME_pushl()
97 pushl %esi FRAME_pushl()
99 movl PARAM_DIVISOR, %esi
103 pushl %ebx FRAME_pushl()
105 movl -4(%edi,%ecx,4), %eax C src high limb
109 pushl %ebp FRAME_pushl()
111 movl PARAM_XSIZE, %ebp
114 leal -4(%ebx,%ebp,4), %ebx C dst one limb below integer part
118 C high<divisor, so high of dst is zero, and avoid one div
120 movl %edx, (%ebx,%ecx,4)
128 C eax scratch (quotient)
131 C edx scratch (remainder)
136 movl -4(%edi,%ecx,4), %eax
141 movl %eax, (%ebx,%ecx,4)
153 C eax scratch (quotient)
156 C edx scratch (remainder)
165 movl %eax, -4(%ebx,%ecx,4)
180 movl PARAM_XSIZE, %ecx
185 cld C better safe than sorry, see mpn/x86/README