FORM  4.1
form3.h
Go to the documentation of this file.
1 
8 /* #[ License : */
9 /*
10  * Copyright (C) 1984-2013 J.A.M. Vermaseren
11  * When using this file you are requested to refer to the publication
12  * J.A.M.Vermaseren "New features of FORM" math-ph/0010025
13  * This is considered a matter of courtesy as the development was paid
14  * for by FOM the Dutch physics granting agency and we would like to
15  * be able to track its scientific use to convince FOM of its value
16  * for the community.
17  *
18  * This file is part of FORM.
19  *
20  * FORM is free software: you can redistribute it and/or modify it under the
21  * terms of the GNU General Public License as published by the Free Software
22  * Foundation, either version 3 of the License, or (at your option) any later
23  * version.
24  *
25  * FORM is distributed in the hope that it will be useful, but WITHOUT ANY
26  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
27  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
28  * details.
29  *
30  * You should have received a copy of the GNU General Public License along
31  * with FORM. If not, see <http://www.gnu.org/licenses/>.
32  */
33 /* #] License : */
34 
35 #ifndef __FORM3H__
36 #define __FORM3H__
37 
38 #ifdef HAVE_CONFIG_H
39 
40 #include "config.h"
41 
42 #else /* HAVE_CONFIG_H */
43 
44 #define VERSION "4.1"
45 #define MAJORVERSION 4
46 #define MINORVERSION 1
47 
48 #ifdef __DATE__
49 #define PRODUCTIONDATE 20131025
50 #else
51 #define PRODUCTIONDATE "25-oct-2013"
52 #endif
53 
54 #undef BETAVERSION
55 
56 #ifdef LINUX32
57 #define UNIX
58 #define LINUX
59 #define ILP32
60 #define SIZEOF_LONG_LONG 8
61 #define _FILE_OFFSET_BITS 64
62 #define WITHZLIB
63 #define WITHGMP
64 #define WITHPOSIXCLOCK
65 #endif
66 
67 #ifdef LINUX64
68 #define UNIX
69 #define LINUX
70 #define LP64
71 #define WITHZLIB
72 #define WITHGMP
73 #define WITHPOSIXCLOCK
74 #endif
75 
76 #ifdef APPLE32
77 #define UNIX
78 #define ILP32
79 #define SIZEOF_LONG_LONG 8
80 #define _FILE_OFFSET_BITS 64
81 #define WITHZLIB
82 #endif
83 
84 #ifdef APPLE64
85 #define UNIX
86 #define LP64
87 #define WITHZLIB
88 #endif
89 
90 #ifdef CYGWIN32
91 #define UNIX
92 #define ILP32
93 #define SIZEOF_LONG_LONG 8
94 #endif
95 
96 #ifdef _MSC_VER
97 #define WINDOWS
98 #define _CRT_SECURE_NO_WARNINGS
99 #if defined(_WIN64)
100 #define LLP64
101 #elif defined(_WIN32)
102 #define ILP32
103 #define SIZEOF_LONG_LONG 8
104 #endif
105 #endif
106 
107 /*
108  * We must not define WITHPOSIXCLOCK in compiling the sequential FORM or ParFORM.
109  */
110 #if !defined(WITHPTHREADS) && defined(WITHPOSIXCLOCK)
111 #undef WITHPOSIXCLOCK
112 #endif
113 
114 #if !defined(__cplusplus) && !defined(inline)
115 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__GNUC__) && !defined(__STRICT_ANSI__))
116 /* "inline" is available. */
117 #elif defined(__GNUC__)
118 /* GNU C compiler has "__inline__". */
119 #define inline __inline__
120 #elif defined(_MSC_VER)
121 /* Microsoft C compiler has "__inline". */
122 #define inline __inline
123 #else
124 /* Inline functions may be not supported. Define "inline" to be empty. */
125 #define inline
126 #endif
127 #endif
128 
129 #endif /* HAVE_CONFIG_H */
130 
131 /*
132  * STATIC_ASSERT(condition) will fail to be compiled if the given
133  * condition is false.
134  */
135 #define STATIC_ASSERT(condition) STATIC_ASSERT__1(condition,__LINE__)
136 #define STATIC_ASSERT__1(X,L) STATIC_ASSERT__2(X,L)
137 #define STATIC_ASSERT__2(X,L) STATIC_ASSERT__3(X,L)
138 #define STATIC_ASSERT__3(X,L) \
139  typedef char static_assertion_failed_##L[(!!(X))*2-1]
140 
141 /*
142  * UNIX or WINDOWS must be defined.
143  */
144 #if defined(UNIX)
145 #define mBSD
146 #define ANSI
147 #elif defined(WINDOWS)
148 #define ANSI
149 #define WIN32_LEAN_AND_MEAN
150 #include <windows.h>
151 #include <io.h>
152 /* Undefine/rename conflicted symbols. */
153 #undef VOID /* WinNT.h */
154 #undef MAXLONG /* WinNT.h */
155 #define WORD WORD__Renamed /* WinDef.h */
156 #define LONG LONG__Renamed /* WinNT.h */
157 #define ULONG ULONG__Renamed /* WinDef.h */
158 #undef CreateFile /* WinBase.h */
159 #undef CopyFile /* WinBase.h */
160 #define OpenFile OpenFile__Renamed /* WinBase.h */
161 #define ReOpenFile ReOpenFile__Renamed /* WinBase.h */
162 #define ReadFile ReadFile__Renamed /* WinBase.h */
163 #define WriteFile WriteFile__Renamed /* WinBase.h */
164 #define DeleteObject DeleteObject__Renamed /* WinGDI.h */
165 #else
166 #error UNIX or WINDOWS must be defined!
167 #endif
168 
169 /*
170  * Data model. ILP32 or LLP64 or LP64 must be defined.
171  *
172  * Here we define basic types WORD, LONG and their unsigned versions
173  * UWORD and ULONG. LONG must be double size of WORD. Their actual types
174  * are system-dependent. BITSINWORD and BITSINLONG are also defined.
175  * INT16, INT32 (also INT64 and INT128 if available) are used for
176  * system independent saved expressions (store.c).
177  */
178 #if defined(ILP32)
179 
180 typedef short WORD;
181 typedef long LONG;
182 typedef unsigned short UWORD;
183 typedef unsigned long ULONG;
184 #define BITSINWORD 16
185 #define BITSINLONG 32
186 #define INT16 short
187 #define INT32 int
188 #undef INT64
189 #undef INT128
190 
191 #ifdef SIZEOF_LONG_LONG
192 #if SIZEOF_LONG_LONG == 8
193 #define INT64 long long
194 #endif
195 #endif
196 
197 #ifndef INT64
198 #error INT64 is not available!
199 #endif
200 
201 #elif defined(LLP64)
202 
203 typedef int WORD;
204 typedef long long LONG;
205 typedef unsigned int UWORD;
206 typedef unsigned long long ULONG;
207 #define BITSINWORD 32
208 #define BITSINLONG 64
209 #define INT16 short
210 #define INT32 int
211 #define INT64 long long
212 #undef INT128
213 
214 #elif defined(LP64)
215 
216 typedef int WORD;
217 typedef long LONG;
218 typedef unsigned int UWORD;
219 typedef unsigned long ULONG;
220 #define BITSINWORD 32
221 #define BITSINLONG 64
222 #define INT16 short
223 #define INT32 int
224 #define INT64 long
225 #undef INT128
226 
227 #else
228 #error ILP32 or LLP64 or LP64 must be defined!
229 #endif
230 
231 STATIC_ASSERT(sizeof(WORD) * 8 == BITSINWORD);
232 STATIC_ASSERT(sizeof(LONG) * 8 == BITSINLONG);
233 STATIC_ASSERT(sizeof(WORD) * 2 == sizeof(LONG));
234 STATIC_ASSERT(sizeof(LONG) >= sizeof(int *));
235 STATIC_ASSERT(sizeof(INT16) == 2);
236 STATIC_ASSERT(sizeof(INT32) == 4);
237 STATIC_ASSERT(sizeof(INT64) == 8);
238 #ifdef INT128
239 STATIC_ASSERT(sizeof(INT128) == 16);
240 #endif
241 
242 #if BITSINWORD == 32
243 #define WORDSIZE32 1
244 #endif
245 
246 typedef void VOID;
247 typedef char SBYTE;
248 typedef unsigned char UBYTE;
249 typedef unsigned int UINT;
250 typedef ULONG RLONG; /* Used in reken.c. */
251 typedef INT64 MLONG; /* See commentary in minos.h. */
252  /* E.g. in 32-bits */
253 #define TOPBITONLY ((ULONG)1 << (BITSINWORD - 1)) /* 0x00008000UL */
254 #define TOPLONGBITONLY ((ULONG)1 << (BITSINLONG - 1)) /* 0x80000000UL */
255 #define SPECMASK ((UWORD)1 << (BITSINWORD - 1)) /* 0x8000U */
256 #define WILDMASK ((UWORD)1 << (BITSINWORD - 2)) /* 0x4000U */
257 #define WORDMASK ((ULONG)FULLMAX - 1) /* 0x0000FFFFUL */
258 #define AWORDMASK (WORDMASK << BITSINWORD) /* 0xFFFF0000UL */
259 #define FULLMAX ((LONG)1 << BITSINWORD) /* 0x00010000L */
260 #define MAXPOSITIVE ((LONG)(TOPBITONLY - 1)) /* 0x00007FFFL */
261 #define MAXLONG ((LONG)(TOPLONGBITONLY - 1)) /* 0x7FFFFFFFL */
262 #define MAXPOSITIVE2 (MAXPOSITIVE / 2) /* 0x00003FFFL */
263 
264 /*
265  * alignof(type) returns the number of bytes used in the alignment of
266  * the type.
267  */
268 #if !defined(alignof)
269 #if defined(__GNUC__)
270 /* GNU C compiler has "__alignof__". */
271 #define alignof(type) __alignof__(type)
272 #elif defined(_MSC_VER)
273 /* Microsoft C compiler has "__alignof". */
274 #define alignof(type) __alignof(type)
275 #elif !defined(__cplusplus)
276 /* Generic case in C. */
277 #include <stddef.h>
278 #define alignof(type) offsetof(struct { char c_; type x_; }, x_)
279 #else
280 /* Generic case in C++, at least works with a POD struct. */
281 #include <cstddef>
282 namespace alignof_impl_ {
283 template<typename T> struct calc {
284  struct X { char c_; T x_; };
285  enum { value = offsetof(X, x_) };
286 };
287 }
288 #define alignof(type) alignof_impl_::calc<type>::value
289 #endif
290 #endif
291 
292 /*
293  * Macros inserted to the end of a structure to align the whole structure.
294  *
295  * In the currently available systems,
296  * sizeof(POSITION) >= sizeof(pointers) == sizeof(LONG) >= sizeof(int)
297  * >= sizeof(WORD) >= sizeof(UBYTE) = 1.
298  * (POSITION is defined in struct.h and contains only an off_t variable.)
299  * Thus, if we put members of a structure in this order and use those macros,
300  * then we can align the data without relying on extra paddings added by
301  * the compiler. For example,
302  * typedef struct {
303  * int *a;
304  * LONG b;
305  * WORD c[2];
306  * UBYTE d;
307  * PADPOINTER(1,0,2,1);
308  * } A;
309  * typedef struct {
310  * POSITION p;
311  * A a; // aligned same as pointers
312  * int *b;
313  * LONG c;
314  * UBYTE d;
315  * PADPOSITION(1,1,0,0,1+sizeof(A));
316  * } B;
317  * The cost for the use of those PADXXX functions is a padding (>= 1 byte) will
318  * be always inserted even in the case that no padding is actually needed.
319  *
320  * Note that there is a 32-bit system in which off_t is aligned on 8-byte
321  * boundary, (e.g., Cygwin).
322  */
323 #define PADDUMMY(type, size) \
324  UBYTE d_u_m_m_y[alignof(type) - ((size) & (alignof(type) - 1))]
325 #define PADPOSITION(ptr_,long_,int_,word_,byte_) \
326  PADDUMMY(off_t, \
327  + sizeof(int *) * (ptr_) \
328  + sizeof(LONG) * (long_) \
329  + sizeof(int) * (int_) \
330  + sizeof(WORD) * (word_) \
331  + sizeof(UBYTE) * (byte_) \
332  )
333 #define PADPOINTER(long_,int_,word_,byte_) \
334  PADDUMMY(int *, \
335  + sizeof(LONG) * (long_) \
336  + sizeof(int) * (int_) \
337  + sizeof(WORD) * (word_) \
338  + sizeof(UBYTE) * (byte_) \
339  )
340 #define PADLONG(int_,word_,byte_) \
341  PADDUMMY(LONG, \
342  + sizeof(int) * (int_) \
343  + sizeof(WORD) * (word_) \
344  + sizeof(UBYTE) * (byte_) \
345  )
346 #define PADINT(word_,byte_) \
347  PADDUMMY(int, \
348  + sizeof(WORD) * (word_) \
349  + sizeof(UBYTE) * (byte_) \
350  )
351 #define PADWORD(byte_) \
352  PADDUMMY(WORD, \
353  + sizeof(UBYTE) * (byte_) \
354  )
355 
356 /*
357 #define WITHPCOUNTER
358 #define DEBUGGINGLOCKS
359 #define WITHSTATS
360 */
361 #define WITHSORTBOTS
362 
363 #include <stdio.h>
364 #include <stdlib.h>
365 #include <ctype.h>
366 #ifdef ANSI
367 #include <stdarg.h>
368 #include <time.h>
369 #endif
370 #ifdef WINDOWS
371 #include "fwin.h"
372 #endif
373 #ifdef UNIX
374 #include <unistd.h>
375 #include <time.h>
376 #include <fcntl.h>
377 #include <sys/file.h>
378 #include "unix.h"
379 #endif
380 #ifdef WITHZLIB
381 #include <zlib.h>
382 #endif
383 #ifdef WITHPTHREADS
384 #include <pthread.h>
385 #endif
386 
387 /*
388  PARALLELCODE indicates code that is common for TFORM and ParFORM but
389  should not be there for sequential FORM.
390 */
391 #if defined(WITHMPI) || defined(WITHPTHREADS)
392 #define PARALLELCODE
393 #endif
394 
395 #include "ftypes.h"
396 #include "fsizes.h"
397 #include "minos.h"
398 #include "structs.h"
399 #include "declare.h"
400 #include "variable.h"
401 
402 /*
403  * The interface to file routines for UNIX or non-UNIX.
404  */
405 #ifdef UNIX
406 
407 #define UFILES
408 typedef struct FiLeS {
409  int descriptor;
410 } FILES;
411 extern FILES *Uopen(char *,char *);
412 extern int Uclose(FILES *);
413 extern size_t Uread(char *,size_t,size_t,FILES *);
414 extern size_t Uwrite(char *,size_t,size_t,FILES *);
415 extern int Useek(FILES *,off_t,int);
416 extern off_t Utell(FILES *);
417 extern void Uflush(FILES *);
418 extern int Ugetpos(FILES *,fpos_t *);
419 extern int Usetpos(FILES *,fpos_t *);
420 extern void Usetbuf(FILES *,char *);
421 #define Usync(f) fsync(f->descriptor)
422 #define Utruncate(f) ftruncate(f->descriptor,0);
423 extern FILES *Ustdout;
424 #define MAX_OPEN_FILES getdtablesize()
425 
426 #else /* UNIX */
427 
428 #define FILES FILE
429 #define Uopen(x,y) fopen(x,y)
430 #define Uflush(x) fflush(x)
431 #define Uclose(x) fclose(x)
432 #define Uread(x,y,z,u) fread(x,y,z,u)
433 #define Uwrite(x,y,z,u) fwrite(x,y,z,u)
434 #define Usetbuf(x,y) setbuf(x,y)
435 #define Useek(x,y,z) fseek(x,y,z)
436 #define Utell(x) ftell(x)
437 #define Ugetpos(x,y) fgetpos(x,y)
438 #define Usetpos(x,y) fsetpos(x,y)
439 #define Usync(x) fflush(x)
440 #define Utruncate(x) _chsize(_fileno(x),0)
441 #define Ustdout stdout
442 #define MAX_OPEN_FILES FOPEN_MAX
443 #define bzero(b,len) (memset((b), 0, (len)), (void)0)
444 
445 #endif /* UNIX */
446 
447 #ifdef WITHMPI
448 #include "parallel.h"
449 #endif
450 
451 #endif /* __FORM3H__ */