summaryrefslogtreecommitdiff
path: root/Build/source/libs/gmp/gmp-6.0.0/mpn/x86_64/popham.asm
blob: 9005f817762fd37e961d0c8e0f0f71f341562fba (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
dnl  AMD64 mpn_popcount, mpn_hamdist -- population count and hamming distance.

dnl  Copyright 2004, 2005, 2007, 2010-2012 Free Software Foundation, Inc.

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 modify
dnl  it under the terms of either:
dnl
dnl    * the GNU Lesser General Public License as published by the Free
dnl      Software Foundation; either version 3 of the License, or (at your
dnl      option) any later version.
dnl
dnl  or
dnl
dnl    * the GNU General Public License as published by the Free Software
dnl      Foundation; either version 2 of the License, or (at your option) any
dnl      later version.
dnl
dnl  or both in parallel, as here.
dnl
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 General Public License
dnl  for more details.
dnl
dnl  You should have received copies of the GNU General Public License and the
dnl  GNU Lesser General Public License along with the GNU MP Library.  If not,
dnl  see https://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(`POP',		`$1')
  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(`POP',		`dnl')
  define(`HAM',		`$1')
')


MULFUNC_PROLOGUE(mpn_popcount mpn_hamdist)

ABI_SUPPORT(DOS64)
ABI_SUPPORT(STD64)

ASM_START()
	TEXT
	ALIGN(32)
PROLOGUE(func)
 POP(`	FUNC_ENTRY(2)		')
 HAM(`	FUNC_ENTRY(3)		')
	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
	FUNC_EXIT()
	ret
EPILOGUE()