Skip to content

Commit 2fe4f9a

Browse files
committed
Add detecting 32 or 64 bit.
Try to build the library with MSVC. Fix cpuid issue with MSVC.
1 parent a4d6de2 commit 2fe4f9a

File tree

11 files changed

+240
-13
lines changed

11 files changed

+240
-13
lines changed

CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ set (OpenVML_FUNC_SUFFIX "")
1717
option(BUILD_SINGLE_THREAD "Only build the single thread" ON)
1818
option(AUTO_DETECT_CPU "Auto detect CPU architecture." ON)
1919
option(BUILD_OpenVML_TEST "Build Test" ON)
20+
option(BUILD_64BIT_INT "Build 64bit int interface(ilp64)" OFF)
2021

2122
#####################################################
2223

@@ -35,10 +36,29 @@ include_directories("${PROJECT_BINARY_DIR}")
3536
include_directories("${PROJECT_BINARY_DIR}/include")
3637
include_directories("${PROJECT_SOURCE_DIR}/include")
3738

39+
#single thread
3840
if(BUILD_SINGLE_THREAD)
3941
set(OPENVML_SINGLE_THREAD 1)
4042
endif(BUILD_SINGLE_THREAD)
4143

44+
#32-bit or 64-bit target
45+
include(${PROJECT_SOURCE_DIR}/cmake/detect_32or64bit.cmake)
46+
detect_32_64_bit(OpenVML_BINARY)
47+
if("${OpenVML_BINARY}" STREQUAL "32")
48+
set(__32BIT__ 1)
49+
elseif("${OpenVML_BINARY}" STREQUAL "64")
50+
set(__64BIT__ 1)
51+
endif()
52+
53+
#32-bit or 64-bit int
54+
if(BUILD_64BIT_INT)
55+
set(USE64BITINT 1)
56+
endif()
57+
58+
if(WIN32)
59+
set(OS_WINDOWS 1)
60+
endif()
61+
4262
configure_file (
4363
"${PROJECT_SOURCE_DIR}/include/openvml_config.h.in"
4464
"${PROJECT_BINARY_DIR}/include/openvml_config.h"
@@ -99,8 +119,10 @@ set_target_properties(${OpenVML_LIBNAME}_static PROPERTIES OUTPUT_NAME ${OpenVML
99119
set_target_properties(${OpenVML_LIBNAME}_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
100120
set_target_properties(${OpenVML_LIBNAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1)
101121

122+
if(NOT MSVC)
102123
target_link_libraries(${OpenVML_LIBNAME} m)
103124
target_link_libraries(${OpenVML_LIBNAME}_static m)
125+
endif()
104126

105127
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
106128

cmake/arch_detect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
int main()
2929
{
30-
#if defined(__x86_64__) || defined(__amd64__)
30+
#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
3131
printf("x86_64");
3232
return 0;
3333
#endif

cmake/auto_detect_cpu.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,29 @@ endif()
1414

1515
#For x86_64
1616
if(OpenVML_ARCH STREQUAL "x86_64")
17+
18+
if(MSVC)
19+
set(OpenVML_CPU_DETECT_FLAGS
20+
${OpenVML_CPU_DETECT_FLAGS} -DCOMPILER_MSVC)
21+
endif()
22+
1723
try_run(cpu_detect_result cpu_detect_compile_result
1824
${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/cmake/cpuid_x86.c
25+
COMPILE_DEFINITIONS ${OpenVML_CPU_DETECT_FLAGS}
1926
RUN_OUTPUT_VARIABLE cpu_detect_output
27+
COMPILE_OUTPUT_VARIABLE cpu_detect_compile_output
2028
)
2129
endif()
2230

2331
if(cpu_detect_compile_result)
2432
if(cpu_detect_result EQUAL 0)
2533
set (OpenVML_CPU_CORENAME ${cpu_detect_output})
34+
else()
35+
message("${cpu_detect_output}")
2636
endif()
37+
else()
38+
message("detect compile error")
39+
message("${cpu_detect_compile_output}")
2740
endif()
2841

2942

cmake/cpuid_x86.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#include <stdio.h>
2727
#include <string.h>
2828

29+
#ifdef COMPILER_MSVC
30+
#include <intrin.h>
31+
#endif
2932

3033
#define VENDOR_UNKNOWN 0
3134
#define VENDOR_INTEL 1
@@ -46,7 +49,15 @@ static char *cpuname[] = {
4649

4750
#define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
4851

49-
static inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
52+
static void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
53+
#ifdef COMPILER_MSVC
54+
int cpuinfo[4];
55+
__cpuid(cpuinfo, op);
56+
*eax=cpuinfo[0];
57+
*ebx=cpuinfo[1];
58+
*ecx=cpuinfo[2];
59+
*edx=cpuinfo[3];
60+
#else
5061
#if defined(__i386__) && defined(__PIC__)
5162
__asm__ __volatile__
5263
("mov %%ebx, %%edi;"
@@ -57,9 +68,10 @@ static inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
5768
__asm__ __volatile__
5869
("cpuid": "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (op) : "cc");
5970
#endif
71+
#endif
6072
}
6173

62-
static inline int have_cpuid(void){
74+
static int have_cpuid(void){
6375
int eax, ebx, ecx, edx;
6476

6577
cpuid(0, &eax, &ebx, &ecx, &edx);
@@ -87,10 +99,14 @@ int get_vendor(void){
8799
}
88100

89101

90-
static inline void xgetbv(int op, int * eax, int * edx){
102+
static void xgetbv(int op, int * eax, int * edx){
103+
#ifdef COMPILER_MSVC
104+
*eax=6; //Assume support for MSVC.
105+
#else
91106
//Use binary code for xgetbv
92107
__asm__ __volatile__
93108
(".byte 0x0f, 0x01, 0xd0": "=a" (*eax), "=d" (*edx) : "c" (op) : "cc");
109+
#endif
94110
}
95111

96112
int support_avx(){

cmake/detect_32or64bit.cmake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#https://github.com/petroules/solar-cmake/blob/master/TargetArch.cmake
2+
3+
set(detect_32_64_code "
4+
#include <stdint.h>
5+
#if INTPTR_MAX == INT32_MAX
6+
#error cmake_BINARY 32
7+
#elif INTPTR_MAX == INT64_MAX
8+
#error cmake_BINARY 64
9+
#else
10+
#error cmake_BINARY unknown
11+
#endif
12+
")
13+
14+
function(detect_32_64_bit output_var)
15+
file(WRITE "${CMAKE_BINARY_DIR}/32_64_bit.c" "${detect_32_64_code}")
16+
17+
enable_language(C)
18+
19+
try_run(
20+
run_result_unused
21+
compile_result_unused
22+
"${CMAKE_BINARY_DIR}"
23+
"${CMAKE_BINARY_DIR}/32_64_bit.c"
24+
COMPILE_OUTPUT_VARIABLE BINARY
25+
)
26+
27+
string(REGEX MATCH "cmake_BINARY ([a-zA-Z0-9_]+)" BINARY "${BINARY}")
28+
29+
string(REPLACE "cmake_BINARY " "" BINARY "${BINARY}")
30+
31+
if (NOT BINARY)
32+
set(BINARY unknown)
33+
endif()
34+
set(${output_var} "${BINARY}" PARENT_SCOPE)
35+
endfunction()

include/openvml_config.h.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@
3030
#define OPENVML_FUNC_PREFIX @OpenVML_FUNC_PREFIX@
3131
#define OPENVML_FUNC_SUFFIX @OpenVML_FUNC_SUFFIX@
3232

33+
#define OPENVML_ARCH @OpenVML_ARCH@
34+
#define OPENVML_CPU_CORENAME @OpenVML_CPU_CORENAME@
35+
#define OPENVML_BINARY @OpenVML_BINARY@
36+
3337
#cmakedefine OPENVML_SINGLE_THREAD
3438
#cmakedefine USE64BITINT
3539
#cmakedefine __64BIT__
40+
#cmakedefine __32BIT__
3641
#cmakedefine OS_WINDOWS
3742
#cmakedefine OS_DARWIN
3843
#cmakedefine OS_LINUX

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set(OpenVML_TESTSRC
22
vml_test.c
33
vml_util.c
4-
timer.c
4+
openvml_timer.c
55
test_add.c
66
test_sub.c
77
test_pow.c

test/ctest.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,13 @@ void assert_fail(const char* caller, int line);
129129
#include <stdarg.h>
130130
#include <stdio.h>
131131
#include <string.h>
132-
#include <sys/time.h>
133-
#include <inttypes.h>
132+
//#include <sys/time.h>
133+
//#include <inttypes.h>
134+
#ifdef _WIN32
135+
#include <io.h>
136+
#else
134137
#include <unistd.h>
138+
#endif
135139
#include <stdint.h>
136140
#include <stdlib.h>
137141

@@ -304,6 +308,7 @@ static int suite_test_filter(struct ctest* t) {
304308
return (suit_match & test_match);
305309
}
306310

311+
/*
307312
static uint64_t getCurrentTime() {
308313
struct timeval now;
309314
gettimeofday(&now, NULL);
@@ -312,6 +317,7 @@ static uint64_t getCurrentTime() {
312317
now64 += (now.tv_usec);
313318
return now64;
314319
}
320+
*/
315321

316322
static void color_print(const char* color, const char* text) {
317323
if (color_output)
@@ -381,7 +387,7 @@ int ctest_main(char * input_suitname, char * input_testname)
381387

382388

383389
color_output = isatty(1);
384-
uint64_t t1 = getCurrentTime();
390+
//uint64_t t1 = getCurrentTime();
385391

386392
struct ctest* ctest_begin = &__TNAME(suite, test);
387393
struct ctest* ctest_end = &__TNAME(suite, test);
@@ -449,11 +455,12 @@ int ctest_main(char * input_suitname, char * input_testname)
449455
index++;
450456
}
451457
}
452-
uint64_t t2 = getCurrentTime();
458+
//uint64_t t2 = getCurrentTime();
453459

454460
const char* color = (num_fail) ? ANSI_BRED : ANSI_GREEN;
455461
char results[80];
456-
sprintf(results, "RESULTS: %d tests (%d ok, %d failed, %d skipped) ran in %"PRIu64" ms", total, num_ok, num_fail, num_skip, (t2 - t1)/1000);
462+
//sprintf(results, "RESULTS: %d tests (%d ok, %d failed, %d skipped) ran in %"PRIu64" ms", total, num_ok, num_fail, num_skip, (t2 - t1)/1000);
463+
sprintf(results, "RESULTS: %d tests (%d ok, %d failed, %d skipped)", total, num_ok, num_fail, num_skip);
457464
color_print(color, results);
458465
return num_fail;
459466
}

test/openvml_timer.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Author: David Robert Nadeau
3+
* Site: http://NadeauSoftware.com/
4+
* License: Creative Commons Attribution 3.0 Unported License
5+
* http://creativecommons.org/licenses/by/3.0/deed.en_US
6+
*/
7+
#if defined(_WIN32)
8+
#include <Windows.h>
9+
10+
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
11+
#include <unistd.h>/* POSIX flags */
12+
#include <time.h>/* clock_gettime(), time() */
13+
#include <sys/time.h>/* gethrtime(), gettimeofday() */
14+
15+
#if defined(__MACH__) && defined(__APPLE__)
16+
#include <mach/mach.h>
17+
#include <mach/mach_time.h>
18+
#endif
19+
20+
#else
21+
#error "Unable to define getRealTime( ) for an unknown OS."
22+
#endif
23+
24+
25+
26+
27+
28+
/**
29+
* Returns the real time, in seconds, or -1.0 if an error occurred.
30+
*
31+
* Time is measured since an arbitrary and OS-dependent start time.
32+
* The returned real time is only useful for computing an elapsed time
33+
* between two calls to this function.
34+
*/
35+
double getRealTime( )
36+
{
37+
#if defined(_WIN32)
38+
FILETIME tm;
39+
ULONGLONG t;
40+
#if defined(NTDDI_WIN8) && NTDDI_VERSION >= NTDDI_WIN8
41+
/* Windows 8, Windows Server 2012 and later. ---------------- */
42+
GetSystemTimePreciseAsFileTime( &tm );
43+
#else
44+
/* Windows 2000 and later. ---------------------------------- */
45+
GetSystemTimeAsFileTime( &tm );
46+
#endif
47+
t = ((ULONGLONG)tm.dwHighDateTime << 32) | (ULONGLONG)tm.dwLowDateTime;
48+
return (double)t / 10000000.0;
49+
50+
#elif (defined(__hpux) || defined(hpux)) || ((defined(__sun__) || defined(__sun) || defined(sun)) && (defined(__SVR4) || defined(__svr4__)))
51+
/* HP-UX, Solaris. ------------------------------------------ */
52+
return (double)gethrtime( ) / 1000000000.0;
53+
54+
#elif defined(__MACH__) && defined(__APPLE__)
55+
/* OSX. ----------------------------------------------------- */
56+
static double timeConvert = 0.0;
57+
if ( timeConvert == 0.0 )
58+
{
59+
mach_timebase_info_data_t timeBase;
60+
(void)mach_timebase_info( &timeBase );
61+
timeConvert = (double)timeBase.numer /
62+
(double)timeBase.denom /
63+
1000000000.0;
64+
}
65+
return (double)mach_absolute_time( ) * timeConvert;
66+
67+
#elif defined(_POSIX_VERSION)
68+
/* POSIX. --------------------------------------------------- */
69+
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
70+
{
71+
struct timespec ts;
72+
#if defined(CLOCK_MONOTONIC_PRECISE)
73+
/* BSD. --------------------------------------------- */
74+
const clockid_t id = CLOCK_MONOTONIC_PRECISE;
75+
#elif defined(CLOCK_MONOTONIC_RAW)
76+
/* Linux. ------------------------------------------- */
77+
const clockid_t id = CLOCK_MONOTONIC_RAW;
78+
#elif defined(CLOCK_HIGHRES)
79+
/* Solaris. ----------------------------------------- */
80+
const clockid_t id = CLOCK_HIGHRES;
81+
#elif defined(CLOCK_MONOTONIC)
82+
/* AIX, BSD, Linux, POSIX, Solaris. ----------------- */
83+
const clockid_t id = CLOCK_MONOTONIC;
84+
#elif defined(CLOCK_REALTIME)
85+
/* AIX, BSD, HP-UX, Linux, POSIX. ------------------- */
86+
const clockid_t id = CLOCK_REALTIME;
87+
#else
88+
const clockid_t id = (clockid_t)-1;/* Unknown. */
89+
#endif /* CLOCK_* */
90+
if ( id != (clockid_t)-1 && clock_gettime( id, &ts ) != -1 )
91+
return (double)ts.tv_sec +
92+
(double)ts.tv_nsec / 1000000000.0;
93+
/* Fall thru. */
94+
}
95+
#endif /* _POSIX_TIMERS */
96+
97+
/* AIX, BSD, Cygwin, HP-UX, Linux, OSX, POSIX, Solaris. ----- */
98+
struct timeval tm;
99+
gettimeofday( &tm, NULL );
100+
return (double)tm.tv_sec + (double)tm.tv_usec / 1000000.0;
101+
#else
102+
return -1.0;/* Failed. */
103+
#endif
104+
}

test/openvml_timer.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* * Copyright (c) 2014, 2015 Zhang Xianyi
2+
* All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without modification,
5+
* are permitted provided that the following conditions are met:
6+
*
7+
* * Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
*
10+
* * Redistributions in binary form must reproduce the above copyright notice, this
11+
* list of conditions and the following disclaimer in the documentation and/or
12+
* other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
double getRealTime();

0 commit comments

Comments
 (0)