Rdtsc

This is a discussion on Rdtsc within the C Programming forums, part of the General Programming Boards category; Hi I was wondering I've been looking through the web trying to find out if the C library or a ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    Rdtsc

    Hi I was wondering I've been looking through the web trying to find out if the C library or a way to use RDTSC and havent been able to get a good understanding or a good sample.. Im really just trying to see how to write a simple call function of RDtsc just to get an idea of the syntax.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Google has plenty of stuff, including examples of C code, for various timers.

    High Precision Event Timer - Wikipedia, the free encyclopedia

    and check out the references at the bottom of the page, as well.


    Pentium Time Stamp Counter

    Pentium Time Stamp Counter
    On a Pentium, you are able to get the number of clock cycles since the CPU was powered up or reset. Determining the difference between two polls of the CPU for the number of clock cycles is a very effective way to time your code. The rdtsc instruction is used to move the 64-bit counter value into the resgisters EDX:EAX. Below is a piece of code that will return the 64-bit wide counter value. Note that this should work on both i386 and x86_64 architectures.

    Code:
    unsigned long long int rdtsc(void)
    {
       unsigned long long int x;
       unsigned a, d;
    
       __asm__ volatile("rdtsc" : "=a" (a), "=d" (d));
    
       return ((unsigned long long)a) | (((unsigned long long)d) << 32);;
    }
    This function will return a single "unsigned long long int" value that is the 64-bit wide counter.

  3. #3
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    If you're using Microsoft's compiler you can use it as an intrinsic function like this:
    Code:
    #include <intrin.h>
    
    #pragma intrinsic(__rdtsc)
    
    int main()
    {
        __int64 i;
        i = __rdtsc();
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Disgusting AT&T syntax.
    Anyway, don't use that timer directly. It will cause bugs on multi-core processors.
    You should use an appropriate function instead. Such a function is QueryPerformanceCounter (I believe that's right) for Windows. There are other such APIs with other operating systems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by Elysia View Post
    Disgusting AT&T syntax.
    I don't understand why people don't like AT&T syntax. The disgusting thing here is x86 assembly. Why can't we all just use SPARC, it has beautiful assembly.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Im sorry i now this is stupid and i should have mentioned this earlier but i need it for a andriod function so im calling a native call in C then im going to try and use RDTSC but i tried the BMJ one on Visual and that works thanks would it work for droid app? when im doing a jni with it

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Does your android run on an x86 processor?
    If not, using the rdtsc instruction (or it's wrapper function) just isn't going to work.

    Exactly how accurate do you need this to be? What are you going to use it for?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. kernel hacking, plz help
    By micke_b in forum Linux Programming
    Replies: 8
    Last Post: 02-18-2008, 03:49 PM
  2. Timing basic operations in C++
    By StevenGarcia in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2007, 02:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21