1 dnl AMD64 mpn_gcd_1 -- mpn by 1 gcd.
3 dnl Based on the K7 gcd_1.asm, by Kevin Ryde. Rehacked for AMD64 by Torbjorn
6 dnl Copyright 2000, 2001, 2002, 2005, 2009 Free Software Foundation, Inc.
8 dnl This file is part of the GNU MP Library.
10 dnl The GNU MP Library is free software; you can redistribute it and/or modify
11 dnl it under the terms of the GNU Lesser General Public License as published
12 dnl by the Free Software Foundation; either version 3 of the License, or (at
13 dnl your option) any later version.
15 dnl The GNU MP Library is distributed in the hope that it will be useful, but
16 dnl WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 dnl or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 dnl License for more details.
20 dnl You should have received a copy of the GNU Lesser General Public License
21 dnl along with the GNU MP Library. If not, see http://www.gnu.org/licenses/.
23 include(`../config.m4')
26 C K8: 6.75 cycles/bit (approx) 1x1 gcd
27 C 10.0 cycles/limb Nx1 reduction (modexact_1_odd)
30 dnl Reduce using x%y if x is more than DIV_THRESHOLD bits bigger than y,
31 dnl where x is the larger of the two. See tune/README for more.
33 dnl div at 80 cycles compared to the gcd at about 7 cycles/bitpair
34 dnl suggests 80/7*2=23
36 deflit(DIV_THRESHOLD, 23)
39 C ctz_table[n] is the number of trailing zeros on n, or MAXSHIFT if n==0.
43 deflit(MASK, eval((1<<MAXSHIFT)-1))
45 DEF_OBJECT(ctz_table,64)
48 ` .byte m4_count_trailing_zeros(i)
52 C mp_limb_t mpn_gcd_1 (mp_srcptr up, mp_size_t n, mp_limb_t vlimb);
58 define(`vlimb', `%rdx')
64 mov (%rdi), %r8 C src low limb
74 mov R32(%rcx), R32(%r8) C common twos
83 sub $8, %rsp C maintain ABI required rsp alignment
85 CALL( mpn_modexact_1_odd)
105 cmovc %r10, %rcx C if x-y gave carry, use x,y-x 0
110 and $MASK, R32(%rcx) C 1
112 mov (%r9,%rcx), R8(%rcx) C 1
114 shr R8(%rcx), %rax C 4
115 cmp $MAXSHIFT, R8(%rcx) C 4
119 je L(strip_x_top) C 5