9dd68d89599cc6fe8bcc2c3f379fb17a50f265ae
[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, iter, p;
32   int iterations = 0;
33   int count;
34
35   flags = (void *)malloc(sizeof(*flags) * SIZE);
36
37   // loop around for measurements
38   while(ITERATIONS > iterations) {
39     for(i = 0; i < SIZE; i++)
40       flags[i] = true;
41     for(i = 2; i < SIZE; i++) {
42       if(flags[i]) {
43         prime = i;
44         for(k = i + prime; k < SIZE; k += prime)
45           flags[k] = false;
46       }
47     }
48     iterations++;
49   }
50   // test correctness
51   count = 0;
52   for(i = 2; i < SIZE; i++) {
53     if(true == flags[i]) {
54       count++;
55     }
56   }
57   mark_count(count);
58 }
59
60 int main(int argc, char *argv[]) {
61   int i;
62
63   printf("Sieve.c\n");
64
65   if (argc <= 1) {
66       printf("\nUsage: Sieve n\n");
67       printf("Continuing with default input.\n");
68   } else {
69       SIZE = atoi(argv[1]);
70   }
71
72   runSieve();
73
74   return 0;
75 }