# HG changeset patch # User Augie Fackler # Date 2015-02-03 18:17:21 # Node ID fb93721cc22957e42f9b16d12c927826f564d419 # Parent e2bf959a5a0d5950df82de2698b25aa76243d790 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. diff --git a/mercurial/util.h b/mercurial/util.h --- a/mercurial/util.h +++ b/mercurial/util.h @@ -196,4 +196,17 @@ static inline void putbe32(uint32_t x, c 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_ */