shithub: sl

Download patch

ref: df22ae34a79a76ed07090be41fce329b8d3cfe6c
parent: 152e9ed832a6d014439ff3fa16ce5230135751c6
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Thu Feb 6 18:51:49 EST 2025

plan 9: simpler and more precise(?) nanosec_monotonic

--- a/src/plan9/sys.c
+++ b/src/plan9/sys.c
@@ -2,14 +2,16 @@
 #include "timefuncs.h"
 #include <tos.h>
 
+#define Nsec 1000000000ULL
+
 double D_PNAN, D_PINF;
 
 double
 sec_realtime(void)
 {
-	vlong t = nsec();
-	vlong ns = t % 1000000000LL;
-	vlong s = (t - ns) / 1000000000LL;
+	uvlong t = nsec();
+	uvlong ns = t % Nsec;
+	uvlong s = (t - ns) / Nsec;
 	return (double)s + (double)ns/1.0e9;
 }
 
@@ -18,11 +20,11 @@
  * so need to use cycles() instead, but fall back to
  * nsec() in case we can't
  */
-uint64_t
+u64int
 nanosec_monotonic(void)
 {
-	static uint64_t fasthz, xstart;
-	uint64_t x, div;
+	static u64int fasthz, xstart;
+	u64int x;
 
 	if(fasthz == ~0ULL)
 		return nsec() - xstart;
@@ -40,10 +42,10 @@
 	cycles(&x);
 	x -= xstart;
 
-	/* this is ugly */
-	for(div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
+	u64int q = x / fasthz;
+	u64int r = x % fasthz;
 
-	return x / (fasthz / div);
+	return q*Nsec + r*Nsec/fasthz;
 }
 
 void