need includes for alloca
[libfirm] / ir / be / test / langshootout / nsieve.c
1 // The Computer Language Shootout
2 // http://shootout.alioth.debian.org/
3 // Precedent C entry modified by bearophile for speed and size, 31 Jan 2006
4 // Compile with:  -O3 -s -std=c99 -fomit-frame-pointer
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 typedef unsigned char boolean;
11
12
13 static void nsieve(int m) {
14     unsigned int count = 0, i, j;
15     boolean * flags = (boolean *) malloc(m * sizeof(boolean));
16     memset(flags, 1, m);
17
18     for (i = 2; i < m; ++i)
19         if (flags[i]) {
20             ++count;
21             for (j = i << 1; j < m; j += i)
22                 if (flags[j]) flags[j] = 0;
23     }
24
25     free(flags);
26     printf("Primes up to %8u %8u\n", m, count);
27 }
28
29 int main(int argc, char * argv[]) {
30         int i;
31     int m = 8;
32
33         if(argc > 1)
34                 m = atoi(argv[1]);
35
36     for (i = 0; i < 3; i++)
37         nsieve(10000 << (m-i));
38     return 0;
39 }