patch [#30227] Various MSVC (32-bit) Warning and Typo Fixes
[blender-staging.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 USHORTTOUCHAR(val) ((unsigned char)(((val) >= 65535-128)? 255: ((val)+128)>>8))
112 #define F3TOCHAR3(v2, v1) {                                                   \
113                 (v1)[0]= FTOCHAR((v2[0]));                                            \
114                 (v1)[1]= FTOCHAR((v2[1]));                                            \
115                 (v1)[2]= FTOCHAR((v2[2]));                                            \
116         }
117 #define F3TOCHAR4(v2, v1) {                                                   \
118                 (v1)[0]= FTOCHAR((v2[0]));                                            \
119                 (v1)[1]= FTOCHAR((v2[1]));                                            \
120                 (v1)[2]= FTOCHAR((v2[2]));                                            \
121                 (v1)[3]= 255;                                                         \
122         }
123 #define F4TOCHAR4(v2, v1) {                                                   \
124                 (v1)[0]= FTOCHAR((v2[0]));                                            \
125                 (v1)[1]= FTOCHAR((v2[1]));                                            \
126                 (v1)[2]= FTOCHAR((v2[2]));                                            \
127                 (v1)[3]= FTOCHAR((v2[3]));                                            \
128         }
129 #define VECCOPY(v1, v2) {                                                     \
130                 *(v1)=   *(v2);                                                       \
131                 *(v1+1)= *(v2+1);                                                     \
132                 *(v1+2)= *(v2+2);                                                     \
133         }
134 #define VECCOPY2D(v1, v2) {                                                   \
135                 *(v1)=   *(v2);                                                       \
136                 *(v1+1)= *(v2+1);                                                     \
137         }
138 #define QUATCOPY(v1, v2) {                                                    \
139                 *(v1)=   *(v2);                                                       \
140                 *(v1+1)= *(v2+1);                                                     \
141                 *(v1+2)= *(v2+2);                                                     \
142                 *(v1+3)= *(v2+3);                                                     \
143         }
144 #define VECADD(v1,v2,v3) {                                                    \
145                 *(v1)=   *(v2)   + *(v3);                                             \
146                 *(v1+1)= *(v2+1) + *(v3+1);                                           \
147                 *(v1+2)= *(v2+2) + *(v3+2);                                           \
148         }
149 #define VECSUB(v1,v2,v3) {                                                    \
150                 *(v1)=   *(v2)   - *(v3);                                             \
151                 *(v1+1)= *(v2+1) - *(v3+1);                                           \
152                 *(v1+2)= *(v2+2) - *(v3+2);                                           \
153         }
154 #define VECSUB2D(v1,v2,v3)     {                                              \
155                 *(v1)=   *(v2)   - *(v3);                                             \
156                 *(v1+1)= *(v2+1) - *(v3+1);                                           \
157         }
158 #define VECADDFAC(v1,v2,v3,fac) {                                             \
159                 *(v1)=   *(v2)   + *(v3)*(fac);                                       \
160                 *(v1+1)= *(v2+1) + *(v3+1)*(fac);                                     \
161                 *(v1+2)= *(v2+2) + *(v3+2)*(fac);                                     \
162         }
163 #define VECSUBFAC(v1,v2,v3,fac) {                                             \
164                 *(v1)=   *(v2)   - *(v3)*(fac);                                       \
165                 *(v1+1)= *(v2+1) - *(v3+1)*(fac);                                     \
166                 *(v1+2)= *(v2+2) - *(v3+2)*(fac);                                     \
167         }
168 #define QUATADDFAC(v1,v2,v3,fac) {                                            \
169                 *(v1)=   *(v2)   + *(v3)*(fac);                                       \
170                 *(v1+1)= *(v2+1) + *(v3+1)*(fac);                                     \
171                 *(v1+2)= *(v2+2) + *(v3+2)*(fac);                                     \
172                 *(v1+3)= *(v2+3) + *(v3+3)*(fac);                                     \
173         }
174
175 #define INPR(v1, v2) ( (v1)[0]*(v2)[0] + (v1)[1]*(v2)[1] + (v1)[2]*(v2)[2] )
176
177 /* some misc stuff.... */
178 #define CLAMP(a, b, c)          if((a)<(b)) (a)=(b); else if((a)>(c)) (a)=(c)
179 #define CLAMPIS(a, b, c) ((a)<(b) ? (b) : (a)>(c) ? (c) : (a))
180 #define CLAMPTEST(a, b, c)      if((b)<(c)) {CLAMP(a, b, c);} else {CLAMP(a, c, b);}
181
182 #define IS_EQ(a,b) ((fabs((double)(a)-(b)) >= (double) FLT_EPSILON) ? 0 : 1)
183 #define IS_EQF(a,b) ((fabsf((float)(a)-(b)) >= (float) FLT_EPSILON) ? 0 : 1)
184
185 #define IS_EQT(a, b, c) ((a > b)? (((a-b) <= c)? 1:0) : ((((b-a) <= c)? 1:0)))
186 #define IN_RANGE(a, b, c) ((b < c)? ((b<a && a<c)? 1:0) : ((c<a && a<b)? 1:0))
187 #define IN_RANGE_INCL(a, b, c) ((b < c)? ((b<=a && a<=c)? 1:0) : ((c<=a && a<=b)? 1:0))
188
189 /* array helpers */
190 #define ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot)                 \
191                 (arr_dtype *)((char*)arr_start + (elem_size*(tot - 1)))
192
193 #define ARRAY_HAS_ITEM(item, arr_start, arr_dtype, elem_size, tot) (          \
194                 (item >= arr_start) &&                                                \
195                 (item <= ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot))       \
196         )
197
198 /* This one rotates the bytes in an int64, int (32) and short (16) */
199 #define SWITCH_INT64(a) {                                                     \
200                 char s_i, *p_i;                                                       \
201                 p_i= (char *)&(a);                                                    \
202                 s_i= p_i[0]; p_i[0]= p_i[7]; p_i[7]= s_i;                             \
203                 s_i= p_i[1]; p_i[1]= p_i[6]; p_i[6]= s_i;                             \
204                 s_i= p_i[2]; p_i[2]= p_i[5]; p_i[5]= s_i;                             \
205                 s_i= p_i[3]; p_i[3]= p_i[4]; p_i[4]= s_i;                             \
206         }
207
208 #define SWITCH_INT(a) {                                                       \
209                 char s_i, *p_i;                                                       \
210                 p_i= (char *)&(a);                                                    \
211                 s_i= p_i[0]; p_i[0]= p_i[3]; p_i[3]= s_i;                             \
212                 s_i= p_i[1]; p_i[1]= p_i[2]; p_i[2]= s_i;                             \
213         }
214
215 #define SWITCH_SHORT(a) {                                                     \
216                 char s_i, *p_i;                                                       \
217                 p_i= (char *)&(a);                                                    \
218                 s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i;                                \
219         }
220
221
222 /* Warning-free macros for storing ints in pointers. Use these _only_
223  * for storing an int in a pointer, not a pointer in an int (64bit)! */
224 #define SET_INT_IN_POINTER(i) ((void*)(intptr_t)(i))
225 #define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i))
226
227 /* Macro to convert a value to string in the preprocessor
228  * STRINGIFY_ARG: gives the argument as a string
229  * STRINGIFY_APPEND: appends any argument 'b' onto the string argument 'a',
230  *   used by STRINGIFY because some preprocessors warn about zero arguments
231  * STRINGIFY: gives the argument's value as a string */
232 #define STRINGIFY_ARG(x) "" #x
233 #define STRINGIFY_APPEND(a, b) "" a #b
234 #define STRINGIFY(x) STRINGIFY_APPEND("", x)
235
236 /* useful for debugging */
237 #define AT __FILE__ ":" STRINGIFY(__LINE__)
238
239 /* so we can use __func__ everywhere */
240 #if defined(_MSC_VER)
241 #  define __func__ __FUNCTION__
242 #endif
243
244
245 /* UNUSED macro, for function argument */
246 #ifdef __GNUC__
247 #  define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
248 #else
249 #  define UNUSED(x) UNUSED_ ## x
250 #endif
251
252 #ifdef __GNUC__
253 #  define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
254 #else
255 #  define UNUSED_FUNCTION(x) UNUSED_ ## x
256 #endif
257
258 #ifdef __GNUC__
259 #  define WARN_UNUSED  __attribute__((warn_unused_result))
260 #else
261 #  define WARN_UNUSED
262 #endif
263
264 /*little macro so inline keyword works*/
265 #if defined(_MSC_VER)
266 #  define BM_INLINE static __forceinline
267 #elif defined(__GNUC__)
268 #  define BM_INLINE static inline __attribute((always_inline))
269 #else
270 /* #warning "MSC/GNUC defines not found, inline non-functional" */
271 #  define BM_INLINE static
272 #endif
273
274
275 /* BLI_assert(), default only to print
276  * for aborting need to define WITH_ASSERT_ABORT
277  */
278 #if !defined NDEBUG
279 #  ifdef WITH_ASSERT_ABORT
280 #    define _dummy_abort abort
281 #  else
282 #    define _dummy_abort() (void)0
283 #  endif
284 #  if defined(__GNUC__) || defined(_MSC_VER) /* check __func__ is available */
285 #    define BLI_assert(a)                                                     \
286         (void)((!(a)) ?  (                                                        \
287                 (                                                                     \
288                 fprintf(stderr,                                                       \
289                         "BLI_assert failed: %s, %s(), %d at \'%s\'\n",                    \
290                         __FILE__, __func__, __LINE__, STRINGIFY(a)),                      \
291                 _dummy_abort(),                                                       \
292                 NULL)) : NULL)
293 #  else
294 #    define BLI_assert(a)                                                     \
295         (void)((!(a)) ?  (                                                        \
296                 (                                                                     \
297                 fprintf(stderr,                                                       \
298                         "BLI_assert failed: %s, %d at \'%s\'\n",                          \
299                         __FILE__, __LINE__, STRINGIFY(a)),                                \
300                 _dummy_abort(),                                                       \
301                 NULL)) : NULL)
302 #  endif
303 #else
304 #  define BLI_assert(a) (void)0
305 #endif
306
307 /* hints for branch pradiction, only use in code that runs a _lot_ where */
308 #ifdef __GNUC__
309 #  define LIKELY(x)       __builtin_expect(!!(x), 1)
310 #  define UNLIKELY(x)     __builtin_expect(!!(x), 0)
311 #else
312 #  define LIKELY(x)       (x)
313 #  define UNLIKELY(x)     (x)
314 #endif
315
316 #endif // __BLI_UTILDEFINES_H__