source: git/libpolys/misc/auxiliary.h @ 461a94

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