www.delorie.com/djgpp/doc/libc/libc_641.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

_rdtsc

Syntax

 
#include <time.h>

unsigned long long _rdtsc(void);

Description

This function invokes the hardware instruction rdtsc which is only supported on some processors. It is incremented once per clock cycle on the main processor. It is a high precision timer which is useful for timing code for optimization. You should not use this function in distributed programs without protecting for processors which do not support the instruction. For a general purpose high precision timer see uclock (see section uclock).

Return Value

The number of processor cycles.

Portability

ANSI/ISO C No
POSIX No

Example

 
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/exceptn.h>

/* Catch rdtsc exception and always return 0LL */
void catch_rdtsc(int val)
{
  short *eip = (short *)__djgpp_exception_state->__eip;
  if(*eip == 0x310f) {
    __djgpp_exception_state->__eip += 2;
    __djgpp_exception_state->__edx = 0;
    longjmp(__djgpp_exception_state, 0);
  }
  return;
}

int main(void)
{
  unsigned long long t;
  
  signal(SIGILL, catch_rdtsc);
  t = _rdtsc();
  printf("Timer value: %llu\n",t);
  
  return 0;
}


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2004   by DJ Delorie     Updated Apr 2004