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.

587 lines
16 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file host.c
  2. ** @brief Host - Definition
  3. ** @author Andrea Vedaldi
  4. ** @see @ref portability
  5. **/
  6. /*
  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. /**
  13. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  14. @page portability Portability features
  15. @author Andrea Vedaldi
  16. @tableofcontents
  17. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  18. Platform dependent details are isolated in the @ref host.h. This
  19. module provides functionalities to identify the host operating system,
  20. C compiler, and CPU architecture. It also provides a few features to
  21. abstract from such details.
  22. @see http://predef.sourceforge.net/index.php
  23. @see http://en.wikipedia.org/wiki/64-bit_computing
  24. @section host-os Host operating system
  25. The module defines a symbol to identify the host operating system:
  26. ::VL_OS_WIN for Windows, ::VL_OS_LINUX for Linux, ::VL_OS_MACOSX for
  27. Mac OS X, and so on.
  28. @section host-compiler Host compiler
  29. The module defines a symbol to identify the host compiler:
  30. ::VL_COMPILER_MSC for Microsoft Visual C++, ::VL_COMPILER_GNUC for
  31. GNU C, and so on. The (integer) value of such symbols corresponds the
  32. version of the compiler.
  33. The module defines a symbol to identify the data model of the
  34. compiler: ::VL_COMPILER_ILP32, ::VL_COMPILER_LP64, or
  35. ::VL_COMPILER_LLP64 (see Sect. @ref host-compiler-data-model). For
  36. convenience, it also defines a number of atomic types of prescribed
  37. width (::vl_int8, ::vl_int16, ::vl_int32, etc.).
  38. @remark While some of such functionalities are provided by the
  39. standard header @c stdint.h, the latter is not supported by all
  40. platforms.
  41. @subsection host-compiler-data-model Data models
  42. The C language defines a number of atomic data types (such as @c char,
  43. @c short, @c int and so on). The number of bits (width) used to
  44. represent each data type depends on the compiler data model. The
  45. different models are *ILP32* (@c int, @c long, and pointer 32 bit),
  46. *LP64* (@c int 32 bit, @c long and pointer 64 bit), *ILP64* (@c int,
  47. @c long, and pointer 64 bit), and *LLP64* (@c int, @c long 32 bit and
  48. pointer 64 -- and `long long` -- 64 bit). Note in particular that
  49. `long long` is 64 bit in all models of interest. The following table
  50. summarizes them:
  51. <table><caption><b>Compiler data models.</b> </caption>
  52. <tr style="font-weight:bold;">
  53. <td>Data model</td>
  54. <td><code>short</code></td>
  55. <td><code>int</code></td>
  56. <td><code>long</code></td>
  57. <td><code>long long</code></td>
  58. <td><code>void*</code></td>
  59. <td>Compiler</td>
  60. </tr>
  61. <tr>
  62. <td>ILP32</td>
  63. <td style="background-color:#ffa;">16</td>
  64. <td style="background-color:#afa;">32</td>
  65. <td style="background-color:#afa;">32</td>
  66. <td>64</td>
  67. <td style="background-color:#afa;">32</td>
  68. <td>Most 32 bit architectures.</td>
  69. </tr>
  70. <tr>
  71. <td>LP64</td>
  72. <td style="background-color:#ffa;">16</td>
  73. <td style="background-color:#afa;">32</td>
  74. <td>64</td>
  75. <td>64</td>
  76. <td>64</td>
  77. <td>UNIX-64 (Linux, Mac OS X)</td>
  78. </tr>
  79. <tr>
  80. <td>ILP64</td>
  81. <td style="background-color:#ffa;">16</td>
  82. <td>64</td>
  83. <td>64</td>
  84. <td>64</td>
  85. <td>64</td>
  86. <td>Alpha, Cray</td>
  87. </tr>
  88. <tr>
  89. <td>SLIP64</td>
  90. <td>64</td>
  91. <td>64</td>
  92. <td>64</td>
  93. <td>64</td>
  94. <td>64</td>
  95. <td></td>
  96. </tr>
  97. <tr>
  98. <td>LLP64</td>
  99. <td style="background-color:#ffa;">16</td>
  100. <td style="background-color:#afa;">32</td>
  101. <td style="background-color:#afa;">32</td>
  102. <td>64</td>
  103. <td>64</td>
  104. <td>Windows-64</td>
  105. </tr>
  106. </table>
  107. Macros such as ::VL_UINT32_C can be used to generate integer literal
  108. with the correct suffix for a type of a given width.
  109. @subsection host-compiler-other Other compiler-specific features
  110. The module provides the macro ::VL_EXPORT to declare symbols exported
  111. from the library and the macro ::VL_INLINE to declare inline
  112. functions. Such features are not part of the C89 standard, and
  113. change depending on the compiler.
  114. @par "Example:"
  115. The following header file declares a function @c f that
  116. should be visible from outside the library.
  117. @code
  118. #include <vl/generic.h>
  119. VL_EXPORT void f () ;
  120. VL_EXPORT int i ;
  121. @endcode
  122. Notice that the macro ::VL_EXPORT needs not to be included again
  123. when the function is defined.
  124. @par "Example:"
  125. The following header file declares an inline function @c f:
  126. @code
  127. #include <vl/generic.h>
  128. VL_INLINE int f() ;
  129. VL_INLINE int f() { return 1 ; }
  130. @endcode
  131. Here the first instruction defines the function @c f, where the
  132. second declares it. Notice that since this is an inline function, its
  133. definition must be found in the header file rather than in an
  134. implementation file. Notice also that definition and declaration can
  135. be merged.
  136. These macros translate according to the following tables:
  137. <table class="doxtable" style="font-size:70%;">
  138. <caption>Macros for exporting library symbols</caption>
  139. <tr>
  140. <td>Platform</td>
  141. <td>Macro name</td>
  142. <td>Value when building the library</td>
  143. <td>Value when importing the library</td>
  144. </tr>
  145. <tr>
  146. <td>Unix/GCC</td>
  147. <td>::VL_EXPORT</td>
  148. <td>empty (assumes <c>-visibility=hidden</c> GCC option)</td>
  149. <td><c>__attribute__((visibility ("default")))</c></td>
  150. </tr>
  151. <tr>
  152. <td>Win/Visual C++</td>
  153. <td>::VL_EXPORT</td>
  154. <td>@c __declspec(dllexport)</td>
  155. <td>@c __declspec(dllimport)</td>
  156. </tr>
  157. </table>
  158. <table class="doxtable" style="font-size:70%;">
  159. <caption>Macros for declaring inline functions</caption>
  160. <tr>
  161. <td>Platform</td>
  162. <td>Macro name</td>
  163. <td>Value</td>
  164. </tr>
  165. <tr>
  166. <td>Unix/GCC</td>
  167. <td>::VL_INLINE</td>
  168. <td>static inline</td>
  169. </tr>
  170. <tr>
  171. <td>Win/Visual C++</td>
  172. <td>::VL_INLINE</td>
  173. <td>static __inline</td>
  174. </tr>
  175. </table>
  176. @section host-arch Host CPU architecture
  177. The module defines a symbol to identify the host CPU architecture:
  178. ::VL_ARCH_IX86 for Intel x86, ::VL_ARCH_IA64 for Intel 64, and so on.
  179. @subsection host-arch-endianness Endianness
  180. The module defines a symbol to identify the host CPU endianness:
  181. ::VL_ARCH_BIG_ENDIAN for big endian and ::VL_ARCH_LITTLE_ENDIAN for
  182. little endian. The functions ::vl_swap_host_big_endianness_8(),
  183. ::vl_swap_host_big_endianness_4(), ::vl_swap_host_big_endianness_2()
  184. to change the endianness of data (from/to host and network order).
  185. Recall that <em>endianness</em> concerns the way multi-byte data
  186. types (such as 16, 32 and 64 bits integers) are stored into the
  187. addressable memory. All CPUs uses a contiguous address range to
  188. store atomic data types (e.g. a 16-bit integer could be assigned to
  189. the addresses <c>0x10001</c> and <c>0x10002</c>), but the order may
  190. differ.
  191. - The convention is <em>big endian</em>, or in <em>network
  192. order</em>, if the most significant byte of the multi-byte data
  193. types is assigned to the smaller memory address. This is the
  194. convention used for instance by the PPC architecture.
  195. - The convention is <em>little endian</em> if the least significant
  196. byte is assigned to the smaller memory address. This is the
  197. convention used for instance by the x86 architecture.
  198. @remark The names &ldquo;big endian&rdquo; and &ldquo;little
  199. endian&rdquo; are a little confusing. &ldquo;Big endian&rdquo; means
  200. &ldquo;big endian first&rdquo;, i.e. the address of the most
  201. significant byte comes first. Similarly, &ldquo;little endian&rdquo;
  202. means &ldquo;little endian first&rdquo;, in the sense that the
  203. address of the least significant byte comes first.
  204. Endianness is a concern when data is either exchanged with processors
  205. that use different conventions, transmitted over a network, or stored
  206. to a file. For the latter two cases, one usually saves data in big
  207. endian (network) order regardless of the host CPU.
  208. @section host-threads Multi-threading
  209. The file defines #VL_THREADS_WIN if multi-threading support is
  210. enabled and the host supports Windows threads and #VL_THREADS_POSIX if
  211. it supports POSIX threads.
  212. **/
  213. /** @def VL_OS_LINUX
  214. ** @brief Defined if the host operating system is Linux.
  215. **/
  216. /** @def VL_OS_MACOSX
  217. ** @brief Defined if the host operating system is Mac OS X.
  218. **/
  219. /** @def VL_OS_WIN
  220. ** @brief Defined if the host operating system is Windows (32 or 64)
  221. **/
  222. /** @def VL_OS_WIN64
  223. ** @brief Defined if the host operating system is Windows-64.
  224. **/
  225. /** @def VL_COMPILER_GNUC
  226. ** @brief Defined if the host compiler is GNU C.
  227. **
  228. ** This macro is defined if the compiler is GNUC.
  229. ** Its value is calculated as
  230. ** @code
  231. ** 10000 * MAJOR + 100 * MINOR + PATCHLEVEL
  232. ** @endcode
  233. ** @see @ref host-compiler
  234. **/
  235. /** @def VL_COMPILER_MSC
  236. ** @brief Defined if the host compiler is Microsoft Visual C++.
  237. ** @see @ref host-compiler
  238. **/
  239. /** @def VL_COMPILER_LCC
  240. ** @brief Defined if the host compiler is LCC.
  241. ** @deprecated The LCC is not supported anymore.
  242. ** @see @ref host-compiler
  243. **/
  244. /** @def VL_COMPILER_LLP64
  245. ** @brief Defined if the host compiler data model is LLP64.
  246. ** @see @ref host-compiler-data-model
  247. **/
  248. /** @def VL_COMPILER_LP64
  249. ** @brief Defined if the host compiler data model is LP64.
  250. ** @see @ref host-compiler-data-model
  251. **/
  252. /** @def VL_COMPILER_ILP32
  253. ** @brief Defined if the host compiler data model is ILP32.
  254. ** @see @ref host-compiler-data-model
  255. **/
  256. /** @def VL_INT8_C(x)
  257. ** @brief Create an integer constant of the specified width and sign
  258. ** @param x integer constant.
  259. ** @return @a x with the correct suffix for the given sign and size.
  260. ** The suffix used depends on the @ref host-compiler-data-model.
  261. ** @par "Example:"
  262. ** The macro <code>VL_INT64_C(1234)</code> is expanded as @c 123L in
  263. ** a LP64 system and as @c 123LL in a LLP64 system.
  264. **/
  265. /** @def VL_INT16_C(x)
  266. ** @copydoc VL_INT8_C */
  267. /** @def VL_INT32_C(x)
  268. ** @copydoc VL_INT8_C */
  269. /** @def VL_INT64_C(x)
  270. ** @copydoc VL_INT8_C */
  271. /** @def VL_UINT8_C(x)
  272. ** @copydoc VL_INT8_C */
  273. /** @def VL_UINT16_C(x)
  274. ** @copydoc VL_INT8_C */
  275. /** @def VL_UINT32_C(x)
  276. ** @copydoc VL_INT8_C */
  277. /** @def VL_UINT64_C(x)
  278. ** @copydoc VL_INT8_C */
  279. /** @def VL_ARCH_IX86
  280. ** @brief Defined if the host CPU is of the Intel x86 family.
  281. ** @see @ref host-arch
  282. **/
  283. /** @def VL_ARCH_IA64
  284. ** @brief Defined if the host CPU is of the Intel Architecture-64 family.
  285. ** @see @ref host-arch
  286. **/
  287. /** @def VL_ARCH_LITTLE_ENDIAN
  288. ** @brief Defined if the host CPU is little endian
  289. ** @see @ref host-arch-endianness
  290. **/
  291. /** @def VL_ARCH_BIG_ENDIAN
  292. ** @brief Defined if the host CPU is big endian
  293. ** @see @ref host-arch-endianness
  294. **/
  295. /** @def VL_INLINE
  296. ** @brief Adds appropriate inline function qualifier
  297. ** @see @ref host-compiler-other
  298. **/
  299. /** @def VL_EXPORT
  300. ** @brief Declares a DLL exported symbol
  301. ** @see @ref host-compiler-other
  302. **/
  303. /** @def VL_DISABLE_SSE2
  304. ** @brief Defined if SSE2 support if disabled
  305. **
  306. ** Define this symbol during compliation of the library and linking
  307. ** to another project to disable VLFeat SSE2 support.
  308. **/
  309. /** @def VL_DISABLE_THREADS
  310. ** @brief Defined if multi-threading support is disabled
  311. **
  312. ** Define this symbol during compilation of the library and linking
  313. ** to another project to disable VLFeat multi-threading support.
  314. **/
  315. /** @def VL_DISABLE_OPENMP
  316. ** @brief Defined if OpenMP support is disabled
  317. **
  318. ** Define this symbol during compilation of the library and linking
  319. ** to another project to disable VLFeat OpenMP support.
  320. **/
  321. /** @def VL_THREADS_WIN
  322. ** @brief Defined if the host uses Windows threads.
  323. ** @see @ref host-threads
  324. **/
  325. /** @def VL_THREADS_POSIX
  326. ** @brief Defiend if the host uses POISX threads.
  327. ** @see @ref host-threads
  328. **/
  329. /** --------------------------------------------------------------- */
  330. #include "host.h"
  331. #include "generic.h"
  332. #include <stdio.h>
  333. #if defined(VL_ARCH_IX86) || defined(VL_ARCH_IA64) || defined(VL_ARCH_X64)
  334. #define HAS_CPUID
  335. #else
  336. #undef HAS_CPUID
  337. #endif
  338. #if defined(HAS_CPUID) & defined(VL_COMPILER_MSC)
  339. #include <intrin.h>
  340. VL_INLINE void
  341. _vl_cpuid (vl_int32* info, int function)
  342. {
  343. __cpuid(info, function) ;
  344. }
  345. #endif
  346. #if defined(HAS_CPUID) & defined(VL_COMPILER_GNUC)
  347. VL_INLINE void
  348. _vl_cpuid (vl_int32* info, int function)
  349. {
  350. #if defined(VL_ARCH_IX86) && (defined(__PIC__) || defined(__pic__))
  351. /* This version is compatible with -fPIC on x386 targets. This special
  352. * case is required becaus
  353. * on such platform -fPIC alocates ebx as global offset table pointer.
  354. * Note that =r below will be mapped to a register different from ebx,
  355. * so the code is sound. */
  356. __asm__ __volatile__
  357. ("pushl %%ebx \n" /* save %ebx */
  358. "cpuid \n"
  359. "movl %%ebx, %1 \n" /* save what cpuid just put in %ebx */
  360. "popl %%ebx \n" /* restore the old %ebx */
  361. : "=a"(info[0]), "=r"(info[1]), "=c"(info[2]), "=d"(info[3])
  362. : "a"(function)
  363. : "cc") ; /* clobbered (cc=condition codes) */
  364. #else /* no -fPIC or -fPIC with a 64-bit target */
  365. __asm__ __volatile__
  366. ("cpuid"
  367. : "=a"(info[0]), "=b"(info[1]), "=c"(info[2]), "=d"(info[3])
  368. : "a"(function)
  369. : "cc") ;
  370. #endif
  371. }
  372. #else
  373. VL_INLINE void _vl_cpuid (vl_int32* info, int function){
  374. info[0] = 0;
  375. info[1] = 0;
  376. info[2] = 0;
  377. info[3] = 0;
  378. };
  379. #endif
  380. void
  381. _vl_x86cpu_info_init (VlX86CpuInfo *self)
  382. {
  383. vl_int32 info [4] ;
  384. int max_func = 0 ;
  385. _vl_cpuid(info, 0) ;
  386. max_func = info[0] ;
  387. self->vendor.words[0] = info[1] ;
  388. self->vendor.words[1] = info[3] ;
  389. self->vendor.words[2] = info[2] ;
  390. if (max_func >= 1) {
  391. _vl_cpuid(info, 1) ;
  392. self->hasMMX = info[3] & (1 << 23) ;
  393. self->hasSSE = info[3] & (1 << 25) ;
  394. self->hasSSE2 = info[3] & (1 << 26) ;
  395. self->hasSSE3 = info[2] & (1 << 0) ;
  396. self->hasSSE41 = info[2] & (1 << 19) ;
  397. self->hasSSE42 = info[2] & (1 << 20) ;
  398. self->hasAVX = info[2] & (1 << 28) ;
  399. }
  400. }
  401. char *
  402. _vl_x86cpu_info_to_string_copy (VlX86CpuInfo const *self)
  403. {
  404. char * string = 0 ;
  405. int length = 0 ;
  406. while (string == 0) {
  407. if (length > 0) {
  408. string = (char*) vl_malloc(sizeof(char) * length) ;
  409. if (string == NULL) break ;
  410. }
  411. length = snprintf(string, length, "%s%s%s%s%s%s%s%s",
  412. self->vendor.string,
  413. self->hasMMX ? " MMX" : "",
  414. self->hasSSE ? " SSE" : "",
  415. self->hasSSE2 ? " SSE2" : "",
  416. self->hasSSE3 ? " SSE3" : "",
  417. self->hasSSE41 ? " SSE41" : "",
  418. self->hasSSE42 ? " SSE42" : "",
  419. self->hasAVX ? " AVX" : "") ;
  420. length += 1 ;
  421. }
  422. return string ;
  423. }
  424. /** ------------------------------------------------------------------
  425. ** @brief Human readable static library configuration
  426. ** @return a new string with the static configuration.
  427. **
  428. ** The string includes information about the compiler, the host, and
  429. ** other static configuration parameters. The string must be released
  430. ** by ::vl_free.
  431. **/
  432. VL_EXPORT char *
  433. vl_static_configuration_to_string_copy ()
  434. {
  435. char const * hostString =
  436. #ifdef VL_ARCH_X64
  437. "X64"
  438. #endif
  439. #ifdef VL_ARCH_IA64
  440. "IA64"
  441. #endif
  442. #ifdef VL_ARCH_IX86
  443. "IX86"
  444. #endif
  445. #ifdef VL_ARCH_PPC
  446. "PPC"
  447. #endif
  448. ", "
  449. #ifdef VL_ARCH_BIG_ENDIAN
  450. "big_endian"
  451. #endif
  452. #ifdef VL_ARCH_LITTLE_ENDIAN
  453. "little_endian"
  454. #endif
  455. ;
  456. char compilerString [1024] ;
  457. char const * libraryString =
  458. #ifndef VL_DISABLE_THREADS
  459. #ifdef VL_THREADS_WIN
  460. "Windows_threads"
  461. #elif VL_THREADS_POSIX
  462. "POSIX_threads"
  463. #endif
  464. #else
  465. "No_threads"
  466. #endif
  467. #ifndef VL_DISABLE_SSE2
  468. ", SSE2"
  469. #endif
  470. #if defined(_OPENMP)
  471. ", OpenMP"
  472. #endif
  473. ;
  474. snprintf(compilerString, 1024,
  475. #ifdef VL_COMPILER_MSC
  476. "Microsoft Visual C++ %d"
  477. #define v VL_COMPILER_MSC
  478. #endif
  479. #ifdef VL_COMPILER_GNUC
  480. "GNU C %d"
  481. #define v VL_COMPILER_GNUC
  482. #endif
  483. " "
  484. #ifdef VL_COMPILER_LP64
  485. "LP64"
  486. #endif
  487. #ifdef VL_COMPILER_LLP64
  488. "LP64"
  489. #endif
  490. #ifdef VL_COMPILER_ILP32
  491. "ILP32"
  492. #endif
  493. , v) ;
  494. {
  495. char * string = 0 ;
  496. int length = 0 ;
  497. while (string == 0) {
  498. if (length > 0) {
  499. string = (char*)vl_malloc(sizeof(char) * length) ;
  500. if (string == NULL) break ;
  501. }
  502. length = snprintf(string, length, "%s, %s, %s",
  503. hostString,
  504. compilerString,
  505. libraryString) ;
  506. length += 1 ;
  507. }
  508. return string ;
  509. }
  510. }