Debian Codegraph @

imdi/ssort.c

raw symbols SCIP
1 /* Test code that generates optimised C sorting */
2 /* code, suitable for classifying an input vector */
3 /* into a particular simplex. */
4
5 /*
6 * Copyright 2000 Graeme W. Gill
7 * All rights reserved.
8 *
9 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
10 * see the License.txt file for licencing details.
11 */
12
13 /* Intended for inclusion in IMDI for 16 bit support */
14
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdarg.h>
19
20 /* Sorting data */
21 #define MAXSV 50 /* Maximum sort variables */
22 typedef struct {
23 int bot; /* 0 if we are doing ==, 1 if >, 2 if else */
24 int dval; /* Difference value to apply */
25 int vals[MAXSV]; /* Variable values */
26 int sort[MAXSV]; /* Sorted index */
27 } sortd;
28
29 /* Context */
30 typedef struct {
31 FILE *of; /* Output file */
32 int indt; /* Indent */
33
34 /* Sort data */
35 int nv; /* Number of variables to sort */
36 int sl; /* Non-zero to generate code for smallest to largest */
37 int eq; /* Non-zero to generate code for equal case */
38 int la; /* Non-zero to generate labels */
39 int rl; /* Recursion level */
40 sortd sd[MAXSV]; /* Data at each recursion level */
41 int ln; /* Label number */
42
43 } fileo;
44
45 void line(fileo *f, char *fmt, ...); /* Output one line */
46 void niline(fileo *f, char *fmt, ...); /* Output one line, no indent */
47 void sline(fileo *f, char *fmt, ...); /* Output start of line line */
48 void mline(fileo *f, char *fmt, ...); /* Output middle of line */
49 void eline(fileo *f, char *fmt, ...); /* Output end of line */
50
51 void cr(fileo *f) { line(f,""); } /* Output a blank line */
52
53 void inc(fileo *f) { f->indt++; } /* Increment the indent level */
54
55 void dec(fileo *f) { f->indt--; } /* Decrement the indent level */
56
57 int
58 main(void) {
59 fileo f[1];
60
61 f->of = fopen("ttt.c","w");
62 f->indt = 0;
63
64 line(f,"/* This is the top */");
65 cr(f);
66 line(f,"#include <stdio.h>");
67 cr(f);
68 line(f,"void main(void) {");
69 inc(f);
70 line(f,"int v0, v1, v2, v3, v4;");
71 line(f,"printf(\"Hi there!\\n\");");
72
73 line(f,"v0 = 1;");
74 line(f,"v1 = 9;");
75 line(f,"v2 = 3;");
76 line(f,"v3 = 2;");
77 line(f,"v4 = 0;");
78
79 /* ================ */
80 /* Sort development */
81 {
82 int xx;
83 int i, j;
84
85 f->nv = 6; /* No variables */
86 f->sl = 1; /* Smallest to largest */
87 f->eq = 0; /* No equals case */
88 f->la = 0; /* No labels */
89 f->rl = 0; /* Top recursion level */
90 f->ln = 0; /* Start label number */
91 f->sd[f->rl].bot = f->eq ? 0 : 1;
92 f->sd[f->rl].dval = 256;
93 for (i = 0; i < f->nv; i++) {
94 f->sd[f->rl].vals[i] = -99999; /* Initial sort value = unknown */
95 f->sd[f->rl].sort[i] = i; /* Initial sort order */
96 }
97 f->sd[f->rl].vals[0] = 0; /* Make sure one is known */
98
99 /* First label is for no known sort */
100 if (f->la)
101 niline(f," label%d:;",f->ln++);
102
103 /* Until we have done every sort condition */
104 for (;f->rl >= 0;) {
105 int ix, nix; /* Variable index that needs comparison */
106 int six, snix; /* Sorted references to ix, nix */
107
108 //printf("\n~~recursion level %d\n",f->rl);
109 //for (i = 0; i < f->nv; i++) {
110 //xx = f->sd[f->rl].sort[i];
111 //printf("Current = %d (index %d)\n",f->sd[f->rl].vals[xx],xx);
112 //}
113 /* see if we have resolved a sort */
114 for (i = 1; i < f->nv; i++) {
115 six = i-1;
116 ix = f->sd[f->rl].sort[i-1];
117 snix = i;
118 nix = f->sd[f->rl].sort[i];
119 if (f->sd[f->rl].vals[ix] == f->sd[f->rl].vals[nix]
120 || f->sd[f->rl].vals[nix] == -99999) {
121 break; /* Not distinguishable or not known */
122 }
123 }
124 //if (i < f->nv)
125 //printf("~~Unresolved sort indexes %d %d\n",ix,nix);
126 //else
127 //printf("~~Resolved sort\n");
128
129 if (i < f->nv) {
130 /* We haven't fully sorted the variables yet. */
131 /* Compare ix to nix */
132 if (f->sd[f->rl].bot == 0) {
133 //printf("~~ Unresolved at level %d, doing comparison for equals\n",f->rl);
134 line(f,"if (v%d == v%d) {",ix,nix); /* } */
135 inc(f);
136 f->sd[f->rl+1] = f->sd[f->rl]; /* Structure copy */
137 f->sd[f->rl+1].bot = 0;
138 f->sd[f->rl].bot = 1;
139 f->rl++;
140 // ~~~~~9
141 /* Find next lowest value from nix */
142 for (i = snix+1; i < f->nv; i++) {
143 int si = f->sd[f->rl].sort[i];
144 if (f->sd[f->rl].vals[si] == -99999)
145 continue;
146 if (f->sd[f->rl].vals[si] < f->sd[f->rl].vals[nix]) {
147 f->sd[f->rl].vals[nix] =
148 (f->sd[f->rl].vals[si] + f->sd[f->rl].vals[nix])/2;
149 break;
150 }
151 }
152 if (i >= f->nv) { /* Not between next lowest */
153 f->sd[f->rl].vals[nix] = f->sd[f->rl].vals[ix] -256;
154 }
155 } else if (f->sd[f->rl].bot == 1) { /* { */
156 //printf("~~ Unresolved at level %d, doing comparison for greater\n",f->rl);
157 if (f->eq) {
158 dec(f);
159 line(f,"} else if (v%d %c v%d) {",ix, f->sl ? '<':'>',nix); /* } */
160 } else
161 line(f,"if (v%d %c v%d) {",ix,f->sl ? '<':'>', nix); /* } */
162 inc(f);
163 if (f->la)
164 niline(f," label%d:;",f->ln++);
165 f->sd[f->rl+1] = f->sd[f->rl]; /* Structure copy */
166 f->sd[f->rl+1].bot = f->eq ? 0 : 1;
167 f->sd[f->rl].bot = 2;
168 f->rl++;
169 /* Find next lowest value from nix */
170 for (i = snix+1; i < f->nv; i++) {
171 int si = f->sd[f->rl].sort[i];
172 if (f->sd[f->rl].vals[si] == -99999)
173 continue;
174 if (f->sd[f->rl].vals[si] < f->sd[f->rl].vals[nix]) {
175 f->sd[f->rl].vals[nix] =
176 (f->sd[f->rl].vals[si] + f->sd[f->rl].vals[nix])/2;
177 break;
178 }
179 }
180 if (i >= f->nv) { /* Not between next lowest */
181 f->sd[f->rl].vals[nix] = f->sd[f->rl].vals[ix] -256;
182 }
183 } else if (f->sd[f->rl].bot == 2) { /* { */
184 //printf("~~ Unresolved at level %d, doing else\n",f->rl);
185 dec(f);
186 line(f,"} else { /* v%d %c v%d */",ix,f->sl ? '>':'<', nix); /* } */
187 inc(f);
188 if (f->la)
189 niline(f," label%d:;",f->ln++);
190 f->sd[f->rl+1] = f->sd[f->rl]; /* Structure copy */
191 f->sd[f->rl+1].bot = f->eq ? 0 : 1;
192 f->sd[f->rl].bot = 3;
193 f->rl++;
194 if (six-1 >= 0) { /* If there is a higher value */
195 int si = f->sd[f->rl].sort[six-1];
196 /* Make equal to next highest, since never been compared */
197 f->sd[f->rl].vals[nix] = f->sd[f->rl].vals[si];
198 } else { /* Nothing above */
199 f->sd[f->rl].vals[nix] = f->sd[f->rl].vals[ix] + 256;
200 }
201 /* sort nix above ix */
202 j = f->sd[f->rl].sort[six];
203 f->sd[f->rl].sort[six] = f->sd[f->rl].sort[snix];
204 f->sd[f->rl].sort[snix] = j;
205 } else { /* We've done both cases - return from recursion */
206 //printf("~~ Unresolved at level %d, going up a level\n",f->rl);
207 dec(f); /* { */
208 line(f,"}");
209 f->rl--; /* Back a recursion level */
210 }
211 } else {
212 //printf("~~ Resolved at level %d, outputing sort code, up a level\n",f->rl);
213 /* We have a resolved sort */
214 /* So output the code for this sort combination */
215 sline(f,"/* Sorted ");
216 for (i = 0; i < f->nv; i++) {
217 mline(f," %d",f->sd[f->rl].sort[i]);
218 }
219 eline(f," */");
220
221 #ifdef NEVER
222 sline(f,"printf(\"Sorted = %%d %%d %%d %%d %%d\\n\"");
223 for (i = 0; i < f->nv; i++) {
224 mline(f,",v%d",f->sd[f->rl].sort[i]);
225 }
226 eline(f,");");
227 #else
228 for (i = 0; i < f->nv; i++) {
229 sline(f,"");
230 mline(f,"op[%d] = v%d",i,f->sd[f->rl].sort[i]);
231 eline(f,";");
232 }
233 line(f,"ip += %d;",f->nv);
234 line(f,"op += %d;",f->nv);
235 line(f,"continue;");
236 #endif
237 f->rl--; /* Back a recursion level */
238 }
239 }
240 }
241 /* ================ */
242 dec(f);
243 line(f,"}");
244
245 fclose(f->of);
246
247 return 0;
248 }
249
250
251 /* Output a line to the file (including trailing \n) */
252 void
253 line(fileo *f, char *fmt, ...)
254 {
255 int i;
256 va_list args;
257
258 /* Indent to the correct level */
259 for (i = 0; i < f->indt; i++)
260 fprintf(f->of," ");
261
262 va_start(args, fmt);
263 vfprintf(f->of, fmt, args);
264 va_end(args);
265 fprintf(f->of, "\n");
266 }
267
268 /* Output a line to the file (including trailing \n) */
269 /* No indent */
270 void
271 niline(fileo *f, char *fmt, ...)
272 {
273 int i;
274 va_list args;
275
276 va_start(args, fmt);
277 vfprintf(f->of, fmt, args);
278 va_end(args);
279 fprintf(f->of, "\n");
280 }
281
282 /* Output the start of a line to the file) */
283 void
284 sline(fileo *f, char *fmt, ...)
285 {
286 int i;
287 va_list args;
288
289 /* Indent to the correct level */
290 for (i = 0; i < f->indt; i++)
291 fprintf(f->of," ");
292
293 va_start(args, fmt);
294 vfprintf(f->of, fmt, args);
295 va_end(args);
296 }
297
298 /* Output the middle of a line to the file) */
299 void
300 mline(fileo *f, char *fmt, ...)
301 {
302 int i;
303 va_list args;
304
305 va_start(args, fmt);
306 vfprintf(f->of, fmt, args);
307 va_end(args);
308 }
309
310 /* Output the end of a line to the file (including trailing \n) */
311 void
312 eline(fileo *f, char *fmt, ...)
313 {
314 int i;
315 va_list args;
316
317 va_start(args, fmt);
318 vfprintf(f->of, fmt, args);
319 va_end(args);
320 fprintf(f->of, "\n");
321 }
322