aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/asmlib/rdtsc64.asm
blob: 42a0e23203280ed0bf438bb4ec6593128818adad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
%include "defs.asm"

;          RDTSC64.ASM
;
; Author:           Agner Fog
; Date created:     2003
; Last modified:    2008-10-16
; Description:
;
; Copyright (c) 2009 GNU General Public License www.gnu.org/licenses
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

default rel

global  ReadTSC: function

SECTION .text  align=16

; ********** ReadTSC function **********
; C++ prototype:
; extern "C" __int64 ReadTSC (void);

; This function returns the value of the time stamp counter, which counts 
; clock cycles. To count how many clock cycles a piece of code takes, call
; Rdtsc before and after the code to measure and calculate the difference.

; The number of clock cycles taken by the ReadTSC function itself is approximately:
; Core 2:   730
; Pentium 4:  700
; Pentium II and Pentium III: 225
; AMD Athlon 64, Opteron: 126
; Does not work on 80386 and 80486.

; Note that clock counts may not be fully reproducible on Intel Core and
; Core 2 processors because the clock frequency can change. More reliable
; instruction timings are obtained with the performance monitor counter
; for "core clock cycles". This requires a kernel mode driver as the one
; included with www.agner.org/optimize/testp.zip.

ReadTSC:
        push    rbx                    ; ebx is modified by cpuid
        sub     eax, eax               ; 0
        cpuid                          ; serialize
        rdtsc                          ; read time stamp counter into edx:eax
        shl     rdx, 32
        or      rax, rdx               ; combine into 64 bit register        
        push    rax
        sub     eax, eax
        cpuid                          ; serialize
        pop     rax                    ; return value
        pop     rbx
        ret
;ReadTSC ENDP