avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Re: Why is gcc promoting on an unsigned char?


From: Neil Johnson
Subject: Re: [avr-gcc-list] Re: Why is gcc promoting on an unsigned char?
Date: Tue, 23 Dec 2003 12:12:19 +0000 (GMT)

Hi,

I thnk Joerg has hit the nail on the head, but without explaining the
"why".  Here's my (brief) attempt.

Ok, here's your code:

>       data |= (unsigned char) (bit_is_set (DATAPIN, DATA) ? i : 0);

As far as the compiler is concerned what you have expressed in the
language is something like:

        Place in var "data" the bitwise OR of the current value of
        var "data" and with the result of a conditional expression:
                The conditional expression evaluates the predicate
                        "bit_is_set (DATAPIN, DATA)"
                If this evaluates to non-zero, the result of the
                expression is the current value of var "i", otherwise
                it is the value zero.

This is one helluva thing to say, when in practice what you, perhaps,
meant to say is what Joerg wrote:

> if (DATAPIN & DATA)
> {
>   data |= i;
> }

Which means something like:

        If the result of the expression "DATAPIN & DATA" is non-zero then
                bitwise OR the var "data" with var "i".

This is far more concise, and will allow the optimizer to do its work
better.

Unless, of course, you would like to pay for a more expensive optimizer
which might have more smarts to try and unravel what you wrote :-)

Cheers, and a Happy Christmas and Fun New Year to All!!
Neil

--
Neil Johnson :: Computer Laboratory :: University of Cambridge ::
http://www.njohnson.co.uk          http://www.cl.cam.ac.uk/~nej22
----  IEE Cambridge Branch: http://www.iee-cambridge.org.uk  ----


reply via email to

[Prev in Thread] Current Thread [Next in Thread]