Source-Changes-D archive

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

Re: CVS commit: src/lib/libc/string



On Mon, 19 Apr 2010, Joerg Sonnenberger wrote:
> On Sun, Apr 18, 2010 at 10:51:35AM +0000, Alan Barrett wrote:
> > Log Message:
> > Bitwise operations on signed types are well-defined if the values
> > happen to be positive, and indeed the values here were guaranteed
> > to be positive, but some compilers complained anyway, so convert
> > the bitwise operations to arithmetic operations.
> 
> In that case use an explicit cast to the unsigned type. What you did is
> penalize the code for no good reason.

I expect the compiler to be able to figure out that some arithmetic
operations involving powers of 2 are equivalent to bitwise operations,
and to choose the most appropriate instructions.  This expectation
is not entirely unjustified; for example, I just verified that
"gcc -O2" emits identical code for the following two functions:

    int f(int i)
    {
        if (i < 0) return 0;
        return i >> 3;
    }

    int g(int i)
    {
        if (i < 0) return 0;
        return i / 8;
    }

--apb (Alan Barrett)


Home | Main Index | Thread Index | Old Index