[fpc-devel] Do bitwise operation (1 or 2) acre about the sign ? (Giving sign related hints on compilation)

Jonas Maebe jonas.maebe at elis.ugent.be
Mon Jan 14 15:10:59 CET 2013


On 14 Jan 2013, at 14:11, Martin wrote:

> Having recently asked about and learned that it is mainly a hint  
> about performance. I have now another question about this hint:
>  project1.lpr(6,9) Hint: Mixing signed expressions and longwords  
> gives a 64bit result
>
> Actually not so much about the hint, as about the fact that in the  
> below example fpc extends the operands to 64 bits.
>
> program Project1;
> var
>  x: cardinal;
>  i, j: integer;
> begin
>  i:= x or j
> end.
>
> I understand the need for a 64 bit operation for +/-/</>/*/...
>
> But the "or" operator works bitwise, it does not treat the operands  
> as numbers which could either be signed or unsigned? Or does it? If  
> it does not, why does it need to use 64 bit?

Pascal evaluates expressions independent of how their result is used.  
The result of that "x or j" could just as well be assigned to a 64 bit  
variable, or e.g. passed to a function that is overloaded with both 32  
and 64 bit parameters. In those cases the result type of that  
expression would matter.

That said, the compiler contains an optimization pass that tries to  
remove 64 bit widenings on 32 bit platforms in case it turns out they  
were not necessary. It will also catch the above example and the  
generated code will only contain a 32 bit operations. Semantically,  
the result of that expression remains 64 bit though.


Jonas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20130114/7a576046/attachment.html>


More information about the fpc-devel mailing list