1 dnl AMD64 mpn_gcd_1 optimised for Intel C2
, NHM
, SBR
and AMD K10
, BD.
3 dnl Based on the K7 gcd_1.asm
, by Kevin Ryde. Rehacked for AMD64 by Torbjorn
6 dnl Copyright
2000-2002, 2005, 2009, 2011, 2012 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
either:
13 dnl
* the GNU Lesser General
Public License as published by the Free
14 dnl Software Foundation
; either version 3 of the License, or (at your
15 dnl option
) any later version.
19 dnl
* the GNU General
Public License as published by the Free Software
20 dnl Foundation
; either version 2 of the License, or (at your option) any
23 dnl
or both
in parallel
, as here.
25 dnl The GNU MP Library is distributed
in the hope that it will be useful
, but
26 dnl WITHOUT ANY WARRANTY
; without even the implied warranty of MERCHANTABILITY
27 dnl
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License
30 dnl You should have received copies of the GNU General
Public License
and the
31 dnl GNU Lesser General
Public License along with the GNU MP Library. If
not,
32 dnl see
https://www.gnu.
org/licenses
/.
34 include(`..
/config.m4
')
48 C Numbers measured with: speed -CD -s16-64 -t48 mpn_gcd_1
51 C * Optimise inner-loop for specific CPUs.
52 C * Use DIV for 1-by-1 reductions, at least for some CPUs.
54 C Threshold of when to call bmod when U is one limb. Should be about
55 C (time_in_cycles(bmod_1,1) + call_overhead) / (cycles/bit).
56 define(`BMOD_THRES_LOG2', 6)
66 IFDOS
(`define
(`STACK_ALLOC
', 40)')
67 IFSTD
(`define
(`STACK_ALLOC
', 8)')
69 C Undo some configure cleverness.
70 C The problem is that C only defines the
'1c' variant
, and that configure
71 C therefore considers modexact_1c to be the base function. It then adds a
72 C special fat rule for mpn_modexact_1_odd
, messing up things when a cpudep
73 C gcd_1 exists without a corresponding cpudep mode1o.
74 ifdef
(`WANT_FAT_BINARY
', `
75 define(`mpn_modexact_1_odd', `MPN_PREFIX`modexact_1_odd_x86_64
'')')
83 mov (up), %rax C U low limb
85 bsf %rax, %rax C min(ctz(u0),ctz(v0))
90 push %rax C preserve common twos over call
91 push v0 C preserve v0 argument over call
92 sub $STACK_ALLOC, %rsp C maintain ABI required rsp alignment
97 C Both U and V are single limbs, reduce with bmod if u0 >> v0.
100 shr $BMOD_THRES_LOG2, %r8
106 cmp $BMOD_1_TO_MOD_1_THRESHOLD, n
108 IFDOS(` mov %rdx, %r8 ')
109 IFDOS
(`
mov %rsi
, %rdx
')
110 IFDOS(` mov %rdi, %rcx ')
111 ASSERT
(nz
, `
test $15, %rsp
')
115 IFDOS(` mov %rdx, %r8 ')
116 IFDOS
(`
mov %rsi
, %rdx
')
117 IFDOS(` mov %rdi, %rcx ')
118 ASSERT
(nz
, `
test $15, %rsp
')
119 CALL( mpn_modexact_1_odd)
122 add $STACK_ALLOC, %rsp
126 C test %rax, %rax C FIXME: does this lower latency?
130 ALIGN(16) C K10 BD C2 NHM SBR
131 L(top): cmovc %r10, %rax C if x-y < 0 0,3 0,3 0,6 0,5 0,5
132 cmovc %r9, %rdx C use x,y-x 0,3 0,3 2,8 1,7 1,7
133 L(mid): shr R8(%rcx), %rax C 1,7 1,6 2,8 2,8 2,8
134 mov %rdx, %r10 C 1 1 4 3 3
135 sub %rax, %r10 C 2 2 5 4 4
136 bsf %r10, %rcx C 3 3 6 5 5
137 mov %rax, %r9 C 2 2 3 3 4
138 sub %rdx, %rax C 2 2 4 3 4