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