svn merge ^/trunk/blender -r41226:41227 .
[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) { type tmp; tmp = a; a = c; c = b; b = tmp; }
57 #define SHIFT4(type, a, b, c, d) { type tmp; tmp = a; a = d; d = c; c = b; b = tmp; }
58
59 /* min/max */
60 #define MIN2(x,y)               ( (x)<(y) ? (x) : (y) )
61 #define MIN3(x,y,z)             MIN2( MIN2((x),(y)) , (z) )
62 #define MIN4(x,y,z,a)           MIN2( MIN2((x),(y)) , MIN2((z),(a)) )
63
64 #define MAX2(x,y)               ( (x)>(y) ? (x) : (y) )
65 #define MAX3(x,y,z)             MAX2( MAX2((x),(y)) , (z) )
66 #define MAX4(x,y,z,a)           MAX2( MAX2((x),(y)) , MAX2((z),(a)) )
67
68 #define INIT_MINMAX(min, max) {                                               \
69                 (min)[0]= (min)[1]= (min)[2]= 1.0e30f;                                \
70                 (max)[0]= (max)[1]= (max)[2]= -1.0e30f;                               \
71         }
72 #define INIT_MINMAX2(min, max) {                                              \
73                 (min)[0]= (min)[1]= 1.0e30f;                                          \
74                 (max)[0]= (max)[1]= -1.0e30f;                                         \
75         }
76 #define DO_MIN(vec, min) {                                                    \
77                 if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0];                           \
78                 if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1];                           \
79                 if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2];                           \
80         }
81 #define DO_MAX(vec, max) {                                                    \
82                 if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0];                           \
83                 if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1];                           \
84                 if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2];                           \
85         }
86 #define DO_MINMAX(vec, min, max) {                                            \
87                 if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0];                           \
88                 if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1];                           \
89                 if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2];                           \
90                 if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0];                           \
91                 if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1];                           \
92                 if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2];                           \
93         }
94 #define DO_MINMAX2(vec, min, max) {                                           \
95                 if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0];                           \
96                 if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1];                           \
97                 if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0];                           \
98                 if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1];                           \
99         }
100
101 /* some math and copy defines */
102
103 #ifndef SWAP
104 #  define SWAP(type, a, b)       { type sw_ap; sw_ap=(a); (a)=(b); (b)=sw_ap; }
105 #endif
106
107 #define ABS(a)          ( (a)<0 ? (-(a)) : (a) )
108
109 #define FTOCHAR(val) ((val)<=0.0f)? 0 : (((val)>(1.0f-0.5f/255.0f))? 255 : (char)((255.0f*(val))+0.5f))
110 #define FTOUSHORT(val) ((val >= 1.0f-0.5f/65535)? 65535: (val <= 0.0f)? 0: (unsigned short)(val*65535.0f + 0.5f))
111 #define F3TOCHAR3(v2, v1) {                                                   \
112                 (v1)[0]= FTOCHAR((v2[0]));                                            \
113                 (v1)[1]= FTOCHAR((v2[1]));                                            \
114                 (v1)[2]= FTOCHAR((v2[2]));                                            \
115         }
116 #define F3TOCHAR4(v2, v1) {                                                   \
117                 (v1)[0]= FTOCHAR((v2[0]));                                            \
118                 (v1)[1]= FTOCHAR((v2[1]));                                            \
119                 (v1)[2]= FTOCHAR((v2[2]));                                            \
120                 (v1)[3]= 255;                                                         \
121         }
122 #define F4TOCHAR4(v2, v1) {                                                   \
123                 (v1)[0]= FTOCHAR((v2[0]));                                            \
124                 (v1)[1]= FTOCHAR((v2[1]));                                            \
125                 (v1)[2]= FTOCHAR((v2[2]));                                            \
126                 (v1)[3]= FTOCHAR((v2[3]));                                            \
127         }
128 #define VECCOPY(v1, v2) {                                                     \
129                 *(v1)=   *(v2);                                                       \
130                 *(v1+1)= *(v2+1);                                                     \
131                 *(v1+2)= *(v2+2);                                                     \
132         }
133 #define VECCOPY2D(v1, v2) {                                                   \
134                 *(v1)=   *(v2);                                                       \
135                 *(v1+1)= *(v2+1);                                                     \
136         }
137 #define QUATCOPY(v1, v2) {                                                    \
138                 *(v1)=   *(v2);                                                       \
139                 *(v1+1)= *(v2+1);                                                     \
140                 *(v1+2)= *(v2+2);                                                     \
141                 *(v1+3)= *(v2+3);                                                     \
142         }
143 #define VECADD(v1,v2,v3) {                                                    \
144                 *(v1)=   *(v2)   + *(v3);                                             \
145                 *(v1+1)= *(v2+1) + *(v3+1);                                           \
146                 *(v1+2)= *(v2+2) + *(v3+2);                                           \
147         }
148 #define VECSUB(v1,v2,v3) {                                                    \
149                 *(v1)=   *(v2)   - *(v3);                                             \
150                 *(v1+1)= *(v2+1) - *(v3+1);                                           \
151                 *(v1+2)= *(v2+2) - *(v3+2);                                           \
152         }
153 #define VECSUB2D(v1,v2,v3)     {                                              \
154                 *(v1)=   *(v2)   - *(v3);                                             \
155                 *(v1+1)= *(v2+1) - *(v3+1);                                           \
156         }
157 #define VECADDFAC(v1,v2,v3,fac) {                                             \
158                 *(v1)=   *(v2)   + *(v3)*(fac);                                       \
159                 *(v1+1)= *(v2+1) + *(v3+1)*(fac);                                     \
160                 *(v1+2)= *(v2+2) + *(v3+2)*(fac);                                     \
161         }
162 #define VECSUBFAC(v1,v2,v3,fac) {                                             \
163                 *(v1)=   *(v2)   - *(v3)*(fac);                                       \
164                 *(v1+1)= *(v2+1) - *(v3+1)*(fac);                                     \
165                 *(v1+2)= *(v2+2) - *(v3+2)*(fac);                                     \
166         }
167 #define QUATADDFAC(v1,v2,v3,fac) {                                            \
168                 *(v1)=   *(v2)   + *(v3)*(fac);                                       \
169                 *(v1+1)= *(v2+1) + *(v3+1)*(fac);                                     \
170                 *(v1+2)= *(v2+2) + *(v3+2)*(fac);                                     \
171                 *(v1+3)= *(v2+3) + *(v3+3)*(fac);                                     \
172         }
173
174 #define INPR(v1, v2) ( (v1)[0]*(v2)[0] + (v1)[1]*(v2)[1] + (v1)[2]*(v2)[2] )
175
176 /* some misc stuff.... */
177 #define CLAMP(a, b, c)          if((a)<(b)) (a)=(b); else if((a)>(c)) (a)=(c)
178 #define CLAMPIS(a, b, c) ((a)<(b) ? (b) : (a)>(c) ? (c) : (a))
179 #define CLAMPTEST(a, b, c)      if((b)<(c)) {CLAMP(a, b, c);} else {CLAMP(a, c, b);}
180
181 #define IS_EQ(a,b) ((fabs((double)(a)-(b)) >= (double) FLT_EPSILON) ? 0 : 1)
182 #define IS_EQF(a,b) ((fabsf((float)(a)-(b)) >= (float) FLT_EPSILON) ? 0 : 1)
183
184 #define IS_EQT(a, b, c) ((a > b)? (((a-b) <= c)? 1:0) : ((((b-a) <= c)? 1:0)))
185 #define IN_RANGE(a, b, c) ((b < c)? ((b<a && a<c)? 1:0) : ((c<a && a<b)? 1:0))
186 #define IN_RANGE_INCL(a, b, c) ((b < c)? ((b<=a && a<=c)? 1:0) : ((c<=a && a<=b)? 1:0))
187
188 /* array helpers */
189 #define ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot)                 \
190                 (arr_dtype *)((char*)arr_start + (elem_size*(tot - 1)))
191
192 #define ARRAY_HAS_ITEM(item, arr_start, arr_dtype, elem_size, tot) (          \
193                 (item >= arr_start) &&                                                \
194                 (item <= ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot))       \
195         )
196
197 /* This one rotates the bytes in an int64, int (32) and short (16) */
198 #define SWITCH_INT64(a) {                                                     \
199                 char s_i, *p_i;                                                       \
200                 p_i= (char *)&(a);                                                    \
201                 s_i= p_i[0]; p_i[0]= p_i[7]; p_i[7]= s_i;                             \
202                 s_i= p_i[1]; p_i[1]= p_i[6]; p_i[6]= s_i;                             \
203                 s_i= p_i[2]; p_i[2]= p_i[5]; p_i[5]= s_i;                             \
204                 s_i= p_i[3]; p_i[3]= p_i[4]; p_i[4]= s_i;                             \
205         }
206
207 #define SWITCH_INT(a) {                                                       \
208                 char s_i, *p_i;                                                       \
209                 p_i= (char *)&(a);                                                    \
210                 s_i= p_i[0]; p_i[0]= p_i[3]; p_i[3]= s_i;                             \
211                 s_i= p_i[1]; p_i[1]= p_i[2]; p_i[2]= s_i;                             \
212         }
213
214 #define SWITCH_SHORT(a) {                                                     \
215                 char s_i, *p_i;                                                       \
216                 p_i= (char *)&(a);                                                    \
217                 s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i;                                \
218         }
219
220
221 /* Warning-free macros for storing ints in pointers. Use these _only_
222  * for storing an int in a pointer, not a pointer in an int (64bit)! */
223 #define SET_INT_IN_POINTER(i) ((void*)(intptr_t)(i))
224 #define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i))
225
226 /* Macro to convert a value to string in the preprocessor
227  * STRINGIFY_ARG: gives the defined name in the string
228  * STRINGIFY: gives the defined value. */
229 #define STRINGIFY_ARG(x) #x
230 #define STRINGIFY(x) STRINGIFY_ARG(x)
231
232 /* useful for debugging */
233 #define AT __FILE__ ":" STRINGIFY(__LINE__)
234
235 /* so we can use __func__ everywhere */
236 #if defined(_MSC_VER)
237 #  define __func__ __FUNCTION__
238 #endif
239
240
241 /* UNUSED macro, for function argument */
242 #ifdef __GNUC__
243 #  define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
244 #else
245 #  define UNUSED(x) UNUSED_ ## x
246 #endif
247
248 #ifdef __GNUC__
249 #  define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
250 #else
251 #  define UNUSED_FUNCTION(x) UNUSED_ ## x
252 #endif
253
254 #ifdef __GNUC__
255 #  define WARN_UNUSED  __attribute__((warn_unused_result))
256 #else
257 #  define WARN_UNUSED
258 #endif
259
260 /*little macro so inline keyword works*/
261 #if defined(_MSC_VER)
262 #  define BM_INLINE static __forceinline
263 #elif defined(__GNUC__)
264 #  define BM_INLINE static inline __attribute((always_inline))
265 #else
266 /* #warning "MSC/GNUC defines not found, inline non-functional" */
267 #  define BM_INLINE static
268 #endif
269
270
271 /* BLI_assert(), default only to print
272  * for aborting need to define WITH_ASSERT_ABORT
273  */
274 #if !defined NDEBUG
275 #  ifdef WITH_ASSERT_ABORT
276 #    define _dummy_abort abort
277 #  else
278 #    define _dummy_abort() (void)0
279 #  endif
280 #  if defined(__GNUC__) || defined(_MSC_VER) /* check __func__ is available */
281 #    define BLI_assert(a)                                                     \
282 do {                                                                          \
283         if (!(a)) {                                                               \
284                 fprintf(stderr,                                                       \
285                         "BLI_assert failed: %s, %s(), %d at \'%s\'\n",                    \
286                         __FILE__, __func__, __LINE__, STRINGIFY(a));                      \
287                 _dummy_abort();                                                       \
288         }                                                                         \
289 } while (0)
290 #  else
291 #    define BLI_assert(a) \
292 do {                                                                          \
293         if (0 == (a)) {                                                           \
294                 fprintf(stderr,                                                       \
295                         "BLI_assert failed: %s, %d at \'%s\'\n",                          \
296                         __FILE__, __LINE__, STRINGIFY(a));                                \
297                 _dummy_abort();                                                       \
298         }                                                                         \
299 } while (0)
300 #  endif
301 #else
302 #  define BLI_assert(a) (void)0
303 #endif
304
305 #define BMEMSET(mem, val, size) {unsigned int _i, _size = (size); char *_c = (char*) mem; for (_i=0; _i<_size; _i++) *_c++ = val;}
306
307 #endif // BLI_UTILDEFINES_H