summaryrefslogtreecommitdiff
path: root/gmp/mpn/x86/k6/mod_34lsub1.asm
blob: a5b7ee1064ae8e4813a7792cdba679a08b325e4c (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
dnl  AMD K6 mpn_mod_34lsub1 -- mpn remainder modulo 2**24-1.

dnl  Copyright 2000, 2001, 2002 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 K6: 2.66 cycles/limb


C mp_limb_t mpn_mod_34lsub1 (mp_srcptr src, mp_size_t size)
C
C An attempt was made to use a loop like
C
C L(top):
C	adcl	(%edx), %eax
C	adcl	4(%edx), %ebx
C	adcl	8(%edx), %esi
C	leal	12(%edx), %edx
C	loop	L(top)
C
C with %ecx starting from floor(size/3), but it still measured 2.66 c/l.
C The form used instead can save about 6 cycles by not dividing by 3.
C
C In the code used, putting the "leal"s at the top of the loop is necessary
C for the claimed speed, anywhere else costs an extra cycle per loop.
C Perhaps a tight loop like this needs short decode instructions at the
C branch target, which would explain the leal/loop form above taking 8
C cycles instead of 7 too.

defframe(PARAM_SIZE, 8)
defframe(PARAM_SRC,  4)

dnl  re-use parameter space
define(SAVE_EBX, `PARAM_SIZE')
define(SAVE_ESI, `PARAM_SRC')

	TEXT
	ALIGN(16)
PROLOGUE(mpn_mod_34lsub1)
deflit(`FRAME',0)

	movl	PARAM_SIZE, %eax
	movl	PARAM_SRC, %edx

	subl	$2, %eax
	ja	L(three_or_more)

Zdisp(	movl,	0,(%edx), %eax)		C avoid code cache line boundary
	jne	L(one)

	movl	%eax, %ecx
	movl	4(%edx), %edx

	shrl	$24, %eax		C src[0] high
	andl	$0x00FFFFFF, %ecx	C src[0] low

	addl	%ecx, %eax
	movl	%edx, %ecx

	shll	$8, %edx
	andl	$0x00FFFF00, %edx	C src[1] high

	shrl	$16, %ecx		C src[1] low
	addl	%ecx, %eax

	addl	%edx, %eax

L(one):
	ret


L(three_or_more):
	C eax	size-2
	C ebx
	C ecx
	C edx	src

	movl	%ebx, SAVE_EBX
	xorl	%ebx, %ebx

	movl	%esi, SAVE_ESI
	pushl	%edi	FRAME_pushl()

	xorl	%esi, %esi
	xorl	%edi, %edi		C and clear carry flag

L(top):
	C eax	counter, limbs
	C ebx	acc 0mod3
	C ecx
	C edx	src, incrementing
	C esi	acc 1mod3
	C edi	acc 2mod3
	C ebp

	leal	-2(%eax), %eax
	leal	12(%edx), %edx

	adcl	-12(%edx), %ebx
	adcl	-8(%edx), %esi
	adcl	-4(%edx), %edi

	decl	%eax
	jg	L(top)


	C ecx is -3, -2 or -1 representing 0, 1 or 2 more limbs, respectively

	movb	$0, %cl
	incl	%eax

	js	L(combine)		C 0 more

Zdisp(	adcl,	0,(%edx), %ebx)		C avoid code cache line crossings

	movb	$8, %cl
	decl	%eax

	js	L(combine)		C 1 more

	adcl	4(%edx), %esi

	movb	$16, %cl


L(combine):
	sbbl	%edx, %edx

	shll	%cl, %edx		C carry
	movl	%ebx, %eax		C 0mod3

	shrl	$24, %eax		C 0mod3 high
	andl	$0x00FFFFFF, %ebx	C 0mod3 low

	subl	%edx, %eax		C apply carry
	movl	%esi, %ecx		C 1mod3

	shrl	$16, %esi		C 1mod3 high
	addl	%ebx, %eax		C apply 0mod3 low

	andl	$0x0000FFFF, %ecx
	addl	%esi, %eax		C apply 1mod3 high

	shll	$8, %ecx		C 1mod3 low
	movl	%edi, %edx		C 2mod3

	shrl	$8, %edx		C 2mod3 high
	addl	%ecx, %eax		C apply 1mod3 low

	addl	%edx, %eax		C apply 2mod3 high
	andl	$0x000000FF, %edi

	shll	$16, %edi		C 2mod3 low
	movl	SAVE_EBX, %ebx

	addl	%edi, %eax		C apply 2mod3 low
	movl	SAVE_ESI, %esi

	popl	%edi

	ret

EPILOGUE()