News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Help with QueryPerformanceCounter and 64 bit numbers

Started by Lonewolff, April 12, 2018, 03:15:46 PM

Previous topic - Next topic

hutch--

The API is simple to use.


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL var1  :DWORD

    push esi
    push edi

    lea esi, var1                           ; load the address
    mov edi, 100                            ; set the counter

  @@:
    invoke QueryPerformanceCounter, esi     ; call the API
    print str$([esi]),13,10                 ; display low DWORD
    sub edi, 1                              ; decrement counter
    jnz @B                                  ; loop again if not 0

    pop edi
    pop esi

    ret

main endp


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

Siekmanski

Creative coders use backward thinking techniques as a strategy.

nidud

#32
deleted

hutch--

You are right, it should be in 32 bit,

    LOCAL var1  :DWORD
    LOCAL dumm  :DWORD

I added it later.

nidud

#34
deleted

dedndave

you might use the LARGE_INTEGER structure, as defined in windows.inc

LARGE_INTEGER UNION
    STRUCT
      LowPart  DWORD ?
      HighPart DWORD ?
    ENDS
  QuadPart QWORD ?
LARGE_INTEGER ENDS


handy, because you can access the values as either 2 DWORDs or 1 QWORD

    LOCAL  liPerfCtr   :LARGE_INTEGER

LordAdef

Wait,
what Hutch was first doing is what I am doing, simply passing the first dword straight away is already what we want. we don't need any extra work unless we want the full qword.


Or I am missing something?




nidud

#37
deleted

LordAdef

But I mean,

queryperformancecounter, addr temp
mov eax, temp

that's the dword we need, right? The low dword is the first data in the union, so this should suffice

jj2007

Most of the time the DWORD is enough; occasionally, you'll get overflow, though.

But what's wrong with the complete solution that I posted in reply #4? Plain Masm32 8)

Btw if you need milliseconds instead of seconds, just insert a line as shown below, and adjust the unit:include \masm32\include\masm32rt.inc

.data?
timeStart dq ?
timeEnd dq ?
timeFrequency dq ?
timeElapsed dq ?

.code
start:
  invoke QueryPerformanceFrequency, addr timeFrequency
  invoke QueryPerformanceCounter, addr timeStart
  invoke Sleep, 300
  invoke QueryPerformanceCounter, addr timeEnd
  fild timeEnd
  fild timeStart
  fsub
  fild timeFrequency
  fdiv
  fmul FP4(1000.0) ; to get milliseconds instead of seconds
  fistp timeElapsed
  inkey str$(dword ptr timeElapsed), " ms elapsed"
  exit
end start

Lonewolff

Quote from: Siekmanski on April 12, 2018, 08:52:41 PM
-> Project doesn't link to ole32.lib.

CoInitialize and CoUninitialize do need ole32.lib, needed for COM.

Weird. Definitely not linking to this yet COM is working perfectly.

Maybe something in d3d11.lib?

I don't call CoInitialize or CoUnititialize anywhere either. Not even in my C++ code. Haven't had to do that since DX9. Maybe DX11 does this under the hood?


[edit]
Arggh! My bad. I am including masm32rt.inc which links to ole32.lib.

Siekmanski

I'm just following the rules.  :biggrin:

EDIT: I'm not certain now if it is really necessary for DirectX.  ::)
I never used CoCreateInstance as far as I can remember in my DirectX code.
Only used it in DirectShow I think.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms678543(v=vs.85).aspx
Creative coders use backward thinking techniques as a strategy.

LordAdef

Guys,

Allow me to keep this topic going for a bit longer.

Since Hutch, Marinus, JJ and I are going through different routes, I wonder how the benchmarks behave (not done it yet). But an interesting one though.

I'm dealing with dwords and doing the stuff in cpu prior to FPU. Marinus is going SIMD with dd, and JJ is full dq.
My approach was:


.data
    SchedulerMS dd 1 ; granularity for Sleep
PerfCountFreq dd 0
LastCounter dd 0
EndCounter dd 0
ElapsedCounter dd 0
tFPS dd 0
MSPerFrame real8 0.0
SleepMS sdword 0
TargetSecPerFrame real8 16.0


.code
inv timeBeginPeriod, SchedulerMS
.IF eax != TIMERR_NOERROR
       console "ATTENTION: timeBeginPeriod failed!" ; (console is my printf macro)
.ENDIF
inv QueryPerformanceFrequency, ADDR PerfCountFreq
inv QueryPerformanceCounter, ADDR LastCounter

; prog loop starts

         ;;; code here

inv QueryPerformanceCounter, ADDR EndCounter
mov ecx, LastCounter
mov eax, EndCounter
sub eax, ecx
mov edx, 1000
mov ElapsedCounter, eax
mul edx

push eax
fild dword ptr [esp]
fidiv PerfCountFreq
fstp MSPerFrame

mov eax, PerfCountFreq
cdq
div dword ptr ElapsedCounter
mov tFPS, eax

fld TargetSecPerFrame
fsub MSPerFrame
fistp SleepMS

cmp SleepMS, 0
jle done
inv Sleep, dword ptr [SleepMS]

done:
[code]


By natural selection, I must be running far behind...but who knows... any comments?


edit to organize the code

Siekmanski

QuoteI'm dealing with dwords and doing the stuff in cpu prior to FPU. Marinus is going SIMD with dd, and JJ is full dq.

Yeah, everybody has his own coding style.  :badgrin:

PerfCountFreq, LastCounter, EndCounter should be QWORD size.
Now they overwrite each other and ElapsedCounter also.

Maybe better to keep it all in the FPU then you can also make a reciprocal of PerfCountFreq and get rid of the fidiv instruction and replace it with fmul.
Creative coders use backward thinking techniques as a strategy.

LordAdef

QuoteMaybe better to keep it all in the FPU then you can also make a reciprocal of PerfCountFreq and get rid of the fidiv instruction and replace it with fmul.

This is the Jochen! nice