1 | /*****************************************************************************\ |
---|
2 | * Computer Algebra System SINGULAR |
---|
3 | \*****************************************************************************/ |
---|
4 | /** @file auxiliary.h |
---|
5 | * |
---|
6 | * All the auxiliary stuff. |
---|
7 | * |
---|
8 | * ABSTRACT: we shall put here everything that does not have its own place. |
---|
9 | * |
---|
10 | * @author Oleksandr Motsak |
---|
11 | * |
---|
12 | * @internal @version \$Id$ |
---|
13 | * |
---|
14 | **/ |
---|
15 | /*****************************************************************************/ |
---|
16 | |
---|
17 | #ifndef MISC_AUXILIARY_H |
---|
18 | #define MISC_AUXILIARY_H |
---|
19 | |
---|
20 | // ----------------- which parts/extensions of Singular to build |
---|
21 | #define HAVE_RINGS |
---|
22 | #define HAVE_PLURAL |
---|
23 | |
---|
24 | // no factory yet... |
---|
25 | // #define HAVE_FACTORY |
---|
26 | |
---|
27 | |
---|
28 | // ---------------- end of parts/extensions |
---|
29 | // ----------------- configure stuff |
---|
30 | |
---|
31 | /* |
---|
32 | // CPU type: i[3456]86: |
---|
33 | #undef SI_CPU_I386 |
---|
34 | // CPU type: sparc: |
---|
35 | #undef SI_CPU_SPARC |
---|
36 | // CPU type: ppc: |
---|
37 | #undef SI_CPU_PPC |
---|
38 | // CPU type: IA64: |
---|
39 | #undef SI_CPU_IA64 |
---|
40 | // CPU type: x86_64: |
---|
41 | #undef SI_CPU_X86_64 |
---|
42 | // Define sizeof(long) |
---|
43 | #define SIZEOF_LONG 4 |
---|
44 | */ |
---|
45 | |
---|
46 | // TODO: the following should go to some config.h... no? |
---|
47 | |
---|
48 | /* Define version as a string */ |
---|
49 | #define S_VERSION1 "spielwiese" |
---|
50 | |
---|
51 | /* Absolute pathname of root directory of Singular source */ |
---|
52 | #define S_ROOT_DIR "" |
---|
53 | |
---|
54 | // ----------------- end of configure stuff |
---|
55 | |
---|
56 | // ---------------- Singular standard types etc. |
---|
57 | // BOOLEAN |
---|
58 | |
---|
59 | #ifndef SIZEOF_LONG |
---|
60 | |
---|
61 | #include <limits.h> |
---|
62 | |
---|
63 | #ifndef LONG_BIT |
---|
64 | #if ULONG_MAX == 0xffffffffUL |
---|
65 | #define LONG_BIT 32 |
---|
66 | #elif ULONG_MAX == 0xffffffffffffffffULL |
---|
67 | #define LONG_BIT 64 |
---|
68 | #else |
---|
69 | #error "Unexpected max for unsigned long" |
---|
70 | #endif |
---|
71 | #endif |
---|
72 | |
---|
73 | #define SIZEOF_LONG (LONG_BIT/CHAR_BIT) |
---|
74 | // another option for SIZEOF_LONG: use omConfig included in <omalloc/omalloc.h>... |
---|
75 | |
---|
76 | #endif |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | #if (SIZEOF_LONG == 8) |
---|
83 | typedef int BOOLEAN; |
---|
84 | /* testet on x86_64, gcc 3.4.6: 2 % */ |
---|
85 | /* testet on IA64, gcc 3.4.6: 1 % */ |
---|
86 | #else |
---|
87 | /* testet on athlon, gcc 2.95.4: 1 % */ |
---|
88 | typedef short BOOLEAN; |
---|
89 | #endif |
---|
90 | |
---|
91 | #ifndef FALSE |
---|
92 | #define FALSE 0 |
---|
93 | #endif |
---|
94 | |
---|
95 | #ifndef TRUE |
---|
96 | #define TRUE 1 |
---|
97 | #endif |
---|
98 | |
---|
99 | #ifndef NULL |
---|
100 | #define NULL (0) |
---|
101 | #endif |
---|
102 | |
---|
103 | // #ifdef _TRY |
---|
104 | #ifndef ABS |
---|
105 | #define ABS(x) ((x)<0?(-(x)):(x)) |
---|
106 | #endif |
---|
107 | // #endif |
---|
108 | |
---|
109 | static const int MAX_INT_LEN= 11; |
---|
110 | typedef void* ADDRESS; |
---|
111 | |
---|
112 | #define loop for(;;) |
---|
113 | |
---|
114 | #if defined(__cplusplus) |
---|
115 | static inline int si_max(const int a, const int b) { return (a>b) ? a : b; } |
---|
116 | static inline int si_min(const int a, const int b) { return (a<b) ? a : b; } |
---|
117 | static inline long si_max(const long a, const long b) { return (a>b) ? a : b; } |
---|
118 | static inline unsigned long si_max(const unsigned long a, const unsigned long b) { return (a>b) ? a : b; } |
---|
119 | static inline long si_min(const long a, const long b) { return (a<b) ? a : b; } |
---|
120 | static inline unsigned long si_min(const unsigned long a, const unsigned long b) { return (a<b) ? a : b; } |
---|
121 | #else |
---|
122 | #define si_max(A,B) ((A) > (B) ? (A) : (B)) |
---|
123 | #define si_min(A,B) ((A) < (B) ? (A) : (B)) |
---|
124 | #endif |
---|
125 | |
---|
126 | |
---|
127 | // ---------------- end of Singular standard types etc. |
---|
128 | // ---------------- defines which depend on the settings above |
---|
129 | |
---|
130 | #if defined(SI_CPU_I386) || defined(SI_CPU_X86_64) |
---|
131 | // the following settings seems to be better on i386 and x86_64 processors |
---|
132 | // define if a*b is with mod instead of tables |
---|
133 | #define HAVE_MULT_MOD |
---|
134 | // #define HAVE_GENERIC_ADD |
---|
135 | // #ifdef HAVE_MULT_MOD |
---|
136 | // #define HAVE_DIV_MOD |
---|
137 | // #endif |
---|
138 | #elif defined(SI_CPU_IA64) |
---|
139 | // the following settings seems to be better on itanium processors |
---|
140 | // #define HAVE_MULT_MOD |
---|
141 | #define HAVE_GENERIC_ADD |
---|
142 | // #ifdef HAVE_MULT_MOD |
---|
143 | // #define HAVE_DIV_MOD |
---|
144 | // #endif |
---|
145 | #elif defined(SI_CPU_SPARC) |
---|
146 | // #define HAVE_GENERIC_ADD |
---|
147 | #define HAVE_MULT_MOD |
---|
148 | #ifdef HAVE_MULT_MOD |
---|
149 | #define HAVE_DIV_MOD |
---|
150 | #endif |
---|
151 | #elif defined(SI_CPU_PPC) |
---|
152 | // the following settings seems to be better on ppc processors |
---|
153 | // testet on: ppc_Linux, 740/750 PowerMac G3, 512k L2 cache |
---|
154 | #define HAVE_MULT_MOD |
---|
155 | // #ifdef HAVE_MULT_MOD |
---|
156 | // #define HAVE_DIV_MOD |
---|
157 | // #endif |
---|
158 | #endif |
---|
159 | |
---|
160 | |
---|
161 | /******************************************************************* |
---|
162 | * DEBUG OPTIONS |
---|
163 | * -- only significant for for compiling without -DNDEBUG |
---|
164 | * -- you better know what your are doing, if you touch this |
---|
165 | ******************************************************************/ |
---|
166 | #ifndef NDEBUG |
---|
167 | |
---|
168 | /* undefine to enable inline */ |
---|
169 | #define NO_INLINE |
---|
170 | |
---|
171 | /* undefine to disable assume -- should normally be defined for NDEBUG */ |
---|
172 | #define HAVE_ASSUME |
---|
173 | |
---|
174 | /* undef PDEBUG to disable checks of polys |
---|
175 | |
---|
176 | define PDEBUG to |
---|
177 | 0 for enabling pTest |
---|
178 | 1 plus tests in Level 1 poly routines (operations on monomials) |
---|
179 | 2 plus tests in Level 2 poly routines (operations on single exponents) |
---|
180 | -- see also polys.h for more info |
---|
181 | |
---|
182 | NOTE: you can set the value of PDEBUG on a per-file basis, before |
---|
183 | including mod2.h, provided ! PDEBUG is defined in mod2.h E.g.: |
---|
184 | |
---|
185 | #define PDEBUG 2 |
---|
186 | #include "auxiliary.h" |
---|
187 | ... |
---|
188 | |
---|
189 | makes sure that all poly operations in your file are done with |
---|
190 | PDEBUG == 2 |
---|
191 | To break after an error occured, set a debugger breakpoint on |
---|
192 | dErrorBreak. |
---|
193 | */ |
---|
194 | #ifndef PDEBUG |
---|
195 | #define PDEBUG 0 |
---|
196 | #endif |
---|
197 | |
---|
198 | /* define MDEBUG to enable memory checks */ |
---|
199 | #define MDEBUG 0 |
---|
200 | |
---|
201 | #ifdef MDEBUG |
---|
202 | /* If ! defined(OM_NDEBUG) and (defined(OM_TRACK) or defined(OM_CHECK) |
---|
203 | then omDebug routines are used for memory allocation/free: |
---|
204 | |
---|
205 | The omDebug routines are controlled by the values of OM_TRACK, OM_CHECK |
---|
206 | and OM_KEEP. There meaning is roughly as follows: |
---|
207 | OM_TRACK: strored with address : extra space |
---|
208 | 0 : no additional info is stored : 0 |
---|
209 | 1 : file:line of location where address was allocated : 1 word |
---|
210 | 2 : plus backtrace of stack where adress was allocated: 6 words |
---|
211 | 3 : plus size/bin info and front-, and back padding : 9 words |
---|
212 | 4 : plus file:line of location where adress was freed : 10 words |
---|
213 | 5 : plus backtrace of stack where adress was allocated: 15 words |
---|
214 | OM_CHECK: checks done |
---|
215 | 0 : no checks |
---|
216 | 1 : constant-time checks: i.e. addr checks only |
---|
217 | 2 : plus linear-time checks and constant related bin check |
---|
218 | 3 : plus quadratic-time checks and linear-time related bin checks and |
---|
219 | constant time all memory checks |
---|
220 | 4 : and so on |
---|
221 | ==> for OM_CHECK >= 3 it gets rather slow |
---|
222 | OM_KEEP: determines whether addresses are really freed ( |
---|
223 | 0 : addresses are really freed |
---|
224 | 1 : addresses are only marked as free and not really freed. |
---|
225 | |
---|
226 | OM_CHECK, OM_TRACK, and OM_KEEP can be set on a per-file basis |
---|
227 | (as can OM_NDEBUG), e.g.: |
---|
228 | #define OM_CHECK 3 |
---|
229 | #define OM_TRACK 5 |
---|
230 | #define OM_KEEP 1 |
---|
231 | #include "mod2.h" |
---|
232 | #include "omalloc.h" |
---|
233 | ensures that all memory allocs/free in this file are done with |
---|
234 | OM_CHECK==3 and OM_TRACK==5, and that all addresses allocated/freed |
---|
235 | in this file are only marked as free and never really freed. |
---|
236 | |
---|
237 | To set OM_CHECK, OM_TRACK and OM_KEEP under dynamic scope, set |
---|
238 | om_Opts.MinCheck, om_Opts.MinTrack to the respectiv values and |
---|
239 | om_Opts.Keep to the number of addresses which are kept before they are |
---|
240 | actually freed. E.g.: |
---|
241 | int check=om_Opts.MinCheck, track=om_Opts.MinTrack, keep= m_OPts.Keep; |
---|
242 | om_Opts.MinCheck = 3; om_Opts.MinTrack = 5; omOpts.Keep = LONG_MAX; |
---|
243 | ExternalRoutine(); |
---|
244 | om_Opts.MinCheck = check; omOpts.MinTrack = track; omOpts.Keep = keep; |
---|
245 | ensures that all calls omDebug routines occuring during the computation of |
---|
246 | ExternalRoutine() are done with OM_CHECK==3 and OM_TRACK==5, and |
---|
247 | calls to omFree only mark addresses as free and not really free them. |
---|
248 | |
---|
249 | Furthermore, the value of OM_SING_KEEP (resp. om_Opts.Keep) specifies |
---|
250 | how many addresses are kept before they are actually freed, independently |
---|
251 | of the value of OM_KEEP. |
---|
252 | |
---|
253 | Some tips on possible values of OM_TRACK, OM_CHECK, OM_KEEP: |
---|
254 | + To find out about an address that has been freed twice, first locate the |
---|
255 | file(s) where the error occured, and then at the beginning of these files: |
---|
256 | #define OM_CHECK 3 |
---|
257 | #define OM_TRACK 5 |
---|
258 | #define OM_KEEP 1 |
---|
259 | #include "mod2.h" |
---|
260 | #include "omalloc.h" |
---|
261 | Under dynamic scope, do (e.g., from within the debugger): |
---|
262 | om_Opts.MinCheck = 3; om_Opts.MinTrack = 5; omOpts.Keep = LONG_MAX; |
---|
263 | + to find out where "memory corruption" occured, increase value of |
---|
264 | OM_CHECK - the higher this value is, the more consistency checks are |
---|
265 | done (However a value > 3 checks the entire memory each time an omalloc |
---|
266 | routine is used!) |
---|
267 | |
---|
268 | Some more tips on the usage of omalloc: |
---|
269 | + omAlloc*, omRealloc*, omFree*, omCheck* omDebug* omTest* rotuines |
---|
270 | assume that sizes are > 0 and pointers are != NULL |
---|
271 | + omalloc*, omrealloc*, omfree* omcheck*, omdebug* omtest* routines allow |
---|
272 | NULL pointers and sizes == 0 |
---|
273 | + You can safely use any free/realloc routine in combination with any alloc |
---|
274 | routine (including the debug versions): E.g., an address allocated with |
---|
275 | omAllocBin can be freed with omfree, or an adress allocated with |
---|
276 | om(Debug)Alloc can be freed with omfree, or omFree, or omFreeSize, etc. |
---|
277 | However, keep in mind that the efficiency decreases from |
---|
278 | Bin over Size to General routines (i.e., omFreeBin is more efficient than |
---|
279 | omFreeSize which is more efficient than omFree, likewise with the alloc |
---|
280 | routines). |
---|
281 | + if OM_CHECK is undefined or 0, then all omCheck routines do nothing |
---|
282 | + if OM_CHECK and OM_TRACK are both undefined (or 0), or if OM_NDEBUG is |
---|
283 | defined, then the "real" alloc/realloc/free macros are used, and all |
---|
284 | omTest, omDebug and omCheck routines are undefined |
---|
285 | + to break after an omError occured within a debugger, |
---|
286 | set a breakpoint on dErrorBreak |
---|
287 | + to do checks from within the debugger, or to do checks with explicit |
---|
288 | check level, use omTest routines. |
---|
289 | */ |
---|
290 | |
---|
291 | /* by default, store alloc info and file/line where addr was freed */ |
---|
292 | #ifndef OM_TRACK |
---|
293 | #define OM_TRACK 4 |
---|
294 | #endif |
---|
295 | /* only do constant-time memory checks */ |
---|
296 | #ifndef OM_CHECK |
---|
297 | #define OM_CHECK 1 |
---|
298 | #endif |
---|
299 | /* Do actually free memory: |
---|
300 | (be careful: if this is set, memory is never really freed, |
---|
301 | but only marked as free) */ |
---|
302 | #ifndef OM_KEEP |
---|
303 | #define OM_KEEP 0 |
---|
304 | #endif |
---|
305 | /* but only after you have freed 1000 more addresses |
---|
306 | (this is actually independent of the value of OM_KEEP and used |
---|
307 | to initialize om_Opts.Keep) */ |
---|
308 | #ifndef OM_SING_KEEP |
---|
309 | #define OM_SING_KEEP 1000 |
---|
310 | #endif |
---|
311 | |
---|
312 | #endif /* MDEBUG */ |
---|
313 | |
---|
314 | |
---|
315 | /* undef KDEBUG for check of data during std computations |
---|
316 | * |
---|
317 | * define KDEBUG to |
---|
318 | * 0 for basic tests |
---|
319 | * 1 for tests in kSpoly |
---|
320 | * NOTE: You can locally enable tests in kspoly by setting the |
---|
321 | * define at the beginning of kspoly.cc |
---|
322 | */ |
---|
323 | #define KDEBUG 0 |
---|
324 | |
---|
325 | /* define LDEBUG checking numbers, undefine otherwise */ |
---|
326 | #define LDEBUG |
---|
327 | /* define RDEBUG checking rings (together with TRACE=9) */ |
---|
328 | #define RDEBUG |
---|
329 | /* define TEST for non time critical tests, undefine otherwise */ |
---|
330 | #define TEST |
---|
331 | |
---|
332 | /* define YYDEBUG 1 for debugging bison texts, 0 otherwise */ |
---|
333 | #define YYDEBUG 1 |
---|
334 | |
---|
335 | #endif |
---|
336 | /* end of debugging option (ifndef NDEBUG) */ |
---|
337 | |
---|
338 | |
---|
339 | |
---|
340 | #ifdef _DEBUG |
---|
341 | # define FORCE_INLINE inline |
---|
342 | #else |
---|
343 | #ifdef NDEBUG |
---|
344 | #if defined(_MSC_VER) |
---|
345 | # define FORCE_INLINE __forceinline |
---|
346 | #elif defined(__GNUC__) && __GNUC__ > 3 |
---|
347 | # define FORCE_INLINE inline __attribute__ ((always_inline)) |
---|
348 | #else |
---|
349 | # define FORCE_INLINE inline |
---|
350 | #endif |
---|
351 | #else |
---|
352 | # define FORCE_INLINE inline |
---|
353 | #endif |
---|
354 | /* NDEBUG */ |
---|
355 | #endif |
---|
356 | /* _DEBUG */ |
---|
357 | |
---|
358 | |
---|
359 | #define DO_PRAGMA(x) _Pragma (#x) |
---|
360 | #define TODO(who, msg) DO_PRAGMA(message ("TODO [for " #who "]: " #msg)) |
---|
361 | |
---|
362 | |
---|
363 | |
---|
364 | #if defined(__GNUC__) && defined(__GNUC_MINOR__) |
---|
365 | #define _GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) |
---|
366 | #else |
---|
367 | #define _GNUC_PREREQ(maj, min) 0 |
---|
368 | #endif |
---|
369 | |
---|
370 | #if _GNUC_PREREQ(3,3) && defined(__ELF__) |
---|
371 | #define FORCE_INTERNAL __attribute__ ((visibility ("internal"))) |
---|
372 | #else |
---|
373 | #define FORCE_INTERNAL |
---|
374 | #endif |
---|
375 | |
---|
376 | #if _GNUC_PREREQ(3,3) |
---|
377 | #define FORCE_DEPRECATED __attribute__ ((deprecated)) |
---|
378 | #else |
---|
379 | #define FORCE_DEPRECATED |
---|
380 | #endif |
---|
381 | |
---|
382 | #ifdef __cplusplus |
---|
383 | # define BEGIN_CDECL extern "C" { |
---|
384 | # define END_CDECL } |
---|
385 | #else |
---|
386 | # define BEGIN_CDECL |
---|
387 | # define END_CDECL |
---|
388 | #endif |
---|
389 | |
---|
390 | |
---|
391 | |
---|
392 | #endif |
---|
393 | /* MISC_AUXILIARY_H */ |
---|
394 | |
---|