Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2013-01

Question about hint in bool eval / Mixing signed expressions and longwords gives a 64bit result

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin

I know it's a hint. And I know that implies it may mean noting, and yet is still present....
Just trying to understand.
if I did "int-longword" (same for addition and reverse order) then I need a int64 to hold the result.

--- 1)
Yet on my 32 bit Win vista (testing with 2.7.1 [2012/12/09] for i386 ) fpc.exe -MObjFPC -Scghi -O1 -g -gl -vewnhi -l project1.lpr

This gives no hint or warning (ignoring the "uninitialized" ones). It still gives none, even if I add " -Cro "

program Project1;
var
   a: LongWord;
   b: Integer;
begin
   b := a - b;  // (+$ffffffff) - ( -$7fffffff ) => can not be hold in
"b" integer
   b := b - a;
end.

Given that this may actually truncate data (or give a run-error), IMHO a warning would be ok.

--- 2)
But then, if I compare the 2 values, resulting in a boolean value (no risk of data truncation), then I get the hint
project1.lpr(7,10) Hint: Mixing signed expressions and longwords gives a 64bit result

program Project1;
var
   a: LongWord;
   b: Integer;
   c: Boolean;
begin
   c := a < b;
end.

In this case, I don't even see why a hint is needed? (I might overlook something).

Reply by email

Jonas Maebe
to Martin
Martin wrote 26 lines
I know it's a hint. And I know that implies it may mean noting, and yet is still present.... Just trying to understand. if I did "int-longword" (same for addition and reverse order) then I need a int64 to hold the result. --- 1) Yet on my 32 bit Win vista (testing with 2.7.1 [2012/12/09] for i386 ) fpc.exe -MObjFPC -Scghi -O1 -g -gl -vewnhi -l project1.lpr This gives no hint or warning (ignoring the "uninitialized" ones). It still gives none, even if I add " -Cro " program Project1; var a: LongWord; b: Integer; begin b := a - b; // (+$ffffffff) - ( -$7fffffff ) => can not be hold in "b" integer b := b - a; end. Given that this may actually truncate data (or give a run-error), IMHO a warning would be ok.

No hint is given because those operations are not evaluated using 64 bit arithmetic. They are no more dangerous than subtracting two longwords or integers from each other, because the result of those operations may not be representable either as a longword/integer.

Show quoted text 17 lines
--- 2) But then, if I compare the 2 values, resulting in a boolean value (no risk of data truncation), then I get the hint project1.lpr(7,10) Hint: Mixing signed expressions and longwords gives a 64bit result program Project1; var a: LongWord; b: Integer; c: Boolean; begin c := a < b; end. In this case, I don't even see why a hint is needed? (I might overlook something).

The hint is because the compiler in this case converts the comparison to a 64 bit operation (otherwise it's impossible to evaluate that expression correctly), and 64 bit arithmetic is slower on 32 bit platforms than 32 bit arithmetic. The hint is only about the potential performance impact.

Jonas

Reply by email