blob: 6ac503e11e35ad844980294645ea8eead938e6e6 (
plain) (
blame)
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
|
/* primes below N. the same loops as tests/bench/sieve.zc.
word: one int64 flag per number (what zc's fast path can do).
byte: one byte per number, the representation a C sieve would actually use.
the count is 9592 either way.
usage: sieve word|byte REPS */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
enum { N = 100000, EXPECT = 9592 };
static int sieve_word(int64_t *restrict f)
{
int c = 0;
for (int i = 2; i < N; i++) f[i] = 1;
for (int i = 2; (long long)i * i < N; i++)
if (f[i])
for (int j = i * i; j < N; j += i) f[j] = 0;
for (int i = 2; i < N; i++) c += (int)f[i];
return c;
}
static int sieve_byte(unsigned char *restrict f)
{
int c = 0;
for (int i = 2; i < N; i++) f[i] = 1;
for (int i = 2; (long long)i * i < N; i++)
if (f[i])
for (int j = i * i; j < N; j += i) f[j] = 0;
for (int i = 2; i < N; i++) c += f[i];
return c;
}
int main(int argc, char **argv)
{
int reps, r = 0, k, byte;
int64_t *w = 0;
unsigned char *b = 0;
if (argc != 3) return 2;
byte = argv[1][0] == 'b';
reps = atoi(argv[2]);
if (byte) b = malloc(N);
else w = malloc((size_t)N * sizeof *w);
if (!w && !b) return 3;
for (k = 0; k < reps; k++) {
r = byte ? sieve_byte(b) : sieve_word(w);
/* the stores have to happen: the next pass must see a clobbered buffer */
__asm__ __volatile__("" : "+r"(r) : "r"(byte ? (void *)b : (void *)w) : "memory");
}
if (r != EXPECT) {
fprintf(stderr, "bad %d\n", r);
return 1;
}
printf("%d\n", r);
free(w);
free(b);
return 0;
}
|