You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.5 KiB

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