code cleanup:
[blender.git] / source / blender / blenlib / BLI_utildefines.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #ifndef __BLI_UTILDEFINES_H__
29 #define __BLI_UTILDEFINES_H__
30
31 /** \file BLI_utildefines.h
32  *  \ingroup bli
33  */
34
35 #ifndef FALSE
36 #  define FALSE 0
37 #endif
38
39 #ifndef TRUE
40 #  define TRUE 1
41 #endif
42
43
44 #define ELEM(a, b, c)           ( (a)==(b) || (a)==(c) )
45 #define ELEM3(a, b, c, d)       ( ELEM(a, b, c) || (a)==(d) )
46 #define ELEM4(a, b, c, d, e)    ( ELEM(a, b, c) || ELEM(a, d, e) )
47 #define ELEM5(a, b, c, d, e, f) ( ELEM(a, b, c) || ELEM3(a, d, e, f) )
48 #define ELEM6(a, b, c, d, e, f, g)      ( ELEM(a, b, c) || ELEM4(a, d, e, f, g) )
49 #define ELEM7(a, b, c, d, e, f, g, h)   ( ELEM3(a, b, c, d) || ELEM4(a, e, f, g, h) )
50 #define ELEM8(a, b, c, d, e, f, g, h, i)        ( ELEM4(a, b, c, d, e) || ELEM4(a, f, g, h, i) )
51 #define ELEM9(a, b, c, d, e, f, g, h, i, j)        ( ELEM4(a, b, c, d, e) || ELEM5(a, f, g, h, i, j) )
52 #define ELEM10(a, b, c, d, e, f, g, h, i, j, k)        ( ELEM4(a, b, c, d, e) || ELEM6(a, f, g, h, i, j, k) )
53 #define ELEM11(a, b, c, d, e, f, g, h, i, j, k, l)        ( ELEM4(a, b, c, d, e) || ELEM7(a, f, g, h, i, j, k, l) )
54
55 /* shift around elements */
56 #define SHIFT3(type, a, b, c)  {                                              \
57                 type tmp;                                                             \
58                 tmp = a;                                                              \
59                 a = c;                                                                \
60                 c = b;                                                                \
61                 b = tmp;                                                              \
62         } (void)0
63 #define SHIFT4(type, a, b, c, d)  {                                           \
64                 type tmp;                                                             \
65                 tmp = a;                                                              \
66                 a = d;                                                                \
67                 d = c;                                                                \
68                 c = b;                                                                \
69                 b = tmp;                                                              \
70         } (void)0
71
72 /* min/max */
73 #define MIN2(x,y)               ( (x)<(y) ? (x) : (y) )
74 #define MIN3(x,y,z)             MIN2( MIN2((x),(y)) , (z) )
75 #define MIN4(x,y,z,a)           MIN2( MIN2((x),(y)) , MIN2((z),(a)) )
76
77 #define MAX2(x,y)               ( (x)>(y) ? (x) : (y) )
78 #define MAX3(x,y,z)             MAX2( MAX2((x),(y)) , (z) )
79 #define MAX4(x,y,z,a)           MAX2( MAX2((x),(y)) , MAX2((z),(a)) )
80
81 #define INIT_MINMAX(min, max) {                                               \
82                 (min)[0]= (min)[1]= (min)[2]= 1.0e30f;                                \
83                 (max)[0]= (max)[1]= (max)[2]= -1.0e30f;                               \
84         }
85 #define INIT_MINMAX2(min, max) {                                              \
86                 (min)[0]= (min)[1]= 1.0e30f;                                          \
87                 (max)[0]= (max)[1]= -1.0e30f;                                         \
88         } (void)0
89 #define DO_MIN(vec, min) {                                                    \
90                 if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0];                           \
91                 if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1];                           \
92                 if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2];                           \
93         } (void)0
94 #define DO_MAX(vec, max) {                                                    \
95                 if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0];                           \
96                 if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1];                           \
97                 if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2];                           \
98         } (void)0
99 #define DO_MINMAX(vec, min, max) {                                            \
100                 if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0];                           \
101                 if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1];                           \
102                 if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2];                           \
103                 if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0];                           \
104                 if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1];                           \
105                 if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2];                           \
106         } (void)0
107 #define DO_MINMAX2(vec, min, max) {                                           \
108                 if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0];                           \
109                 if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1];                           \
110                 if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0];                           \
111                 if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1];                           \
112         } (void)0
113
114 /* some math and copy defines */
115
116 #ifndef SWAP
117 #  define SWAP(type, a, b)       { type sw_ap; sw_ap=(a); (a)=(b); (b)=sw_ap; }
118 #endif
119
120 #define ABS(a)          ( (a)<0 ? (-(a)) : (a) )
121
122 #define FTOCHAR(val) ((val)<=0.0f)? 0 : (((val)>(1.0f-0.5f/255.0f))? 255 : (char)((255.0f*(val))+0.5f))
123 #define FTOUSHORT(val) ((val >= 1.0f-0.5f/65535)? 65535: (val <= 0.0f)? 0: (unsigned short)(val*65535.0f + 0.5f))
124 #define USHORTTOUCHAR(val) ((unsigned char)(((val) >= 65535-128)? 255: ((val)+128)>>8))
125 #define F3TOCHAR3(v2, v1) {                                                   \
126                 (v1)[0]= FTOCHAR((v2[0]));                                            \
127                 (v1)[1]= FTOCHAR((v2[1]));                                            \
128                 (v1)[2]= FTOCHAR((v2[2]));                                            \
129         } (void)0
130 #define F3TOCHAR4(v2, v1) {                                                   \
131                 (v1)[0]= FTOCHAR((v2[0]));                                            \
132                 (v1)[1]= FTOCHAR((v2[1]));                                            \
133                 (v1)[2]= FTOCHAR((v2[2]));                                            \
134                 (v1)[3]= 255;                                                         \
135         } (void)0
136 #define F4TOCHAR4(v2, v1) {                                                   \
137                 (v1)[0]= FTOCHAR((v2[0]));                                            \
138                 (v1)[1]= FTOCHAR((v2[1]));                                            \
139                 (v1)[2]= FTOCHAR((v2[2]));                                            \
140                 (v1)[3]= FTOCHAR((v2[3]));                                            \
141         } (void)0
142 #define VECCOPY(v1, v2) {                                                     \
143                 *(v1)=   *(v2);                                                       \
144                 *(v1+1)= *(v2+1);                                                     \
145                 *(v1+2)= *(v2+2);                                                     \
146         } (void)0
147 #define VECCOPY2D(v1, v2) {                                                   \
148                 *(v1)=   *(v2);                                                       \
149                 *(v1+1)= *(v2+1);                                                     \
150         } (void)0
151 #define VECADD(v1,v2,v3) {                                                    \
152                 *(v1)=   *(v2)   + *(v3);                                             \
153                 *(v1+1)= *(v2+1) + *(v3+1);                                           \
154                 *(v1+2)= *(v2+2) + *(v3+2);                                           \
155         } (void)0
156 #define VECSUB(v1,v2,v3) {                                                    \
157                 *(v1)=   *(v2)   - *(v3);                                             \
158                 *(v1+1)= *(v2+1) - *(v3+1);                                           \
159                 *(v1+2)= *(v2+2) - *(v3+2);                                           \
160         } (void)0
161 #define VECSUB2D(v1,v2,v3)     {                                              \
162                 *(v1)=   *(v2)   - *(v3);                                             \
163                 *(v1+1)= *(v2+1) - *(v3+1);                                           \
164         } (void)0
165 #define VECADDFAC(v1,v2,v3,fac) {                                             \
166                 *(v1)=   *(v2)   + *(v3)*(fac);                                       \
167                 *(v1+1)= *(v2+1) + *(v3+1)*(fac);                                     \
168                 *(v1+2)= *(v2+2) + *(v3+2)*(fac);                                     \
169         } (void)0
170 #define VECSUBFAC(v1,v2,v3,fac) {                                             \
171                 *(v1)=   *(v2)   - *(v3)*(fac);                                       \
172                 *(v1+1)= *(v2+1) - *(v3+1)*(fac);                                     \
173                 *(v1+2)= *(v2+2) - *(v3+2)*(fac);                                     \
174         } (void)0
175
176 #define INPR(v1, v2) ( (v1)[0]*(v2)[0] + (v1)[1]*(v2)[1] + (v1)[2]*(v2)[2] )
177
178 /* some misc stuff.... */
179 #define CLAMP(a, b, c)          if((a)<(b)) (a)=(b); else if((a)>(c)) (a)=(c)
180 #define CLAMPIS(a, b, c) ((a)<(b) ? (b) : (a)>(c) ? (c) : (a))
181 #define CLAMPTEST(a, b, c)                                                    \
182         if ((b) < (c)) {                                                          \
183                 CLAMP(a, b, c);                                                       \
184         }                                                                         \
185         else {                                                                    \
186                 CLAMP(a, c, b);                                                       \
187         } (void)
188
189 #define IS_EQ(a,b) ((fabs((double)(a)-(b)) >= (double) FLT_EPSILON) ? 0 : 1)
190 #define IS_EQF(a,b) ((fabsf((float)(a)-(b)) >= (float) FLT_EPSILON) ? 0 : 1)
191
192 #define IS_EQT(a, b, c) ((a > b)? (((a-b) <= c)? 1:0) : ((((b-a) <= c)? 1:0)))
193 #define IN_RANGE(a, b, c) ((b < c)? ((b<a && a<c)? 1:0) : ((c<a && a<b)? 1:0))
194 #define IN_RANGE_INCL(a, b, c) ((b < c)? ((b<=a && a<=c)? 1:0) : ((c<=a && a<=b)? 1:0))
195
196 /* array helpers */
197 #define ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot)                 \
198                 (arr_dtype *)((char*)arr_start + (elem_size*(tot - 1)))
199
200 #define ARRAY_HAS_ITEM(item, arr_start, arr_dtype, elem_size, tot) (          \
201                 (item >= arr_start) &&                                                \
202                 (item <= ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot))       \
203         )
204
205 /* This one rotates the bytes in an int64, int (32) and short (16) */
206 #define SWITCH_INT64(a) {                                                     \
207                 char s_i, *p_i;                                                       \
208                 p_i= (char *)&(a);                                                    \
209                 s_i= p_i[0]; p_i[0]= p_i[7]; p_i[7]= s_i;                             \
210                 s_i= p_i[1]; p_i[1]= p_i[6]; p_i[6]= s_i;                             \
211                 s_i= p_i[2]; p_i[2]= p_i[5]; p_i[5]= s_i;                             \
212                 s_i= p_i[3]; p_i[3]= p_i[4]; p_i[4]= s_i;                             \
213         } (void)0
214
215 #define SWITCH_INT(a) {                                                       \
216                 char s_i, *p_i;                                                       \
217                 p_i= (char *)&(a);                                                    \
218                 s_i= p_i[0]; p_i[0]= p_i[3]; p_i[3]= s_i;                             \
219                 s_i= p_i[1]; p_i[1]= p_i[2]; p_i[2]= s_i;                             \
220         } (void)0
221
222 #define SWITCH_SHORT(a) {                                                     \
223                 char s_i, *p_i;                                                       \
224                 p_i= (char *)&(a);                                                    \
225                 s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i;                                \
226         } (void)0
227
228
229 /* Warning-free macros for storing ints in pointers. Use these _only_
230  * for storing an int in a pointer, not a pointer in an int (64bit)! */
231 #define SET_INT_IN_POINTER(i)    ((void *)(intptr_t)(i))
232 #define GET_INT_FROM_POINTER(i)  ((int)(intptr_t)(i))
233
234 /* Macro to convert a value to string in the preprocessor
235  * STRINGIFY_ARG: gives the argument as a string
236  * STRINGIFY_APPEND: appends any argument 'b' onto the string argument 'a',
237  *   used by STRINGIFY because some preprocessors warn about zero arguments
238  * STRINGIFY: gives the argument's value as a string */
239 #define STRINGIFY_ARG(x) "" #x
240 #define STRINGIFY_APPEND(a, b) "" a #b
241 #define STRINGIFY(x) STRINGIFY_APPEND("", x)
242
243 /* useful for debugging */
244 #define AT __FILE__ ":" STRINGIFY(__LINE__)
245
246 /* so we can use __func__ everywhere */
247 #if defined(_MSC_VER)
248 #  define __func__ __FUNCTION__
249 #endif
250
251
252 /* UNUSED macro, for function argument */
253 #ifdef __GNUC__
254 #  define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
255 #else
256 #  define UNUSED(x) UNUSED_ ## x
257 #endif
258
259 #ifdef __GNUC__
260 #  define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
261 #else
262 #  define UNUSED_FUNCTION(x) UNUSED_ ## x
263 #endif
264
265 #ifdef __GNUC__
266 #  define WARN_UNUSED  __attribute__((warn_unused_result))
267 #else
268 #  define WARN_UNUSED
269 #endif
270
271 /*little macro so inline keyword works*/
272 #if defined(_MSC_VER)
273 #  define BLI_INLINE static __forceinline
274 #elif defined(__GNUC__)
275 #  define BLI_INLINE static inline __attribute((always_inline))
276 #else
277 /* #warning "MSC/GNUC defines not found, inline non-functional" */
278 #  define BLI_INLINE static
279 #endif
280
281
282 /* BLI_assert(), default only to print
283  * for aborting need to define WITH_ASSERT_ABORT
284  */
285 #ifndef NDEBUG
286 #  ifdef WITH_ASSERT_ABORT
287 #    define _BLI_DUMMY_ABORT abort
288 #  else
289 #    define _BLI_DUMMY_ABORT() (void)0
290 #  endif
291 #  if defined(__GNUC__) || defined(_MSC_VER) /* check __func__ is available */
292 #    define BLI_assert(a)                                                     \
293         (void)((!(a)) ?  (                                                        \
294                 (                                                                     \
295                 fprintf(stderr,                                                       \
296                         "BLI_assert failed: %s:%d, %s(), at \'%s\'\n",                    \
297                         __FILE__, __LINE__, __func__, STRINGIFY(a)),                      \
298                 _BLI_DUMMY_ABORT(),                                                   \
299                 NULL)) : NULL)
300 #  else
301 #    define BLI_assert(a)                                                     \
302         (void)((!(a)) ?  (                                                        \
303                 (                                                                     \
304                 fprintf(stderr,                                                       \
305                         "BLI_assert failed: %s:%d, at \'%s\'\n",                          \
306                         __FILE__, __LINE__, STRINGIFY(a)),                                \
307                 _BLI_DUMMY_ABORT(),                                                   \
308                 NULL)) : NULL)
309 #  endif
310 #else
311 #  define BLI_assert(a) (void)0
312 #endif
313
314 /* hints for branch pradiction, only use in code that runs a _lot_ where */
315 #ifdef __GNUC__
316 #  define LIKELY(x)       __builtin_expect(!!(x), 1)
317 #  define UNLIKELY(x)     __builtin_expect(!!(x), 0)
318 #else
319 #  define LIKELY(x)       (x)
320 #  define UNLIKELY(x)     (x)
321 #endif
322
323 #endif // __BLI_UTILDEFINES_H__