[PATCH 6 of 8 faster-obsmarkers] getbefloat64: drop unnecessary memcpy
Julien Cristau
julien.cristau at logilab.fr
Tue Feb 3 14:12:08 UTC 2015
On Tue, Feb 3, 2015 at 23:00:38 +0900, Yuya Nishihara wrote:
> On Mon, 2 Feb 2015 11:11:37 -0500, Augie Fackler wrote:
> > I originally had written this, but then someone mumbled something
> > about strict aliasing rules in C. Since I have no idea if this is
> > actually legal, I left it as its own patch.
> >
> > On Mon, Feb 2, 2015 at 11:01 AM, Augie Fackler <raf at durin42.com> wrote:
> > > # HG changeset patch
> > > # User Martin von Zweigbergk <martinvonz at google.com>
> > > # Date 1421790379 28800
> > > # Tue Jan 20 13:46:19 2015 -0800
> > > # Branch stable
> > > # Node ID 22d73e82c50d1126c7640d960cf74fcf328add6c
> > > # Parent bbd5e20889d06d6dccc8b3e83946d65d0cc79570
> > > getbefloat64: drop unnecessary memcpy
> > >
> > > diff --git a/mercurial/util.h b/mercurial/util.h
> > > --- a/mercurial/util.h
> > > +++ b/mercurial/util.h
> > > @@ -199,14 +199,12 @@ static inline void putbe32(uint32_t x, c
> > > 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;
> > > + return *((const double*)&t);
>
> IIRC, common idiom is to use union { double, uint64_t }.
>
> But Python itself violates strict aliasing rules, so maybe we can expect
> that -fno-strict-aliasing or something is always set.
>
> https://hg.python.org/cpython/file/v2.7.9/configure#l5983
>
-fno-strict-aliasing can't guarantee correct alignment, though, so
unless you're sure the const char * is always 8-byte aligned you can't
just cast it. IOW, the union is probably safest.
Julien
--
Julien Cristau <julien.cristau at logilab.fr>
Logilab http://www.logilab.fr/
Informatique scientifique & gestion de connaissances
More information about the Mercurial-devel
mailing list