title: rdtsc, too many cycles
body: <pre><code>#include &lt;stdio.h&gt;
  static inline unsigned long long tick() 
  {
          unsigned long long d;
          __asm__ __volatile__ ("rdtsc" : "=A" (d) );
          return d;
  }
  
  int main()
  {
          long long res;
          res=tick();
  
          res=tick()-res;
          printf("%d",res);
          return 0;
  }
  </code></pre>
  
  <p>I have compiled this code with gcc with -O0 -O1 -O2 -O3 optimizations. And I always get 2000-2500 cycles. Can anyone explain the reason for this output? How to spend these cycles?</p>
  
  <p><strike>First function "tick" is wrong. This is right</strike>.</p>
  
  <p>Another version of function "tick"</p>
  
  <pre><code>static __inline__ unsigned long long tick()
  {
    unsigned hi, lo;
    __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
    return ( (unsigned long long)lo)|( ((unsigned long long)hi)&lt;&lt;32 );
  }
  </code></pre>
  
  <p>This is assembly code for -O3   </p>
  
  <pre><code> .file  "rdtsc.c"
  .section    .rodata.str1.1,"aMS",@progbits,1
  .LC0:
      .string "%d"
      .text
      .p2align 4,,15
  .globl main
      .type   main, @function
  main:
      leal    4(%esp), %ecx
      andl    $-16, %esp
      pushl   -4(%ecx)
      pushl   %ebp
      movl    %esp, %ebp
      subl    $40, %esp
      movl    %ecx, -16(%ebp)
      movl    %ebx, -12(%ebp)
      movl    %esi, -8(%ebp)
      movl    %edi, -4(%ebp)
  #APP
  # 6 "rdtsc.c" 1
      rdtsc
  # 0 "" 2
  #NO_APP
      movl    %edx, %edi
      movl    %eax, %esi
  #APP
  # 6 "rdtsc.c" 1
      rdtsc
  # 0 "" 2
  #NO_APP
      movl    %eax, %ecx
      movl    %edx, %ebx
      subl    %esi, %ecx
      sbbl    %edi, %ebx
      movl    %ecx, 4(%esp)
      movl    %ebx, 8(%esp)
      movl    $.LC0, (%esp)
      call    printf
      movl    -16(%ebp), %ecx
      xorl    %eax, %eax
      movl    -12(%ebp), %ebx
      movl    -8(%ebp), %esi
      movl    -4(%ebp), %edi
      movl    %ebp, %esp
      popl    %ebp
      leal    -4(%ecx), %esp
      ret
      .size   main, .-main
      .ident  "GCC: (Debian 4.3.2-1.1) 4.3.2"
      .section    .note.GNU-stack,"",@progbits
  </code></pre>
  
  <p>This is CPU</p>
  
  <pre><code>processor   : 0
  vendor_id   : GenuineIntel
  cpu family  : 15
  model       : 4
  model name  : Intel(R) Xeon(TM) CPU 3.00GHz
  stepping    : 3
  cpu MHz     : 3000.105
  cache size  : 2048 KB
  fdiv_bug    : no
  hlt_bug     : no
  f00f_bug    : no
  coma_bug    : no
  fpu     : yes
  fpu_exception   : yes
  cpuid level : 5
  wp      : yes
  flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss constant_tsc up pebs bts pni
  bogomips    : 6036.62
  clflush size    : 64
  </code></pre>