summaryrefslogtreecommitdiff
path: root/mpn/x86_64/diveby3.asm
blob: 6b28fd6b1428cc82159cd569902671910e155abe (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
dnl  AMD64 mpn_divexact_by3 -- mpn division by 3, expecting no remainder.

dnl  Copyright 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
dnl
dnl  This file is part of the GNU MP Library.
dnl
dnl  The GNU MP Library is free software; you can redistribute it and/or
dnl  modify it under the terms of the GNU Lesser General Public License as
dnl  published by the Free Software Foundation; either version 3 of the
dnl  License, or (at your option) any later version.
dnl
dnl  The GNU MP Library is distributed in the hope that it will be useful,
dnl  but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dnl  Lesser General Public License for more details.
dnl
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
C K8:		 5
C P4:		20.5
C P6-15:	 8

C INPUT PARAMETERS
define(`rp',	`%rdi')
define(`up',	`%rsi')
define(`n',	`%rdx')
define(`cy',	`%rcx')

ASM_START()
PROLOGUE(mpn_divexact_by3c)
	push	%rbx
	movq	(up), %rax
	movq	$0x5555555555555555, %r10
	leaq	(%r10,%r10), %r11		C 0xAAAAAAAAAAAAAAAA
	mov	%rcx, %r8
	imulq	%r10, %r8
	leaq	1(%r10,%r10), %r9		C 0xAAAAAAAAAAAAAAAB
	leaq	(up,n,8), up
	leaq	-8(rp,n,8), rp
	negq	n

	imulq	%r9, %rax

	incq	n
	jz,pn	.Lendo

	.align 16
.Loop:	movq	(up,n,8), %rbx
	imulq	%r9, %rbx
	addq	%r8, %rax
	cmpq	%rcx, -8(up,n,8)		C highly unlikely carry!!!
	movq	%rax, (rp,n,8)
	jc,pn	.Lc0
.Lb0:	xor	%r8, %r8
	cmpq	%rax, %r10
	cmovc	%r10, %r8
	setc	%cl
	cmpq	%rax, %r11
	cmovc	%r11, %r8
	adcq	$0, %rcx

	incq	n
	jz,pn	.Lende

	movq	(up,n,8), %rax
	imulq	%r9, %rax
	addq	%r8, %rbx
	cmpq	%rcx, -8(up,n,8)
	movq	%rbx, (rp,n,8)
	jc,pn	.Lc1
.Lb1:	xor	%r8, %r8
	cmpq	%rbx, %r10
	cmovc	%r10, %r8
	setc	%cl
	cmpq	%rbx, %r11
	cmovc	%r11, %r8
	adcq	$0, %rcx

	incq	n
	jnz,pt	.Loop

.Lendo:	addq	%r8, %rax
	cmpq	%rcx, -8(up,n,8)
	movq	%rax, (rp,n,8)
	jc,pn	.Lc2
.Lb2:	xor	%r8, %r8
	cmpq	%rax, %r10
	cmovc	%r10, %r8
	setc	%cl
	cmpq	%rax, %r11
	cmovc	%r11, %r8
	adcq	$0, %rcx
	movq	%rcx, %rax
	pop	%rbx
	ret

.Lende:	addq	%r8, %rbx
	cmpq	%rcx, -8(up,n,8)
	movq	%rbx, (rp,n,8)
	jc,pn	.Lc3
.Lb3:	xor	%r8, %r8
	cmpq	%rbx, %r10
	cmovc	%r10, %r8
	setc	%cl
	cmpq	%rbx, %r11
	cmovc	%r11, %r8
	adcq	$0, %rcx
	movq	%rcx, %rax
	pop	%rbx
	ret

.Lc0:	incq	%rax
	jmp .Lb0
.Lc1:	incq	%rbx
	jmp .Lb1
.Lc2:	incq	%rax
	jmp .Lb2
.Lc3:	incq	%rbx
	jmp .Lb3
EPILOGUE()