This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RS6000 PATCH] Implement bswapsi2 intrinsic


Today appears to be a byte swapping day! :-)

The following patch provides an efficient implementation of GCC's
__builtin_bswap32 intrinsic for the PowerPC backend.

Henry Warren's book "Hacker's Delight" in chapter 7 on rearranging bits
and bytes contains the following paragraph:
> PowerPC can do the byte-reversal operation in only three instructions
> [Hay1]: A rotate left of 8, which positions two of the bytes, followed
> by two "rlwimi" (rotate left word immediate then mask insert)
> instructions.
>
> [Hay1] Hay R.W., Private Communication

The following patch implements this idiom.
Previously on powerpc-apple-darwin7.9.0 at -O2 we'd generate:

_foo:   mflr r0
        stw r0,8(r1)
        stwu r1,-64(r1)
        bl ___bswapsi2
        addi r1,r1,64
        lwz r0,8(r1)
        mtlr r0
        blr

With the patch below, we now generate:

_foo:   mr r0,r3
        rlwinm r3,r3,8,0xffffffff
        rlwimi r3,r0,24,0,7
        rlwimi r3,r0,24,16,23
        blr


Even as a middle-end maintainer, zero_extact makes me nervous due to the
influences of target bit and byte ordering.  However the code below
produces the correct results on powerpc-apple-darwin7.9.0, and should be
portable to other endian PowerPC systems [fingers crossed].

The following patch has been tested powerpc-apple-darwin7.9.0 with a full
"make bootstrap", all default languages, and regression tested with a
top-level "make -k check" with no new failures.

Ok for mainline?


2007-02-09  Roger Sayle  <roger@eyesopen.com>

        * config/rs6000/rs6000.md (bswapsi2): New define_expand.

        * gcc.target/powerpc/builtin-bswap-1.c: New test case.


Roger
--

Attachment: patchb.txt
Description: Text document

Attachment: builtin-bswap-1.c
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]