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