Blame mpn/x86/k7/gcd_1.asm

Packit 5c3484
dnl  x86 mpn_gcd_1 optimised for AMD K7.
Packit 5c3484
Packit 5c3484
dnl  Contributed to the GNU project by by Kevin Ryde.  Rehacked by Torbjorn
Packit 5c3484
dnl  Granlund.
Packit 5c3484
Packit 5c3484
dnl  Copyright 2000-2002, 2005, 2009, 2011, 2012, 2014, 2015 Free Software
Packit 5c3484
dnl  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 K7	 5.31
Packit 5c3484
C AMD K8,K9	 5.33
Packit 5c3484
C AMD K10	 5.30
Packit 5c3484
C AMD bd1	 ?
Packit 5c3484
C AMD bobcat	 7.02
Packit 5c3484
C Intel P4-2	10.1
Packit 5c3484
C Intel P4-3/4	10.0
Packit 5c3484
C Intel P6/13	 5.88
Packit 5c3484
C Intel core2	 6.26
Packit 5c3484
C Intel NHM	 6.83
Packit 5c3484
C Intel SBR	 8.50
Packit 5c3484
C Intel atom	 8.90
Packit 5c3484
C VIA nano	 ?
Packit 5c3484
C Numbers measured with: speed -CD -s16-32 -t16 mpn_gcd_1
Packit 5c3484
Packit 5c3484
C TODO
Packit 5c3484
C  * Tune overhead, this takes 2-3 cycles more than old code when v0 is tiny.
Packit 5c3484
C  * Stream things better through registers, avoiding some copying.
Packit 5c3484
C  * For ELF, avoid putting GOT base in both ebx and esi.  Needs special
Packit 5c3484
C    LEA/LEAL or else discrete code here.
Packit 5c3484
Packit 5c3484
C ctz_table[n] is the number of trailing zeros on n, or MAXSHIFT if n==0.
Packit 5c3484
Packit 5c3484
deflit(MAXSHIFT, 6)
Packit 5c3484
deflit(MASK, eval((m4_lshift(1,MAXSHIFT))-1))
Packit 5c3484
Packit 5c3484
DEF_OBJECT(ctz_table,64)
Packit 5c3484
	.byte	MAXSHIFT
