| 1 |
/** @file generic.h |
| 2 |
** @brief Generic (@ref generic) |
| 3 |
** @author Andrea Vedaldi |
| 4 |
**/ |
| 5 |
|
| 6 |
/* |
| 7 |
Copyright (C) 2013 Andrea Vedaldi. |
| 8 |
Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson. |
| 9 |
All rights reserved. |
| 10 |
|
| 11 |
This file is part of the VLFeat library and is made available under |
| 12 |
the terms of the BSD license (see the COPYING file). |
| 13 |
*/ |
| 14 |
|
| 15 |
#ifndef VL_GENERIC_H |
| 16 |
#define VL_GENERIC_H |
| 17 |
|
| 18 |
#include "host.h" |
| 19 |
#include "random.h" |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <stddef.h> |
| 23 |
#include <time.h> |
| 24 |
#include <assert.h> |
| 25 |
|
| 26 |
/** @brief Library version string */ |
| 27 |
#define VL_VERSION_STRING "0.9.20" |
| 28 |
|
| 29 |
/** @brief Maximum length (in characters) of an error message */ |
| 30 |
#define VL_ERR_MSG_LEN 1024 |
| 31 |
|
| 32 |
/** @name Type identifiers for atomic data types |
| 33 |
** @{ */ |
| 34 |
|
| 35 |
#define VL_TYPE_FLOAT 1 /**< @c float type */ |
| 36 |
#define VL_TYPE_DOUBLE 2 /**< @c double type */ |
| 37 |
#define VL_TYPE_INT8 3 /**< @c ::vl_int8 type */ |
| 38 |
#define VL_TYPE_UINT8 4 /**< @c ::vl_uint8 type */ |
| 39 |
#define VL_TYPE_INT16 5 /**< @c ::vl_int16 type */ |
| 40 |
#define VL_TYPE_UINT16 6 /**< @c ::vl_uint16 type */ |
| 41 |
#define VL_TYPE_INT32 7 /**< @c ::vl_int32 type */ |
| 42 |
#define VL_TYPE_UINT32 8 /**< @c ::vl_uint32 type */ |
| 43 |
#define VL_TYPE_INT64 9 /**< @c ::vl_int64 type */ |
| 44 |
#define VL_TYPE_UINT64 10 /**< @c ::vl_uint64 type */ |
| 45 |
|
| 46 |
typedef vl_uint32 vl_type ; |
| 47 |
|
| 48 |
/** @brief Get the name of a data type. |
| 49 |
** @param type data type. |
| 50 |
** @return data name of the data type. |
| 51 |
** |
| 52 |
** @c type is one of ::VL_TYPE_FLOAT, ::VL_TYPE_DOUBLE, |
| 53 |
** ::VL_TYPE_INT8, ::VL_TYPE_INT16, ::VL_TYPE_INT32, ::VL_TYPE_INT64, |
| 54 |
** ::VL_TYPE_UINT8, ::VL_TYPE_UINT16, ::VL_TYPE_UINT32, ::VL_TYPE_UINT64. |
| 55 |
**/ |
| 56 |
|
| 57 |
void vl_constructor(); |
| 58 |
void vl_destructor(); |
| 59 |
|
| 60 |
VL_INLINE char const * |
| 61 |
vl_get_type_name (vl_type type) |
| 62 |
{ |
| 63 |
switch (type) { |
| 64 |
case VL_TYPE_FLOAT : return "float" ; |
| 65 |
case VL_TYPE_DOUBLE : return "double" ; |
| 66 |
case VL_TYPE_INT8 : return "int8" ; |
| 67 |
case VL_TYPE_INT16 : return "int16" ; |
| 68 |
case VL_TYPE_INT32 : return "int32" ; |
| 69 |
case VL_TYPE_INT64 : return "int64" ; |
| 70 |
case VL_TYPE_UINT8 : return "int8" ; |
| 71 |
case VL_TYPE_UINT16 : return "int16" ; |
| 72 |
case VL_TYPE_UINT32 : return "int32" ; |
| 73 |
case VL_TYPE_UINT64 : return "int64" ; |
| 74 |
default: return NULL ; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** @brief Get data type size. |
| 79 |
** @param type data type. |
| 80 |
** @return size (in byte) |
| 81 |
** |
| 82 |
** @c type is one of ::VL_TYPE_FLOAT, ::VL_TYPE_DOUBLE, |
| 83 |
** ::VL_TYPE_INT8, ::VL_TYPE_INT16, ::VL_TYPE_INT32, ::VL_TYPE_INT64, |
| 84 |
** ::VL_TYPE_UINT8, ::VL_TYPE_UINT16, ::VL_TYPE_UINT32, ::VL_TYPE_UINT64. |
| 85 |
**/ |
| 86 |
|
| 87 |
VL_INLINE vl_size |
| 88 |
vl_get_type_size (vl_type type) |
| 89 |
{ |
| 90 |
vl_size dataSize = 0 ; |
| 91 |
switch (type) { |
| 92 |
case VL_TYPE_DOUBLE : dataSize = sizeof(double) ; break ; |
| 93 |
case VL_TYPE_FLOAT : dataSize = sizeof(float) ; break ; |
| 94 |
case VL_TYPE_INT64 : case VL_TYPE_UINT64 : dataSize = sizeof(vl_int64) ; break ; |
| 95 |
case VL_TYPE_INT32 : case VL_TYPE_UINT32 : dataSize = sizeof(vl_int32) ; break ; |
| 96 |
case VL_TYPE_INT16 : case VL_TYPE_UINT16 : dataSize = sizeof(vl_int16) ; break ; |
| 97 |
case VL_TYPE_INT8 : case VL_TYPE_UINT8 : dataSize = sizeof(vl_int8) ; break ; |
| 98 |
default: |
| 99 |
abort() ; |
| 100 |
} |
| 101 |
return dataSize ; |
| 102 |
} |
| 103 |
/** @} */ |
| 104 |
|
| 105 |
VL_EXPORT char const * vl_get_version_string (void) ; |
| 106 |
VL_EXPORT char * vl_configuration_to_string_copy (void) ; |
| 107 |
VL_EXPORT void vl_set_simd_enabled (vl_bool x) ; |
| 108 |
VL_EXPORT vl_bool vl_get_simd_enabled (void) ; |
| 109 |
VL_EXPORT vl_bool vl_cpu_has_avx (void) ; |
| 110 |
VL_EXPORT vl_bool vl_cpu_has_sse3 (void) ; |
| 111 |
VL_EXPORT vl_bool vl_cpu_has_sse2 (void) ; |
| 112 |
VL_EXPORT vl_size vl_get_num_cpus (void) ; |
| 113 |
VL_EXPORT VlRand * vl_get_rand (void) ; |
| 114 |
|
| 115 |
/** @name Multi-thread computations |
| 116 |
** @{ */ |
| 117 |
VL_EXPORT vl_size vl_get_max_threads (void) ; |
| 118 |
VL_EXPORT void vl_set_num_threads (vl_size n) ; |
| 119 |
VL_EXPORT vl_size vl_get_thread_limit (void) ; |
| 120 |
/** @} (*/ |
| 121 |
|
| 122 |
/** ------------------------------------------------------------------ |
| 123 |
** @name Error handling |
| 124 |
** @{ */ |
| 125 |
#define VL_ERR_OK 0 /**< No error */ |
| 126 |
#define VL_ERR_OVERFLOW 1 /**< Buffer overflow error */ |
| 127 |
#define VL_ERR_ALLOC 2 /**< Resource allocation error */ |
| 128 |
#define VL_ERR_BAD_ARG 3 /**< Bad argument or illegal data error */ |
| 129 |
#define VL_ERR_IO 4 /**< Input/output error */ |
| 130 |
#define VL_ERR_EOF 5 /**< End-of-file or end-of-sequence error */ |
| 131 |
#define VL_ERR_NO_MORE 5 /**< End-of-sequence @deprecated */ |
| 132 |
|
| 133 |
VL_EXPORT int vl_get_last_error (void) ; |
| 134 |
VL_EXPORT char const * vl_get_last_error_message (void) ; |
| 135 |
VL_EXPORT int vl_set_last_error (int error, char const * errorMessage, ...) ; |
| 136 |
/** @} */ |
| 137 |
|
| 138 |
/** ------------------------------------------------------------------ |
| 139 |
** @name Memory allocation |
| 140 |
** @{ */ |
| 141 |
VL_EXPORT void |
| 142 |
vl_set_alloc_func (void *(*malloc_func) (size_t), |
| 143 |
void *(*realloc_func) (void*,size_t), |
| 144 |
void *(*calloc_func) (size_t, size_t), |
| 145 |
void (*free_func) (void*)) ; |
| 146 |
VL_EXPORT void *vl_malloc (size_t n) ; |
| 147 |
VL_EXPORT void *vl_realloc (void *ptr, size_t n) ; |
| 148 |
VL_EXPORT void *vl_calloc (size_t n, size_t size) ; |
| 149 |
VL_EXPORT void *vl_memalign (size_t n, size_t size) ; |
| 150 |
VL_EXPORT void vl_free (void* ptr) ; |
| 151 |
/** @} */ |
| 152 |
|
| 153 |
/** ------------------------------------------------------------------ |
| 154 |
** @name Logging |
| 155 |
** @{ */ |
| 156 |
/** @brief Customizable printf function pointer type */ |
| 157 |
typedef int(*printf_func_t) (char const *format, ...) ; |
| 158 |
VL_EXPORT void vl_set_printf_func (printf_func_t printf_func) ; |
| 159 |
VL_EXPORT printf_func_t vl_get_printf_func (void) ; |
| 160 |
|
| 161 |
/** @def VL_PRINTF |
| 162 |
** @brief Call user-customizable @c printf function |
| 163 |
** |
| 164 |
** The function calls the user customizable @c printf. |
| 165 |
**/ |
| 166 |
|
| 167 |
/** @def VL_PRINT |
| 168 |
** @brief Same as ::VL_PRINTF (legacy code) |
| 169 |
**/ |
| 170 |
|
| 171 |
#define VL_PRINTF (*vl_get_printf_func()) |
| 172 |
#define VL_PRINT (*vl_get_printf_func()) |
| 173 |
/** @} */ |
| 174 |
|
| 175 |
/** ------------------------------------------------------------------ |
| 176 |
** @name Common operations |
| 177 |
** @{ */ |
| 178 |
|
| 179 |
/** @brief Compute the minimum between two values |
| 180 |
** @param x value |
| 181 |
** @param y value |
| 182 |
** @return the minimum of @a x and @a y. |
| 183 |
**/ |
| 184 |
#define VL_MIN(x,y) (((x)<(y))?(x):(y)) |
| 185 |
|
| 186 |
/** @brief Compute the maximum between two values |
| 187 |
** @param x value. |
| 188 |
** @param y value. |
| 189 |
** @return the maximum of @a x and @a y. |
| 190 |
**/ |
| 191 |
#define VL_MAX(x,y) (((x)>(y))?(x):(y)) |
| 192 |
|
| 193 |
/** @brief Signed left shift operation |
| 194 |
** @param x value. |
| 195 |
** @param n number of shift positions. |
| 196 |
** @return @c x << n . |
| 197 |
** The macro is equivalent to the builtin @c << operator, but it |
| 198 |
** supports negative shifts too. |
| 199 |
**/ |
| 200 |
#define VL_SHIFT_LEFT(x,n) (((n)>=0)?((x)<<(n)):((x)>>-(n))) |
| 201 |
/* @} */ |
| 202 |
|
| 203 |
/** ------------------------------------------------------------------ |
| 204 |
** @name Measuring time |
| 205 |
** @{ |
| 206 |
**/ |
| 207 |
VL_EXPORT void vl_tic (void) ; |
| 208 |
VL_EXPORT double vl_toc (void) ; |
| 209 |
VL_EXPORT double vl_get_cpu_time (void) ; |
| 210 |
/** @} */ |
| 211 |
|
| 212 |
/* VL_GENERIC_H */ |
| 213 |
#endif |