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]

Re: make sure struct real_value's bitfields are packed


On Mon, Mar 22, 2004 at 09:56:00AM +0100, Falk Hueffner wrote:
> Alexandre Oliva <aoliva@redhat.com> writes:
> 
> > On Mar 21, 2004, Roger Sayle <roger@eyesopen.com> wrote:
> >
> >> On 21 Mar 2004, Alexandre Oliva wrote:
> >> +#define REAL_EXP(REAL) \
> >> +  ((int)((REAL)->uexp ^ (unsigned int)(1 << (EXP_BITS - 1))) \
> >> +   - (1 << (EXP_BITS - 1)))
> >
> >> I hadn't come across this "(x ^ signbit) - signbit" idiom for
> >> implementing sign-extension before.  Nice.
> >
> > Yeah, I found it great as well when I learned about it in the binutils
> > list :-)
> >
> > Maybe the middle-end could recognize such sequences and attempt to
> > optimize them into shifts or other forms of sign extension?  Maybe
> > it already does? :-) Dunno, I haven't checked.
> 
> I don't really like the idiom. It's obfuscated and requires a large
> constant, which is costly on some targets. Plain old
> (x << EXP_BITS) >> EXP_BITS is much more readable and already gets
> recognized by gcc as sign extension.

That old familiar shift left, shift right might not work at all.  Even
if you know the number of bits in x.  According to ISO/IEC 9899:1999,
shift right of negative signed quantities is implementation defined.

$ cat /tmp/sign.c
int foo (int x)
{
  return ((x & 0xff) ^ 0x80) - 0x80;
}
$ gcc -S -O2 -fomit-frame-pointer /tmp/sign.c
$ cat sign.s
        .file   "sign.c"
        .text
        .p2align 4,,15
.globl foo
        .type   foo, @function
foo:
        movsbl  4(%esp),%eax
        ret
        .size   foo, .-foo
        .ident  "GCC: (GNU) 3.3 20030226 (prerelease) (SuSE Linux)"

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre


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