Blender V2.61 - r43446

BLI_utildefines.h

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00028 #ifndef BLI_UTILDEFINES_H
00029 #define BLI_UTILDEFINES_H
00030 
00035 #ifndef FALSE
00036 #  define FALSE 0
00037 #endif
00038 
00039 #ifndef TRUE
00040 #  define TRUE 1
00041 #endif
00042 
00043 
00044 #define ELEM(a, b, c)           ( (a)==(b) || (a)==(c) )
00045 #define ELEM3(a, b, c, d)       ( ELEM(a, b, c) || (a)==(d) )
00046 #define ELEM4(a, b, c, d, e)    ( ELEM(a, b, c) || ELEM(a, d, e) )
00047 #define ELEM5(a, b, c, d, e, f) ( ELEM(a, b, c) || ELEM3(a, d, e, f) )
00048 #define ELEM6(a, b, c, d, e, f, g)      ( ELEM(a, b, c) || ELEM4(a, d, e, f, g) )
00049 #define ELEM7(a, b, c, d, e, f, g, h)   ( ELEM3(a, b, c, d) || ELEM4(a, e, f, g, h) )
00050 #define ELEM8(a, b, c, d, e, f, g, h, i)        ( ELEM4(a, b, c, d, e) || ELEM4(a, f, g, h, i) )
00051 #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) )
00052 #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) )
00053 #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) )
00054 
00055 /* shift around elements */
00056 #define SHIFT3(type, a, b, c) { type tmp; tmp = a; a = c; c = b; b = tmp; }
00057 #define SHIFT4(type, a, b, c, d) { type tmp; tmp = a; a = d; d = c; c = b; b = tmp; }
00058 
00059 /* min/max */
00060 #define MIN2(x,y)               ( (x)<(y) ? (x) : (y) )
00061 #define MIN3(x,y,z)             MIN2( MIN2((x),(y)) , (z) )
00062 #define MIN4(x,y,z,a)           MIN2( MIN2((x),(y)) , MIN2((z),(a)) )
00063 
00064 #define MAX2(x,y)               ( (x)>(y) ? (x) : (y) )
00065 #define MAX3(x,y,z)             MAX2( MAX2((x),(y)) , (z) )
00066 #define MAX4(x,y,z,a)           MAX2( MAX2((x),(y)) , MAX2((z),(a)) )
00067 
00068 #define INIT_MINMAX(min, max) {                                               \
00069         (min)[0]= (min)[1]= (min)[2]= 1.0e30f;                                \
00070         (max)[0]= (max)[1]= (max)[2]= -1.0e30f;                               \
00071     }
00072 #define INIT_MINMAX2(min, max) {                                              \
00073         (min)[0]= (min)[1]= 1.0e30f;                                          \
00074         (max)[0]= (max)[1]= -1.0e30f;                                         \
00075     }
00076 #define DO_MIN(vec, min) {                                                    \
00077         if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0];                           \
00078         if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1];                           \
00079         if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2];                           \
00080     }
00081 #define DO_MAX(vec, max) {                                                    \
00082         if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0];                           \
00083         if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1];                           \
00084         if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2];                           \
00085     }
00086 #define DO_MINMAX(vec, min, max) {                                            \
00087         if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0];                           \
00088         if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1];                           \
00089         if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2];                           \
00090         if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0];                           \
00091         if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1];                           \
00092         if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2];                           \
00093     }
00094 #define DO_MINMAX2(vec, min, max) {                                           \
00095         if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0];                           \
00096         if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1];                           \
00097         if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0];                           \
00098         if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1];                           \
00099     }
00100 
00101 /* some math and copy defines */
00102 
00103 #ifndef SWAP
00104 #  define SWAP(type, a, b)       { type sw_ap; sw_ap=(a); (a)=(b); (b)=sw_ap; }
00105 #endif
00106 
00107 #define ABS(a)          ( (a)<0 ? (-(a)) : (a) )
00108 
00109 #define FTOCHAR(val) ((val)<=0.0f)? 0 : (((val)>(1.0f-0.5f/255.0f))? 255 : (char)((255.0f*(val))+0.5f))
00110 #define FTOUSHORT(val) ((val >= 1.0f-0.5f/65535)? 65535: (val <= 0.0f)? 0: (unsigned short)(val*65535.0f + 0.5f))
00111 #define USHORTTOUCHAR(val) ((unsigned char)(((val) >= 65535-128)? 255: ((val)+128)>>8))
00112 #define F3TOCHAR3(v2, v1) {                                                   \
00113         (v1)[0]= FTOCHAR((v2[0]));                                            \
00114         (v1)[1]= FTOCHAR((v2[1]));                                            \
00115         (v1)[2]= FTOCHAR((v2[2]));                                            \
00116     }
00117 #define F3TOCHAR4(v2, v1) {                                                   \
00118         (v1)[0]= FTOCHAR((v2[0]));                                            \
00119         (v1)[1]= FTOCHAR((v2[1]));                                            \
00120         (v1)[2]= FTOCHAR((v2[2]));                                            \
00121         (v1)[3]= 255;                                                         \
00122     }
00123 #define F4TOCHAR4(v2, v1) {                                                   \
00124         (v1)[0]= FTOCHAR((v2[0]));                                            \
00125         (v1)[1]= FTOCHAR((v2[1]));                                            \
00126         (v1)[2]= FTOCHAR((v2[2]));                                            \
00127         (v1)[3]= FTOCHAR((v2[3]));                                            \
00128     }
00129 #define VECCOPY(v1, v2) {                                                     \
00130         *(v1)=   *(v2);                                                       \
00131         *(v1+1)= *(v2+1);                                                     \
00132         *(v1+2)= *(v2+2);                                                     \
00133     }
00134 #define VECCOPY2D(v1, v2) {                                                   \
00135         *(v1)=   *(v2);                                                       \
00136         *(v1+1)= *(v2+1);                                                     \
00137     }
00138 #define QUATCOPY(v1, v2) {                                                    \
00139         *(v1)=   *(v2);                                                       \
00140         *(v1+1)= *(v2+1);                                                     \
00141         *(v1+2)= *(v2+2);                                                     \
00142         *(v1+3)= *(v2+3);                                                     \
00143     }
00144 #define VECADD(v1,v2,v3) {                                                    \
00145         *(v1)=   *(v2)   + *(v3);                                             \
00146         *(v1+1)= *(v2+1) + *(v3+1);                                           \
00147         *(v1+2)= *(v2+2) + *(v3+2);                                           \
00148     }
00149 #define VECSUB(v1,v2,v3) {                                                    \
00150         *(v1)=   *(v2)   - *(v3);                                             \
00151         *(v1+1)= *(v2+1) - *(v3+1);                                           \
00152         *(v1+2)= *(v2+2) - *(v3+2);                                           \
00153     }
00154 #define VECSUB2D(v1,v2,v3)     {                                              \
00155         *(v1)=   *(v2)   - *(v3);                                             \
00156         *(v1+1)= *(v2+1) - *(v3+1);                                           \
00157     }
00158 #define VECADDFAC(v1,v2,v3,fac) {                                             \
00159         *(v1)=   *(v2)   + *(v3)*(fac);                                       \
00160         *(v1+1)= *(v2+1) + *(v3+1)*(fac);                                     \
00161         *(v1+2)= *(v2+2) + *(v3+2)*(fac);                                     \
00162     }
00163 #define VECSUBFAC(v1,v2,v3,fac) {                                             \
00164         *(v1)=   *(v2)   - *(v3)*(fac);                                       \
00165         *(v1+1)= *(v2+1) - *(v3+1)*(fac);                                     \
00166         *(v1+2)= *(v2+2) - *(v3+2)*(fac);                                     \
00167     }
00168 #define QUATADDFAC(v1,v2,v3,fac) {                                            \
00169         *(v1)=   *(v2)   + *(v3)*(fac);                                       \
00170         *(v1+1)= *(v2+1) + *(v3+1)*(fac);                                     \
00171         *(v1+2)= *(v2+2) + *(v3+2)*(fac);                                     \
00172         *(v1+3)= *(v2+3) + *(v3+3)*(fac);                                     \
00173     }
00174 
00175 #define INPR(v1, v2) ( (v1)[0]*(v2)[0] + (v1)[1]*(v2)[1] + (v1)[2]*(v2)[2] )
00176 
00177 /* some misc stuff.... */
00178 #define CLAMP(a, b, c)      if((a)<(b)) (a)=(b); else if((a)>(c)) (a)=(c)
00179 #define CLAMPIS(a, b, c) ((a)<(b) ? (b) : (a)>(c) ? (c) : (a))
00180 #define CLAMPTEST(a, b, c)  if((b)<(c)) {CLAMP(a, b, c);} else {CLAMP(a, c, b);}
00181 
00182 #define IS_EQ(a,b) ((fabs((double)(a)-(b)) >= (double) FLT_EPSILON) ? 0 : 1)
00183 #define IS_EQF(a,b) ((fabsf((float)(a)-(b)) >= (float) FLT_EPSILON) ? 0 : 1)
00184 
00185 #define IS_EQT(a, b, c) ((a > b)? (((a-b) <= c)? 1:0) : ((((b-a) <= c)? 1:0)))
00186 #define IN_RANGE(a, b, c) ((b < c)? ((b<a && a<c)? 1:0) : ((c<a && a<b)? 1:0))
00187 #define IN_RANGE_INCL(a, b, c) ((b < c)? ((b<=a && a<=c)? 1:0) : ((c<=a && a<=b)? 1:0))
00188 
00189 /* array helpers */
00190 #define ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot)                 \
00191         (arr_dtype *)((char*)arr_start + (elem_size*(tot - 1)))
00192 
00193 #define ARRAY_HAS_ITEM(item, arr_start, arr_dtype, elem_size, tot) (          \
00194         (item >= arr_start) &&                                                \
00195         (item <= ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot))       \
00196     )
00197 
00198 /* This one rotates the bytes in an int64, int (32) and short (16) */
00199 #define SWITCH_INT64(a) {                                                     \
00200         char s_i, *p_i;                                                       \
00201         p_i= (char *)&(a);                                                    \
00202         s_i= p_i[0]; p_i[0]= p_i[7]; p_i[7]= s_i;                             \
00203         s_i= p_i[1]; p_i[1]= p_i[6]; p_i[6]= s_i;                             \
00204         s_i= p_i[2]; p_i[2]= p_i[5]; p_i[5]= s_i;                             \
00205         s_i= p_i[3]; p_i[3]= p_i[4]; p_i[4]= s_i;                             \
00206     }
00207 
00208 #define SWITCH_INT(a) {                                                       \
00209         char s_i, *p_i;                                                       \
00210         p_i= (char *)&(a);                                                    \
00211         s_i= p_i[0]; p_i[0]= p_i[3]; p_i[3]= s_i;                             \
00212         s_i= p_i[1]; p_i[1]= p_i[2]; p_i[2]= s_i;                             \
00213     }
00214 
00215 #define SWITCH_SHORT(a) {                                                     \
00216         char s_i, *p_i;                                                       \
00217         p_i= (char *)&(a);                                                    \
00218         s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i;                                \
00219     }
00220 
00221 
00222 /* Warning-free macros for storing ints in pointers. Use these _only_
00223  * for storing an int in a pointer, not a pointer in an int (64bit)! */
00224 #define SET_INT_IN_POINTER(i) ((void*)(intptr_t)(i))
00225 #define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i))
00226 
00227 /* Macro to convert a value to string in the preprocessor
00228  * STRINGIFY_ARG: gives the defined name in the string
00229  * STRINGIFY: gives the defined value. */
00230 #define STRINGIFY_ARG(x) #x
00231 #define STRINGIFY(x) STRINGIFY_ARG(x)
00232 
00233 /* useful for debugging */
00234 #define AT __FILE__ ":" STRINGIFY(__LINE__)
00235 
00236 /* so we can use __func__ everywhere */
00237 #if defined(_MSC_VER)
00238 #  define __func__ __FUNCTION__
00239 #endif
00240 
00241 
00242 /* UNUSED macro, for function argument */
00243 #ifdef __GNUC__
00244 #  define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
00245 #else
00246 #  define UNUSED(x) UNUSED_ ## x
00247 #endif
00248 
00249 #ifdef __GNUC__
00250 #  define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
00251 #else
00252 #  define UNUSED_FUNCTION(x) UNUSED_ ## x
00253 #endif
00254 
00255 #ifdef __GNUC__
00256 #  define WARN_UNUSED  __attribute__((warn_unused_result))
00257 #else
00258 #  define WARN_UNUSED
00259 #endif
00260 
00261 /*little macro so inline keyword works*/
00262 #if defined(_MSC_VER)
00263 #  define BM_INLINE static __forceinline
00264 #elif defined(__GNUC__)
00265 #  define BM_INLINE static inline __attribute((always_inline))
00266 #else
00267 /* #warning "MSC/GNUC defines not found, inline non-functional" */
00268 #  define BM_INLINE static
00269 #endif
00270 
00271 
00272 /* BLI_assert(), default only to print
00273  * for aborting need to define WITH_ASSERT_ABORT
00274  */
00275 #if !defined NDEBUG
00276 #  ifdef WITH_ASSERT_ABORT
00277 #    define _dummy_abort abort
00278 #  else
00279 #    define _dummy_abort() (void)0
00280 #  endif
00281 #  if defined(__GNUC__) || defined(_MSC_VER) /* check __func__ is available */
00282 #    define BLI_assert(a)                                                     \
00283     (void)((!(a)) ?  (                                                        \
00284         (                                                                     \
00285         fprintf(stderr,                                                       \
00286             "BLI_assert failed: %s, %s(), %d at \'%s\'\n",                    \
00287             __FILE__, __func__, __LINE__, STRINGIFY(a)),                      \
00288         _dummy_abort(),                                                       \
00289         NULL)) : NULL)
00290 #  else
00291 #    define BLI_assert(a)                                                     \
00292     (void)((!(a)) ?  (                                                        \
00293         (                                                                     \
00294         fprintf(stderr,                                                       \
00295             "BLI_assert failed: %s, %d at \'%s\'\n",                          \
00296             __FILE__, __LINE__, STRINGIFY(a)),                                \
00297         _dummy_abort(),                                                       \
00298         NULL)) : NULL)
00299 #  endif
00300 #else
00301 #  define BLI_assert(a) (void)0
00302 #endif
00303 
00304 /* hints for branch pradiction, only use in code that runs a _lot_ where */
00305 #ifdef __GNUC__
00306 #  define LIKELY(x)       __builtin_expect(!!(x), 1)
00307 #  define UNLIKELY(x)     __builtin_expect(!!(x), 0)
00308 #else
00309 #  define LIKELY(x)       (x)
00310 #  define UNLIKELY(x)     (x)
00311 #endif
00312 
00313 #endif // BLI_UTILDEFINES_H