shithub: bench9

Download patch

ref: a7f13757d67c3169a938c95d2db667d1bd4ee407
parent: bbdcad5c85ca1a0daa21bf1f8f70ea9394cb7a02
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun Nov 1 09:26:11 EST 2020

clean up assembly mess

--- a/b.c
+++ b/b.c
@@ -19,11 +19,8 @@
 
 Res cpuid(ulong ax, ulong cx);
 
-void _tstart(uvlong *c);
 void _tend(uvlong *c);
-void _tstartp(uvlong *c);
 void _tendp(uvlong *c);
-void (*tstart)(uvlong *c);
 void (*tend)(uvlong *c);
 
 uvlong
@@ -66,15 +63,9 @@
 	fmtinstall(L'σ', σfmt);
 	fmtinstall(L'τ', τfmt);
 
-	if(tstart == nil){
+	if(tend == nil){
 		r = cpuid(0x80000001, 2);
-		if((r.dx & (1<<27)) != 0){
-			tstart = _tstartp;
-			tend = _tendp;
-		}else{
-			tstart = _tstart;
-			tend = _tend;
-		}
+		tend = (r.dx & (1<<27)) != 0 ? _tendp : _tend;
 	}
 
 	memset(b, 0, sizeof(*b));
@@ -87,8 +78,8 @@
 		uvlong v;
 		for(i = 0, v = 0; i < 100; i++){
 			uvlong s, e;
-			_tstart(&s);
-			_tend(&e);
+			tstart(&s);
+			tend(&e);
 			v += e - s;
 		}
 		adj = v / i;
--- a/b.h
+++ b/b.h
@@ -24,7 +24,7 @@
 
 /* private */
 void benchstep(B *b);
-extern void (*tstart)(uvlong *c);
+void tstart(uvlong *c);
 extern void (*tend)(uvlong *c);
 
 /* public */
--- a/b_amd64.s
+++ b/b_amd64.s
@@ -1,4 +1,6 @@
-TEXT _tstart(SB), 1, $0
+#define RDTSCP WORD $0x010f; BYTE $0xf9
+
+TEXT tstart(SB), 1, $0
 	CPUID
 	RDTSC
 	MOVL AX, 0(RARG)
@@ -6,21 +8,15 @@
 	RET
 
 TEXT _tend(SB), 1, $0
-	CPUID
+	/* it needs MOVL CR0, AX; MOV AX, CR0 but it's userspace */
 	RDTSC
 	MOVL AX, 0(RARG)
 	MOVL DX, 4(RARG)
-	RET
-
-TEXT _tstartp(SB), 1, $0
 	CPUID
-	WORD $0x010f; BYTE $0xf9 // RDTSCP
-	MOVL AX, 0(RARG)
-	MOVL DX, 4(RARG)
 	RET
 
 TEXT _tendp(SB), 1, $0
-	WORD $0x010f; BYTE $0xf9 // RDTSCP
+	RDTSCP
 	MOVL AX, 0(RARG)
 	MOVL DX, 4(RARG)
 	CPUID