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