public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* swap instruction generation
@ 2011-04-18 13:21 naga raj
  2011-04-18 18:59 ` Ian Lance Taylor
  0 siblings, 1 reply; 6+ messages in thread
From: naga raj @ 2011-04-18 13:21 UTC (permalink / raw)
  To: gcc-help

Hi,

  I need to emit a swap instruction from a embedded target which is
using Gcc-4.1.2.

  I am trying to emit a instruction which will perform following operation

  R0=0x12345678;

  swap R1,R0

  Then after this R1 should contain R1= 0x00007856;


 As there is no bswap rtl expression in Gcc-4.1.2 I have tried various
alternatives like:

 1. Used rotate rtl expression

  2. ashift,ashiftrt and or to generate the above instruction


 but compiler is not generating(emitting) swap instruction.


Please help me..


Thanks,
Nagaraju

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: swap instruction generation
  2011-04-18 13:21 swap instruction generation naga raj
@ 2011-04-18 18:59 ` Ian Lance Taylor
  2011-09-21  6:24   ` naga raj
  0 siblings, 1 reply; 6+ messages in thread
From: Ian Lance Taylor @ 2011-04-18 18:59 UTC (permalink / raw)
  To: naga raj; +Cc: gcc-help

naga raj <gnuuser.raj@gmail.com> writes:

>   I need to emit a swap instruction from a embedded target which is
> using Gcc-4.1.2.
>
>   I am trying to emit a instruction which will perform following operation
>
>   R0=0x12345678;
>
>   swap R1,R0
>
>   Then after this R1 should contain R1= 0x00007856;

Not 0x78563421?

>  As there is no bswap rtl expression in Gcc-4.1.2 I have tried various
> alternatives like:
>
>  1. Used rotate rtl expression
>
>   2. ashift,ashiftrt and or to generate the above instruction
>
>
>  but compiler is not generating(emitting) swap instruction.

Current gcc has a bswap RTL pattern which swaps bytes in a word.  It was
added in gcc 4.3.  That's the thing to use if your instruction actually
swaps bytes in a word.  For versions of gcc before that you will have to
use an intrinsic function.

Ian

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: swap instruction generation
  2011-04-18 18:59 ` Ian Lance Taylor
@ 2011-09-21  6:24   ` naga raj
  2011-09-21 13:27     ` Ian Lance Taylor
  0 siblings, 1 reply; 6+ messages in thread
From: naga raj @ 2011-09-21  6:24 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

Hi Ian,

   I am using Gcc-4.6.0 and I have used bswap RTL pattern for both SI
and HI modes to generate swapb & swaph instructions respectively.

  (define_insn "bswapsi2"
  [(set (match_operand:SI 0 "register_operand" "=r")
        (bswap:SI (match_operand:SI 1 "register_operand" "r")))]
  ""
  "swapb %0, %1"
)

(define_insn "bswaphi2"
  [(set (match_operand:HI           0 "register_operand" "=r")
        (bswap:HI (match_operand:HI 1 "register_operand"  "r")))]
  ""
  "swaph %0, %1"
)



I have written a sample example to generate these instructions..
int swapb(int n)
{
  return ((((n) & 0xff000000) >> 24)
            | (((n) & 0x00ff0000) >>  8)
            | (((n) & 0x0000ff00) <<  8)
            | (((n) & 0x000000ff) << 24));

}
short int swaph(short int n)
{
return  ((((n) & 0xff00) >>  8)
            | (((n) & 0xff) <<  8));
}
int main()
{
  volatile int a=0x12345678;
   volatile short int b=0x1234;
  a=swapb(a);
  b=swaph(b);
 return 0;
}

with this example "swapb" instruction has generated but I am unable to
generate "swaph"(HI mode of bswap RTL pattern) instruction

I have tried all possibilities that I know.
Am I missing something or this approach is wrong.
Please guide me to generate swaph instruction.

Thanks in Advance,
Nag

