shithub: qk1

Download patch

ref: dcf466b34d62b087c003ecc6347a43e247217b65
parent: 42681cb8ee1bab11448ddda478f5791b6b7699e9
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Feb 8 04:55:13 EST 2021

replace nsec() with nanosec(): less context switching

--- a/fns.h
+++ b/fns.h
@@ -54,6 +54,7 @@
 void	fppsgl(void);
 void	fppdbl(void);
 void	shutdown(void);
+uvlong	nanosec(void);
 
 #pragma	varargck	argpos	dprint	1
 #pragma varargck	argpos	fatal	1
--- a/mkfile
+++ b/mkfile
@@ -35,6 +35,7 @@
 	mathlib.$O\
 	menu.$O\
 	model.$O\
+	nanosec.$O\
 	net_dgrm.$O\
 	net_loop.$O\
 	net_main.$O\
--- /dev/null
+++ b/nanosec.c
@@ -1,0 +1,36 @@
+#include <u.h>
+#include <libc.h>
+#include <tos.h>
+
+/*
+ * nsec() is wallclock and can be adjusted by timesync
+ * so need to use cycles() instead, but fall back to
+ * nsec() in case we can't
+ */
+uvlong
+nanosec(void)
+{
+	static uvlong fasthz, xstart;
+	uvlong x, div;
+
+	if(fasthz == ~0ULL)
+		return nsec() - xstart;
+
+	if(fasthz == 0){
+		if(_tos->cyclefreq){
+			fasthz = _tos->cyclefreq;
+			cycles(&xstart);
+		} else {
+			fasthz = ~0ULL;
+			xstart = nsec();
+		}
+		return 0;
+	}
+	cycles(&x);
+	x -= xstart;
+
+	/* this is ugly */
+	for(div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
+
+	return x / (fasthz / div);
+}
--- a/qk1.c
+++ b/qk1.c
@@ -78,7 +78,7 @@
 
 	if(t0 == 0.0)
 		t0 = time(nil);
-	return nsec() / 1000000000.0 - t0;
+	return nanosec() / 1000000000.0 - t0;
 }
 
 void
--- a/qw/mkfile
+++ b/qw/mkfile
@@ -38,6 +38,7 @@
 	md4.$O\
 	menu.$O\
 	model.$O\
+	nanosec.$O\
 	net_chan.$O\
 	net_udp.$O\
 	nonintel.$O\
@@ -75,6 +76,7 @@
 	cvar.$O\
 	mathlib.$O\
 	md4.$O\
+	nanosec.$O\
 	net_chan.$O\
 	net_udp.$O\
 	pmove.$O\
--- /dev/null
+++ b/qw/nanosec.c
@@ -1,0 +1,36 @@
+#include <u.h>
+#include <libc.h>
+#include <tos.h>
+
+/*
+ * nsec() is wallclock and can be adjusted by timesync
+ * so need to use cycles() instead, but fall back to
+ * nsec() in case we can't
+ */
+uvlong
+nanosec(void)
+{
+	static uvlong fasthz, xstart;
+	uvlong x, div;
+
+	if(fasthz == ~0ULL)
+		return nsec() - xstart;
+
+	if(fasthz == 0){
+		if(_tos->cyclefreq){
+			fasthz = _tos->cyclefreq;
+			cycles(&xstart);
+		} else {
+			fasthz = ~0ULL;
+			xstart = nsec();
+		}
+		return 0;
+	}
+	cycles(&x);
+	x -= xstart;
+
+	/* this is ugly */
+	for(div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
+
+	return x / (fasthz / div);
+}
--- a/qw/snd.c
+++ b/qw/snd.c
@@ -586,8 +586,8 @@
 		}
 	}
 	if(sndt == 0)
-		sndt = nsec() - Te9 / Fpsmax;
-	nsamp = (nsec() - sndt) / (Te9 / Srate);
+		sndt = nanosec() - Te9 / Fpsmax;
+	nsamp = (nanosec() - sndt) / (Te9 / Srate);
 	if(!cls.timedemo)
 		nsamp = nsamp + 15 & ~15;
 	if(nsamp > Ssamp)
@@ -600,7 +600,7 @@
 		fprint(2, "sndwrite: %r\n");
 		shutsnd();
 	}
-	sndt = nsec();
+	sndt = nanosec();
 }
 
 void
--- a/qw/sys.c
+++ b/qw/sys.c
@@ -106,11 +106,7 @@
 double
 Sys_DoubleTime(void)
 {
-	static long secbase;
-
-	if(secbase == 0)
-		secbase = time(nil);
-	return nsec()/1000000000.0 - secbase;
+	return nanosec()/1000000000.0;
 }
 
 void
--- a/qw/sys.h
+++ b/qw/sys.h
@@ -25,5 +25,6 @@
 void Sys_SetFPCW(void);
 void Sys_Init(void);
 void initparm(quakeparms_t *);
+uvlong nanosec(void);
 
 void*	emalloc(ulong);
--- a/snd.c
+++ b/snd.c
@@ -561,8 +561,8 @@
 		}
 	}
 	if(sndt == 0)
-		sndt = nsec() - Te9 / Fpsmax;
-	nsamp = (nsec() - sndt) / (Te9 / Srate);
+		sndt = nanosec() - Te9 / Fpsmax;
+	nsamp = (nanosec() - sndt) / (Te9 / Srate);
 	if(!cls.timedemo)
 		nsamp = nsamp + 15 & ~15;
 	if(nsamp > Ssamp)
@@ -576,7 +576,7 @@
 		fprint(2, "sndwrite: %r\n");
 		shutsnd();
 	}
-	sndt = nsec();
+	sndt = nanosec();
 }
 
 void