瀏覽代碼

Added high resolution timing.

woollybah 5 年之前
父節點
當前提交
98485e6b53
共有 2 個文件被更改,包括 45 次插入0 次删除
  1. 18 0
      stdc.mod/stdc.bmx
  2. 27 0
      stdc.mod/stdc.c

+ 18 - 0
stdc.mod/stdc.bmx

@@ -373,8 +373,26 @@ Function time_:Int( time:Byte Ptr )
 Function localtime_:Byte Ptr( time:Byte Ptr )
 Function strftime_:Int( buf:Byte Ptr,size:Int,fmt$,time:Byte Ptr )
 
+?Not macos
+Function clock_gettime_(id:Int, spec:STimeSpec Var)
+?macos
+Function mach_absolute_time_ns:ULong()
+?
+
+Function errno_:Int()
+
 End Extern
 
+Struct STimeSpec
+	Field tv_sec:Size_T
+	Field tv_nsec:Size_T
+	
+	Method New(tv_sec:Size_T, tv_nsec:Size_T)
+		Self.tv_sec = tv_sec
+		Self.tv_nsec = tv_nsec
+	End Method
+End Struct
+
 Private
 
 Extern "c"

+ 27 - 0
stdc.mod/stdc.c

@@ -333,6 +333,15 @@ int ftruncate_(FILE* stream, BBLONG size) {
 	return _chsize_s(fileno(stream), size);
 }
 
+int clock_gettime_(int id, struct timespec * spec) {
+	__int64 ftime;
+	GetSystemTimeAsFileTime((FILETIME*)&ftime);
+	ftime -= 116444736000000000LL;
+	spec->tv_sec = ftime / 10000000LL;
+	spec->tv_nsec = ftime % 10000000LL *100;
+	return 0;
+}
+
 #else
 
 int getchar_(){
@@ -476,6 +485,24 @@ BBLONG ftell_( FILE* stream ) {
 int ftruncate_(FILE* stream, BBLONG size) {
 	return ftruncate(fileno(stream), size);
 }
+#ifndef __APPLE__
+int clock_gettime_(int id, struct timespec * spec) {
+	return clock_gettime(id, spec);
+}
+#endif
+#endif
+
+#ifdef __APPLE__
+#include <mach/mach_time.h>
+
+BBULONG mach_absolute_time_ns() {
+	static mach_timebase_info_data_t s_timebase_info;
+	static int inited = 0;
+	if (!inited) {
+        mach_timebase_info(&s_timebase_info);
+    };
+	return (mach_absolute_time() * s_timebase_info.numer) / s_timebase_info.denom;
+}
 #endif
 
 int fclose_( FILE* stream ) {