Thread overview
[Issue 7181] New: Make bswap a recognized sequence, rather than an intrinsic
Dec 29, 2011
Don
Dec 29, 2011
Don
Apr 16, 2012
Walter Bright
December 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7181

           Summary: Make bswap a recognized sequence, rather than an
                    intrinsic
           Product: D
           Version: D1 & D2
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: clugdbug@yahoo.com.au


--- Comment #0 from Don <clugdbug@yahoo.com.au> 2011-12-29 02:15:03 PST ---
In the same way that abs, rol and ror are recognized, bswap(int x)
could be identified from:

( x << 24 ) | ( x << 8 ) & 0xff0000 | ( x >> 8 ) & 0xff00 | ( x >> 24 );

and this would be completely portable.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7181


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #1 from bearophile_hugs@eml.cc 2011-12-29 04:21:37 PST ---
(In reply to comment #0)
> In the same way that abs, rol and ror are recognized, bswap(int x)
> could be identified from:
> 
> ( x << 24 ) | ( x << 8 ) & 0xff0000 | ( x >> 8 ) & 0xff00 | ( x >> 24 );
> 
> and this would be completely portable.

Even if this pattern gets recognized and optimized, I suggest to keep a bswap function in Phobos, to avoid writing all that bug-prone stuff (I'd like a rol/ror function pair too in Phobos).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7181



--- Comment #2 from Don <clugdbug@yahoo.com.au> 2011-12-29 09:29:47 PST ---
(In reply to comment #1)
> (In reply to comment #0)
> > In the same way that abs, rol and ror are recognized, bswap(int x)
> > could be identified from:
> > 
> > ( x << 24 ) | ( x << 8 ) & 0xff0000 | ( x >> 8 ) & 0xff00 | ( x >> 24 );
> > 
> > and this would be completely portable.
> 
> Even if this pattern gets recognized and optimized, I suggest to keep a bswap function in Phobos, to avoid writing all that bug-prone stuff (I'd like a rol/ror function pair too in Phobos).

Definitely. It would just change from:

int bswap(int); /* intrinsic */

into

int bswap(int x)
{
    return ( x << 24 ) | ( x << 8 ) & 0xff0000 | ( x >> 8 ) & 0xff00 | ( x >>
24 );
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7181


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei@metalanguage.com


--- Comment #3 from Andrei Alexandrescu <andrei@metalanguage.com> 2011-12-29 12:23:38 PST ---
Wonder if other patterns would need to be figured as well, e.g.

    auto p1 = cast(char*) &x;
    int y = void;
    auto p2 = cast(char*) &y;
    p2[0] = p1[3];
    p2[1] = p1[2];
    p2[2] = p1[1];
    p2[3] = p1[0];

Or (probably more realistically) patterns involving temporaries and 2
expressions:

    auto y = ( x << 24 ) | ( x << 8 ) & 0xff0000;
    y |= ( x >> 8 ) & 0xff00 | ( x >> 24 );

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=7181



--- Comment #4 from Andrei Alexandrescu <andrei@metalanguage.com> 2011-12-29 12:25:27 PST ---
(In reply to comment #2)
> (In reply to comment #1)
> > (In reply to comment #0)
> > > In the same way that abs, rol and ror are recognized, bswap(int x)
> > > could be identified from:
> > > 
> > > ( x << 24 ) | ( x << 8 ) & 0xff0000 | ( x >> 8 ) & 0xff00 | ( x >> 24 );
> > > 
> > > and this would be completely portable.
> > 
> > Even if this pattern gets recognized and optimized, I suggest to keep a bswap function in Phobos, to avoid writing all that bug-prone stuff (I'd like a rol/ror function pair too in Phobos).
> 
> Definitely. It would just change from:
> 
> int bswap(int); /* intrinsic */
> 
> into
> 
> int bswap(int x)
> {
>     return ( x << 24 ) | ( x << 8 ) & 0xff0000 | ( x >> 8 ) & 0xff00 | ( x >>
> 24 );
> }

I only now realized the meaning of this remark... took me some 10 minutes. Clever.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7181


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> 2012-04-16 02:17:30 PDT ---
The following is now recognized and replaced with bswap:

    (p[0]<<24)|(p[1]<<16)|(p[2]<<8)|(p[3]<<0)

where p is a pointer to a ubyte. I realize that there are many, many ways to write bswap, but this is the recognized one. (The operands to | can at least appear in any order.)

Also,

    (p[3]<<24)|(p[2]<<16)|(p[1]<<8)|(p[0]<<0)

is now recognized and replaced with *cast(uint*)p, at least for x86 byte
ordering.

What remains to be done is to provide such a body for core.bitop.bswap() and remove bswap from the compiler intrinsics.

For reference:

   http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------