user1669844
user1669844

Reputation: 763

Compiler complaining about volatile keyword

I was trying to compile the following code from this question and it fails to compile with: error C2059: syntax error: 'volatile'

#include<stdint.h>
#include<stdio.h>

static inline uint64_t rdtscp( uint32_t & aux )
{
    uint64_t rax,rdx;
    asm volatile ( "rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : : );
    return (rdx << 32) + rax;
}

I was using x64 msvc v19(WINE) compiler without any flags on godbolt

Upvotes: 3

Views: 751

Answers (1)

Acorn
Acorn

Reputation: 26086

asm volatile is a GNU extension. The qualifier is documented here.

For MSVC, use the __rdtscp intrinsic instead.


Also, note that you can use the intrinsic in all the major compilers, e.g.:

#include <iostream>
#include <cstdint>

#ifdef _WIN32
#  include <intrin.h>
#else
#  include <x86intrin.h>
#endif

int main()
{
    uint64_t i;
    uint32_t ui;
    i = __rdtscp(&ui);
    std::cout
        << "Ticks: " << i << '\n'
        << "TSC_AUX: " << ui << '\n'
    ;
    return 0;
}

Upvotes: 3

Related Questions