git: plan9front

Download patch

ref: 2b8e615cfc98718314ddc1151934ef2f24db8de3
parent: 08babeb6de7bb4298c4683c1f57c235a8eda06d6
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Wed Feb 19 11:36:42 EST 2025

vmx: nanosec: replace the loop with a few div, mod and mul ops

--- a/sys/src/cmd/vmx/nanosec.c
+++ b/sys/src/cmd/vmx/nanosec.c
@@ -2,6 +2,8 @@
 #include <libc.h>
 #include <tos.h>
 
+#define Nsec 1000000000ULL
+
 /*
  * nsec() is wallclock and can be adjusted by timesync
  * so need to use cycles() instead, but fall back to
@@ -11,7 +13,7 @@
 nanosec(void)
 {
 	static uvlong fasthz, xstart;
-	uvlong x, div;
+	uvlong x;
 
 	if(fasthz == ~0ULL)
 		return nsec() - xstart;
@@ -18,21 +20,19 @@
 
 	if(fasthz == 0){
 		if(_tos->cyclefreq){
-			cycles(&xstart);
 			fasthz = _tos->cyclefreq;
+			cycles(&xstart);
 		} else {
-			xstart = nsec();
 			fasthz = ~0ULL;
-			fprint(2, "cyclefreq not available, falling back to nsec()\n");
-			fprint(2, "you might want to disable aux/timesync\n");
-			return 0;
+			xstart = nsec();
 		}
+		return 0;
 	}
 	cycles(&x);
 	x -= xstart;
 
-	/* this is ugly */
-	for(div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
+	uvlong q = x / fasthz;
+	uvlong r = x % fasthz;
 
-	return x / (fasthz / div);
+	return q*Nsec + r*Nsec/fasthz;
 }
--