How to make cross-platform language C ++ inline?

I hacked the following code:

unsigned long long get_cc_time () volatile {
  uint64 ret;
  __asm__ __volatile__("rdtsc" : "=A" (ret) : :);
  return ret;
}

It works on g ++, but not on Visual Studio. How can I port it? What are the right macros to detect VS / g ++?

+3
source share
4 answers
#if defined(_MSC_VER)
// visual c
#elif defined(__GCCE__)
// gcce
#else
// unknown
#endif

My built-in assembler skills are rusty, but they work like:

__asm
{
// some assembler code
}

But to use rdtsc you can just use intrinsics:

unsigned __int64 counter;
counter = __rdtsc();

http://msdn.microsoft.com/en-us/library/twchhe95.aspx

+3
source

_MSC_VER V++, " Microsoft" MSDN , -, , . #ifdef, , , gcc V++.

#ifdef _MSC_VER
    //VC++ version
#else
    //gcc version
#endif
+2

RDTSC :

  • TSC , , / , TSC "" , / .
  • TSC , , "C1 clock ramping". (, , , , , TSC , ).
  • TSC HPET.

OS , :

, Microsoft Visual ++ 64- , , __rdtsc(), .

+2

OP : , :

#ifdef _MSC_VER
#   define ASM(asm_literal) \
        __asm { \
            asm_literal \
        };
#elif __GNUC__ || __clang__
#   define ASM(asm_literal) \
        "__asm__(\"" \
            #asm_literal \
        "\" : : );"
#endif

, , .

float abs(float x) {
    ASM( fld     dword ptr[x] );
    ASM( fabs                 );
    ASM( fstp    dword ptr[x] );

    return x;
}

, GCC clang AT & T/UNIX, MSVC Intel ( ). , , GCC/clang Intel. __asm__(".intel_syntax noprefix");/__asm__(".att_syntax prefix"); ( reset , , , , C). :

#ifdef _MSC_VER
#   define ASM(asm_literal) \
        __asm { \
            asm_literal \
        };
#elif __GNUC__ || __clang__
#   define ASM(asm_literal) \
        "__asm__(\".intel_syntax noprefix\");" \
        "__asm__(\"" \
            #asm_literal \
        "\" : : );" \
        "__asm__(\".att_syntax prefix\");"
#endif

Or you can also compile using GCC / clang using a flag -masm=intelthat includes global syntax.

+2
source

Source: https://habr.com/ru/post/1706810/