www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7181] New: Make bswap a recognized sequence, rather than an intrinsic

reply d-bugmail puremagic.com writes:
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



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: -------
Dec 29 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7181


bearophile_hugs eml.cc changed:

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




 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: -------
Dec 29 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7181






 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: -------
Dec 29 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7181


Andrei Alexandrescu <andrei metalanguage.com> changed:

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



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: -------
Dec 29 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7181




12:25:27 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.
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: -------
Dec 29 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7181


Walter Bright <bugzilla digitalmars.com> changed:

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



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: -------
Apr 16 2012