Error in inline assembler in gcc

I have successfully written some built-in assembler in gcc to rotate one bit right following some nice instructions: http://www.cs.dartmouth.edu/~sergey/cs108/2009/gcc-inline-asm.pdf

Here is an example:

static inline int ror(int v) {
    asm ("ror %0;" :"=r"(v) /* output */ :"0"(v) /* input */ );
    return v;
}

However, I want the code to count the clock cycles and see some incorrect (possibly microsoft) format. I don't know how to do this in gcc. Any help?

unsigned __int64 inline GetRDTSC() {
   __asm {
      ; Flush the pipeline
      XOR eax, eax
      CPUID
      ; Get RDTSC counter in edx:eax
      RDTSC
   }
}

I tried:

static inline unsigned long long getClocks() {
    asm("xor %%eax, %%eax" );
    asm(CPUID);
    asm(RDTSC : : %%edx %%eax); //Get RDTSC counter in edx:eax

but I don’t know how to get a pair of edx: eax for 64-bit output, and I don’t know how to really clear the pipeline.

Also, the best source code I found was: http://www.strchr.com/performance_measurements_with_rdtsc

pentium, , intel/AMD, , . -, x86, , , .

+5
2

, :

inline unsigned long long rdtsc() {
  unsigned int lo, hi;
  asm volatile (
     "cpuid \n"
     "rdtsc" 
   : "=a"(lo), "=d"(hi) /* outputs */
   : "a"(0)             /* inputs */
   : "%ebx", "%ecx");     /* clobbers*/
  return ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
}

, ASM, - . C, , ASM. , "a" 0, , eax. , - , , .

, "" . CPUID eax, ebx, ecx edx. , , , . eax edx, . clobbers, , , .

+10

. , , .

unsigned int hi,lo;
unsigned long long value;
asm (
    "cpuid\n\t"
    "rdtsc"
    : "d" (hi), "a" (lo)
);
value = (((unsigned long long)hi) << 32) | lo;
+1

All Articles