util: add getbefloat64
authorAugie Fackler <augie@google.com>
Tue, 03 Feb 2015 13:17:21 -0500
changeset 24016 fb93721cc229
parent 24015 e2bf959a5a0d
child 24017 72c9b5ae7278
util: add getbefloat64 As far as I can tell, this is wrong. double's format isn't strictly specified in the C standard, but the wikipedia article implies that platforms implementing optional Annex F "IEC 60559 floating-point arithmetic" will work correctly. My local C experts believe doing *((double *) &t) is a strict aliasing violation, and that using a union is also one. Doing memcpy appears to be the least-undefined behavior possible.
mercurial/util.h
--- a/mercurial/util.h	Tue Jan 20 14:09:57 2015 -0500
+++ b/mercurial/util.h	Tue Feb 03 13:17:21 2015 -0500
@@ -196,4 +196,17 @@
 	c[3] = (x) & 0xff;
 }
 
+static inline double getbefloat64(const char *c)
+{
+	const unsigned char *d = (const unsigned char *)c;
+	double ret;
+	int i;
+	uint64_t t = 0;
+	for (i = 0; i < 8; i++) {
+		t = (t<<8) + d[i];
+	}
+	memcpy(&ret, &t, sizeof(t));
+	return ret;
+}
+
 #endif /* _HG_UTIL_H_ */