This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

(libf2c) libu77 win32 dtime/etime fix


This patch fixes libg2c dtime and etime functions for all variants of
windows32. Also includes a small fix from Tim Prince for etime on
windows95. Been in use for a while, and should be well tested by now.

My last posting, which did not include Tim's fix, never made it in. 
Dave Love had been away and Craig probably too busy with other things, 
so here it is again.

Diff against 19990410 CVS. Bootstrap sanity check.

Mon Apr 12 00:46:26 1999  Mumit Khan  <khan@xraylith.wisc.edu>

	* libU77/dtime_.c: Handle all variants of WIN32.
	* libU77/etime_.c: Likewise.

Index: libU77/dtime_.c
===================================================================
RCS file: /homes/khan/src/CVSROOT/egcs-19990410/libf2c/libU77/dtime_.c,v
retrieving revision 1.1.1.1
diff -u -3 -p -r1.1.1.1 dtime_.c
--- libU77/dtime_.c	1999/04/10 23:56:14	1.1.1.1
+++ libU77/dtime_.c	1999/04/12 05:45:49
@@ -33,6 +33,11 @@ Boston, MA 02111-1307, USA.  */
 #  include <sys/time.h>
 #  include <sys/resource.h>
 #endif
+#if defined (_WIN32)
+#  include <windows.h>
+#  undef min
+#  undef max
+#endif
 #include <errno.h>		/* for ENOSYS */
 #include "f2c.h"
 
@@ -50,7 +55,73 @@ double G77_dtime_0 (tarray)
 double G77_dtime_0 (real tarray[2])
 #endif
 {
-#if defined (HAVE_GETRUSAGE) || defined (HAVE_TIMES)
+#if defined (_WIN32)
+  static int win32_platform = -1;
+
+  if (win32_platform == -1)
+    {
+      OSVERSIONINFO osv;
+      osv.dwOSVersionInfoSize = sizeof (osv);
+      GetVersionEx (&osv);
+      win32_platform = osv.dwPlatformId;
+    }
+  
+  /* We need to use this hack on non-NT platforms, where the first call
+     returns 0.0 and subsequent ones return the correct value. */
+  if (win32_platform != VER_PLATFORM_WIN32_NT)
+    {
+      static unsigned long long clock_freq;
+      static unsigned long long old_count;
+      unsigned long long count;
+      double delta;
+      LARGE_INTEGER counter_val;
+
+      if (clock_freq == 0)
+	{
+	  LARGE_INTEGER freq;
+	  if (! QueryPerformanceFrequency (&freq))
+	    {
+	      errno = ENOSYS;
+	      return 0.0;
+	    }
+	  else
+	    {
+	      clock_freq = ((unsigned long long) freq.HighPart << 32)
+                           + ((unsigned) freq.LowPart);
+	    }
+	}
+
+      if (! QueryPerformanceCounter (&counter_val))
+	return -1.0;
+
+      count = ((unsigned long long) counter_val.HighPart << 32)
+              + (unsigned) counter_val.LowPart;
+      delta = ((double) (count - old_count)) / clock_freq;
+      tarray[0] = (float) delta;
+      tarray[1] = 0.0;
+      old_count = count;
+    }
+  else
+    {
+      static unsigned long long old_utime, old_stime;
+      unsigned long long utime, stime;
+      FILETIME creation_time, exit_time, kernel_time, user_time;
+
+      GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
+		       &kernel_time, &user_time);
+      utime = ((unsigned long long) user_time.dwHighDateTime << 32) 
+	      + (unsigned) user_time.dwLowDateTime;
+      stime = ((unsigned long long) kernel_time.dwHighDateTime << 32) 
+	      + (unsigned) kernel_time.dwLowDateTime;
+
+      tarray[0] = (utime - old_utime) / 1.0e7;
+      tarray[1] = (stime - old_stime) / 1.0e7;
+      old_utime = utime;
+      old_stime = stime;
+    }
+  return tarray[0] + tarray[1];
+
+#elif defined (HAVE_GETRUSAGE) || defined (HAVE_TIMES)
   /* The getrusage version is only the default for convenience. */
 #ifdef HAVE_GETRUSAGE
   float utime, stime;