On Tue, Apr 19, 2011 at 12:25 AM, Ian Lance Taylor <iant@google.com> wrote:
> naga raj <gnuuser.raj@gmail.com> writes:
>
>>   I need to emit a swap instruction from a embedded target which is
>> using Gcc-4.1.2.
>>
>>   I am trying to emit a instruction which will perform following operation
>>
>>   R0=0x12345678;
>>
>>   swap R1,R0
>>
>>   Then after this R1 should contain R1= 0x00007856;
>
> Not 0x78563421?
>
>>  As there is no bswap rtl expression in Gcc-4.1.2 I have tried various
>> alternatives like:
>>
>>  1. Used rotate rtl expression
>>
>>   2. ashift,ashiftrt and or to generate the above instruction
>>
>>
>>  but compiler is not generating(emitting) swap instruction.
>
> Current gcc has a bswap RTL pattern which swaps bytes in a word.  It was
> added in gcc 4.3.  That's the thing to use if your instruction actually
> swaps bytes in a word.  For versions of gcc before that you will have to
> use an intrinsic function.
>
> Ian
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: swap instruction generation
  2011-09-21  6:24   ` naga raj
@ 2011-09-21 13:27     ` Ian Lance Taylor
  2011-09-22  9:03       ` naga raj
  0 siblings, 1 reply; 6+ messages in thread
From: Ian Lance Taylor @ 2011-09-21 13:27 UTC (permalink / raw)
  To: naga raj; +Cc: gcc-help

naga raj <gnuuser.raj@gmail.com> writes:

>    I am using Gcc-4.6.0 and I have used bswap RTL pattern for both SI
> and HI modes to generate swapb & swaph instructions respectively.
>
>   (define_insn "bswapsi2"
>   [(set (match_operand:SI 0 "register_operand" "=r")
>         (bswap:SI (match_operand:SI 1 "register_operand" "r")))]
>   ""
>   "swapb %0, %1"
> )
>
> (define_insn "bswaphi2"
>   [(set (match_operand:HI           0 "register_operand" "=r")
>         (bswap:HI (match_operand:HI 1 "register_operand"  "r")))]
>   ""
>   "swaph %0, %1"
> )
>
>
>
> I have written a sample example to generate these instructions..
> int swapb(int n)
> {
>   return ((((n) & 0xff000000) >> 24)
>             | (((n) & 0x00ff0000) >>  8)
>             | (((n) & 0x0000ff00) <<  8)
>             | (((n) & 0x000000ff) << 24));
>
> }
> short int swaph(short int n)
> {
> return  ((((n) & 0xff00) >>  8)
>             | (((n) & 0xff) <<  8));
> }
> int main()
> {
>   volatile int a=0x12345678;
>    volatile short int b=0x1234;
>   a=swapb(a);
>   b=swaph(b);
>  return 0;
> }
>
> with this example "swapb" instruction has generated but I am unable to
> generate "swaph"(HI mode of bswap RTL pattern) instruction
>
> I have tried all possibilities that I know.
> Am I missing something or this approach is wrong.
> Please guide me to generate swaph instruction.

I'm surprised that you even get swapb when using a volatile variable.  I
would remove the volatile.  Just make them global variables or function
arguments or something.

Also you should make b an unsigned short, so that gcc doesn't have to
worry about sign extending the result when the expression is implicitly
calculated in type int.

Good luck.

Ian

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: swap instruction generation
  2011-09-21 13:27     ` Ian Lance Taylor
@ 2011-09-22  9:03       ` naga raj
  2011-09-22 16:08         ` Ian Lance Taylor
  0 siblings, 1 reply; 6+ messages in thread
From: naga raj @ 2011-09-22  9:03 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

Hi Ian,

