changeset 24016:fb93721cc229

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.
author Augie Fackler <augie@google.com>
date Tue, 03 Feb 2015 13:17:21 -0500
parents e2bf959a5a0d
children 72c9b5ae7278
files mercurial/util.h
diffstat 1 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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_ */