50
41
#define LenString_MAX FILENAME_MAX*2
51
42
#define LenErrorMsg_MAX LenString_MAX
53
/* on some arcitectures it could be a good idea to use long rather than int */
54
/* this has not really been tested */
62
#define INDEX_T_MAX INT_MAX
63
#define INDEX_T_MIN -INT_MAX
64
#define EPSILON DBL_EPSILON
65
#define LARGE_POSITIVE_FLOAT DBL_MAX
66
#define SMALL_NEGATIVE_FLOAT -DBL_MAX
72
/*#define IS_NAN(__VAL__) ( (__VAL__) == NAN )*/ /* this does not work */
73
/* #define IS_NAN(__VAL__) ( ! ( ( (__VAL__) >= 0. ) || ( (__VAL__) <= 0. ) ) ) this does not work */
76
#define IS_NAN(__VAL__) (isnan(__VAL__))
78
#define IS_NAN(__VAL__) (_isnan(__VAL__))
80
/* If we do not have isnan then we can't reliably check for NaN - return false */
81
#define IS_NAN(__VAL__) (0)
84
/**************************************************************/
86
/* some useful functions: */
91
#define DBLE(_x_) (double)(_x_)
92
#define INDEX1(_X1_) (_X1_)
93
#define INDEX2(_X1_,_X2_,_N1_) ((_X1_)+(_N1_)*(_X2_))
94
#define INDEX3(_X1_,_X2_,_X3_,_N1_,_N2_) ((_X1_)+(_N1_)*INDEX2(_X2_,_X3_,_N2_))
95
#define INDEX4(_X1_,_X2_,_X3_,_X4_,_N1_,_N2_,_N3_) ((_X1_)+(_N1_)*INDEX3(_X2_,_X3_,_X4_,_N2_,_N3_))
96
#define INDEX5(_X1_,_X2_,_X3_,_X4_,_X5_,_N1_,_N2_,_N3_,_N4_) ((_X1_)+(_N1_)*INDEX4(_X2_,_X3_,_X4_,_X5_,_N2_,_N3_,_N4_))
97
#define INDEX6(_X1_,_X2_,_X3_,_X4_,_X5_,_X6_,_N1_,_N2_,_N3_,_N4_,_N5_) ((_X1_)+(_N1_)*INDEX5(_X2_,_X3_,_X4_,_X5_,_X6_,_N2_,_N3_,_N4_,_N5_))
99
#define MAX(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ? (_arg1_) : (_arg2_))
100
#define MAX3(_arg1_,_arg2_,_arg3_) MAX(_arg1_,MAX(_arg2_,_arg3_))
101
#define MIN(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ? (_arg2_) : (_arg1_))
102
#define MIN3(_arg1_,_arg2_,_arg3_) MIN(_arg1_,MIN(_arg2_,_arg3_))
103
#define ABS(_arg_) MAX((_arg_),-(_arg_))
104
#define SIGN(_arg_) ((_arg_)>0 ? 1 : ((_arg_)<0 ? -1 : 0 ))
105
#define SWAP(_a0_,_a1_,_type_) { \
111
#define XNOR(_a0_,_a1_) ( ( (_a0_) && (_a1_) ) || ( !(_a0_) && !(_a1_) ) )
112
/**************************************************************/
113
/* memory allocation: */
114
/* Wise to not use PASO_MALLOC/FREE/REALLOC and */
115
/* PASO_THREAD... directly. These are only for tailoring */
116
/* the main macros that follow */
117
/**************************************************************/
119
#if defined(_WIN32) /* Use python for memory management on windows. */
123
#define PASO_MALLOC PyMem_Malloc
124
#define PASO_FREE PyMem_Free
125
#define PASO_REALLOC PyMem_Realloc
131
#define PASO_MALLOC malloc
132
#define PASO_FREE free
133
#define PASO_REALLOC realloc
138
# if (defined __STDC__ && __STDC__)
139
# define __const const
146
/* FIXME: This is not satisfactory. */
147
/* _ECC, __INTEL_COMPILER, and other */
148
/* intel compiler pre-defines need to be handled */
149
/* (__ICL, __ICC come to mind) */
150
/* Also, _WIN32 may take this branch one day... */
151
/* SO KEEP ALL THREAD_MEMALLOC/FREEs CONFINED TO THE PASO LIBRARY. */
153
#if defined(__ECC) && defined(_OPENMP) /* ECC version of intel compiler with openmp. */
155
#define PASO_THREAD_MALLOC kmp_malloc
156
#define PASO_THREAD_FREE kmp_free
158
#define PASO_THREAD_MALLOC PASO_MALLOC
159
#define PASO_THREAD_FREE PASO_FREE
163
/* Prepare for the day that this becomes sharable. */
164
/* and we wish to do multi-file optimisations on windows */
169
# ifndef PASO_STATIC_LIB
172
# define PASO_DLL_API __declspec(dllexport)
174
# define PASO_DLL_API __declspec(dllimport)
180
/******************The main macros ************************************/
182
#define MEMALLOC(_LENGTH_,_TYPE_) \
183
(_TYPE_*) PASO_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
185
/* do {} while(0) - an old trick for bracketing a macro that */
186
/* makes sure a semi-colon does no harm. */
188
#define MEMFREE(_PTR_) \
191
if ((void *)(_PTR_) != NULL ) { PASO_FREE(_PTR_); (_PTR_) = NULL; } \
194
#define MEMREALLOC(_RETP_,_POINTER_,_LENGTH_,_TYPE_) \
197
if( (_POINTER_)!=NULL ) \
199
_RETP_ = (_TYPE_*)PASO_REALLOC((void*)(_POINTER_), \
200
((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
204
_RETP_ = (_TYPE_*)PASO_MALLOC( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
208
#define TMPMEMALLOC MEMALLOC
209
#define TMPMEMFREE MEMFREE
210
#define TMPMEMREALLOC MEMREALLOC
212
#define THREAD_MEMALLOC(_LENGTH_,_TYPE_) \
213
(_TYPE_*) PASO_THREAD_MALLOC/**/(((size_t)(_LENGTH_))*sizeof(_TYPE_))
215
#define THREAD_MEMFREE(_PTR_) \
218
if ((void *)(_PTR_) != NULL ) { PASO_THREAD_FREE(_PTR_); (_PTR_) = NULL; } \
223
This was useful for seeing what memory is being allocated if you get an "Out of memory" error
224
if used again, bracket with do {} while(0)
225
#define TMPMEMALLOC(_LENGTH_,_TYPE_) (printf("TMPMEMALLOC at %s %d #bytes=%d*%d=%d\n", __FILE__, __LINE__, _LENGTH_, sizeof(_TYPE_), ((size_t)(_LENGTH_) * sizeof(_TYPE_))) , (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_)))
227
This was useful for seeing where memory is being freed if you get an "glibc detected...free" error
228
if used again, bracket with do {} while(0)
229
#define TMPMEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { printf("TMPMEMFREE AAA %s %d\n", __FILE__, __LINE__); free(_PTR_); (_PTR_) = NULL; printf("TMPMEMFREE BBB %s %d\n", __FILE__, __LINE__); }