Re: Integral conversions in C/C++

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

 



John Fine wrote:
> Andrew Haley wrote:
> 
>> Christian Böhme wrote:
>>  
>>
>>> If, as already assumed elsewhere, the 32 bit machine in question
>>> provided a 32 unsigned integer multiplication that produces a 64
>>> bit unsigned integer result, the compiler will _never_ emit code
>>> using this instruction based on information withheld by a standard
>>> conforming frontend that the type of the result _must_ be that
>>> of the operands and precluding the conversion to a type large
>>> enough such that the multiplication is closed.
>>>   
>>
>> Right: the compiler can use such an instruction, but it must discard
>> the upper part of the result.
>
> I don't know if any optimizer does pay attention to this case, but the
> standard can't prevent an optimizer from doing so.
> 
> If you cast a 32bit value (int or unsigned int) to 64bit then
> immediately multiply it by another 32bit value,

That's not what the example did.

> the standard meaning is
> to do a 64bit by 64bit multiply and keep only the low 64 bits of the
> 128bit result.

Right.

> But, if the machine has a 32bit by 32bit multiply that produces the
> identical 64bit result, the optimizer is free to use it.

Of course, by the usual "as if" rule.  

> The standard
> may forget that each input to the multiply only has 32 significant
> bits.  But the optimizer can remember that.  So it has two nominally
> 64bit values to multiply, which each only have 32 significant bits.  So
> it can know that the full 64bit result of a 32bit multiply would match
> the low 64bit result of a 64bit multiply.
> 
> So I hope the "_never_" stated above is actually incorrect.

It was correct, for the example as stated:

-->8---

#include <stdint.h>

// ...

int64_t a;
uint32_t b = 8;

a = -(b * 2u);

--->8---

Andrew.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux