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 | * |
---|
13 | **/ |
---|
14 | /*****************************************************************************/ |
---|
15 | |
---|
16 | #ifndef MISC_AUXILIARY_H |
---|
17 | #define MISC_AUXILIARY_H |
---|
18 | |
---|
19 | /* please include libpolysconfig.h exclusively via <misc/auxiliary.h> and before any other header */ |
---|
20 | #include "libpolysconfig.h" |
---|
21 | |
---|
22 | // ----------------- which parts/extensions of Singular to build |
---|
23 | #ifndef HAVE_RINGS |
---|
24 | #undef HAVE_RINGS |
---|
25 | #endif |
---|
26 | |
---|
27 | #ifndef HAVE_PLURAL |
---|
28 | #undef HAVE_PLURAL |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifndef HAVE_DL |
---|
32 | #undef HAVE_DL |
---|
33 | #endif |
---|
34 | |
---|
35 | #ifndef HAVE_NTL |
---|
36 | #undef HAVE_NTL |
---|
37 | #endif |
---|
38 | |
---|
39 | /* letterplace gb:*/ |
---|
40 | #ifndef HAVE_SHIFTBBA |
---|
41 | #undef HAVE_SHIFTBBA |
---|
42 | #endif |
---|
43 | |
---|
44 | #ifndef HAVE_POLYEXTENSIONS |
---|
45 | #undef HAVE_POLYEXTENSIONS |
---|
46 | #endif |
---|
47 | |
---|
48 | #ifndef DISABLE_GMP_CPP |
---|
49 | #undef DISABLE_GMP_CPP |
---|
50 | #endif |
---|
51 | |
---|
52 | #ifndef SINGULAR |
---|
53 | #undef SINGULAR |
---|
54 | #endif |
---|
55 | |
---|
56 | #ifndef NOSTREAMIO |
---|
57 | #undef NOSTREAMIO |
---|
58 | #endif |
---|
59 | |
---|
60 | /* the following cunstruct is to make it painless to add -DHAVE_NUMSTATS to CPPFLAGS for configure */ |
---|
61 | #ifndef HAVE_NUMSTATS |
---|
62 | /* #define HAVE_NUMSTATS */ |
---|
63 | #undef HAVE_NUMSTATS |
---|
64 | #endif /* HAVE_NUMSTATS */ |
---|
65 | // ---------------- end of parts/extensions |
---|
66 | |
---|
67 | // ---------------- Singular standard types etc. |
---|
68 | /* SI_INTEGER_VARIANT: 1: from longrat.cc |
---|
69 | * 2: GMP |
---|
70 | * 3: rintegers.cc */ |
---|
71 | #define SI_INTEGER_VARIANT 2 |
---|
72 | |
---|
73 | /* SI_BIGINT_VARIANT: 1: from longrat.cc |
---|
74 | * 2: given by SI_INTEGER_VARIANT */ |
---|
75 | #define SI_BIGINT_VARIANT 1 |
---|
76 | |
---|
77 | /* preparation for versio 4.1.0: */ |
---|
78 | #ifndef SINGULAR_4_1 |
---|
79 | /* #define SINGULAR_4_1 */ |
---|
80 | #undef SINGULAR_4_1 |
---|
81 | #endif /* SINGULAR_4_1 */ |
---|
82 | |
---|
83 | #ifndef SIZEOF_LONG |
---|
84 | |
---|
85 | #include <misc/mylimits.h> |
---|
86 | |
---|
87 | #ifndef LONG_BIT |
---|
88 | #if ULONG_MAX == 0xffffffffUL |
---|
89 | #define LONG_BIT 32 |
---|
90 | #elif ULONG_MAX == 0xffffffffffffffffULL |
---|
91 | #define LONG_BIT 64 |
---|
92 | #else |
---|
93 | #error "Unexpected max for unsigned long" |
---|
94 | #endif |
---|
95 | #endif |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | #define SIZEOF_LONG (LONG_BIT/CHAR_BIT) |
---|
100 | // another option for SIZEOF_LONG: use omConfig included in <omalloc/omalloc.h>... |
---|
101 | |
---|
102 | #endif |
---|
103 | |
---|
104 | #include <sys/types.h> |
---|
105 | #if SIZEOF_LONG == 4 |
---|
106 | typedef long long int64; |
---|
107 | #elif SIZEOF_LONG == 8 |
---|
108 | typedef long int64; |
---|
109 | #else |
---|
110 | #error "Unexpected SIZEOF_LONG" |
---|
111 | #endif |
---|
112 | |
---|
113 | |
---|
114 | #ifndef CHAR_BIT |
---|
115 | #define CHAR_BIT (8) |
---|
116 | #endif /*ifndef CHAR_BIT*/ |
---|
117 | |
---|
118 | |
---|
119 | #ifndef BIT_SIZEOF_LONG |
---|
120 | #define BIT_SIZEOF_LONG ((CHAR_BIT)*(SIZEOF_LONG)) |
---|
121 | #endif /*ifndef BIT_SIZEOF_LONG*/ |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | #if (SIZEOF_LONG == 8) |
---|
127 | typedef int BOOLEAN; |
---|
128 | /* testet on x86_64, gcc 3.4.6: 2 % */ |
---|
129 | /* testet on IA64, gcc 3.4.6: 1 % */ |
---|
130 | #else |
---|
131 | /* testet on athlon, gcc 2.95.4: 1 % */ |
---|
132 | typedef short BOOLEAN; |
---|
133 | #endif |
---|
134 | |
---|
135 | #ifndef FALSE |
---|
136 | #define FALSE 0 |
---|
137 | #endif |
---|
138 | |
---|
139 | #ifndef TRUE |
---|
140 | #define TRUE 1 |
---|
141 | #endif |
---|
142 | |
---|
143 | #ifndef NULL |
---|
144 | #define NULL (0) |
---|
145 | #endif |
---|
146 | |
---|
147 | #ifndef NULLp |
---|
148 | #define NULLp ((void*)NULL) |
---|
149 | #endif |
---|
150 | |
---|
151 | // #ifdef _TRY |
---|
152 | #ifndef ABS |
---|
153 | #define ABS(x) ((x)<0?(-(x)):(x)) |
---|
154 | #endif |
---|
155 | // #endif |
---|
156 | |
---|
157 | typedef void* ADDRESS; |
---|
158 | |
---|
159 | #define loop for(;;) |
---|
160 | |
---|
161 | #if defined(__cplusplus) |
---|
162 | static inline int si_max(const int a, const int b) { return (a>b) ? a : b; } |
---|
163 | static inline int si_min(const int a, const int b) { return (a<b) ? a : b; } |
---|
164 | static inline long si_max(const long a, const long b) { return (a>b) ? a : b; } |
---|
165 | static inline unsigned long si_max(const unsigned long a, const unsigned long b) { return (a>b) ? a : b; } |
---|
166 | static inline long si_min(const long a, const long b) { return (a<b) ? a : b; } |
---|
167 | static inline unsigned long si_min(const unsigned long a, const unsigned long b) { return (a<b) ? a : b; } |
---|
168 | #else |
---|
169 | #define si_max(A,B) ((A) > (B) ? (A) : (B)) |
---|
170 | #define si_min(A,B) ((A) < (B) ? (A) : (B)) |
---|
171 | #endif |
---|
172 | |
---|
173 | #define SSI_BASE 16 |
---|
174 | |
---|
175 | // ---------------- defines which depend on the settings above |
---|
176 | |
---|
177 | #ifndef HAVE_MULT_MOD |
---|
178 | #undef HAVE_MULT_MOD |
---|
179 | #endif |
---|
180 | |
---|
181 | #ifndef HAVE_DIV_MOD |
---|
182 | #undef HAVE_DIV_MOD |
---|
183 | #endif |
---|
184 | |
---|
185 | #ifndef HAVE_GENERIC_ADD |
---|
186 | #undef HAVE_GENERIC_ADD |
---|
187 | #endif |
---|
188 | |
---|
189 | /******************************************************************* |
---|
190 | * DEBUG OPTIONS |
---|
191 | * -- only significant for for compiling without -DSING_NDEBUG |
---|
192 | * -- you better know what your are doing, if you touch this |
---|
193 | ******************************************************************/ |
---|
194 | #ifndef SING_NDEBUG |
---|
195 | |
---|
196 | /* undefine to enable inline */ |
---|
197 | #define NO_INLINE |
---|
198 | |
---|
199 | /* undefine to disable assume -- should normally be defined for SING_NDEBUG */ |
---|
200 | #define HAVE_ASSUME |
---|
201 | |
---|
202 | /* undef PDEBUG to disable checks of polys |
---|
203 | |
---|
204 | define PDEBUG to |
---|
205 | 0 for enabling pTest |
---|
206 | 1 plus tests in Level 1 poly routines (operations on monomials) |
---|
207 | 2 plus tests in Level 2 poly routines (operations on single exponents) |
---|
208 | -- see also polys.h for more info |
---|
209 | |
---|
210 | NOTE: you can set the value of PDEBUG on a per-file basis, before |
---|
211 | including mod2.h, provided ! PDEBUG is defined in mod2.h E.g.: |
---|
212 | |
---|
213 | #define PDEBUG 2 |
---|
214 | |
---|
215 | ... |
---|
216 | |
---|
217 | makes sure that all poly operations in your file are done with |
---|
218 | PDEBUG == 2 |
---|
219 | To break after an error occurred, set a debugger breakpoint on |
---|
220 | dErrorBreak. |
---|
221 | */ |
---|
222 | #ifndef PDEBUG |
---|
223 | #define PDEBUG 0 |
---|
224 | #endif |
---|
225 | |
---|
226 | /* define MDEBUG to enable memory checks */ |
---|
227 | //////////////////////////////////////////// #define MDEBUG 0 |
---|
228 | |
---|
229 | #ifdef MDEBUG |
---|
230 | /* If ! defined(OM_NDEBUG) and (defined(OM_TRACK) or defined(OM_CHECK) |
---|
231 | then omDebug routines are used for memory allocation/free: |
---|
232 | |
---|
233 | The omDebug routines are controlled by the values of OM_TRACK, OM_CHECK |
---|
234 | and OM_KEEP. There meaning is roughly as follows: |
---|
235 | OM_TRACK: strored with address : extra space |
---|
236 | 0 : no additional info is stored : 0 |
---|
237 | 1 : file:line of location where address was allocated : 1 word |
---|
238 | 2 : plus backtrace of stack where adress was allocated: 6 words |
---|
239 | 3 : plus size/bin info and front-, and back padding : 9 words |
---|
240 | 4 : plus file:line of location where adress was freed : 10 words |
---|
241 | 5 : plus backtrace of stack where adress was allocated: 15 words |
---|
242 | OM_CHECK: checks done |
---|
243 | 0 : no checks |
---|
244 | 1 : constant-time checks: i.e. addr checks only |
---|
245 | 2 : plus linear-time checks and constant related bin check |
---|
246 | 3 : plus quadratic-time checks and linear-time related bin checks and |
---|
247 | constant time all memory checks |
---|
248 | 4 : and so on |
---|
249 | ==> for OM_CHECK >= 3 it gets rather slow |
---|
250 | OM_KEEP: determines whether addresses are really freed ( |
---|
251 | 0 : addresses are really freed |
---|
252 | 1 : addresses are only marked as free and not really freed. |
---|
253 | |
---|
254 | OM_CHECK, OM_TRACK, and OM_KEEP can be set on a per-file basis |
---|
255 | (as can OM_NDEBUG), e.g.: |
---|
256 | #define OM_CHECK 3 |
---|
257 | #define OM_TRACK 5 |
---|
258 | #define OM_KEEP 1 |
---|
259 | |
---|
260 | #include <omalloc/omalloc.h> |
---|
261 | ensures that all memory allocs/free in this file are done with |
---|
262 | OM_CHECK==3 and OM_TRACK==5, and that all addresses allocated/freed |
---|
263 | in this file are only marked as free and never really freed. |
---|
264 | |
---|
265 | To set OM_CHECK, OM_TRACK and OM_KEEP under dynamic scope, set |
---|
266 | om_Opts.MinCheck, om_Opts.MinTrack to the respectiv values and |
---|
267 | om_Opts.Keep to the number of addresses which are kept before they are |
---|
268 | actually freed. E.g.: |
---|
269 | int check=om_Opts.MinCheck, track=om_Opts.MinTrack, keep= m_OPts.Keep; |
---|
270 | om_Opts.MinCheck = 3; om_Opts.MinTrack = 5; omOpts.Keep = LONG_MAX; |
---|
271 | ExternalRoutine(); |
---|
272 | om_Opts.MinCheck = check; omOpts.MinTrack = track; omOpts.Keep = keep; |
---|
273 | ensures that all calls omDebug routines occuring during the computation of |
---|
274 | ExternalRoutine() are done with OM_CHECK==3 and OM_TRACK==5, and |
---|
275 | calls to omFree only mark addresses as free and not really free them. |
---|
276 | |
---|
277 | Furthermore, the value of OM_SING_KEEP (resp. om_Opts.Keep) specifies |
---|
278 | how many addresses are kept before they are actually freed, independently |
---|
279 | of the value of OM_KEEP. |
---|
280 | |
---|
281 | Some tips on possible values of OM_TRACK, OM_CHECK, OM_KEEP: |
---|
282 | + To find out about an address that has been freed twice, first locate the |
---|
283 | file(s) where the error occurred, and then at the beginning of these files: |
---|
284 | #define OM_CHECK 3 |
---|
285 | #define OM_TRACK 5 |
---|
286 | #define OM_KEEP 1 |
---|
287 | #include <kernel/mod2.h> |
---|
288 | #include <omalloc/omalloc.h> |
---|
289 | Under dynamic scope, do (e.g., from within the debugger): |
---|
290 | om_Opts.MinCheck = 3; om_Opts.MinTrack = 5; omOpts.Keep = LONG_MAX; |
---|
291 | + to find out where "memory corruption" occurred, increase value of |
---|
292 | OM_CHECK - the higher this value is, the more consistency checks are |
---|
293 | done (However a value > 3 checks the entire memory each time an omalloc |
---|
294 | routine is used!) |
---|
295 | |
---|
296 | Some more tips on the usage of omalloc: |
---|
297 | + omAlloc*, omRealloc*, omFree*, omCheck* omDebug* omTest* rotuines |
---|
298 | assume that sizes are > 0 and pointers are != NULL |
---|
299 | + omalloc*, omrealloc*, omfree* omcheck*, omdebug* omtest* routines allow |
---|
300 | NULL pointers and sizes == 0 |
---|
301 | + You can safely use any free/realloc routine in combination with any alloc |
---|
302 | routine (including the debug versions): E.g., an address allocated with |
---|
303 | omAllocBin can be freed with omfree, or an adress allocated with |
---|
304 | om(Debug)Alloc can be freed with omfree, or omFree, or omFreeSize, etc. |
---|
305 | However, keep in mind that the efficiency decreases from |
---|
306 | Bin over Size to General routines (i.e., omFreeBin is more efficient than |
---|
307 | omFreeSize which is more efficient than omFree, likewise with the alloc |
---|
308 | routines). |
---|
309 | + if OM_CHECK is undefined or 0, then all omCheck routines do nothing |
---|
310 | + if OM_CHECK and OM_TRACK are both undefined (or 0), or if OM_NDEBUG is |
---|
311 | defined, then the "real" alloc/realloc/free macros are used, and all |
---|
312 | omTest, omDebug and omCheck routines are undefined |
---|
313 | + to break after an omError occurred within a debugger, |
---|
314 | set a breakpoint on dErrorBreak |
---|
315 | + to do checks from within the debugger, or to do checks with explicit |
---|
316 | check level, use omTest routines. |
---|
317 | */ |
---|
318 | |
---|
319 | /* by default, store alloc info and file/line where addr was freed */ |
---|
320 | #ifndef OM_TRACK |
---|
321 | #define OM_TRACK 4 |
---|
322 | #endif |
---|
323 | /* only do constant-time memory checks */ |
---|
324 | #ifndef OM_CHECK |
---|
325 | #define OM_CHECK 1 |
---|
326 | #endif |
---|
327 | /* Do actually free memory: |
---|
328 | (be careful: if this is set, memory is never really freed, |
---|
329 | but only marked as free) */ |
---|
330 | #ifndef OM_KEEP |
---|
331 | #define OM_KEEP 0 |
---|
332 | #endif |
---|
333 | /* but only after you have freed 1000 more addresses |
---|
334 | (this is actually independent of the value of OM_KEEP and used |
---|
335 | to initialize om_Opts.Keep) */ |
---|
336 | #ifndef OM_SING_KEEP |
---|
337 | #define OM_SING_KEEP 1000 |
---|
338 | #endif |
---|
339 | |
---|
340 | #endif /* MDEBUG */ |
---|
341 | |
---|
342 | |
---|
343 | /* undef KDEBUG for check of data during std computations |
---|
344 | * |
---|
345 | * define KDEBUG to |
---|
346 | * 0 for basic tests |
---|
347 | * 1 for tests in kSpoly |
---|
348 | * NOTE: You can locally enable tests in kspoly by setting the |
---|
349 | * define at the beginning of kspoly.cc |
---|
350 | */ |
---|
351 | #define KDEBUG 0 |
---|
352 | |
---|
353 | /* define LDEBUG checking numbers, undefine otherwise */ |
---|
354 | #define LDEBUG |
---|
355 | |
---|
356 | /* define RDEBUG checking rings (together with TRACE=9) */ |
---|
357 | #define RDEBUG |
---|
358 | |
---|
359 | /* define TEST for non time critical tests, undefine otherwise */ |
---|
360 | #define TEST |
---|
361 | |
---|
362 | /* define YYDEBUG 1 for debugging bison texts, 0 otherwise */ |
---|
363 | #define YYDEBUG 1 |
---|
364 | |
---|
365 | #endif |
---|
366 | /* end of debugging option (ifndef SING_NDEBUG) */ |
---|
367 | |
---|
368 | |
---|
369 | |
---|
370 | #ifdef _DEBUG |
---|
371 | # define FORCE_INLINE inline |
---|
372 | #else |
---|
373 | #ifdef SING_NDEBUG |
---|
374 | #if defined(_MSC_VER) |
---|
375 | # define FORCE_INLINE __forceinline |
---|
376 | #elif defined(__GNUC__) && __GNUC__ > 3 |
---|
377 | # define FORCE_INLINE inline __attribute__ ((always_inline)) |
---|
378 | #else |
---|
379 | # define FORCE_INLINE inline |
---|
380 | #endif |
---|
381 | #else |
---|
382 | # define FORCE_INLINE inline |
---|
383 | #endif |
---|
384 | /* SING_NDEBUG */ |
---|
385 | #endif |
---|
386 | /* _DEBUG */ |
---|
387 | |
---|
388 | |
---|
389 | #define DO_PRAGMA(x) _Pragma (#x) |
---|
390 | #define TODO(who, msg) DO_PRAGMA(message ("TODO [for " #who "]: " #msg)) |
---|
391 | |
---|
392 | |
---|
393 | |
---|
394 | #if defined(__GNUC__) && defined(__GNUC_MINOR__) |
---|
395 | #define _GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) |
---|
396 | #else |
---|
397 | #define _GNUC_PREREQ(maj, min) 0 |
---|
398 | #endif |
---|
399 | |
---|
400 | #if _GNUC_PREREQ(3,3) && defined(__ELF__) |
---|
401 | #define FORCE_INTERNAL __attribute__ ((visibility ("internal"))) |
---|
402 | #else |
---|
403 | #define FORCE_INTERNAL |
---|
404 | #endif |
---|
405 | |
---|
406 | #if _GNUC_PREREQ(3,3) |
---|
407 | #define FORCE_DEPRECATED __attribute__ ((deprecated)) |
---|
408 | #else |
---|
409 | #define FORCE_DEPRECATED |
---|
410 | #endif |
---|
411 | |
---|
412 | #ifdef __cplusplus |
---|
413 | # define BEGIN_CDECL extern "C" { |
---|
414 | # define END_CDECL } |
---|
415 | #else |
---|
416 | # define BEGIN_CDECL |
---|
417 | # define END_CDECL |
---|
418 | #endif |
---|
419 | |
---|
420 | #ifdef __cplusplus |
---|
421 | // hack to workaround warnings when casting void pointers |
---|
422 | // retrieved from dlsym? to function pointers. |
---|
423 | // see: http://trac.osgeo.org/qgis/ticket/234, http://www.trilithium.com/johan/2004/12/problem-with-dlsym/ |
---|
424 | template<typename A, typename B> |
---|
425 | inline B cast_A_to_B( A a ) |
---|
426 | { |
---|
427 | union |
---|
428 | { |
---|
429 | A a; |
---|
430 | B b; |
---|
431 | } u; |
---|
432 | |
---|
433 | u.a = a; |
---|
434 | return u.b; |
---|
435 | } |
---|
436 | |
---|
437 | template<typename A> |
---|
438 | inline void* cast_A_to_vptr( A a ) |
---|
439 | { |
---|
440 | return cast_A_to_B<A, void*>(a); |
---|
441 | } |
---|
442 | |
---|
443 | |
---|
444 | template<typename A> |
---|
445 | inline A cast_vptr_to_A( void * p ) |
---|
446 | { |
---|
447 | return cast_A_to_B<void*, A>(p); |
---|
448 | } |
---|
449 | #endif |
---|
450 | |
---|
451 | |
---|
452 | |
---|
453 | #endif |
---|
454 | /* MISC_AUXILIARY_H */ |
---|
455 | |
---|