1 dnl AMD64 mpn_modexact_1_odd -- exact division style remainder.
3 dnl Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
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')
33 C mp_limb_t mpn_modexact_1_odd (mp_srcptr src, mp_size_t size,
35 C mp_limb_t mpn_modexact_1c_odd (mp_srcptr src, mp_size_t size,
36 C mp_limb_t divisor, mp_limb_t carry);
39 C The dependent chain in the main loop is
48 C The movq load from src seems to need to be scheduled back before the jz to
49 C achieve this speed, out-of-order execution apparently can't completely
50 C hide the latency otherwise.
52 C The l=src[i]-cbit step is rotated back too, since that allows us to avoid
53 C it for the first iteration (where there's no cbit).
55 C The code alignment used (32-byte) for the loop also seems necessary.
56 C Without that the non-PIC case has adcq crossing the 0x60 offset,
57 C apparently making it run at 11 cycles instead of 10.
61 C divq for size==1 was measured at about 79 cycles, compared to the inverse
62 C at about 25 cycles (both including function call overheads), so that's not
67 C For PIC, we shouldn't really need the GOT fetch for binvert_limb_table,
68 C it'll be in rodata or text in libgmp.so and can be accessed directly %rip
69 C relative. This would be for small model only (something we don't
70 C presently detect, but which is all that gcc 3.3.3 supports), since 8-byte
71 C PC-relative relocations are apparently not available. Some rough
72 C experiments with binutils 2.13 looked worrylingly like it might come out
73 C with an unwanted text segment relocation though, even with ".protected".
79 PROLOGUE(mpn_modexact_1_odd)
83 PROLOGUE(mpn_modexact_1c_odd)
93 movq binvert_limb_table@GOTPCREL(%rip), %r9
95 movabsq $binvert_limb_table, %r9
99 movq %rcx, %r10 C initial carry
101 movzbl (%r9,%rdx), %edx C inv 8 bits
103 movq (%rdi), %rax C src[0]
104 leaq (%rdi,%rsi,8), %r11 C src end
105 movq %r8, %rdi C d, made available to imull
107 leal (%rdx,%rdx), %ecx C 2*inv
108 imull %edx, %edx C inv*inv
112 imull %edi, %edx C inv*inv*d
114 subl %edx, %ecx C inv = 2*inv - inv*inv*d, 16 bits
116 leal (%rcx,%rcx), %edx C 2*inv
117 imull %ecx, %ecx C inv*inv
119 imull %edi, %ecx C inv*inv*d
121 subl %ecx, %edx C inv = 2*inv - inv*inv*d, 32 bits
122 xorl %ecx, %ecx C initial cbit
124 leaq (%rdx,%rdx), %r9 C 2*inv
125 imulq %rdx, %rdx C inv*inv
127 imulq %r8, %rdx C inv*inv*d
129 subq %rdx, %r9 C inv = 2*inv - inv*inv*d, 64 bits
130 movq %r10, %rdx C initial climb
132 ASSERT(e,` C d*inv == 1 mod 2^64
143 C rax l = src[i]-cbit
144 C rcx new cbit, 0 or 1
145 C rdx climb, high of last product
146 C rsi counter, limbs, negative
152 subq %rdx, %rax C l = src[i]-cbit - climb
154 adcq $0, %rcx C more cbit
155 imulq %r9, %rax C q = l * inverse
157 mulq %r8 C climb = high (q * d)
159 movq (%r11,%rsi,8), %rax C src[i+1]
160 subq %rcx, %rax C next l = src[i+1] - cbit
168 subq %rdx, %rax C l = src[i]-cbit - climb
170 adcq $0, %rcx C more cbit
171 imulq %r9, %rax C q = l * inverse
173 mulq %r8 C climb = high (q * d)
175 leaq (%rcx,%rdx), %rax C climb+cbit
178 EPILOGUE(mpn_modexact_1c_odd)
179 EPILOGUE(mpn_modexact_1_odd)