-
Notifications
You must be signed in to change notification settings - Fork 9
/
lztest.c
265 lines (222 loc) · 4.78 KB
/
lztest.c
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* Compare times for firstbitnz instructions */
/* Usage: lztest K N
- measures the time for 1000*N popcount operations on words
with K one bits, comparing with the macro FIRSTBITNZ.
Compile with values for WORDSIZE and FBNZ with FBNZ values:
1 = __builtin_clz* */
#define MAXN WORDSIZE
#ifndef WORDSIZE
#define WORDSIZE 32
#endif
#ifndef FBNZ
#error Need a value for FBNZ
#endif
#if FBNZ==1
#define NEWFBNZ(x,c) {c = FIRSTBITNZ(x);}
#endif
#include "gtools.h"
#ifdef __INTEL_COMPILER
#include <nmmintrin.h>
#endif
#if WORDSIZE==64
#define FIRSTBITMAC(x) ((x) & MSK3232 ? \
(x) & MSK1648 ? \
(x) & MSK0856 ? \
0+leftbit[((x)>>56) & MSK8] : \
8+leftbit[(x)>>48] \
: (x) & MSK0840 ? \
16+leftbit[(x)>>40] : \
24+leftbit[(x)>>32] \
: (x) & MSK1616 ? \
(x) & MSK0824 ? \
32+leftbit[(x)>>24] : \
40+leftbit[(x)>>16] \
: (x) & MSK0808 ? \
48+leftbit[(x)>>8] : \
56+leftbit[x])
#elif WORDSIZE==32
#define FIRSTBITMAC(x) ((x) & MSK1616 ? ((x) & MSK0824 ? \
leftbit[((x)>>24) & MSK8] : 8+leftbit[(x)>>16]) \
: ((x) & MSK0808 ? 16+leftbit[(x)>>8] : 24+leftbit[x]))
#else
#define FIRSTBITMAC(x) ((x) & MSK0808 ? leftbit[((x)>>8) & MSK8] : 8+leftbit[x])
#endif
#if FBNZ==1
#define NEWFBNZ(x,c) {c = FIRSTBITNZ(x);}
#endif
static setword
ransetword(int k) /* setword with k random bits */
{
register setword w,rb;
register int j;
j = 0;
w = 0;
for (;;)
{
if (j == k) return w;
rb = bit[random() % WORDSIZE];
if ((rb & w) == 0)
{
w |= rb;
++j;
}
}
}
static double
timemac(setword *x, int n, int iters, int *sump)
{
register int c,i,j,sum;
register setword w;
int it;
double t;
sum = 0;
for (i = 0; i < n; ++i)
{
w = x[i];
sum += FIRSTBITMAC(w);
}
t = CPUTIME;
for (it = 0; it < iters; ++it)
{
for (i = 0; i < n; ++i)
{
w = x[i];
sum += FIRSTBITMAC(w);
}
sum ^= it;
}
t = CPUTIME - t;
*sump = sum;
return t;
}
static double
timeold(setword *x, int n, int iters, int *sump)
{
register int c,i,j,sum;
register setword w;
int it;
double t;
sum = 0;
for (i = 0; i < n; ++i)
{
w = x[i];
sum += FIRSTBITNZ(w);
}
t = CPUTIME;
for (it = 0; it < iters; ++it)
{
for (i = 0; i < n; ++i)
{
w = x[i];
sum += FIRSTBITNZ(w);
}
sum ^= it;
}
t = CPUTIME - t;
*sump = sum;
return t;
}
static double
timenew(setword *x, int n, int iters, int *sump)
{
register int c,i,j,sum;
register setword w;
int it;
double t;
sum = 0;
for (i = 0; i < n; ++i)
{
w = x[i];
NEWFBNZ(w,c);
sum += c;
}
t = CPUTIME;
for (it = 0; it < iters; ++it)
{
for (i = 0; i < n; ++i)
{
w = x[i];
NEWFBNZ(w,c);
sum += c;
}
sum ^= it;
}
t = CPUTIME - t;
*sump = sum;
return t;
}
static double
timenull(setword *x, int n, int iters, int *sump)
{
register int c,i,j,sum;
register setword w;
int it;
double t;
sum = 0;
for (i = 0; i < n; ++i)
{
w = x[i];
NEWFBNZ(w,c);
sum += c;
}
t = CPUTIME;
for (it = 0; it < iters; ++it)
{
for (i = 0; i < n; ++i)
{
w = x[i];
c = w;
sum += c;
}
sum ^= it;
}
t = CPUTIME - t;
*sump = sum;
return t;
}
int
main(int argc, char *argv[])
{
int i,k,iters;
setword x[1000];
double tnull,told,tnew,tmac;
int summac,sumold,sumnew,sumnull;
printf("WORDSIZE=%d FBNZ=%s ",WORDSIZE,
FBNZ==1 ? "intrinsic" : "undefined");
#ifdef SETWORD_SHORT
printf(" setword=unsigned short ");
#endif
#ifdef SETWORD_INT
printf(" setword=unsigned int ");
#endif
#ifdef SETWORD_LONG
printf(" setword=unsigned long ");
#endif
#ifdef SETWORD_LONGLONG
printf(" setword=unsigned long long ");
#endif
#ifdef __SSE4_2__
printf("__SSE4_2__ ");
#endif
#ifdef __INTEL_COMPILER
printf("__INTEL_COMPILER ");
#endif
printf("\n");
if (argc != 3)
{
fprintf(stderr,"Usage: lztest num1bits numiters\n");
exit(1);
}
k = atoi(argv[1]);
if (k > WORDSIZE) k = WORDSIZE;
iters = atoi(argv[2]);
for (i = 0; i < 1000; ++i)
x[i] = ransetword(k);
tnull = timenull(x,1000,iters,&sumnull);
tmac = timemac(x,1000,iters,&summac);
tnew = timenew(x,1000,iters,&sumnew);
if (summac != sumnew) printf("*** sum mismatch (mac/old)\n");
printf("macro=%3.2f new=%3.2f\n",
tmac-tnull,tnew-tnull);
return 0;
}