Patchwork [3,of,8,faster-obsmarkers] util: add getbefloat64

login
register
mail settings
Submitter Augie Fackler
Date Feb. 2, 2015, 4:01 p.m.
Message ID <1810456c6b6759220847.1422892884@arthedain.pit.corp.google.com>
Download mbox | patch
Permalink /patch/7597/
State Superseded
Headers show

Comments

Augie Fackler - Feb. 2, 2015, 4:01 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1421781073 18000
#      Tue Jan 20 14:11:13 2015 -0500
# Branch stable
# Node ID 1810456c6b6759220847e4fa5b3c7a3b4a3af138
# Parent  20ac130001c176b0fbc2c99d9fe3741df3b71bca
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.

Someone else should probably figure out how to make this correct.
Martin von Zweigbergk - Feb. 2, 2015, 6:25 p.m.
On Mon Feb 02 2015 at 8:04:59 AM Augie Fackler <raf@durin42.com> wrote:

> # HG changeset patch
> # User Augie Fackler <augie@google.com>
> # Date 1421781073 18000
> #      Tue Jan 20 14:11:13 2015 -0500
> # Branch stable
> # Node ID 1810456c6b6759220847e4fa5b3c7a3b4a3af138
> # Parent  20ac130001c176b0fbc2c99d9fe3741df3b71bca
> 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.
>

That seems to be new in C99 (
http://en.wikipedia.org/wiki/C99#IEEE.C2.A0754_floating_point_support). Do
we care about C89? If we do, can we simply conditionally not compile this
on platforms that use non-IEEE-754 floating point (if any)?


> Someone else should probably figure out how to make this correct.
>
> 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_ */
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel
>
Augie Fackler - Feb. 2, 2015, 6:26 p.m.
On Mon, Feb 2, 2015 at 1:25 PM, Martin von Zweigbergk
<martinvonz@google.com> wrote:
> That seems to be new in C99
> (http://en.wikipedia.org/wiki/C99#IEEE.C2.A0754_floating_point_support). Do
> we care about C89? If we do, can we simply conditionally not compile this on
> platforms that use non-IEEE-754 floating point (if any)?


I thought about that some, and came to the conclusion that someone for
whom this matters can send us the necessary ifdefs for their platform.
My guess is it'll never happen.
Martin von Zweigbergk - Feb. 2, 2015, 6:29 p.m.
On Mon Feb 02 2015 at 10:26:50 AM Augie Fackler <raf@durin42.com> wrote:

> On Mon, Feb 2, 2015 at 1:25 PM, Martin von Zweigbergk
> <martinvonz@google.com> wrote:
> > That seems to be new in C99
> > (http://en.wikipedia.org/wiki/C99#IEEE.C2.A0754_floating_point_support).
> Do
> > we care about C89? If we do, can we simply conditionally not compile
> this on
> > platforms that use non-IEEE-754 floating point (if any)?
>
>
> I thought about that some, and came to the conclusion that someone for
> whom this matters can send us the necessary ifdefs for their platform.
> My guess is it'll never happen.
>

Yes, exactly what I mean. And I also expect it to never happen :-)

Patch

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_ */