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