shithub: pt2-clone

Download patch

ref: 6353bd43eba7f3fb2ad05a0d50e1d2aeb6840356
parent: d934fd2f446df35dc6408b129dce2e439f8d6e3b
author: Olav Sørensen <olav.sorensen@live.no>
date: Sun Feb 27 08:52:41 EST 2022

Added more bits for fractional part of HPC timers

--- a/src/pt2_hpc.c
+++ b/src/pt2_hpc.c
@@ -1,5 +1,5 @@
 /*
-** High Performance Counter delay routines
+** Hardware Performance Counter delay routines
 */
 
 #ifdef _WIN32
@@ -13,6 +13,10 @@
 #include <stdbool.h>
 #include "pt2_hpc.h"
 
+#define FRAC_BITS 53
+#define FRAC_SCALE (1ULL << FRAC_BITS)
+#define FRAC_MASK (FRAC_SCALE-1)
+
 hpcFreq_t hpcFreq;
 
 #ifdef _WIN32 // Windows usleep() implementation
@@ -68,9 +72,9 @@
 	double dDurationInt;
 	double dDurationFrac = modf(dDuration, &dDurationInt);
 
-	// set 64:32 values
-	hpc->duration64Int = (uint64_t)floor(dDurationInt);
-	hpc->duration64Frac = (uint64_t)((dDurationFrac * (UINT32_MAX+1.0)) + 0.5); // rounded
+	// set 64:53fp values
+	hpc->duration64Int = (uint64_t)dDurationInt;
+	hpc->duration64Frac = (uint64_t)round(dDurationFrac * FRAC_SCALE);
 }
 
 void hpc_ResetEndTime(hpc_t *hpc)
@@ -113,9 +117,9 @@
 	hpc->endTime64Int += hpc->duration64Int;
 
 	hpc->endTime64Frac += hpc->duration64Frac;
-	if (hpc->endTime64Frac > UINT32_MAX)
+	if (hpc->endTime64Frac >= FRAC_SCALE)
 	{
-		hpc->endTime64Frac &= UINT32_MAX;
+		hpc->endTime64Frac &= FRAC_MASK;
 		hpc->endTime64Int++;
 	}
 }