Packit 5c3484
forloop(i,1,MASK,
Packit 5c3484
`	.byte	m4_count_trailing_zeros(i)
Packit 5c3484
')
Packit 5c3484
END_OBJECT(ctz_table)
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(`DIV_THRES_LOG2', 7)
Packit 5c3484
Packit 5c3484
Packit 5c3484
define(`up',    `%edi')
Packit 5c3484
define(`n',     `%esi')
Packit 5c3484
define(`v0',    `%edx')
Packit 5c3484
Packit 5c3484
Packit 5c3484
ASM_START()
Packit 5c3484
	TEXT
Packit 5c3484
	ALIGN(16)
Packit 5c3484
PROLOGUE(mpn_gcd_1)
Packit 5c3484
	push	%edi
Packit 5c3484
	push	%esi
Packit 5c3484
Packit 5c3484
	mov	12(%esp), up
Packit 5c3484
	mov	16(%esp), n
Packit 5c3484
	mov	20(%esp), v0
Packit 5c3484
Packit 5c3484
	mov	(up), %eax		C U low limb
Packit 5c3484
	or	v0, %eax		C x | y
Packit 5c3484
	mov	$-1, %ecx
Packit 5c3484
Packit 5c3484
L(twos):
Packit 5c3484
	inc	%ecx
Packit 5c3484
	shr	%eax
Packit 5c3484
	jnc	L(twos)
Packit 5c3484
Packit 5c3484
	shr	%cl, v0
Packit 5c3484
	mov	%ecx, %eax		C common twos
Packit 5c3484
Packit 5c3484
L(divide_strip_y):
Packit 5c3484
	shr	v0
Packit 5c3484
	jnc	L(divide_strip_y)
Packit 5c3484
	adc	v0, v0
Packit 5c3484
Packit 5c3484
	push	%eax
Packit 5c3484
	push	v0
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), %ecx
Packit 5c3484
	mov	%ecx, %eax
Packit 5c3484
	shr	$DIV_THRES_LOG2, %ecx
Packit 5c3484
	cmp	%ecx, v0
Packit 5c3484
	ja	L(reduced)
Packit 5c3484
Packit 5c3484
	mov	v0, %esi
Packit 5c3484
	xor	%edx, %edx
Packit 5c3484
	div	%esi
Packit 5c3484
	mov	%edx, %eax
Packit 5c3484
	jmp	L(reduced)
Packit 5c3484
Packit 5c3484
L(reduce_nby1):
Packit 5c3484
ifdef(`PIC_WITH_EBX',`dnl
Packit 5c3484
	push	%ebx
Packit 5c3484
	add	$-4, %esp
Packit 5c3484
	call	L(movl_eip_ebx)
Packit 5c3484
	add	$_GLOBAL_OFFSET_TABLE_, %ebx
Packit 5c3484
')
Packit 5c3484
	push	v0			C param 3
Packit 5c3484
	push	n			C param 2
Packit 5c3484
	push	up			C param 1
Packit 5c3484
	cmp	$BMOD_1_TO_MOD_1_THRESHOLD, n
Packit 5c3484
	jl	L(bmod)
Packit 5c3484
	CALL(	mpn_mod_1)
Packit 5c3484
	jmp	L(called)
Packit 5c3484
L(bmod):
Packit 5c3484
	CALL(	mpn_modexact_1_odd)
Packit 5c3484
Packit 5c3484
L(called):
Packit 5c3484
ifdef(`PIC_WITH_EBX',`dnl
Packit 5c3484
	add	$16, %esp	C deallocate params
Packit 5c3484
	pop	%ebx
Packit 5c3484
',`
Packit 5c3484
	add	$12, %esp		C deallocate params
Packit 5c3484
')
Packit 5c3484
L(reduced):
Packit 5c3484
	pop	%edx
Packit 5c3484
Packit 5c3484
	LEAL(	ctz_table, %esi)
Packit 5c3484
	test	%eax, %eax
Packit 5c3484
	mov	%eax, %ecx
Packit 5c3484
	jnz	L(mid)
Packit 5c3484
	jmp	L(end)
Packit 5c3484
Packit 5c3484
	ALIGN(16)			C               K8    BC    P4    NHM   SBR
Packit 5c3484
L(top):	cmovc(	%ecx, %eax)		C if x-y < 0	0
Packit 5c3484
	cmovc(	%edi, %edx)		C use x,y-x	0
Packit 5c3484
L(mid):	and	$MASK, %ecx		C		0
Packit 5c3484
	movzbl	(%esi,%ecx), %ecx	C		1
Packit 5c3484
	jz	L(shift_alot)		C		1
Packit 5c3484
	shr	%cl, %eax		C		3
Packit 5c3484
	mov	%eax, %edi		C		4
Packit 5c3484
	mov	%edx, %ecx		C		3
Packit 5c3484
	sub	%eax, %ecx		C		4
Packit 5c3484
	sub	%edx, %eax		C		4
Packit 5c3484
	jnz	L(top)			C		5
Packit 5c3484
Packit 5c3484
L(end):	pop	%ecx
Packit 5c3484
	mov	%edx, %eax
Packit 5c3484
	shl	%cl, %eax
Packit 5c3484
	pop	%esi
Packit 5c3484
	pop	%edi
Packit 5c3484
	ret
Packit 5c3484
Packit 5c3484
L(shift_alot):
Packit 5c3484
	shr	$MAXSHIFT, %eax
Packit 5c3484
	mov	%eax, %ecx
Packit 5c3484
	jmp	L(mid)
Packit 5c3484
Packit 5c3484
ifdef(`PIC_WITH_EBX',`dnl
Packit 5c3484
L(movl_eip_ebx):
Packit 5c3484
	mov	(%esp), %ebx
Packit 5c3484
	ret
Packit 5c3484
')
Packit 5c3484
EPILOGUE()
Packit 5c3484
ASM_END()