X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/6cb6b7ede2d178e03fa817bc28474c175f5a93b9..c1aeb5145ef5940247379a3d5a1be00913c0b4b8:/btime.pas?ds=sidebyside diff --git a/btime.pas b/btime.pas old mode 100755 new mode 100644 index 4636a9b..7adcdd8 --- a/btime.pas +++ b/btime.pas @@ -12,6 +12,11 @@ unit btime; interface +{$ifdef win32} +uses + ltimevalstuff; +{$endif} + type float=extended; @@ -48,13 +53,18 @@ function unixtimefloat_systemtime:float; function oletounixfloat(t:float):float; function oletounix(t:tdatetime):integer; -function unixtoole(i:integer):tdatetime; +function unixtoole(i:float):tdatetime; {$ifdef win32} function mmtimefloat:float; function qpctimefloat:float; {$endif} +{$ifdef win32} +procedure gettimeofday(var tv:ttimeval); +{$endif} + + const mmtime_driftavgsize=32; mmtime_warmupnum=4; @@ -96,7 +106,7 @@ uses {$ifdef VER1_0} linux, {$else} - baseunix,unix,unixutil, {needed for 2.0.2} + baseunix,unix,unixutil,sockets, {unixutil and sockets needed by unixstuff.inc on some compiler versions} {$endif} {$else} windows,unitsettc,mmsystem, @@ -120,7 +130,7 @@ begin result := trunc(oletounixfloat(t)); end; -function unixtoole(i:integer):tdatetime; +function unixtoole(i:float):tdatetime; begin result := ((i)/86400)+daysdifference; end; @@ -169,6 +179,22 @@ end; {$else} {delphi 3} {------------------------------ windows/delphi code to read time} + +{simulate gettimeofday on windows so one can always use gettimeofday if preferred} + +procedure gettimeofday(var tv:ttimeval); +var + e:extended; +begin + e := unixtimefloat; + tv.tv_sec := round(int(e)); + tv.tv_usec := trunc(frac(e)*1000000); + {just in case} + if (tv.tv_usec < 0) then tv.tv_usec := 0; + if (tv.tv_usec > 999999) then tv.tv_usec := 999999; +end; + + { time float: gettickcount resolution: 9x: ~55 ms NT: 1/64th of a second @@ -200,7 +226,7 @@ var f,g:float; o:tosversioninfo; isnt:boolean; - is9x:boolean; +{ is9x:boolean;} begin if (performancecountfreq = 0) then qpctimefloat; ticks_freq_known := false; @@ -213,21 +239,35 @@ begin o.dwOSVersionInfoSize := sizeof(o); getversionex(o); isnt := o.dwPlatformId = VER_PLATFORM_WIN32_NT; - is9x := o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS; +{ is9x := o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;} ticks_freq2 := f; mmtime_synchedqpc := false; { - NT 64 Hz + NT 5 64 Hz (RTC) identify mode as: nt64 QPC rate: either 3579545 or TSC freq QPC synched to gettickcount: no duration between 2 ticks is constant: yes gettickcount tick duration: 64 Hz + + NT 6 64 Hz (HPET?) + identify mode as: nt64 + QPC rate: TSC freq / 1000 + QPC synched to gettickcount: no + duration between 2 ticks is constant: yes + gettickcount tick duration: ~15.6001007 ms, ~64.102 Hz + } if (f >= 0.014) and (f <= 0.018) and isnt then begin + if (performancecountfreq = 3579545) or (performancecountfreq > 50000000) then begin + {typical XP} + ticks_freq := 1/64; + end else begin + {typical windows 7} + ticks_freq := 0.0156001007; + end; ticks_freq_known := true; - ticks_freq := 1/64; mmtime_synchedqpc := false; end; @@ -249,7 +289,7 @@ begin end; {9x} - if (performancecountfreq = 1193182) and (g >= 0.050) and (g <= 0.060) then begin + if (performancecountfreq = 1193182) and (f >= 0.050) and (f <= 0.060) then begin ticks_freq_known := true; ticks_freq := 65536 / (colorburst / 3); mmtime_synchedqpc := true; @@ -301,14 +341,14 @@ const maxretries=5; margin=0.002; var - jump:float; - mm,f,qpc,newdrift,f1,f2:float; +{ jump:float;} + mm,f,qpc,newdrift:float; qpcjumped:boolean; - a,b,c:integer; - retrycount:integer; + a,b:integer; +{ retrycount:integer;} begin if not ticks_freq_known then measure_ticks_freq; - retrycount := maxretries; +{ retrycount := maxretries;} qpc := qpctimefloat; mm := mmtimefloat; @@ -325,7 +365,7 @@ begin mmtime_prev_lastsyncqpc := mmtime_lastsyncqpc; mm := mmtimefloat; - dec(retrycount); + { dec(retrycount);} settc; result := qpctimefloat; f := mmtimefloat; @@ -356,6 +396,7 @@ begin { mmtime_drift := mmtime_drift + mmtime_driftavg[a];} end; { mmtime_drift := mmtime_drift / b;} + a := 5; if (b = 1) then a := 5 else if (b = 2) then a := 15 else if (b = 3) then a := 30 else if (b = 4) then a := 60 else if (b = 5) then a := 120 else if (b >= 5) then a := 120; mmtime_nextdriftcorrection := qpc + a; if (b >= 2) then warmup_finished := true; @@ -373,17 +414,17 @@ begin qpc := qpctimefloat; result := (qpc - mmtime_lastsyncqpc) * mmtime_drift + mmtime_lastsyncmm; - f := (qpc - mmtime_prev_lastsyncqpc) * mmtime_prev_drift + mmtime_prev_lastsyncmm; + {f := (qpc - mmtime_prev_lastsyncqpc) * mmtime_prev_drift + mmtime_prev_lastsyncmm; jump := result-f; - {writeln('jump ',formatfloat('0.000000',jump),' drift ',formatfloat('0.00000000',mmtime_drift),' duration ',formatfloat('0.000',(mmtime_lastsyncqpc-mmtime_prev_lastsyncqpc)),' ',formatfloat('0.00000000',jump/(mmtime_lastsyncqpc-mmtime_prev_lastsyncqpc)));} + writeln('jump ',formatfloat('0.000000',jump),' drift ',formatfloat('0.00000000',mmtime_drift),' duration ',formatfloat('0.000',(mmtime_lastsyncqpc-mmtime_prev_lastsyncqpc)),' ',formatfloat('0.00000000',jump/(mmtime_lastsyncqpc-mmtime_prev_lastsyncqpc)));} f := result; end; result := f; - if (result < mmtime_lastresult) then result := mmtime_lastresult + 0.000001; + if (result < mmtime_lastresult) then result := mmtime_lastresult; mmtime_lastresult := result; end;