combo error
[libfirm] / ir / be / test / harness.c
1 #include <stdio.h>
2 #define MAXPRINTABLE 128
3 #include "rantext.h"
4 #if ULTRIX || _AIX || _WIN32
5 #include <fcntl.h>
6 #else
7 #include <sys/fcntl.h>
8 #endif
9
10 double ran();
11 double ran2();
12 char   getranchar();
13 #define BUFFERSIZE      14500000
14 long int seedi;
15 #define COMPRESS 0
16 #define UNCOMPRESS 1
17
18 double prob_tab[MAXPRINTABLE][MAXPRINTABLE];
19
20 int
21 add_line(char* buf, int count, int num_letters, char letter)
22 {
23 int i;
24   for (i = count; i < (count + num_letters); i ++)
25   {
26         buf[i]=letter;
27   }
28         count=i;
29         buf[count]='\n';
30         count=count+1;
31         return(count);
32 }
33
34 fill_text_buffer(int count, char start_char, char* text_buffer)
35 {
36         long int total;         /* normalization */
37         int i,j;                /* array indexers */
38         char c1,c2;             /* character holders for level-three search */
39         int     bufindex;
40
41         /*
42          * For each ith, jth element in the frequency table, set the
43          * ith, jth, element of the probability table to the frequency
44          * table entry divided by the total.
45          */
46
47         for (i=0;i<128;i++)
48         {
49                 total = 0.0;
50                 for (j=0;j<128;j++)
51                 {
52                         total += freq_tab[i][j];
53                 }
54                 if (total<1)
55                         total=1;
56                 for (j=0;j<128;j++)
57                 {
58                         prob_tab[i][j] = (double)freq_tab[i][j]/(double)total;
59                 }
60         }
61         /*
62          * For each ith element in the probability table, make the
63          * jth elements cumulative in order to simplify 'getranchar'.
64         */
65
66         for (i=0;i<128;i++)
67         {
68                 for (j=1;j<128;j++)
69                 {
70                         prob_tab[i][j]+=prob_tab[i][j-1];
71                 }
72         }
73
74 #if SDEBUG
75         fprintf(stderr,
76                 "Probability table built, about to open file \n");
77         fflush(stderr) ;
78 #endif /* SDEBUG */
79
80         /*
81          * Start off the simulation with seed letter.
82          */
83
84         c1=start_char;
85
86         /*
87          * Get "count" characters and spit 'em out.
88          */
89
90         count-- ; /* pre-decrement for the final new line */
91
92         bufindex = 0 ;
93         while (count>0)
94         {
95                 c2=getranchar(c1,ran2());
96                 text_buffer[bufindex++]=c2 ;
97 #if SDEBUG>2
98                 fprintf(stderr,"Character number %d is %c\n",
99                         bufindex,c2) ;
100                 fflush(stderr) ;
101 #endif /* SDEBUG */
102                 c1=c2;
103                 count--;
104         }
105         /*
106          * Complete the last line
107          */
108
109         c2 = '\n' ;
110         text_buffer[bufindex++]=c2 ;
111 }
112
113 /* Routine For dumping contents of a buffer to the output
114         Jeff Reilly, 1/15/95                            */
115 print_buffer(int count, char* text_buffer)
116
117 {
118 int i;
119
120 for (i=0;i<count;i++)
121         printf("%c", text_buffer[i]);
122
123
124 }
125
126 char getranchar(c,rnno)
127 char c;
128 double rnno;
129
130 {
131         int mid, k;
132         int low=0;
133         int high=127;
134
135         /*
136  * Ascend the jth column (given by c1).
137  * if the cumulative probability exceeds the random number, then
138  * the current (char) i is the character to return.
139  */
140         if (rnno > prob_tab[c][127])
141                 return ('e');
142
143         for (k=0;k<7;k++)
144         {
145                 mid = (low+high)>>1;
146                 if (rnno < prob_tab[c][mid])
147                         high = mid;
148                 else if (rnno > prob_tab[c][mid])
149                         low = mid + 1;
150                 else    /* exact match found -unlikely */
151                         return ((char)mid);
152         }
153         return ((char)low);
154 }
155
156 double ran2()
157 {
158   seedi=((314157*seedi)+19)&0xffffff;
159   return ( (double) seedi/(double)0xffffff);
160 }
161
162 double ran()
163 /* See "Random Number Generators: Good Ones Are Hard To Find", */
164 /*     Park & Miller, CACM 31#10 October 1988 pages 1192-1201. */
165 /***********************************************************/
166 /* THIS IMPLEMENTATION REQUIRES AT LEAST 32 BIT INTEGERS ! */
167 /***********************************************************/
168 #define _A_MULTIPLIER  16807L
169 #define _M_MODULUS     2147483647L /* (2**31)-1 */
170 #define _Q_QUOTIENT    127773L     /* 2147483647 / 16807 */
171 #define _R_REMAINDER   2836L       /* 2147483647 % 16807 */
172 {
173         long lo;
174         long hi;
175         long test;
176
177         hi = seedi / _Q_QUOTIENT;
178         lo = seedi % _Q_QUOTIENT;
179         test = _A_MULTIPLIER * lo - _R_REMAINDER * hi;
180         if (test > 0) {
181                 seedi = test;
182         } else {
183                 seedi = test + _M_MODULUS;
184         }
185         return ( (float) seedi / _M_MODULUS);
186 }
187
188 compare_buffer(char* buf1, int count1, char* buf2, int count2)
189 {
190 if (count1 == count2)
191 {
192   printf("Files both have length %d\n", count1);
193   if (count1 > 0)
194   {
195     if ( (buf1[0] == buf2[0]) && (buf1[count1-1] == buf2[count2-1]) )
196     {
197       printf("First character (%c) and Last Character (%c) match. \n", buf1[0], buf1[count1-1]);
198     }
199     else
200     {
201       printf("First and last characters do not match.\n");
202       printf("%c does not match %c\n", buf1[0], buf2[0]);
203       printf("or %c does not match %c\n", buf1[count1-1], buf2[count2-1]);
204     }
205   }
206 }
207 else
208 {
209   printf("Warning: Files of differing lengths: %d and %d\n", count1, count2);
210 }
211
212
213 }
214
215 char    orig_text_buffer[BUFFERSIZE], comp_text_buffer[BUFFERSIZE], new_text_buffer[BUFFERSIZE];
216
217
218 int main(int argc, char *argv[])
219
220 {
221 int count, i;
222 int new_count;
223 char    start_char;
224 int comp_count = 0;
225
226         printf("SPEC 129.compress harness\n");
227
228         //scanf("%i     %c      %li", &count, &start_char, &seedi);
229         count = 10;
230         start_char=10;
231         seedi = 12345;
232         printf("Initial File Size:%i    Start character:%c\n", count, start_char, seedi);
233         fill_text_buffer(count, start_char, orig_text_buffer);
234         for (i = 1; i <= 25; i++)
235         {
236           new_count=add_line(orig_text_buffer, count, i, start_char);
237           count=new_count;
238           //oper=COMPRESS;
239           printf("The starting size is: %d\n", count);
240           //comp_count=spec_select_action(orig_text_buffer, count, oper, comp_text_buffer);
241           printf("The compressed size is: %d\n", comp_count);
242           //oper=UNCOMPRESS;
243           //new_count=spec_select_action(comp_text_buffer, comp_count, oper, new_text_buffer);
244           printf("The compressed/uncompressed size is: %d\n", new_count);
245           //compare_buffer(orig_text_buffer, count, new_text_buffer, new_count);
246         }
247 /* Remove comments for Debugging */
248 /*
249         printf("Original Text File:\n");
250         print_buffer(count, orig_text_buffer);
251         printf("New Text File:\n");
252         print_buffer(count, new_text_buffer); */
253
254         return 0;
255 }