00001
00010 #ifndef _GK_MACROS_H_
00011 #define _GK_MACROS_H_
00012
00013
00014
00015
00016 #define gk_max(a, b) ((a) >= (b) ? (a) : (b))
00017 #define gk_min(a, b) ((a) >= (b) ? (b) : (a))
00018 #define gk_max3(a, b, c) ((a) >= (b) && (a) >= (c) ? (a) : ((b) >= (a) && (b) >= (c) ? (b) : (c)))
00019 #define gk_SWAP(a, b, tmp) do {(tmp) = (a); (a) = (b); (b) = (tmp);} while(0)
00020 #define INC_DEC(a, b, val) do {(a) += (val); (b) -= (val);} while(0)
00021 #define sign(a, b) ((a >= 0 ? b : -b))
00022
00023 #define ONEOVERRANDMAX (1.0/(RAND_MAX+1.0))
00024 #define RandomInRange(u) ((int) (ONEOVERRANDMAX*(u)*rand()))
00025
00026 #define gk_abs(x) ((x) >= 0 ? (x) : -(x))
00027
00028
00029
00030
00031
00032 #define gk_clearcputimer(tmr) (tmr = 0.0)
00033 #define gk_startcputimer(tmr) (tmr -= gk_CPUSeconds())
00034 #define gk_stopcputimer(tmr) (tmr += gk_CPUSeconds())
00035 #define gk_getcputimer(tmr) (tmr)
00036
00037 #define gk_clearwctimer(tmr) (tmr = 0.0)
00038 #define gk_startwctimer(tmr) (tmr -= gk_WClockSeconds())
00039 #define gk_stopwctimer(tmr) (tmr += gk_WClockSeconds())
00040 #define gk_getwctimer(tmr) (tmr)
00041
00042
00043
00044
00045 #define IFSET(a, flag, cmd) if ((a)&(flag)) (cmd);
00046
00047
00048
00049
00050
00051 #define GKSETJMP() (setjmp(gk_return_to_entry))
00052 #define gk_sigcatch() (setjmp(gk_jbufs[gk_cur_jbufs]))
00053
00054
00055
00056
00057
00058 #ifdef DMALLOC
00059 # define MALLOC_CHECK(ptr) \
00060 if (malloc_verify((ptr)) == DMALLOC_VERIFY_ERROR) { \
00061 printf("***MALLOC_CHECK failed on line %d of file %s: " #ptr "\n", \
00062 __LINE__, __FILE__); \
00063 abort(); \
00064 }
00065 #else
00066 # define MALLOC_CHECK(ptr) ;
00067 #endif
00068
00069
00070
00071
00072
00073 #define MAKECSR(i, n, a) \
00074 do { \
00075 for (i=1; i<n; i++) a[i] += a[i-1]; \
00076 for (i=n; i>0; i--) a[i] = a[i-1]; \
00077 a[0] = 0; \
00078 } while(0)
00079
00080 #define SHIFTCSR(i, n, a) \
00081 do { \
00082 for (i=n; i>0; i--) a[i] = a[i-1]; \
00083 a[0] = 0; \
00084 } while(0)
00085
00086
00087
00088
00089
00090 #define GKASSERT(expr) \
00091 if (!(expr)) { \
00092 printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
00093 __LINE__, __FILE__); \
00094 abort(); \
00095 }
00096
00097 #define GKASSERTP(expr,msg) \
00098 if (!(expr)) { \
00099 printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
00100 __LINE__, __FILE__); \
00101 printf msg ; \
00102 printf("\n"); \
00103 abort(); \
00104 }
00105
00106 #define GKCUASSERT(expr) \
00107 if (!(expr)) { \
00108 printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
00109 __LINE__, __FILE__); \
00110 }
00111
00112 #define GKCUASSERTP(expr,msg) \
00113 if (!(expr)) { \
00114 printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
00115 __LINE__, __FILE__); \
00116 printf msg ; \
00117 printf("\n"); \
00118 }
00119
00120
00121
00122
00123 #ifndef NDEBUG
00124 # define ASSERT(expr) \
00125 if (!(expr)) { \
00126 printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
00127 __LINE__, __FILE__); \
00128 assert(expr); \
00129 }
00130
00131 # define ASSERTP(expr,msg) \
00132 if (!(expr)) { \
00133 printf("***ASSERTION failed on line %d of file %s: " #expr "\n", \
00134 __LINE__, __FILE__); \
00135 printf msg ; \
00136 printf("\n"); \
00137 assert(expr); \
00138 }
00139 #else
00140 # define ASSERT(expr) ;
00141 # define ASSERTP(expr,msg) ;
00142 #endif
00143
00144 #ifndef NDEBUG2
00145 # define ASSERT2 ASSERT
00146 # define ASSERTP2 ASSERTP
00147 #else
00148 # define ASSERT2(expr) ;
00149 # define ASSERTP2(expr,msg) ;
00150 #endif
00151
00152
00153 #endif