1

Clang had decided to give me a error. It has decided to yell at me for using __rdtscp (This isn't my code, I have no Idea what that is)

 error: use of undeclared identifier '__rdtscp'
                        i2 = __rdtscp(&ui2);
                             ^
 error: use of undeclared identifier '__rdtscp'
                        i3 = __rdtscp(&ui3);
                             ^
 error: use of undeclared identifier '__rdtscp'
                        i2 = __rdtscp(&ui2);
                             ^
 error: use of undeclared identifier '__rdtscp'
                        i3 = __rdtscp(&ui3);
                             ^
 error: use of undeclared identifier '__rdtscp'
                i2 = __rdtscp(&ui2);
                     ^
 error: use of undeclared identifier '__rdtscp'
                i3 = __rdtscp(&ui3);

I really would like figure out why Clang doesn't like me. Does anyone know what __rdtscp is and how I can get clang to calm down about it?

user3597842
  • 13
  • 1
  • 4

2 Answers2

4

Assuming you are compiling for x86, there is a __rdtscp builtin function. If you are compiling for other processing architectures, it won't be available, since rdtscp is basically the name of a x86 instruction.

This code works for me, using clang++ 3.5.0 as of yesterday:

#include <iostream>
#include <x86intrin.h>

int main()
{
    unsigned int dummy;
    unsigned long long t1 = __rdtscp(&dummy);
    std::cout << "Hello" << std::endl;

    unsigned long long t2 = __rdtscp(&dummy);
    std::cout << "Time: " << t2 - t1 << std::endl;
}

You will need to include x86intrin.h to make the translation from __rdtscp to the actual __builtin_ia32_rdtscp function that the compiler knows.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • That's not helpful. It looks like it is a MacroMonopoly - I mean Microsoft Specific function. Is it? – user3597842 May 03 '14 at 02:56
  • 1
    Not sure what you mean by that - this code compiles in clang on my linux machine. I haven't dug through ALL of the code inside clang itself, but the `__builtin_ia32_rdtscp()` should work on any x86. The `x86intrin.h` file has a `inline` function that translates from `__rdtscp` to `__builtin_ia32_rdtscp()`. Does the posted code not compile on your system? If so, what error do you get, which compiler, and what procoessor/OS are you compiling on/in? – Mats Petersson May 03 '14 at 08:17
  • 1
    @user3597842: No, it is not Microsoft specific (nor a macro), but an **intel** intrinsic function, and as such "standard" for the architecture. However, as Mats Petersson points out, the two mainstream open source compilers _also_ implement it as a `__builtin_ia32_` variant, which saves you from having to include `x86intrin.h` (which is an architecture-aware standard header including the other intrinsic headers based on feature availability, in this case `immintrin.h`). The builtin simply inserts the assembly instruction at the point of its occurrence. – Damon Dec 15 '16 at 09:28
  • **MSVC defines it in ``, so you can portably use it with a `#ifdef _MSC_VER`. [Get CPU cycle count?](https://stackoverflow.com/a/51907627) has an example** – Peter Cordes Aug 18 '18 at 10:45
0

__rdtscp() on some platforms is a compiler intrinsic for the RDTSCP instruction, which is the recently introduced serialized version of RDTSC ("Read Time Stamp Counter"), used for counting the number of processor cycles, for example in benchmarking or timer code.

Here is a way is a way to use rdtscp() in C or C++ when using gcc. Perhaps the same approach will work for Clang?

tetromino
  • 3,490
  • 1
  • 15
  • 8
  • Not just some, all 4 major x86 compilers support the `__rdtscp()` intrinsic with the right headers, like Mats's answer shows. Also [Get CPU cycle count?](https://stackoverflow.com/a/51907627) – Peter Cordes Aug 18 '18 at 10:43