summaryrefslogtreecommitdiff
path: root/mpn/x86_64/popham.asm
blob: 9db368106f67bdfea96ab171b9da0b84eb7debd6 (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
dnl  AMD64 mpn_popcount, mpn_hamdist -- population count and hamming distance.

dnl  Copyright 2004, 2005, 2007, 2010 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		     popcount	      hamdist
C		    cycles/limb	    cycles/limb
C AMD K8,K9		 6		 7
C AMD K10		 6		 7
C Intel P4		12		14.3
C Intel core2		 7		 8
C Intel corei		 ?		 7.3
C Intel atom		16.5		17.5
C VIA nano		 8.75		10.4

C TODO
C  * Tune.  It should be possible to reach 5 c/l for popcount and 6 c/l for
C    hamdist for K8/K9.


ifdef(`OPERATION_popcount',`
  define(`func',`mpn_popcount')
  define(`up',		`%rdi')
  define(`n',		`%rsi')
  define(`h55555555',	`%r10')
  define(`h33333333',	`%r11')
  define(`h0f0f0f0f',	`%rcx')
  define(`h01010101',	`%rdx')
  define(`HAM',		`dnl')
')
ifdef(`OPERATION_hamdist',`
  define(`func',`mpn_hamdist')
  define(`up',		`%rdi')
  define(`vp',		`%rsi')
  define(`n',		`%rdx')
  define(`h55555555',	`%r10')
  define(`h33333333',	`%r11')
  define(`h0f0f0f0f',	`%rcx')
  define(`h01010101',	`%r14')
  define(`HAM',		`$1')
')


MULFUNC_PROLOGUE(mpn_popcount mpn_hamdist)

ASM_START()
	TEXT
	ALIGN(32)
PROLOGUE(func)

	push	%r12
	push	%r13
 HAM(`	push	%r14		')

	mov	$0x5555555555555555, h55555555
	mov	$0x3333333333333333, h33333333
	mov	$0x0f0f0f0f0f0f0f0f, h0f0f0f0f
	mov	$0x0101010101010101, h01010101

	lea	(up,n,8), up
 HAM(`	lea	(vp,n,8), vp	')
	neg	n

	xor	R32(%rax), R32(%rax)

	bt	$0, R32(n)
	jnc	L(top)

	mov	(up,n,8), %r8
 HAM(`	xor	(vp,n,8), %r8	')

	mov	%r8, %r9
	shr	%r8
	and	h55555555, %r8
	sub	%r8, %r9

	mov	%r9, %r8
	shr	$2, %r9
	and	h33333333, %r8
	and	h33333333, %r9
	add	%r8, %r9		C 16 4-bit fields (0..4)

	mov	%r9, %r8
	shr	$4, %r9
	and	h0f0f0f0f, %r8
	and	h0f0f0f0f, %r9
	add	%r8, %r9		C 8 8-bit fields (0..16)

	imul	h01010101, %r9		C sum the 8 fields in high 8 bits
	shr	$56, %r9

	mov	%r9, %rax		C add to total
	add	$1, n
	jz	L(end)

	ALIGN(16)
L(top):	mov	(up,n,8), %r8
	mov	8(up,n,8), %r12
 HAM(`	xor	(vp,n,8), %r8	')
 HAM(`	xor	8(vp,n,8), %r12	')

	mov	%r8, %r9
	mov	%r12, %r13
	shr	%r8
	shr	%r12
	and	h55555555, %r8
	and	h55555555, %r12
	sub	%r8, %r9
	sub	%r12, %r13

	mov	%r9, %r8
	mov	%r13, %r12
	shr	$2, %r9
	shr	$2, %r13
	and	h33333333, %r8
	and	h33333333, %r9
	and	h33333333, %r12
	and	h33333333, %r13
	add	%r8, %r9		C 16 4-bit fields (0..4)
	add	%r12, %r13		C 16 4-bit fields (0..4)

	add	%r13, %r9		C 16 4-bit fields (0..8)
	mov	%r9, %r8
	shr	$4, %r9
	and	h0f0f0f0f, %r8
	and	h0f0f0f0f, %r9
	add	%r8, %r9		C 8 8-bit fields (0..16)

	imul	h01010101, %r9		C sum the 8 fields in high 8 bits
	shr	$56, %r9

	add	%r9, %rax		C add to total
	add	$2, n
	jnc	L(top)

L(end):
 HAM(`	pop	%r14		')
	pop	%r13
	pop	%r12
	ret

EPILOGUE()