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