D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7181 - Make bswap a recognized sequence, rather than an intrinsic
Summary: Make bswap a recognized sequence, rather than an intrinsic
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-12-29 02:15 UTC by Don
Modified: 2024-12-13 17:57 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Don 2011-12-29 02:15:03 UTC
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.
Comment 1 bearophile_hugs 2011-12-29 04:21:37 UTC
(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).
Comment 2 Don 2011-12-29 09:29:47 UTC
(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 );
}
Comment 3 Andrei Alexandrescu 2011-12-29 12:23:38 UTC
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 );
Comment 4 Andrei Alexandrescu 2011-12-29 12:25:27 UTC
(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.
Comment 5 Walter Bright 2012-04-16 02:17:30 UTC
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
Comment 6 dlangBugzillaToGithub 2024-12-13 17:57:24 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18391

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB