1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /*************************************************************** |
---|
5 | * File: p_ProcsGenerate.cc |
---|
6 | * Purpose: generate p_Procs*.inc at compile |
---|
7 | * Note: this file is included by p_Procs.cc |
---|
8 | * Author: obachman (Olaf Bachmann) |
---|
9 | * Created: 8/00 |
---|
10 | * Version: $Id$ |
---|
11 | *******************************************************************/ |
---|
12 | |
---|
13 | |
---|
14 | #include <stdio.h> |
---|
15 | #include <stdlib.h> |
---|
16 | #include <string.h> |
---|
17 | |
---|
18 | #include "polys/config.h" |
---|
19 | #include <reporter/reporter.h> |
---|
20 | |
---|
21 | |
---|
22 | #ifdef p_Procs_Static |
---|
23 | #include <polys/p_Procs_Static.h> |
---|
24 | #else |
---|
25 | #include <polys/p_Procs_Dynamic.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <polys/p_Procs_Impl.h> |
---|
29 | |
---|
30 | #ifndef p_Procs_Static |
---|
31 | int FieldGeneralProcs = 0, |
---|
32 | FieldIndepProcs = 0, |
---|
33 | FieldZpProcs = 0, |
---|
34 | FieldQProcs = 0, |
---|
35 | #ifdef HAVE_RINGS |
---|
36 | RingGeneralProcs = 0, |
---|
37 | #endif |
---|
38 | KernelProcs = 0, |
---|
39 | UnknownProcs = 0; |
---|
40 | |
---|
41 | // returns 1, if proc should go into kernel, 0 otherwise |
---|
42 | int IsKernelProc(p_Proc proc, p_Field field, p_Length length, p_Ord ord) |
---|
43 | { |
---|
44 | // general procs go into kernel |
---|
45 | if (field == FieldGeneral && length == LengthGeneral && ord == OrdGeneral) |
---|
46 | return 1; |
---|
47 | |
---|
48 | // plus procs with FieldZp |
---|
49 | if ((field == FieldZp || field == FieldQ) && |
---|
50 | // which are not general in length or ord |
---|
51 | !((length == LengthGeneral && p_ProcDependsOn_Length(proc)) || |
---|
52 | (ord == OrdGeneral && p_ProcDependsOn_Ord(proc))) && |
---|
53 | // and whose length is smaller than five |
---|
54 | (!p_ProcDependsOn_Length(proc) || (length >= LengthFour))) |
---|
55 | return 1; |
---|
56 | |
---|
57 | return 0; |
---|
58 | } |
---|
59 | |
---|
60 | #endif |
---|
61 | |
---|
62 | #define DoSetProc(what, field, length, ord) \ |
---|
63 | GenerateProc(#what, what##_Proc, field, length, ord) |
---|
64 | |
---|
65 | char*** generated_p_procs; |
---|
66 | |
---|
67 | inline int AlreadyHaveProc(p_Proc proc, p_Field field, p_Length length, p_Ord ord) |
---|
68 | { |
---|
69 | return (generated_p_procs[proc])[index(proc, field, length, ord)] != 0; |
---|
70 | } |
---|
71 | |
---|
72 | const char* macros_field[] = {"n_Copy","n_Delete", "n_Mult", "n_Add", "n_Sub", "n_IsZero", "n_Equal" , "n_Neg", "n_InpMult", "n_InpAdd", NULL}; |
---|
73 | |
---|
74 | const char* macros_length[] = |
---|
75 | {"p_MemCopy", "p_MemAdd", "p_MemSum", "p_MemDiff", NULL}; |
---|
76 | |
---|
77 | const char* macros_length_ord[] = {"p_MemCmp", NULL}; |
---|
78 | int DummyProcs = 0; |
---|
79 | |
---|
80 | int NumberOfHaveProcs = 0; |
---|
81 | |
---|
82 | void AddProc(const char* s_what, p_Proc proc, p_Field field, p_Length length, p_Ord ord) |
---|
83 | { |
---|
84 | NumberOfHaveProcs++; |
---|
85 | int i; |
---|
86 | const char* s_length = p_LengthEnum_2_String(length); |
---|
87 | const char* s_ord = p_OrdEnum_2_String(ord); |
---|
88 | const char* s_field = p_FieldEnum_2_String(field); |
---|
89 | char* s_full_proc_name = (char*) malloc(200); |
---|
90 | |
---|
91 | sprintf(s_full_proc_name, "%s__%s_%s_%s", s_what, s_field, s_length, s_ord); |
---|
92 | |
---|
93 | (generated_p_procs[proc])[index(proc, field, length, ord)] = s_full_proc_name; |
---|
94 | // define all macros |
---|
95 | printf("\n// definition of %s\n", s_full_proc_name); |
---|
96 | #ifndef p_Procs_Static |
---|
97 | if (IsKernelProc(proc, field, length, ord)) |
---|
98 | { |
---|
99 | KernelProcs++; |
---|
100 | printf("#ifdef p_Procs_Kernel\n"); |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | const char* module = p_ProcField_2_Module(proc, field); |
---|
105 | if (strcmp(module, "FieldGeneral") == 0) |
---|
106 | FieldGeneralProcs++; |
---|
107 | else if (strcmp(module, "FieldIndep") == 0) |
---|
108 | FieldIndepProcs++; |
---|
109 | else if (strcmp(module, "FieldZp") == 0) |
---|
110 | FieldZpProcs++; |
---|
111 | else if (strcmp(module, "FieldQ") == 0) |
---|
112 | FieldQProcs++; |
---|
113 | #ifdef HAVE_RINGS |
---|
114 | else if (strcmp(module, "RingGeneral") == 0) |
---|
115 | RingGeneralProcs++; |
---|
116 | #endif |
---|
117 | else |
---|
118 | UnknownProcs++; |
---|
119 | |
---|
120 | printf("#ifdef p_Procs_%s\n", module); |
---|
121 | } |
---|
122 | #endif |
---|
123 | #ifdef HAVE_RINGS |
---|
124 | if (strcmp(s_field, "RingGeneral") == 0) |
---|
125 | printf("#define HAVE_ZERODIVISORS\n"); |
---|
126 | #endif |
---|
127 | i = 0; |
---|
128 | while (macros_field[i] != NULL) |
---|
129 | { |
---|
130 | printf("#undef %s\n#define %s\t%s_%s\n", |
---|
131 | macros_field[i], macros_field[i], macros_field[i], s_field); |
---|
132 | i++; |
---|
133 | } |
---|
134 | i = 0; |
---|
135 | while (macros_length[i] != NULL) |
---|
136 | { |
---|
137 | printf("#undef %s\n#define %s\t%s_%s\n", |
---|
138 | macros_length[i], macros_length[i], macros_length[i], s_length); |
---|
139 | i++; |
---|
140 | } |
---|
141 | i = 0; |
---|
142 | while (macros_length_ord[i] != NULL) |
---|
143 | { |
---|
144 | printf("#undef %s\n#define %s\t%s_%s_%s\n", |
---|
145 | macros_length_ord[i], macros_length_ord[i], macros_length_ord[i], s_length, s_ord); |
---|
146 | i++; |
---|
147 | } |
---|
148 | |
---|
149 | // define DECLARE_LENGTH |
---|
150 | printf("#undef DECLARE_LENGTH\n"); |
---|
151 | printf("#undef p_MemAddAdjust\n"); |
---|
152 | if (length != LengthGeneral) |
---|
153 | { |
---|
154 | printf("#define DECLARE_LENGTH(what) ((void)0)\n"); |
---|
155 | printf("#define p_MemAddAdjust(p, r) ((void)0)\n"); |
---|
156 | } |
---|
157 | else |
---|
158 | { |
---|
159 | printf("#define DECLARE_LENGTH(what) what\n"); |
---|
160 | if (proc != pp_Mult_Coeff_mm_DivSelectMult_Proc) |
---|
161 | printf("#define p_MemAddAdjust(p, r) p_MemAdd_NegWeightAdjust(p, r)\n"); |
---|
162 | else |
---|
163 | printf("#define p_MemAddAdjust(p, r) ((void)0)\n"); |
---|
164 | } |
---|
165 | |
---|
166 | // define DECLARE_ORDSGN |
---|
167 | printf("#undef DECLARE_ORDSGN\n"); |
---|
168 | if (ord != OrdGeneral) |
---|
169 | printf("#define DECLARE_ORDSGN(what) ((void)0)\n"); |
---|
170 | else |
---|
171 | printf("#define DECLARE_ORDSGN(what) what\n"); |
---|
172 | |
---|
173 | if (proc == pp_Mult_Coeff_mm_DivSelectMult_Proc) |
---|
174 | { |
---|
175 | printf("#undef DECLARE_LENGTH_2\n"); |
---|
176 | printf("#undef p_MemCmp_Bitmask_2\n"); |
---|
177 | if (length != LengthGeneral) |
---|
178 | { |
---|
179 | printf("#define DECLARE_LENGTH_2(what) ((void)0)\n"); |
---|
180 | if (length < LengthTwo) |
---|
181 | printf("#define p_MemCmp_Bitmask_2 p_MemCmp_Bitmask_%s\n", p_LengthEnum_2_String((p_Length) ((int) length + 2))); |
---|
182 | else |
---|
183 | printf("#define p_MemCmp_Bitmask_2 p_MemCmp_Bitmask_LengthZero\n"); |
---|
184 | } |
---|
185 | else |
---|
186 | { |
---|
187 | printf("#define DECLARE_LENGTH_2(what) what \n"); |
---|
188 | printf("#define p_MemCmp_Bitmask_2 p_MemCmp_Bitmask_LengthGeneral\n"); |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | printf("#undef p_MemAddAdjust\n"); |
---|
193 | printf("#define p_MemAddAdjust(p, r) ((void)0)\n"); |
---|
194 | } |
---|
195 | |
---|
196 | printf("#undef %s\n#define %s %s\n", s_what, s_what, s_full_proc_name); |
---|
197 | printf("#include polys/1\n", s_what); |
---|
198 | printf("#undef %s\n", s_what); |
---|
199 | #ifdef HAVE_RINGS |
---|
200 | if (strcmp(s_field, "RingGeneral") == 0) |
---|
201 | printf("#undef HAVE_ZERODIVISORS\n"); |
---|
202 | #endif |
---|
203 | #ifndef p_Procs_Static |
---|
204 | printf("#endif // p_Procs_[Kernel|Field*]\n"); |
---|
205 | #endif |
---|
206 | } |
---|
207 | |
---|
208 | void GenerateProc(const char* s_what, p_Proc proc, p_Field field, p_Length length, p_Ord ord) |
---|
209 | { |
---|
210 | if (! AlreadyHaveProc(proc, field, length, ord)) |
---|
211 | AddProc(s_what, proc, field, length, ord); |
---|
212 | } |
---|
213 | |
---|
214 | int main() |
---|
215 | { |
---|
216 | int field = FieldGeneral; |
---|
217 | int length = LengthGeneral; |
---|
218 | int ord = OrdGeneral; |
---|
219 | int i; |
---|
220 | |
---|
221 | |
---|
222 | printf("/* -*-c++-*- */\n"); |
---|
223 | printf("/***************************************************************\n"); |
---|
224 | printf(" * This file was generated automatically by p_ProcsGenerate.cc: DO NOT EDIT\n"); |
---|
225 | printf(" *\n"); |
---|
226 | printf(" * This file provides the needed implementation of p_Procs for\n"); |
---|
227 | printf(" * %s\n", |
---|
228 | #if defined(p_Procs_Static) |
---|
229 | "p_Procs_Static" |
---|
230 | #else |
---|
231 | "p_Procs_Dynamic" |
---|
232 | #endif |
---|
233 | ); |
---|
234 | printf(" * See the end for a summary.\n"); |
---|
235 | printf(" *******************************************************************/\n"); |
---|
236 | |
---|
237 | |
---|
238 | generated_p_procs = (char***) malloc(p_Unknown_Proc*sizeof(char**)); |
---|
239 | for (i=0; i<p_Unknown_Proc; i++) |
---|
240 | { |
---|
241 | generated_p_procs[i] = |
---|
242 | (char**) calloc(index((p_Proc)i, FieldUnknown, LengthUnknown, OrdUnknown), sizeof(char*)); |
---|
243 | } |
---|
244 | |
---|
245 | // set default procs |
---|
246 | for (field = 0; field < (int) FieldUnknown; field++) |
---|
247 | { |
---|
248 | for (length=0; length < (int) LengthUnknown; length++) |
---|
249 | { |
---|
250 | for (ord=0; ord < (int)OrdUnknown; ord++) |
---|
251 | { |
---|
252 | if (IsValidSpec((p_Field) field, (p_Length) length, (p_Ord) ord)) |
---|
253 | SetProcs((p_Field) field, (p_Length) length, (p_Ord) ord); |
---|
254 | } |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | // we only need lookup tables for p_Procs_Static |
---|
259 | #ifdef p_Procs_Static |
---|
260 | int j; |
---|
261 | printf("\n" |
---|
262 | "/***************************************************************\n" |
---|
263 | "Names of procs for RDEBUG */\n" |
---|
264 | "#ifdef RDEBUG\n"); |
---|
265 | |
---|
266 | for (i=0; i<p_Unknown_Proc; i++) |
---|
267 | { |
---|
268 | printf("static const char* %s_names[] = {", p_ProcEnum_2_String((p_Proc)i)); |
---|
269 | for (j=0;j<index((p_Proc)i, FieldUnknown, LengthUnknown, OrdUnknown); j++) |
---|
270 | { |
---|
271 | char* s = (generated_p_procs[i])[j]; |
---|
272 | if (s != 0) |
---|
273 | { |
---|
274 | printf("\n\"%s\",", s); |
---|
275 | } |
---|
276 | else |
---|
277 | printf("0,"); |
---|
278 | |
---|
279 | } |
---|
280 | printf("\n};\n"); |
---|
281 | } |
---|
282 | printf("\n #endif // RDEBUG\n\n" |
---|
283 | "/***************************************************************/\n" |
---|
284 | "/* Tables for lookup of procedures: */\n"); |
---|
285 | |
---|
286 | for (i=0; i<p_Unknown_Proc; i++) |
---|
287 | { |
---|
288 | printf("static const %s_Ptr %s_funcs[] = {", p_ProcEnum_2_String((p_Proc)i), p_ProcEnum_2_String((p_Proc)i)); |
---|
289 | for (j=0;j<index((p_Proc)i, FieldUnknown, LengthUnknown, OrdUnknown); j++) |
---|
290 | { |
---|
291 | char* s = (generated_p_procs[i])[j]; |
---|
292 | if (s != 0) |
---|
293 | { |
---|
294 | printf("\n%s,", s); |
---|
295 | } |
---|
296 | else |
---|
297 | printf("0,"); |
---|
298 | } |
---|
299 | printf("\n};\n"); |
---|
300 | } |
---|
301 | #endif |
---|
302 | |
---|
303 | printf("\n/***************************************************************"); |
---|
304 | printf("* Summary:\n"); |
---|
305 | printf("* HAVE_FAST_P_PROCS = %d,\n",HAVE_FAST_P_PROCS); |
---|
306 | printf("* HAVE_FAST_FIELD = %d,\n",HAVE_FAST_FIELD); |
---|
307 | printf("* HAVE_FAST_LENGTH = %d,\n",HAVE_FAST_LENGTH); |
---|
308 | printf("* HAVE_FAST_ORD = %d,\n",HAVE_FAST_ORD); |
---|
309 | printf("* HAVE_FAST_ZERO_ORD = %d\n",HAVE_FAST_ZERO_ORD); |
---|
310 | printf("*\n"); |
---|
311 | printf("* Generated PolyProcs= %d\n",NumberOfHaveProcs); |
---|
312 | |
---|
313 | #ifndef p_Procs_Static |
---|
314 | printf(" *\n"); |
---|
315 | printf("* KernelProcs = %d\n",KernelProcs); |
---|
316 | printf("* FieldIndepProcs = %d\n",FieldIndepProcs); |
---|
317 | printf("* FieldZpProcs = %d\n",FieldZpProcs); |
---|
318 | printf("* FieldQProcs = %d\n",FieldQProcs); |
---|
319 | printf("* FieldGeneralProcs = %d\n",FieldGeneralProcs); |
---|
320 | printf("* FieldUnknownProcs = %d\n",UnknownProcs); |
---|
321 | #endif |
---|
322 | |
---|
323 | printf("*\n"); |
---|
324 | printf("*******************************************************************/\n"); |
---|
325 | } |
---|
326 | |
---|
327 | |
---|