source: git/libpolys/misc/auxiliary.h @ 756676e

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