fix problems with multispill/belady
[libfirm] / ir / be / test / Sieve.c
1 /*
2  * Project:     GCC-firm
3  * File name:   test/Sieve.c
4  * Purpose:     Eratosthenes Sieve prime number benchmark in Java
5  * Author:      Boris Boesler
6  * Modified by: Michael Beck (for GCC-firm)
7  * Created:     XX.08.2001
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2001 Universitaet Karlsruhe
10  * Licence:
11  */
12
13 #include <stdlib.h>
14 #include <stdio.h>
15
16 typedef char boolean;
17
18 #define true    1
19 #define false   0
20
21 static int SIZE = 500; //8190;
22 /* Gl: 8190 takes too long for continuous testing. */
23
24 static void mark_count(int c) {
25         printf("number of primes in [2..%d) : %d (correct: 2..500: 95)\n", SIZE, c);
26 }
27
28 static void runSieve(void) {
29         int ITERATIONS = 100000;
30         boolean *flags;
31         int i, prime, k;
32         int iterations = 0;
33         int count;
34         int s = SIZE;
35
36         flags = (void *)malloc(sizeof(*flags) * s);
37
38         // loop around for measurements
39         while(ITERATIONS > iterations) {
40 #if 0
41                 for(i = 0; i < SIZE; i++)
42                         flags[i] = true;
43 #endif
44                 memset(flags, 0x01010101, s);
45                 for(i = 2; i < s; i++) {
46                         if(flags[i]) {
47                                 prime = i;
48                                 for(k = i + prime; k < s; k += prime)
49                                         flags[k] = false;
50                         }
51                 }
52                 iterations++;
53         }
54         // test correctness
55         count = 0;
56         for(i = 2; i < s; i++) {
57                 if(true == flags[i]) {
58                         count++;
59                 }
60         }
61         mark_count(count);
62 }
63
64 int main(int argc, char *argv[]) {
65         printf("Sieve.c\n");
66
67         if (argc <= 1) {
68                 printf("\nUsage: Sieve n\n");
69                 printf("Continuing with default input.\n");
70         } else {
71                 SIZE = atoi(argv[1]);
72         }
73
74         runSieve();
75
76         return 0;
77 }