-
Notifications
You must be signed in to change notification settings - Fork 8
/
ssse3_popcount.cpp
229 lines (212 loc) · 7.29 KB
/
ssse3_popcount.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
*
* ssse3_popcount.h
* Soap3(gpu)
*
* Copyright (C) 2011, HKU
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#ifdef __x86_64
//#define ALIGN_DATA
#ifdef ALIGN_DATA
# define __aligned__ __attribute__((aligned(16)))
#else
# define __aligned__
#endif
#define MAX_CHUNKS 2048
// lookup for SSE
uint8_t POPCOUNT_4bit[16] __aligned__ =
{
/* 0 */ 0,
/* 1 */ 1,
/* 2 */ 1,
/* 3 */ 2,
/* 4 */ 1,
/* 5 */ 2,
/* 6 */ 2,
/* 7 */ 3,
/* 8 */ 1,
/* 9 */ 2,
/* a */ 2,
/* b */ 3,
/* c */ 2,
/* d */ 3,
/* e */ 3,
/* f */ 4
};
// ---- SSSE3 - naive approach --------------------------------------------
uint32_t ssse3_popcount1 ( uint8_t * buffer, int chunks16 )
{
uint32_t dummy __attribute__ ( ( unused ) );
static char MASK_4bit[16] = {0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf};
uint32_t result, tmp;
int n;
__asm__ volatile ( "movdqu (%%eax), %%xmm7" : : "a" ( POPCOUNT_4bit ) );
__asm__ volatile ( "movdqu (%%eax), %%xmm6" : : "a" ( MASK_4bit ) );
result = 0;
for ( n = 0; n < chunks16; n++ )
{
__asm__ volatile (
#ifdef ALIGN_DATA
"movdqa (%%ebx), %%xmm0 \n"
#else
"movdqu (%%ebx), %%xmm0 \n"
#endif
"movdqa %%xmm0, %%xmm1 \n"
"psrlw $4, %%xmm1 \n"
"pand %%xmm6, %%xmm0 \n" // xmm0 := lower nibbles
"pand %%xmm6, %%xmm1 \n" // xmm1 := higher nibbles
"movdqa %%xmm7, %%xmm2 \n"
"movdqa %%xmm7, %%xmm3 \n" // get popcount
"pshufb %%xmm0, %%xmm2 \n" // for all nibbles
"pshufb %%xmm1, %%xmm3 \n" // using PSHUFB
"paddb %%xmm2, %%xmm3 \n" // popcount for all bytes
"pxor %%xmm0, %%xmm0 \n"
"psadbw %%xmm3, %%xmm0 \n" // sum popcounts
"movhlps %%xmm0, %%xmm1 \n"
"paddd %%xmm0, %%xmm1 \n"
"movd %%xmm1, %%eax \n"
: "=a" ( tmp )
: "b" ( &buffer[n*16] )
);
result += tmp;
}
return result;
}
// ---- SSSE3 - better alorithm, minimized psadbw usage -------------------
uint32_t ssse3_popcount2 ( uint8_t * buffer, int chunks16 )
{
static char MASK_4bit[16] = {0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf};
uint32_t result = 0;
__asm__ volatile ( "movdqu (%%eax), %%xmm7" : : "a" ( POPCOUNT_4bit ) );
__asm__ volatile ( "movdqu (%%eax), %%xmm6" : : "a" ( MASK_4bit ) );
__asm__ volatile ( "pxor %%xmm5, %%xmm5" : : ); // xmm5 -- global accumulator
int n, i = 0;
__asm__ volatile ( "pxor %xmm4, %xmm4" ); // xmm4 -- local accumulator
for ( n = 0; n < chunks16; n++ )
{
__asm__ volatile (
#if ALIGN_DATA
"movdqa (%%eax), %%xmm0 \n"
#else
"movdqu (%%eax), %%xmm0 \n"
#endif
"movdqa %%xmm0, %%xmm1 \n"
"psrlw $4, %%xmm1 \n"
"pand %%xmm6, %%xmm0 \n" // xmm0 := lower nibbles
"pand %%xmm6, %%xmm1 \n" // xmm1 := higher nibbles
"movdqa %%xmm7, %%xmm2 \n"
"movdqa %%xmm7, %%xmm3 \n" // get popcount
"pshufb %%xmm0, %%xmm2 \n" // for all nibbles
"pshufb %%xmm1, %%xmm3 \n" // using PSHUFB
"paddb %%xmm2, %%xmm4 \n" // update local
"paddb %%xmm3, %%xmm4 \n" // accumulator
:
: "a" ( &buffer[i] )
);
i += 16;
}
// update global accumulator (two 32-bits counters)
__asm__ volatile (
"pxor %xmm0, %xmm0 \n"
"psadbw %xmm0, %xmm4 \n"
"paddd %xmm4, %xmm5 \n"
);
// finally add together 32-bits counters stored in global accumulator
__asm__ volatile (
"movhlps %%xmm5, %%xmm0 \n"
"paddd %%xmm5, %%xmm0 \n"
"movd %%xmm0, %%eax \n"
: "=a" ( result )
);
return result;
}
// ---- SSSE3 - better alorithm, inner loop unrolled ----------------------
uint32_t ssse3_popcount3 ( uint8_t * buffer, int chunks16 )
{
static char MASK_4bit[16] = {0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf};
uint32_t result = 0;
__asm__ volatile ( "movdqu (%%eax), %%xmm7" : : "a" ( POPCOUNT_4bit ) );
__asm__ volatile ( "movdqu (%%eax), %%xmm6" : : "a" ( MASK_4bit ) );
__asm__ volatile ( "pxor %%xmm5, %%xmm5" : : ); // xmm5 -- global accumulator
int k, n, i;
i = 0;
while ( chunks16 > 0 )
{
// max(POPCOUNT_8bit) = 8, thus byte-wise addition could be done
// for floor(255/8) = 31 iterations
#define MAX (7*4)
if ( chunks16 > MAX )
{
k = MAX;
chunks16 -= MAX;
}
else
{
k = chunks16;
chunks16 = 0;
}
#undef MAX
__asm__ volatile ( "pxor %xmm4, %xmm4" ); // xmm4 -- local accumulator
const int c = 4;
for ( n = 0; n < k; n += c )
{
#define body(index) \
__asm__ volatile( \
"movdqa (%%eax), %%xmm0 \n" \
"movdqa %%xmm0, %%xmm1 \n" \
"psrlw $4, %%xmm1 \n" \
"pand %%xmm6, %%xmm0 \n" \
"pand %%xmm6, %%xmm1 \n" \
"movdqa %%xmm7, %%xmm2 \n" \
"movdqa %%xmm7, %%xmm3 \n" \
"pshufb %%xmm0, %%xmm2 \n" \
"pshufb %%xmm1, %%xmm3 \n" \
"paddb %%xmm2, %%xmm4 \n" \
"paddb %%xmm3, %%xmm4 \n" \
: : "a" (&buffer[index]));
body ( i );
body ( i + 1 * 16 );
body ( i + 2 * 16 );
body ( i + 3 * 16 );
#undef body
// i += 4*16;
i += c * 16;
}
// update global accumulator (two 32-bits counters)
__asm__ volatile (
"pxor %xmm0, %xmm0 \n"
"psadbw %xmm0, %xmm4 \n"
"paddd %xmm4, %xmm5 \n"
);
}
// finally add together 32-bits counters stored in global accumulator
__asm__ volatile (
"movhlps %%xmm5, %%xmm0 \n"
"paddd %%xmm5, %%xmm0 \n"
"movd %%xmm0, %%eax \n"
: "=a" ( result )
);
return result;
}
#endif