source: git/libpolys/misc/auxiliary.h @ 9bb030

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