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 | * @internal @version \$Id$ |
---|
13 | * |
---|
14 | **/ |
---|
15 | /*****************************************************************************/ |
---|
16 | |
---|
17 | #ifndef MISC_AUXILIARY_H |
---|
18 | #define MISC_AUXILIARY_H |
---|
19 | |
---|
20 | // ----------------- configure stuff |
---|
21 | |
---|
22 | /* CPU type: i[3456]86: */ |
---|
23 | #undef SI_CPU_I386 |
---|
24 | /* CPU type: sparc: */ |
---|
25 | #undef SI_CPU_SPARC |
---|
26 | /* CPU type: ppc: */ |
---|
27 | #undef SI_CPU_PPC |
---|
28 | /* CPU type: IA64: */ |
---|
29 | #undef SI_CPU_IA64 |
---|
30 | /* CPU type: x86_64: */ |
---|
31 | #undef SI_CPU_X86_64 |
---|
32 | |
---|
33 | /* Define sizeof(long) */ |
---|
34 | #define SIZEOF_LONG 4 |
---|
35 | |
---|
36 | // ----------------- end of configure stuff |
---|
37 | |
---|
38 | // BOOLEAN |
---|
39 | |
---|
40 | #if (SIZEOF_LONG == 8) |
---|
41 | typedef int BOOLEAN; |
---|
42 | /* testet on x86_64, gcc 3.4.6: 2 % */ |
---|
43 | /* testet on IA64, gcc 3.4.6: 1 % */ |
---|
44 | #else |
---|
45 | /* testet on athlon, gcc 2.95.4: 1 % */ |
---|
46 | typedef short BOOLEAN; |
---|
47 | #endif |
---|
48 | |
---|
49 | #ifndef FALSE |
---|
50 | #define FALSE 0 |
---|
51 | #endif |
---|
52 | |
---|
53 | #ifndef TRUE |
---|
54 | #define TRUE 1 |
---|
55 | #endif |
---|
56 | |
---|
57 | #ifndef NULL |
---|
58 | #define NULL (0) |
---|
59 | #endif |
---|
60 | |
---|
61 | // #ifdef _TRY |
---|
62 | #ifndef ABS |
---|
63 | #define ABS(x) ((x)<0?(-(x)):(x)) |
---|
64 | #endif |
---|
65 | // #endif |
---|
66 | |
---|
67 | static const int MAX_INT_LEN= 11; |
---|
68 | typedef void* ADDRESS; |
---|
69 | |
---|
70 | #define loop for(;;) |
---|
71 | |
---|
72 | static inline int si_max(const int a, const int b) { return (a>b) ? a : b; } |
---|
73 | |
---|
74 | |
---|
75 | #if defined(SI_CPU_I386) || defined(SI_CPU_X86_64) |
---|
76 | // the following settings seems to be better on i386 and x86_64 processors |
---|
77 | // define if a*b is with mod instead of tables |
---|
78 | #define HAVE_MULT_MOD |
---|
79 | // #define HAVE_GENERIC_ADD |
---|
80 | // #ifdef HAVE_MULT_MOD |
---|
81 | // #define HAVE_DIV_MOD |
---|
82 | // #endif |
---|
83 | #elif defined(SI_CPU_IA64) |
---|
84 | // the following settings seems to be better on itanium processors |
---|
85 | // #define HAVE_MULT_MOD |
---|
86 | #define HAVE_GENERIC_ADD |
---|
87 | // #ifdef HAVE_MULT_MOD |
---|
88 | // #define HAVE_DIV_MOD |
---|
89 | // #endif |
---|
90 | #elif defined(SI_CPU_SPARC) |
---|
91 | // #define HAVE_GENERIC_ADD |
---|
92 | #define HAVE_MULT_MOD |
---|
93 | #ifdef HAVE_MULT_MOD |
---|
94 | #define HAVE_DIV_MOD |
---|
95 | #endif |
---|
96 | #elif defined(SI_CPU_PPC) |
---|
97 | // the following settings seems to be better on ppc processors |
---|
98 | // testet on: ppc_Linux, 740/750 PowerMac G3, 512k L2 cache |
---|
99 | #define HAVE_MULT_MOD |
---|
100 | // #ifdef HAVE_MULT_MOD |
---|
101 | // #define HAVE_DIV_MOD |
---|
102 | // #endif |
---|
103 | #endif |
---|
104 | |
---|
105 | |
---|
106 | /******************************************************************* |
---|
107 | * DEBUG OPTIONS |
---|
108 | * -- only significant for for compiling without -DNDEBUG |
---|
109 | * -- you better know what your are doing, if you touch this |
---|
110 | ******************************************************************/ |
---|
111 | #ifndef NDEBUG |
---|
112 | |
---|
113 | /* undefine to enable inline */ |
---|
114 | #define NO_INLINE |
---|
115 | |
---|
116 | /* undefine to disable assume -- should normally be defined for NDEBUG */ |
---|
117 | #define HAVE_ASSUME |
---|
118 | |
---|
119 | /* undef PDEBUG to disable checks of polys |
---|
120 | |
---|
121 | define PDEBUG to |
---|
122 | 0 for enabling pTest |
---|
123 | 1 plus tests in Level 1 poly routines (operations on monomials) |
---|
124 | 2 plus tests in Level 2 poly routines (operations on single exponents) |
---|
125 | -- see also polys.h for more info |
---|
126 | |
---|
127 | NOTE: you can set the value of PDEBUG on a per-file basis, before |
---|
128 | including mod2.h, provided ! PDEBUG is defined in mod2.h E.g.: |
---|
129 | |
---|
130 | #define PDEBUG 2 |
---|
131 | #include "auxiliary.h" |
---|
132 | ... |
---|
133 | |
---|
134 | makes sure that all poly operations in your file are done with |
---|
135 | PDEBUG == 2 |
---|
136 | To break after an error occured, set a debugger breakpoint on |
---|
137 | dErrorBreak. |
---|
138 | */ |
---|
139 | #ifndef PDEBUG |
---|
140 | #define PDEBUG 0 |
---|
141 | #endif |
---|
142 | |
---|
143 | /* define MDEBUG to enable memory checks */ |
---|
144 | #define MDEBUG 0 |
---|
145 | |
---|
146 | #ifdef MDEBUG |
---|
147 | /* If ! defined(OM_NDEBUG) and (defined(OM_TRACK) or defined(OM_CHECK) |
---|
148 | then omDebug routines are used for memory allocation/free: |
---|
149 | |
---|
150 | The omDebug routines are controlled by the values of OM_TRACK, OM_CHECK |
---|
151 | and OM_KEEP. There meaning is roughly as follows: |
---|
152 | OM_TRACK: strored with address : extra space |
---|
153 | 0 : no additional info is stored : 0 |
---|
154 | 1 : file:line of location where address was allocated : 1 word |
---|
155 | 2 : plus backtrace of stack where adress was allocated: 6 words |
---|
156 | 3 : plus size/bin info and front-, and back padding : 9 words |
---|
157 | 4 : plus file:line of location where adress was freed : 10 words |
---|
158 | 5 : plus backtrace of stack where adress was allocated: 15 words |
---|
159 | OM_CHECK: checks done |
---|
160 | 0 : no checks |
---|
161 | 1 : constant-time checks: i.e. addr checks only |
---|
162 | 2 : plus linear-time checks and constant related bin check |
---|
163 | 3 : plus quadratic-time checks and linear-time related bin checks and |
---|
164 | constant time all memory checks |
---|
165 | 4 : and so on |
---|
166 | ==> for OM_CHECK >= 3 it gets rather slow |
---|
167 | OM_KEEP: determines whether addresses are really freed ( |
---|
168 | 0 : addresses are really freed |
---|
169 | 1 : addresses are only marked as free and not really freed. |
---|
170 | |
---|
171 | OM_CHECK, OM_TRACK, and OM_KEEP can be set on a per-file basis |
---|
172 | (as can OM_NDEBUG), e.g.: |
---|
173 | #define OM_CHECK 3 |
---|
174 | #define OM_TRACK 5 |
---|
175 | #define OM_KEEP 1 |
---|
176 | #include "mod2.h" |
---|
177 | #include "omalloc.h" |
---|
178 | ensures that all memory allocs/free in this file are done with |
---|
179 | OM_CHECK==3 and OM_TRACK==5, and that all addresses allocated/freed |
---|
180 | in this file are only marked as free and never really freed. |
---|
181 | |
---|
182 | To set OM_CHECK, OM_TRACK and OM_KEEP under dynamic scope, set |
---|
183 | om_Opts.MinCheck, om_Opts.MinTrack to the respectiv values and |
---|
184 | om_Opts.Keep to the number of addresses which are kept before they are |
---|
185 | actually freed. E.g.: |
---|
186 | int check=om_Opts.MinCheck, track=om_Opts.MinTrack, keep= m_OPts.Keep; |
---|
187 | om_Opts.MinCheck = 3; om_Opts.MinTrack = 5; omOpts.Keep = LONG_MAX; |
---|
188 | ExternalRoutine(); |
---|
189 | om_Opts.MinCheck = check; omOpts.MinTrack = track; omOpts.Keep = keep; |
---|
190 | ensures that all calls omDebug routines occuring during the computation of |
---|
191 | ExternalRoutine() are done with OM_CHECK==3 and OM_TRACK==5, and |
---|
192 | calls to omFree only mark addresses as free and not really free them. |
---|
193 | |
---|
194 | Furthermore, the value of OM_SING_KEEP (resp. om_Opts.Keep) specifies |
---|
195 | how many addresses are kept before they are actually freed, independently |
---|
196 | of the value of OM_KEEP. |
---|
197 | |
---|
198 | Some tips on possible values of OM_TRACK, OM_CHECK, OM_KEEP: |
---|
199 | + To find out about an address that has been freed twice, first locate the |
---|
200 | file(s) where the error occured, and then at the beginning of these files: |
---|
201 | #define OM_CHECK 3 |
---|
202 | #define OM_TRACK 5 |
---|
203 | #define OM_KEEP 1 |
---|
204 | #include "mod2.h" |
---|
205 | #include "omalloc.h" |
---|
206 | Under dynamic scope, do (e.g., from within the debugger): |
---|
207 | om_Opts.MinCheck = 3; om_Opts.MinTrack = 5; omOpts.Keep = LONG_MAX; |
---|
208 | + to find out where "memory corruption" occured, increase value of |
---|
209 | OM_CHECK - the higher this value is, the more consistency checks are |
---|
210 | done (However a value > 3 checks the entire memory each time an omalloc |
---|
211 | routine is used!) |
---|
212 | |
---|
213 | Some more tips on the usage of omalloc: |
---|
214 | + omAlloc*, omRealloc*, omFree*, omCheck* omDebug* omTest* rotuines |
---|
215 | assume that sizes are > 0 and pointers are != NULL |
---|
216 | + omalloc*, omrealloc*, omfree* omcheck*, omdebug* omtest* routines allow |
---|
217 | NULL pointers and sizes == 0 |
---|
218 | + You can safely use any free/realloc routine in combination with any alloc |
---|
219 | routine (including the debug versions): E.g., an address allocated with |
---|
220 | omAllocBin can be freed with omfree, or an adress allocated with |
---|
221 | om(Debug)Alloc can be freed with omfree, or omFree, or omFreeSize, etc. |
---|
222 | However, keep in mind that the efficiency decreases from |
---|
223 | Bin over Size to General routines (i.e., omFreeBin is more efficient than |
---|
224 | omFreeSize which is more efficient than omFree, likewise with the alloc |
---|
225 | routines). |
---|
226 | + if OM_CHECK is undefined or 0, then all omCheck routines do nothing |
---|
227 | + if OM_CHECK and OM_TRACK are both undefined (or 0), or if OM_NDEBUG is |
---|
228 | defined, then the "real" alloc/realloc/free macros are used, and all |
---|
229 | omTest, omDebug and omCheck routines are undefined |
---|
230 | + to break after an omError occured within a debugger, |
---|
231 | set a breakpoint on dErrorBreak |
---|
232 | + to do checks from within the debugger, or to do checks with explicit |
---|
233 | check level, use omTest routines. |
---|
234 | */ |
---|
235 | |
---|
236 | /* by default, store alloc info and file/line where addr was freed */ |
---|
237 | #ifndef OM_TRACK |
---|
238 | #define OM_TRACK 4 |
---|
239 | #endif |
---|
240 | /* only do constant-time memory checks */ |
---|
241 | #ifndef OM_CHECK |
---|
242 | #define OM_CHECK 1 |
---|
243 | #endif |
---|
244 | /* Do actually free memory: |
---|
245 | (be careful: if this is set, memory is never really freed, |
---|
246 | but only marked as free) */ |
---|
247 | #ifndef OM_KEEP |
---|
248 | #define OM_KEEP 0 |
---|
249 | #endif |
---|
250 | /* but only after you have freed 1000 more addresses |
---|
251 | (this is actually independent of the value of OM_KEEP and used |
---|
252 | to initialize om_Opts.Keep) */ |
---|
253 | #ifndef OM_SING_KEEP |
---|
254 | #define OM_SING_KEEP 1000 |
---|
255 | #endif |
---|
256 | |
---|
257 | #endif /* MDEBUG */ |
---|
258 | |
---|
259 | |
---|
260 | /* undef KDEBUG for check of data during std computations |
---|
261 | * |
---|
262 | * define KDEBUG to |
---|
263 | * 0 for basic tests |
---|
264 | * 1 for tests in kSpoly |
---|
265 | * NOTE: You can locally enable tests in kspoly by setting the |
---|
266 | * define at the beginning of kspoly.cc |
---|
267 | */ |
---|
268 | #define KDEBUG 0 |
---|
269 | |
---|
270 | /* define LDEBUG checking numbers, undefine otherwise */ |
---|
271 | #define LDEBUG |
---|
272 | /* define RDEBUG checking rings (together with TRACE=9) */ |
---|
273 | #define RDEBUG |
---|
274 | /* define TEST for non time critical tests, undefine otherwise */ |
---|
275 | #define TEST |
---|
276 | |
---|
277 | /* define YYDEBUG 1 for debugging bison texts, 0 otherwise */ |
---|
278 | #define YYDEBUG 1 |
---|
279 | |
---|
280 | #endif |
---|
281 | /* end of debugging option (ifndef NDEBUG) */ |
---|
282 | |
---|
283 | |
---|
284 | |
---|
285 | #ifdef _DEBUG |
---|
286 | # define FORCEINLINE inline |
---|
287 | #else |
---|
288 | #ifdef NDEBUG |
---|
289 | #if defined(_MSC_VER) |
---|
290 | # define FORCEINLINE __forceinline |
---|
291 | #elif defined(__GNUC__) && __GNUC__ > 3 |
---|
292 | # define FORCEINLINE inline __attribute__ ((always_inline)) |
---|
293 | #else |
---|
294 | # define FORCEINLINE inline |
---|
295 | #endif |
---|
296 | #else |
---|
297 | # define FORCEINLINE inline |
---|
298 | #endif |
---|
299 | /* NDEBUG */ |
---|
300 | #endif |
---|
301 | /* _DEBUG */ |
---|
302 | |
---|
303 | |
---|
304 | |
---|
305 | #endif |
---|
306 | /* MISC_AUXILIARY_H */ |
---|
307 | |
---|