Blame mpn/x86_64/core2/gcd_1.asm

Packit 5c3484
dnl  AMD64 mpn_gcd_1 optimised for Intel C2, NHM, SBR and AMD K10, BD.
Packit 5c3484
Packit 5c3484
dnl  Based on the K7 gcd_1.asm, by Kevin Ryde.  Rehacked for AMD64 by Torbjorn
Packit 5c3484
dnl  Granlund.
Packit 5c3484
Packit 5c3484
dnl  Copyright 2000-2002, 2005, 2009, 2011, 2012 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
dnl  This file is part of the GNU MP Library.
Packit 5c3484
dnl
Packit 5c3484
dnl  The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
dnl  it under the terms of either:
Packit 5c3484
dnl
Packit 5c3484
dnl    * the GNU Lesser General Public License as published by the Free
Packit 5c3484
dnl      Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
dnl      option) any later version.
Packit 5c3484
dnl
Packit 5c3484
dnl  or
Packit 5c3484
dnl
Packit 5c3484
dnl    * the GNU General Public License as published by the Free Software
Packit 5c3484
dnl      Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
dnl      later version.
Packit 5c3484
dnl
Packit 5c3484
dnl  or both in parallel, as here.
Packit 5c3484
dnl
Packit 5c3484
dnl  The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
dnl  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
dnl  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
dnl  for more details.
Packit 5c3484
dnl
Packit 5c3484
dnl  You should have received copies of the GNU General Public License and the
Packit 5c3484
dnl  GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
dnl  see https://www.gnu.org/licenses/.
Packit 5c3484
Packit 5c3484
include(`../config.m4')
Packit 5c3484
Packit 5c3484
Packit 5c3484
C	     cycles/bit (approx)
Packit 5c3484
C AMD K8,K9	 8.50
Packit 5c3484
C AMD K10	 4.30
Packit 5c3484
C AMD bd1	 5.00
Packit 5c3484
C AMD bobcat	10.0
Packit 5c3484
C Intel P4	18.6
Packit 5c3484
C Intel core2	 3.83
Packit 5c3484
C Intel NHM	 5.17
Packit 5c3484
C Intel SBR	 4.69
Packit 5c3484
C Intel atom	17.0
Packit 5c3484
C VIA nano	 5.44
Packit 5c3484
C Numbers measured with: speed -CD -s16-64 -t48 mpn_gcd_1
Packit 5c3484
Packit 5c3484
C TODO
Packit 5c3484
C  * Optimise inner-loop for specific CPUs.
Packit 5c3484
C  * Use DIV for 1-by-1 reductions, at least for some CPUs.
Packit 5c3484
Packit 5c3484
C Threshold of when to call bmod when U is one limb.  Should be about
Packit 5c3484
C (time_in_cycles(bmod_1,1) + call_overhead) / (cycles/bit).
Packit 5c3484
define(`BMOD_THRES_LOG2', 6)
Packit 5c3484
Packit 5c3484
C INPUT PARAMETERS
Packit 5c3484
define(`up',    `%rdi')
Packit 5c3484
define(`n',     `%rsi')
Packit 5c3484
define(`v0',    `%rdx')
Packit 5c3484
Packit 5c3484
ABI_SUPPORT(DOS64)
Packit 5c3484
ABI_SUPPORT(STD64)
Packit 5c3484
Packit 5c3484
IFDOS(`define(`STACK_ALLOC', 40)')
Packit 5c3484
IFSTD(`define(`STACK_ALLOC', 8)')
Packit 5c3484
Packit 5c3484
C Undo some configure cleverness.
Packit 5c3484
C The problem is that C only defines the '1c' variant, and that configure
Packit 5c3484
C therefore considers modexact_1c to be the base function.  It then adds a
Packit 5c3484
C special fat rule for mpn_modexact_1_odd, messing up things when a cpudep
Packit 5c3484
C gcd_1 exists without a corresponding cpudep mode1o.
Packit 5c3484
ifdef(`WANT_FAT_BINARY', `
Packit 5c3484
  define(`mpn_modexact_1_odd', `MPN_PREFIX`modexact_1_odd_x86_64'')')
Packit 5c3484
Packit 5c3484
Packit 5c3484
ASM_START()
Packit 5c3484
	TEXT
Packit 5c3484
	ALIGN(16)
Packit 5c3484
PROLOGUE(mpn_gcd_1)
Packit 5c3484
	FUNC_ENTRY(3)
Packit 5c3484
	mov	(up), %rax	C U low limb
Packit 5c3484
	or	v0, %rax
Packit 5c3484
	bsf	%rax, %rax	C min(ctz(u0),ctz(v0))
Packit 5c3484
Packit 5c3484
	bsf	v0, %rcx
Packit 5c3484
	shr	R8(%rcx), v0
Packit 5c3484
Packit 5c3484
	push	%rax		C preserve common twos over call
Packit 5c3484
	push	v0		C preserve v0 argument over call
Packit 5c3484
	sub	$STACK_ALLOC, %rsp	C maintain ABI required rsp alignment
Packit 5c3484
Packit 5c3484
	cmp	$1, n
Packit 5c3484
	jnz	L(reduce_nby1)
Packit 5c3484
Packit 5c3484
C Both U and V are single limbs, reduce with bmod if u0 >> v0.
Packit 5c3484
	mov	(up), %r8
Packit 5c3484
	mov	%r8, %rax
Packit 5c3484
	shr	$BMOD_THRES_LOG2, %r8
Packit 5c3484
	cmp	%r8, v0
Packit 5c3484
	ja	L(reduced)
Packit 5c3484
	jmp	L(bmod)
Packit 5c3484
Packit 5c3484
L(reduce_nby1):
Packit 5c3484
	cmp	$BMOD_1_TO_MOD_1_THRESHOLD, n
Packit 5c3484
	jl	L(bmod)
Packit 5c3484
IFDOS(`	mov	%rdx, %r8	')
Packit 5c3484
IFDOS(`	mov	%rsi, %rdx	')
Packit 5c3484
IFDOS(`	mov	%rdi, %rcx	')
Packit 5c3484
	ASSERT(nz, `test $15, %rsp')
Packit 5c3484
	CALL(	mpn_mod_1)
Packit 5c3484
	jmp	L(reduced)
Packit 5c3484
L(bmod):
Packit 5c3484
IFDOS(`	mov	%rdx, %r8	')
Packit 5c3484
IFDOS(`	mov	%rsi, %rdx	')
Packit 5c3484
IFDOS(`	mov	%rdi, %rcx	')
Packit 5c3484
	ASSERT(nz, `test $15, %rsp')
Packit 5c3484
	CALL(	mpn_modexact_1_odd)
Packit 5c3484
L(reduced):
Packit 5c3484
Packit 5c3484
	add	$STACK_ALLOC, %rsp
Packit 5c3484
	pop	%rdx
Packit 5c3484
Packit 5c3484
	bsf	%rax, %rcx
Packit 5c3484
C	test	%rax, %rax	C FIXME: does this lower latency?
Packit 5c3484
	jnz	L(mid)
Packit 5c3484
	jmp	L(end)
Packit 5c3484
Packit 5c3484
	ALIGN(16)		C               K10   BD    C2    NHM   SBR
Packit 5c3484
L(top):	cmovc	%r10, %rax	C if x-y < 0    0,3   0,3   0,6   0,5   0,5
Packit 5c3484
	cmovc	%r9, %rdx	C use x,y-x     0,3   0,3   2,8   1,7   1,7
Packit 5c3484
L(mid):	shr	R8(%rcx), %rax	C               1,7   1,6   2,8   2,8   2,8
Packit 5c3484
	mov	%rdx, %r10	C               1     1     4     3     3
Packit 5c3484
	sub	%rax, %r10	C               2     2     5     4     4
Packit 5c3484
	bsf	%r10, %rcx	C               3     3     6     5     5
Packit 5c3484
	mov	%rax, %r9	C               2     2     3     3     4
Packit 5c3484
	sub	%rdx, %rax	C               2     2     4     3     4
Packit 5c3484
	jnz	L(top)		C
Packit 5c3484
Packit 5c3484
L(end):	pop	%rcx
Packit 5c3484
	mov	%rdx, %rax
Packit 5c3484
	shl	R8(%rcx), %rax
Packit 5c3484
	FUNC_EXIT()
Packit 5c3484
	ret
Packit 5c3484
EPILOGUE()
rpm-build 01f633
CF_PROT