summaryrefslogtreecommitdiff
path: root/mpn/x86_64/invert_limb.asm
blob: 06cf1414af69ddbe57828a358cffaa3b24770333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
dnl  AMD64 mpn_invert_limb -- Invert a normalized limb.

dnl  Contributed to the GNU project by Torbjorn Granlund and Niels Möller.

dnl  Copyright 2004, 2007, 2008, 2009, 2011 Free Software Foundation, Inc.

dnl  This file is part of the GNU MP Library.

dnl  The GNU MP Library is free software; you can redistribute it and/or modify
dnl  it under the terms of the GNU Lesser General Public License as published
dnl  by the Free Software Foundation; either version 3 of the License, or (at
dnl  your option) any later version.

dnl  The GNU MP Library is distributed in the hope that it will be useful, but
dnl  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
dnl  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
dnl  License for more details.

dnl  You should have received a copy of the GNU Lesser General Public License
dnl  along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.

include(`../config.m4')


C	     cycles/limb (approx)	div
C AMD K8,K9	 48			 71
C AMD K10	 48			 77
C Intel P4	135			161
C Intel core2	 69			116
C Intel corei	 55			 89
C Intel atom	129			191
C VIA nano	 79			157

C rax rcx rdx rdi rsi r8

ABI_SUPPORT(DOS64)
ABI_SUPPORT(ELF64)

ASM_START()
	TEXT
	ALIGN(16)
PROLOGUE(mpn_invert_limb)		C			Kn	C2	Ci
	DOS64_ENTRY(1)
	mov	%rdi, %rax		C			 0	 0	 0
	shr	$55, %rax		C			 1	 1	 1
ifdef(`PIC',`
ifdef(`DARWIN',`
	mov	mpn_invert_limb_table@GOTPCREL(%rip), %r8
	add	$-512, %r8
',`
	.protected mpn_invert_limb_table
	lea	-512+mpn_invert_limb_table(%rip), %r8
')',`
	movabs	$-512+mpn_invert_limb_table, %r8
')
	movzwl	(%r8,%rax,2), R32(%rcx)	C	%rcx = v0

	C v1 = (v0 << 11) - (v0*v0*d40 >> 40) - 1
	mov	%rdi, %rsi		C			 0	 0	 0
	mov	R32(%rcx), R32(%rax)	C			 4	 5	 5
	imul	R32(%rcx), R32(%rcx)	C			 4	 5	 5
	shr	$24, %rsi		C			 1	 1	 1
	inc	%rsi			C	%rsi = d40
	imul	%rsi, %rcx		C			 8	10	 8
	shr	$40, %rcx		C			12	15	11
	sal	$11, R32(%rax)		C			 5	 6	 6
	dec	R32(%rax)
	sub	R32(%rcx), R32(%rax)	C	%rax = v1

	C v2 = (v1 << 13) + (v1 * (2^60 - v1*d40) >> 47)
	mov	$0x1000000000000000, %rcx
	imul	%rax, %rsi		C			14	17	13
	sub	%rsi, %rcx
	imul	%rax, %rcx
	sal	$13, %rax
	shr	$47, %rcx
	add	%rax, %rcx		C	%rcx = v2

	C v3 = (v2 << 31) + (v2 * (2^96 - v2 * d63 + ((v2 >> 1) & mask)) >> 65
	mov	%rdi, %rsi		C			 0	 0	 0
	shr	%rsi			C d/2
	sbb	%rax, %rax		C -d0 = -(d mod 2)
	sub	%rax, %rsi		C d63 = ceil(d/2)
	imul	%rcx, %rsi		C v2 * d63
	and	%rcx, %rax		C v2 * d0
	shr	%rax			C (v2>>1) * d0
	sub	%rsi, %rax		C (v2>>1) * d0 - v2 * d63
	mul	%rcx
	sal	$31, %rcx
	shr	%rdx
	add	%rdx, %rcx		C	%rcx = v3

	mov	%rdi, %rax
	mul	%rcx
	add	%rdi, %rax
	mov	%rcx, %rax
	adc	%rdi, %rdx
	sub	%rdx, %rax

	DOS64_EXIT()
	ret
EPILOGUE()
ASM_END()