4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
30 #ifndef BLI_UTILDEFINES_H
31 #define BLI_UTILDEFINES_H
33 /** \file BLI_utildefines.h
46 #define ELEM(a, b, c) ( (a)==(b) || (a)==(c) )
47 #define ELEM3(a, b, c, d) ( ELEM(a, b, c) || (a)==(d) )
48 #define ELEM4(a, b, c, d, e) ( ELEM(a, b, c) || ELEM(a, d, e) )
49 #define ELEM5(a, b, c, d, e, f) ( ELEM(a, b, c) || ELEM3(a, d, e, f) )
50 #define ELEM6(a, b, c, d, e, f, g) ( ELEM(a, b, c) || ELEM4(a, d, e, f, g) )
51 #define ELEM7(a, b, c, d, e, f, g, h) ( ELEM3(a, b, c, d) || ELEM4(a, e, f, g, h) )
52 #define ELEM8(a, b, c, d, e, f, g, h, i) ( ELEM4(a, b, c, d, e) || ELEM4(a, f, g, h, i) )
53 #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) )
54 #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) )
55 #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) )
57 /* shift around elements */
58 #define SHIFT3(type, a, b, c) { type tmp; tmp = a; a = c; c = b; b = tmp; }
59 #define SHIFT4(type, a, b, c, d) { type tmp; tmp = a; a = d; d = c; c = b; b = tmp; }
62 #define MIN2(x,y) ( (x)<(y) ? (x) : (y) )
63 #define MIN3(x,y,z) MIN2( MIN2((x),(y)) , (z) )
64 #define MIN4(x,y,z,a) MIN2( MIN2((x),(y)) , MIN2((z),(a)) )
66 #define MAX2(x,y) ( (x)>(y) ? (x) : (y) )
67 #define MAX3(x,y,z) MAX2( MAX2((x),(y)) , (z) )
68 #define MAX4(x,y,z,a) MAX2( MAX2((x),(y)) , MAX2((z),(a)) )
70 #define INIT_MINMAX(min, max) { (min)[0]= (min)[1]= (min)[2]= 1.0e30f; (max)[0]= (max)[1]= (max)[2]= -1.0e30f; }
72 #define INIT_MINMAX2(min, max) { (min)[0]= (min)[1]= 1.0e30f; (max)[0]= (max)[1]= -1.0e30f; }
74 #define DO_MIN(vec, min) { if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0]; \
75 if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1]; \
76 if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2]; } \
78 #define DO_MAX(vec, max) { if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0]; \
79 if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1]; \
80 if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2]; } \
82 #define DO_MINMAX(vec, min, max) { if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0]; \
83 if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1]; \
84 if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2]; \
85 if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0]; \
86 if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1]; \
87 if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2]; } \
89 #define DO_MINMAX2(vec, min, max) { if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0]; \
90 if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1]; \
91 if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0]; \
92 if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1]; }
94 /* some math and copy defines */
97 #define SWAP(type, a, b) { type sw_ap; sw_ap=(a); (a)=(b); (b)=sw_ap; }
100 #define ABS(a) ( (a)<0 ? (-(a)) : (a) )
102 #define AVG2(x, y) ( 0.5 * ((x) + (y)) )
104 #define FTOCHAR(val) ((val)<=0.0f)? 0 : (((val)>(1.0f-0.5f/255.0f))? 255 : (char)((255.0f*(val))+0.5f))
105 #define FTOUSHORT(val) ((val >= 1.0f-0.5f/65535)? 65535: (val <= 0.0f)? 0: (unsigned short)(val*65535.0f + 0.5f))
106 #define F3TOCHAR3(v2,v1) (v1)[0]=FTOCHAR((v2[0])); (v1)[1]=FTOCHAR((v2[1])); (v1)[2]=FTOCHAR((v2[2]))
107 #define F3TOCHAR4(v2,v1) { (v1)[0]=FTOCHAR((v2[0])); (v1)[1]=FTOCHAR((v2[1])); (v1)[2]=FTOCHAR((v2[2])); \
109 #define F4TOCHAR4(v2,v1) { (v1)[0]=FTOCHAR((v2[0])); (v1)[1]=FTOCHAR((v2[1])); (v1)[2]=FTOCHAR((v2[2])); \
110 (v1)[3]=FTOCHAR((v2[3])); (v1)[3]=FTOCHAR((v2[3])); }
113 #define VECCOPY(v1,v2) {*(v1)= *(v2); *(v1+1)= *(v2+1); *(v1+2)= *(v2+2);}
114 #define VECCOPY2D(v1,v2) {*(v1)= *(v2); *(v1+1)= *(v2+1);}
115 #define QUATCOPY(v1,v2) {*(v1)= *(v2); *(v1+1)= *(v2+1); *(v1+2)= *(v2+2); *(v1+3)= *(v2+3);}
116 #define LONGCOPY(a, b, c) {int lcpc=c, *lcpa=(int *)a, *lcpb=(int *)b; while(lcpc-->0) *(lcpa++)= *(lcpb++);}
119 #define VECADD(v1,v2,v3) {*(v1)= *(v2) + *(v3); *(v1+1)= *(v2+1) + *(v3+1); *(v1+2)= *(v2+2) + *(v3+2);}
120 #define VECSUB(v1,v2,v3) {*(v1)= *(v2) - *(v3); *(v1+1)= *(v2+1) - *(v3+1); *(v1+2)= *(v2+2) - *(v3+2);}
121 #define VECSUB2D(v1,v2,v3) {*(v1)= *(v2) - *(v3); *(v1+1)= *(v2+1) - *(v3+1);}
122 #define VECADDFAC(v1,v2,v3,fac) {*(v1)= *(v2) + *(v3)*(fac); *(v1+1)= *(v2+1) + *(v3+1)*(fac); *(v1+2)= *(v2+2) + *(v3+2)*(fac);}
123 #define VECSUBFAC(v1,v2,v3,fac) {*(v1)= *(v2) - *(v3)*(fac); *(v1+1)= *(v2+1) - *(v3+1)*(fac); *(v1+2)= *(v2+2) - *(v3+2)*(fac);}
124 #define QUATADDFAC(v1,v2,v3,fac) {*(v1)= *(v2) + *(v3)*(fac); *(v1+1)= *(v2+1) + *(v3+1)*(fac); *(v1+2)= *(v2+2) + *(v3+2)*(fac); *(v1+3)= *(v2+3) + *(v3+3)*(fac);}
126 #define INPR(v1, v2) ( (v1)[0]*(v2)[0] + (v1)[1]*(v2)[1] + (v1)[2]*(v2)[2] )
128 /* some misc stuff.... */
129 #define CLAMP(a, b, c) if((a)<(b)) (a)=(b); else if((a)>(c)) (a)=(c)
130 #define CLAMPIS(a, b, c) ((a)<(b) ? (b) : (a)>(c) ? (c) : (a))
131 #define CLAMPTEST(a, b, c) if((b)<(c)) {CLAMP(a, b, c);} else {CLAMP(a, c, b);}
133 #define IS_EQ(a,b) ((fabs((double)(a)-(b)) >= (double) FLT_EPSILON) ? 0 : 1)
134 #define IS_EQF(a,b) ((fabsf((float)(a)-(b)) >= (float) FLT_EPSILON) ? 0 : 1)
136 #define IS_EQT(a, b, c) ((a > b)? (((a-b) <= c)? 1:0) : ((((b-a) <= c)? 1:0)))
137 #define IN_RANGE(a, b, c) ((b < c)? ((b<a && a<c)? 1:0) : ((c<a && a<b)? 1:0))
138 #define IN_RANGE_INCL(a, b, c) ((b < c)? ((b<=a && a<=c)? 1:0) : ((c<=a && a<=b)? 1:0))
141 #define ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot) (arr_dtype *)((char*)arr_start + (elem_size*(tot - 1)))
142 #define ARRAY_HAS_ITEM(item, arr_start, arr_dtype, elem_size, tot) ((item >= arr_start) && (item <= ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot)))
144 /* This one rotates the bytes in an int64, int (32) and short (16) */
145 #define SWITCH_INT64(a) { \
148 s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
149 s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
150 s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
151 s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
153 #define SWITCH_INT(a) { \
156 s_i=p_i[0]; p_i[0]=p_i[3]; p_i[3]=s_i; \
157 s_i=p_i[1]; p_i[1]=p_i[2]; p_i[2]=s_i; }
159 #define SWITCH_SHORT(a) { \
162 s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i; }
165 /* Warning-free macros for storing ints in pointers. Use these _only_
166 * for storing an int in a pointer, not a pointer in an int (64bit)! */
167 #define SET_INT_IN_POINTER(i) ((void*)(intptr_t)(i))
168 #define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i))
170 /* Macro to convert a value to string in the preprocessor
171 * STRINGIFY_ARG: gives the defined name in the string
172 * STRINGIFY: gives the defined value. */
173 #define STRINGIFY_ARG(x) #x
174 #define STRINGIFY(x) STRINGIFY_ARG(x)
176 /* useful for debugging */
177 #define AT __FILE__ ":" STRINGIFY(__LINE__)
179 /* UNUSED macro, for function argument */
181 # define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
183 # define UNUSED(x) UNUSED_ ## x
187 # define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
189 # define UNUSED_FUNCTION(x) UNUSED_ ## x
193 # define WARN_UNUSED __attribute__((warn_unused_result))
198 /*little macro so inline keyword works*/
199 #if defined(_MSC_VER)
200 # define BM_INLINE static __forceinline
201 #elif defined(__GNUC__)
202 # define BM_INLINE static inline __attribute((always_inline))
204 /* #warning "MSC/GNUC defines not found, inline non-functional" */
205 # define BM_INLINE static
209 /* BLI_assert(), default only to print
210 * for aborting need to define WITH_ASSERT_ABORT
213 # ifdef WITH_ASSERT_ABORT
214 # define _dummy_abort abort
216 # define _dummy_abort() (void)0
218 # ifdef __GNUC__ /* just want to check if __func__ is available */
219 # define BLI_assert(a) \
223 "BLI_assert failed: %s, %s(), %d at \'%s\'\n", \
224 __FILE__, __func__, __LINE__, STRINGIFY(a)); \
229 # define BLI_assert(a) \
233 "BLI_assert failed: %s, %d at \'%s\'\n", \
234 __FILE__, __LINE__, STRINGIFY(a)); \
240 # define BLI_assert(a) (void)0
243 #endif // BLI_UTILDEFINES_H