Index: libU77/etime_.c
===================================================================
RCS file: /homes/khan/src/CVSROOT/egcs-19990410/libf2c/libU77/etime_.c,v
retrieving revision 1.1.1.1
diff -u -3 -p -r1.1.1.1 etime_.c
--- libU77/etime_.c	1999/04/10 23:56:14	1.1.1.1
+++ libU77/etime_.c	1999/04/12 05:45:53
@@ -33,6 +33,11 @@ Boston, MA 02111-1307, USA.  */
 #  include <sys/time.h>
 #  include <sys/resource.h>
 #endif
+#if defined (_WIN32)
+#  include <windows.h>
+#  undef min
+#  undef max
+#endif
 #include <errno.h>		/* for ENOSYS */
 #include "f2c.h"
 
@@ -50,7 +55,72 @@ double G77_etime_0 (tarray)
 double G77_etime_0 (real tarray[2])
 #endif
 {
-#if defined (HAVE_GETRUSAGE) || defined (HAVE_TIMES)
+#if defined (_WIN32)
+  static int win32_platform = -1;
+  double usertime, systime;
+
+  if (win32_platform == -1)
+    {
+      OSVERSIONINFO osv;
+      osv.dwOSVersionInfoSize = sizeof (osv);
+      GetVersionEx (&osv);
+      win32_platform = osv.dwPlatformId;
+    }
+  
+  /* non-NT platforms don't have a clue as to how long a process has
+     been running, so simply return the uptime. Bad judgement call? */
+  if (win32_platform != VER_PLATFORM_WIN32_NT)
+    {
+      static unsigned long long clock_freq;
+      static unsigned long long old_count;
+      unsigned long long count;
+      LARGE_INTEGER counter_val;
+
+      if (clock_freq == 0)
+	{
+	  LARGE_INTEGER freq;
+	  if (! QueryPerformanceFrequency (&freq))
+	    {
+	      errno = ENOSYS;
+	      return 0.0;
+	    }
+	  else
+	    {
+	      clock_freq = ((unsigned long long) freq.HighPart << 32)
+                           + ((unsigned) freq.LowPart);
+	      if (! QueryPerformanceCounter (&counter_val))
+		return -1.0;
+	      old_count = ((unsigned long long) counter_val.HighPart << 32)
+	                  + (unsigned) counter_val.LowPart;
+	    }
+	}
+
+      if (! QueryPerformanceCounter (&counter_val))
+	return -1.0;
+
+      count = ((unsigned long long) counter_val.HighPart << 32)
+              + (unsigned) counter_val.LowPart;
+      tarray[0] = usertime = (double) (count - old_count) / clock_freq;
+      tarray[1] = systime = 0.0;
+    }
+  else
+    {
+      FILETIME creation_time, exit_time, kernel_time, user_time;
+      unsigned long long utime, stime;
+
+      GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
+		       &kernel_time, &user_time);
+      utime = ((unsigned long long) user_time.dwHighDateTime << 32)
+	      + (unsigned) user_time.dwLowDateTime;
+      stime = ((unsigned long long) kernel_time.dwHighDateTime << 32)
+	      + (unsigned) kernel_time.dwLowDateTime;
+
+      tarray[0] = usertime = utime / 1.0e7;
+      tarray[1] = systime = stime / 1.0e7;
+  }
+  return usertime + systime;
+
+#elif defined (HAVE_GETRUSAGE) || defined (HAVE_TIMES)
   /* The getrusage version is only the default for convenience. */
 #ifdef HAVE_GETRUSAGE
   struct rusage rbuff;

Regards,
Mumit



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]