]> www.ginac.de Git - cln.git/blob - src/base/cl_macros.h
* src/base/digitseq/cl_asm.h: Test if (intDsize==32) for MIPS and HPPA,
[cln.git] / src / base / cl_macros.h
1 // CLN internal macros
2
3 #ifndef _CL_MACROS_H
4 #define _CL_MACROS_H
5
6 #include "cln/types.h"
7
8 // Concatenation of macroexpanded tokens.
9 // Example:
10 //   #undef x
11 //   #define y 16
12 //   CONCAT(x,y)        ==>  'x16' (not 'xy' !)
13   #define CONCAT_(xxx,yyy)  xxx##yyy
14   #define CONCAT3_(aaa,bbb,ccc)  aaa##bbb##ccc
15   #define CONCAT4_(aaa,bbb,ccc,ddd)  aaa##bbb##ccc##ddd
16   #define CONCAT5_(aaa,bbb,ccc,ddd,eee)  aaa##bbb##ccc##ddd##eee
17   #define CONCAT6_(aaa,bbb,ccc,ddd,eee,fff)  aaa##bbb##ccc##ddd##eee##fff
18   #define CONCAT7_(aaa,bbb,ccc,ddd,eee,fff,ggg)  aaa##bbb##ccc##ddd##eee##fff##ggg
19   #define CONCAT(xxx,yyy)  CONCAT_(xxx,yyy)
20   #define CONCAT3(aaa,bbb,ccc)  CONCAT3_(aaa,bbb,ccc)
21   #define CONCAT4(aaa,bbb,ccc,ddd)  CONCAT4_(aaa,bbb,ccc,ddd)
22   #define CONCAT5(aaa,bbb,ccc,ddd,eee)  CONCAT5_(aaa,bbb,ccc,ddd,eee)
23   #define CONCAT6(aaa,bbb,ccc,ddd,eee,fff)  CONCAT6_(aaa,bbb,ccc,ddd,eee,fff)
24   #define CONCAT7(aaa,bbb,ccc,ddd,eee,fff,ggg)  CONCAT7_(aaa,bbb,ccc,ddd,eee,fff,ggg)
25
26 // Convert tokens to strings.
27 // STRING(token)  ==>  "token"
28   #define STRING(token) #token
29   #define STRINGIFY(token) STRING(token)
30
31 // Declare functions that don't return.
32 // nonreturning_function(extern,exit,(void)); == extern void exit (void);
33   #ifdef __GNUC__
34     #if (__GNUC__ >= 3) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 9))
35       #define nonreturning_function(storclass,funname,arguments)  \
36         storclass void funname arguments __attribute__((__noreturn__))
37     #else
38       #define nonreturning_function(storclass,funname,arguments)  \
39         typedef void CONCAT3(funname,_function_,__LINE__) arguments; \
40         storclass __volatile__ CONCAT3(funname,_function_,__LINE__) funname
41     #endif
42   #else
43     #define nonreturning_function(storclass,funname,arguments)  \
44       storclass void funname arguments
45   #endif
46
47 // Declaration of variables.
48   #define var
49
50 // `if' with more than one clause:
51 // if (cond1) ... {elif (condi) ...} [else ...]
52   #define elif  else if
53
54 // Endless loop, leave with  break;  or return...;
55   #define loop  while (1)
56
57 // Reversed end condition.
58 // Allows   until (expression) statement
59 // and      do statement until (expression);
60   #define until(expression)  while(!(expression))
61
62 // Boolean values.
63   #define FALSE  0
64   #define TRUE   1
65
66 // Ignore a value (instead of assigning it to a variable).
67 // unused ...
68   #if defined(__GNUC__) || defined(__KCC) // avoid a gcc warning "statement with no effect"
69     #define unused  (void)
70   #else
71     #define unused
72   #endif
73
74 // Denotes a point where control flow can never arrive.
75 // NOTREACHED
76   #define NOTREACHED  cl_notreached_abort(__FILE__,__LINE__);
77 namespace cln {
78   nonreturning_function(extern,cl_notreached_abort, (const char* filename, int lineno));
79 }  // namespace cln
80
81 // Check an arithmetic expression.
82 // ASSERT(expr)
83   #define ASSERT(expr)  { if (!(expr)) { NOTREACHED } }
84
85 // alloca()
86   #if defined(__GNUC__) && !defined(__riscos) && !defined(__convex__)
87     #undef alloca
88     #define alloca  __builtin_alloca
89   #elif defined(_MSC_VER)
90     #include <malloc.h>
91     #define alloca  _alloca
92   #elif defined(HAVE_ALLOCA_H) || defined(__riscos)
93     #include <alloca.h>
94     #ifndef alloca // Sometimes `alloca' is defined as a macro...
95       #if defined(__osf__)
96         extern "C" char* alloca (int size);
97       #else
98         extern "C" void* alloca (int size);
99       #endif
100     #endif
101   #elif defined(_AIX)
102     #pragma alloca // AIX requires this to be the first thing in the file.
103   #elif defined(WATCOM)
104     #include <malloc.h> // defines `alloca' as a macro
105   #elif !defined(NO_ALLOCA)
106     extern "C" void* alloca (int size);
107   #endif
108
109 // NULL pointer.
110   #undef NULL
111   #define NULL  0
112
113 // Bit number n (0<=n<32 or 0<=n<64)
114   #ifdef HAVE_FAST_LONGLONG
115     #define bit(n)  (1LL<<(n))
116   #else
117     #define bit(n)  (1L<<(n))
118   #endif
119 // Bit number n (0<n<=32) mod 2^32
120   #ifdef HAVE_FAST_LONGLONG
121     #define bitm(n)  (2LL<<((n)-1))
122   #else
123     #define bitm(n)  (2L<<((n)-1))
124   #endif
125 // Test bit n in x, n constant, x a cl_uint:
126   #if !(defined(__sparc__) || defined(__sparc64__))
127     #define bit_test(x,n)  ((x) & bit(n))
128   #else
129     // On Sparcs long constants are slower than shifts.
130     #if !defined(__GNUC__)
131       #define bit_test(x,n)  \
132         ((n)<12 ? ((x) & bit(n)) : ((sint32)((uint32)(x) << (31-(n))) < 0))
133     #else // gcc optimizes boolean expressions better this way:
134       #define bit_test(x,n)  \
135         (   ( ((n)<12) && ((x) & bit(n)) )                           \
136          || ( ((n)>=12) && ((sint32)((uint32)(x) << (31-(n))) < 0) ) \
137         )
138     #endif
139   #endif
140 // minus bit number n (0<=n<32 or 0<=n<64)
141   #ifdef HAVE_FAST_LONGLONG
142     #define minus_bit(n)  (-1LL<<(n))
143   #else
144     #define minus_bit(n)  (-1L<<(n))
145   #endif
146 // minus bit number n (0<n<=32) mod 2^32
147   #ifdef HAVE_FAST_LONGLONG
148     #define minus_bitm(n)  (-2LL<<((n)-1))
149   #else
150     #define minus_bitm(n)  (-2L<<((n)-1))
151   #endif
152
153 // Return 2^n, n a constant expression.
154 // Same as bit(n), but undefined if n<0 or n>={long_}long_bitsize.
155   #ifdef HAVE_FAST_LONGLONG
156     #define bitc(n)  (1ULL << (((n) >= 0 && (n) < long_long_bitsize) ? (n) : 0))
157   #else
158     #define bitc(n)  (1UL << (((n) >= 0 && (n) < long_bitsize) ? (n) : 0))
159   #endif
160
161 // floor(a,b) for a>=0, b>0 returns floor(a/b).
162 // b should be a constant expression.
163   #define floor(a_from_floor,b_from_floor)  ((a_from_floor) / (b_from_floor))
164 // Save the macro in case we need to include <cmath>.
165   #define cln_floor(a_from_floor,b_from_floor)  ((a_from_floor) / (b_from_floor))
166
167 // ceiling(a,b) for a>=0, b>0 returns ceiling(a/b) = floor((a+b-1)/b).
168 // b should be a constant expression.
169   #define ceiling(a_from_ceiling,b_from_ceiling)  \
170     (((a_from_ceiling) + (b_from_ceiling) - 1) / (b_from_ceiling))
171
172 // round_down(a,b) decreases a>=0 such that it becomes divisible by b>0.
173 // b should be a constant expression.
174   #define round_down(a_from_round,b_from_round)  \
175     (floor(a_from_round,b_from_round)*(b_from_round))
176
177 // round_up(a,b) increases a>=0 such that it becomes divisible by b>0.
178 // b should be a constant expression.
179   #define round_up(a_from_round,b_from_round)  \
180     (ceiling(a_from_round,b_from_round)*(b_from_round))
181
182 // We never call malloc(0), so no need to handle it.
183   #define __MALLOC_0_RETURNS_NULL
184
185 // Loop which executes a statement a given number of times.
186 // dotimesC(countvar,count,statement);
187 // countvar must be of type `uintC'. It is modified!
188   #define dotimesC(countvar_from_dotimesC,count_from_dotimesC,statement_from_dotimesC)  \
189     { countvar_from_dotimesC = (count_from_dotimesC);         \
190       until (countvar_from_dotimesC==0)                       \
191         {statement_from_dotimesC; countvar_from_dotimesC--; } \
192     }
193   #define dotimespC(countvar_from_dotimespC,count_from_dotimespC,statement_from_dotimespC)  \
194     { countvar_from_dotimespC = (count_from_dotimespC);                   \
195       do {statement_from_dotimespC} until (--countvar_from_dotimespC==0); \
196     }
197
198 // doconsttimes(count,statement);
199 // führt statement count mal aus (count mal der Code!),
200 // wobei count eine constant-expression >=0, <=8 ist.
201   #define doconsttimes(count_from_doconsttimes,statement_from_doconsttimes)  \
202     { if (0 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
203       if (1 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
204       if (2 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
205       if (3 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
206       if (4 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
207       if (5 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
208       if (6 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
209       if (7 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
210     }
211
212 // DOCONSTTIMES(count,macroname);
213 // ruft count mal den Macro macroname auf (count mal der Code!),
214 // wobei count eine constant-expression >=0, <=8 ist.
215 // Dabei bekommt macroname der Reihe nach die Werte 0,...,count-1 übergeben.
216   #define DOCONSTTIMES(count_from_DOCONSTTIMES,macroname_from_DOCONSTTIMES)  \
217     { if (0 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((0 < (count_from_DOCONSTTIMES) ? 0 : 0)); } \
218       if (1 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((1 < (count_from_DOCONSTTIMES) ? 1 : 0)); } \
219       if (2 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((2 < (count_from_DOCONSTTIMES) ? 2 : 0)); } \
220       if (3 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((3 < (count_from_DOCONSTTIMES) ? 3 : 0)); } \
221       if (4 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((4 < (count_from_DOCONSTTIMES) ? 4 : 0)); } \
222       if (5 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((5 < (count_from_DOCONSTTIMES) ? 5 : 0)); } \
223       if (6 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((6 < (count_from_DOCONSTTIMES) ? 6 : 0)); } \
224       if (7 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((7 < (count_from_DOCONSTTIMES) ? 7 : 0)); } \
225     }
226
227 // AT_INITIALIZATION(id) { ... }
228 // executes the given code at initialization time of the file.
229 // The id is something unique.
230   #define AT_INITIALIZATION(id)  \
231     class CONCAT3(INIT_CLASS_,id,__LINE__) {                            \
232       public: CONCAT3(INIT_CLASS_,id,__LINE__) (void);                  \
233     } CONCAT4(INIT_CLASS_,id,__LINE__,_DUMMY);                          \
234     inline CONCAT3(INIT_CLASS_,id,__LINE__)::CONCAT3(INIT_CLASS_,id,__LINE__) (void)
235
236 // AT_DESTRUCTION(id) { ... }
237 // executes the given code at destruction time of the file.
238 // The id is something unique.
239   #define AT_DESTRUCTION(id)  \
240     class CONCAT3(DESTR_CLASS_,id,__LINE__) {                           \
241       public: ~CONCAT3(DESTR_CLASS_,id,__LINE__) (void);                \
242     } CONCAT4(DESTR_CLASS_,id,__LINE__,_DUMMY);                         \
243     CONCAT3(DESTR_CLASS_,id,__LINE__)::~CONCAT3(DESTR_CLASS_,id,__LINE__) (void)
244
245 // Inside a class definition:
246 // Overload `new' so that a class object can be allocated anywhere.
247 #if !((defined(__rs6000__) || defined(__alpha__)) && !defined(__GNUC__))
248 #define ALLOCATE_ANYWHERE(classname)  \
249     /* Ability to place an object at a given address. */                \
250 public:                                                                 \
251     void* operator new (size_t size) { return malloc_hook(size); }      \
252     void* operator new (size_t size, classname* ptr) { unused size; return ptr; } \
253     void operator delete (void* ptr) { free_hook(ptr); }
254 #else
255 // For some compilers, work around template problem with "classname".
256 #define ALLOCATE_ANYWHERE(classname)  \
257     /* Ability to place an object at a given address. */                \
258 public:                                                                 \
259     void* operator new (size_t size) { return malloc_hook(size); }      \
260     void* operator new (size_t size, void* ptr) { unused size; return ptr; } \
261     void operator delete (void* ptr) { free_hook(ptr); }
262 #endif
263
264 // init1(type, object) (value);
265 // initializes `object' with `value', by calling `type''s constructor.
266 // (The identifiers `init' and `Init' are already in use by <streambuf.h>,
267 // it's a shame!)
268 #define init1(type,lvalue)  (void) new (&(lvalue)) type
269
270 // MAYBE_INLINE normally expands to nothing.
271 // Useful for including the implementation of some file inline into another.
272   #define MAYBE_INLINE
273   #define MAYBE_INLINE2
274
275 #endif /* _CL_MACROS_H */