00001
00006
00007
00008 #ifndef VL_GENERIC_H
00009 #define VL_GENERIC_H
00010
00011 #include <stddef.h>
00012 #include <assert.h>
00013
00015 #define VL_VERSION_STRING "0.1"
00016
00018 #define VL_INLINE static __inline__
00019
00023 #define VL_STRINGIFY_(x) # x
00024
00036 #define VL_STRINGIFY(x) VL_STRINGIFY_(x)
00037
00043 typedef long long vl_int64 ;
00044 typedef int vl_int32 ;
00045 typedef short vl_int16 ;
00046 typedef char vl_int8 ;
00048 typedef long long unsigned vl_uint64 ;
00049 typedef int unsigned vl_uint32 ;
00050 typedef short unsigned vl_uint16 ;
00051 typedef char unsigned vl_uint8 ;
00053 typedef int vl_int ;
00054 typedef unsigned int vl_uint ;
00055 typedef float vl_single ;
00056 typedef double vl_double ;
00057 typedef unsigned int vl_uidx ;
00058 typedef size_t vl_size ;
00060 typedef int vl_bool ;
00064 #define VL_BIG_INT 2147483647
00065
00067 #define VL_SMALL_INT (- VL_BIG_INT - 1)
00068
00070 #define VL_LOG_OF_2 0.693147180559945
00071
00072
00073
00074
00075
00076
00077
00078
00080 static union { vl_uint32 raw ; vl_single value ; }
00081 const vl_nan_f =
00082 { 0x7FC00000UL } ;
00083
00085 static union { vl_uint32 raw ; vl_single value ; }
00086 const vl_infinity_f =
00087 { 0x7F800000UL } ;
00088
00090 static union { vl_uint64 raw ; vl_double value ; }
00091 const vl_nan_d =
00092 { 0x7FF8000000000000ULL } ;
00093
00095 static union { vl_uint64 raw ; vl_double value ; }
00096 const vl_infinity_d =
00097 { 0x7FF0000000000000ULL } ;
00098
00100 #define VL_NAN_F (vl_nan_f.value)
00101
00103 #define VL_INFINITY_F (vl_infinity_f.value)
00104
00106 #define VL_NAN_D (vl_nan_d.value)
00107
00109 #define VL_INFINITY_D (vl_infinity_d.value)
00110
00111 #ifdef __VISUALC__
00112 #undef VL_INLINE
00113 #define VL_INLINE __inline
00114 #define fscanf fscanf_s
00115 #define snprintf _snprintf
00116 #define isnan _isnan
00117 #endif
00118
00135 void vl_set_alloc_func (void *(*malloc_func) (vl_size),
00136 void *(*realloc_func) (void*,vl_size),
00137 void *(*calloc_func) (vl_size, vl_size),
00138 void (*free_func) (void*)) ;
00139 VL_INLINE void *vl_malloc (vl_size n) ;
00140 VL_INLINE void *vl_realloc (void *ptr, vl_size n) ;
00141 VL_INLINE void *vl_calloc (vl_size n, vl_size size) ;
00142 VL_INLINE void vl_free (void* ptr) ;
00143
00144 void vl_set_printf_func (int(*printf_func)(char const *str, ...)) ;
00145
00156 #define VL_PRINTF(format, ...) \
00157 ((*vl_printf_func)((format), __VA_ARGS__))
00158
00169 #define VL_PRINT(string) \
00170 ((*vl_printf_func)(string))
00171
00172
00181 extern int vl_err_no ;
00182
00187 #define VL_ERR_MSG_LEN 1024
00188
00190 extern char vl_err_msg [VL_ERR_MSG_LEN] ;
00191
00192 #define VL_ERR_OK 0
00193 #define VL_ERR_OVERFLOW 1
00194 #define VL_ERR_ALLOC 2
00195 #define VL_ERR_BAD_ARG 3
00196 #define VL_ERR_IO 4
00197 #define VL_ERR_EOF 5
00198 #define VL_ERR_NO_MORE 5
00212 #define VL_MIN(x,y) (((x)<(y))?(x):(y))
00213
00219 #define VL_MAX(x,y) (((x)>(y))?(x):(y))
00220
00230 #define VL_SHIFT_LEFT(x,n) (((n)>=0)?((x)<<(n)):((x)>>-(n)))
00231
00232
00236 char const * vl_get_version_string () ;
00237
00242 #define VL_LITTLE_ENDIAN 0
00243 #define VL_BIG_ENDIAN 1
00251 #if \
00252 defined(__LITTLE_ENDIAN__) || \
00253 defined(__i386__) || defined(__ia64__) || \
00254 defined(WIN32) || defined(__x86_64) || \
00255 defined(__alpha__) || defined(__alpha) || \
00256 defined(__arm__) || defined(__SYMBIAN32__) || \
00257 (defined(__mips__) && defined(__MIPSEL__))
00258 #define VL_ENDIANNESS VL_LITTLE_ENDIAN
00259 #else
00260 #define VL_ENDIANNESS VL_BIG_ENDIAN
00261 #endif
00262
00263 VL_INLINE int vl_get_endianness () ;
00264 VL_INLINE void vl_adapt_endianness_8 (void *dst, void* src) ;
00265 VL_INLINE void vl_adapt_endianness_4 (void *dst, void* src) ;
00266 VL_INLINE void vl_adapt_endianness_2 (void *dst, void* src) ;
00276 VL_INLINE int
00277 vl_get_endianness ()
00278 {
00279 return VL_ENDIANNESS ;
00280 }
00281
00290 VL_INLINE void
00291 vl_adapt_endianness_8 (void *dst, void* src)
00292 {
00293 char *dst_ = (char*) dst ;
00294 char *src_ = (char*) src ;
00295 #if VL_ENDIANNESS == VL_BIG_ENDIAN
00296 dst_ [0] = src_ [0] ;
00297 dst_ [1] = src_ [1] ;
00298 dst_ [2] = src_ [2] ;
00299 dst_ [3] = src_ [3] ;
00300 dst_ [4] = src_ [4] ;
00301 dst_ [5] = src_ [5] ;
00302 dst_ [6] = src_ [6] ;
00303 dst_ [7] = src_ [7] ;
00304 #else
00305 dst_ [0] = src_ [7] ;
00306 dst_ [1] = src_ [6] ;
00307 dst_ [2] = src_ [5] ;
00308 dst_ [3] = src_ [4] ;
00309 dst_ [4] = src_ [3] ;
00310 dst_ [5] = src_ [2] ;
00311 dst_ [6] = src_ [1] ;
00312 dst_ [7] = src_ [0] ;
00313 #endif
00314 }
00315
00324 VL_INLINE void
00325 vl_adapt_endianness_4 (void *dst, void* src)
00326 {
00327 char *dst_ = (char*) dst ;
00328 char *src_ = (char*) src ;
00329 #if VL_ENDIANNESS == VL_BIG_ENDIAN
00330 dst_ [0] = src_ [0] ;
00331 dst_ [1] = src_ [1] ;
00332 dst_ [2] = src_ [2] ;
00333 dst_ [3] = src_ [3] ;
00334 #else
00335 dst_ [0] = src_ [3] ;
00336 dst_ [1] = src_ [2] ;
00337 dst_ [2] = src_ [1] ;
00338 dst_ [3] = src_ [0] ;
00339 #endif
00340 }
00341
00350 VL_INLINE void
00351 vl_adapt_endianness_2 (void *dst, void* src)
00352 {
00353 char *dst_ = (char*) dst ;
00354 char *src_ = (char*) src ;
00355 #if VL_ENDIANNESS == VL_BIG_ENDIAN
00356 dst_ [0] = src_ [0] ;
00357 dst_ [1] = src_ [1] ;
00358 #else
00359 dst_ [0] = src_ [1] ;
00360 dst_ [1] = src_ [0] ;
00361 #endif
00362 }
00363
00364 extern int (*vl_printf_func) (char const * format, ...) ;
00365 extern void *(*vl_malloc_func) (vl_size) ;
00366 extern void *(*vl_realloc_func) (void*,vl_size) ;
00367 extern void *(*vl_calloc_func) (vl_size, vl_size) ;
00368 extern void (*vl_free_func) (void*) ;
00369
00379 VL_INLINE
00380 void*
00381 vl_malloc (vl_size n)
00382 {
00383 return (*vl_malloc_func)(n) ;
00384 }
00385
00397 VL_INLINE
00398 void*
00399 vl_realloc (void* ptr, vl_size n)
00400 {
00401 return (*vl_realloc_func)(ptr, n) ;
00402 }
00403
00415 VL_INLINE
00416 void*
00417 vl_calloc (vl_size n, vl_size size)
00418 {
00419 return (*vl_calloc_func)(n, size) ;
00420 }
00421
00430 VL_INLINE
00431 void
00432 vl_free (void *ptr)
00433 {
00434 (*vl_free_func)(ptr) ;
00435 }
00436
00437
00438
00439
00440 #endif