On Wed, Sep 21, 2011 at 6:57 PM, Ian Lance Taylor <iant@google.com> wrote:
> naga raj <gnuuser.raj@gmail.com> writes:
>
>>    I am using Gcc-4.6.0 and I have used bswap RTL pattern for both SI
>> and HI modes to generate swapb & swaph instructions respectively.
>>
>>   (define_insn "bswapsi2"
>>   [(set (match_operand:SI 0 "register_operand" "=r")
>>         (bswap:SI (match_operand:SI 1 "register_operand" "r")))]
>>   ""
>>   "swapb %0, %1"
>> )
>>
>> (define_insn "bswaphi2"
>>   [(set (match_operand:HI           0 "register_operand" "=r")
>>         (bswap:HI (match_operand:HI 1 "register_operand"  "r")))]
>>   ""
>>   "swaph %0, %1"
>> )
>>
>>
>>
>> I have written a sample example to generate these instructions..
>> int swapb(int n)
>> {
>>   return ((((n) & 0xff000000) >> 24)
>>             | (((n) & 0x00ff0000) >>  8)
>>             | (((n) & 0x0000ff00) <<  8)
>>             | (((n) & 0x000000ff) << 24));
>>
>> }
>> short int swaph(short int n)
>> {
>> return  ((((n) & 0xff00) >>  8)
>>             | (((n) & 0xff) <<  8));
>> }
>> int main()
>> {
>>   volatile int a=0x12345678;
>>    volatile short int b=0x1234;
>>   a=swapb(a);
>>   b=swaph(b);
>>  return 0;
>> }
>>
>> with this example "swapb" instruction has generated but I am unable to
>> generate "swaph"(HI mode of bswap RTL pattern) instruction
>>
>> I have tried all possibilities that I know.
>> Am I missing something or this approach is wrong.
>> Please guide me to generate swaph instruction.
>
> I'm surprised that you even get swapb when using a volatile variable.  I
> would remove the volatile.  Just make them global variables or function
> arguments or something.
>
> Also you should make b an unsigned short, so that gcc doesn't have to
> worry about sign extending the result when the expression is implicitly
> calculated in type int.
>
 I have tried with all the possibilities that you have mentioned but
swaph instruction was not generated.
Instead of swaph compiler is generating

 1f0:   64640408        bslli   r3, r4, 8   (left shift )

 1f4:   64840008        bsrli   r4, r4, 8  (right shift)

 1f8:   80632000        or      r3, r3, r4 (or operation)


Thanks,
Nagaraju

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: swap instruction generation
  2011-09-22  9:03       ` naga raj
@ 2011-09-22 16:08         ` Ian Lance Taylor
  0 siblings, 0 replies; 6+ messages in thread
From: Ian Lance Taylor @ 2011-09-22 16:08 UTC (permalink / raw)
  To: naga raj; +Cc: gcc-help

naga raj <gnuuser.raj@gmail.com> writes:

>  I have tried with all the possibilities that you have mentioned but
> swaph instruction was not generated.
> Instead of swaph compiler is generating
>
>  1f0:   64640408        bslli   r3, r4, 8   (left shift )
>
>  1f4:   64840008        bsrli   r4, r4, 8  (right shift)
>
>  1f8:   80632000        or      r3, r3, r4 (or operation)

I have no other suggestions.  I do see bswap:HI used on x86_64 for this
input file.

int swapb(int n)
{
  return ((((n) & 0xff000000) >> 24)
            | (((n) & 0x00ff0000) >>  8)
            | (((n) & 0x0000ff00) <<  8)
            | (((n) & 0x000000ff) << 24));

}
short int swaph(unsigned short int n)
{
return  ((((n) & 0xff00) >>  8)
            | (((n) & 0xff) <<  8));
}
int main(int a, unsigned short b)
{
  a=swapb(a);
  b=swaph(b);
  return a + b;
}

Ian

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-09-22 16:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-18 13:21 swap instruction generation naga raj
2011-04-18 18:59 ` Ian Lance Taylor
2011-09-21  6:24   ` naga raj
2011-09-21 13:27     ` Ian Lance Taylor
2011-09-22  9:03       ` naga raj
2011-09-22 16:08         ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).