Lazarus

Programming => General => Topic started by: process_1 on August 05, 2020, 12:33:58 pm

Title: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 05, 2020, 12:33:58 pm
If the variable is already QWORD, no point for the warning:

Code: Pascal  [Select][+][-]
  1. var
  2.   qw: QWORD;
  3. begin
  4.  
  5.   // qw:= $FFFFFFFFFFFFFFFF;  // By compiler it is -1 and it shows range check warning, literals are treated as int64?
  6.   qw:= QWORD($FFFFFFFFFFFFFFFF); // This will pass with exact cast, no warning
  7.  
  8.   writeln(inttostr(qw));
  9.  
  10.  
                                   
Title: Re: Implicit converison, literals and QWORD variable issues
Post by: jamie on August 05, 2020, 12:58:06 pm
Yes old problem that will never get fixed.
Untyped numeric constants are supposed  adapt to the type on the left.

That also goes for doing math within a prototype parameter with constants, the compiler converts it to integer and looks for the overload for that instead of keeping with the starting type on the left.
Basically things like byte word parameters change over to integer types.

 As much as I hate Delphi lately I can say it handles this correctly.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 05, 2020, 01:14:24 pm
But:

Code: Pascal  [Select][+][-]
  1.  
  2.   if qw < 0 then  // Warning: Comparison might be always false due to range of constant and expression
  3.      Writeln('QWORD variable s less than 0!?!');   // Warning: unreachable code
  4.  
  5.   if word($FFFFFFFFFFFFFFFF) < 0 then
  6.      Writeln('Cast unsigned QWORD is less than 0!?!');    // Warning: unreachable code
  7.  
  8.   if $FFFFFFFFFFFFFFFF < 0 then
  9.      Writeln('Uncast literal $FFFFFFFFFFFFFFFF is treated as -1 !');  // It is always executed
  10.  
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: jamie on August 05, 2020, 01:33:54 pm
But:

Code: Pascal  [Select][+][-]
  1.  
  2.   if qw < 0 then  // Warning: Comparison might be always false due to range of constant and expression
  3.      Writeln('QWORD variable s less than 0!?!');   // Warning: unreachable code
  4.  
  5.   if word($FFFFFFFFFFFFFFFF) < 0 then
  6.      Writeln('Cast unsigned QWORD is less than 0!?!');    // Warning: unreachable code
  7.  
  8.   if $FFFFFFFFFFFFFFFF < 0 then
  9.      Writeln('Uncast literal $FFFFFFFFFFFFFFFF is treated as -1 !');  // It is always executed
  10.  

Yes of course. That is the only time it should dominate.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: PascalDragon on August 05, 2020, 02:45:29 pm
If the variable is already QWORD, no point for the warning:

Code: Pascal  [Select][+][-]
  1. var
  2.   qw: QWORD;
  3. begin
  4.  
  5.   // qw:= $FFFFFFFFFFFFFFFF;  // By compiler it is -1 and it shows range check warning, literals are treated as int64?
  6.   qw:= QWORD($FFFFFFFFFFFFFFFF); // This will pass with exact cast, no warning
  7.  
  8.   writeln(inttostr(qw));
  9.  
  10.  
                                 

Pascal languages prefer signed types to unsigned types thus such a constant is treated as Int64 instead of QWord. This is by design.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 05, 2020, 03:38:41 pm
Pascal languages prefer signed types to unsigned types thus such a constant is treated as Int64 instead of QWord. This is by design.

What is the behavior of recent Delphi release for such clauses?

But, is this the point of such ridicilous warnings that compiler have to teach me what I want  (???) , or that I tell to compiler exactly what I want?

For example, such  unsigned 64-bit constants I used as:

Code: Pascal  [Select][+][-]
  1. const
  2.   c64: array [0..1] of QWord =
  3.     (
  4.     $0000000000000000,
  5.     $FFFFFFFFFFFFFFFF
  6.     );
  7.  

Certainly, this implies that compiler is not smart at all if he force me or warining me to write something like this:

Code: Pascal  [Select][+][-]
  1. const
  2.   c64: array [0..1] of QWord =
  3.     (
  4.     QWord($0000000000000000),
  5.     QWord($FFFFFFFFFFFFFFFF)
  6.     );
  7.  

That is insane, as I aleady expicitly told to compiler what type of data I use.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: PascalDragon on August 05, 2020, 03:59:33 pm
But, is this the point of such ridicilous warnings that compiler have to teach me what I want  (???) , or that I tell to compiler exactly what I want?

Best read the comments of this (https://bugs.freepascal.org/view.php?id=34864) bug report which was essentially about the same topic.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 05, 2020, 08:06:40 pm
Best read the comments of this (https://bugs.freepascal.org/view.php?id=34864) bug report which was essentially about the same topic.

And this bug report was rejected?

The Rio is the current version of Delphi, AFAIK, then:

http://docwiki.embarcadero.com/RADStudio/Rio/en/Declared_Constants

Here, the 64-bit native integer type is marked as such and UInt64 is also eqivalent type. This should meant that this should never be executed:

Code: Pascal  [Select][+][-]
  1.   if $FFFFFFFFFFFFFFFF < 0 then // This should'n give warning  on Rio
  2.      Writeln('Uncast literal $FFFFFFFFFFFFFFFF is treated as -1 !');  // It should never be executed on Rio
  3.  

Can anyone who have Rio confirm all this?

If the compiler support Uint64 natively, then that is the highest integer type, not int64. Decision that that is int64 have no sense at all.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: winni on August 05, 2020, 10:04:54 pm

If the compiler support Uint64 natively, then that is the highest integer type, not int64. Decision that that is int64 have no sense at all.

Hi!

All the discussions are so liberal. We can discuss about anything.

And if someone - like you - checks that there is nonsense in the system :
It is allowed to say that.

But changes? You just said that one of The Ten Commandments is nonsense.

There will be no changes. We are here in a church . Not in a parliament!

Winni
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: PascalDragon on August 05, 2020, 10:07:59 pm
Best read the comments of this (https://bugs.freepascal.org/view.php?id=34864) bug report which was essentially about the same topic.

And this bug report was rejected?

Yes.

If the compiler support Uint64 natively, then that is the highest integer type, not int64. Decision that that is int64 have no sense at all.

Again: Pascal has a preference for signed types, thus it's irrelevant that an unsigned type exists that has the same width.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 05, 2020, 10:21:27 pm
There will be no changes. We are here in a church . Not in a parliament!

Yeap, I'm well aware of inquisition existance...

But, regarding the order of types, this decision have no sense at all, period. This is the "Holy Grail" during implicit conversion. Or not?

Even with Delphi mode is declared, behavior is the same, which breaks Delphi compatibility, if that is how this works on Rio according to standard type conventions order priorities.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: winni on August 05, 2020, 10:40:23 pm
Hi!

When N. Wirth made his first paper about Pascal he defines 4 simple types:
Integer, Real, Boolean, Char .

From the point of view "The rest is an subrange or an array of simple type" this is correct.
But the reality showed that there were two other kind of types which were badly needed:

The String and the Unsigned.

UCSD Pascal intodruced the String and the Byte
Turbo Pascal introduced the Word
Wirth himself introduced the Cardinal in Modula II. 
A lot of Pascal flavours have now a  Cardinal. Or a Word. Or both .

So Wirth recogniced his mistake.
Only fpc has  not recognized that.

Winni
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: ASBzone on August 05, 2020, 10:51:44 pm
So Wirth recogniced his mistake.
Only fpc has  not recognized that.


I'm not sure I get your point on this...
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 05, 2020, 11:01:37 pm
So Wirth recogniced his mistake.
Only fpc has  not recognized that.

I do not think that Wirth was involved with extensions mainly Borland/Embarcadero made. The Wirth probably hasn't had even a vision of object Pascal in his time, nor 64-bit CPUs. All what comes later is quite arbitrary, but based on some primary logic and order of things.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: winni on August 05, 2020, 11:06:54 pm

I'm not sure I get your point on this...

Hi!

Then the other way round:

Every bloody mistake and error of Delphi or Turbo Pascal must be copied.
To make it "compatible".

But if Delphi makes something correct (seldom enough) this is not adapted:
Code: Text  [Select][+][-]
  1. $FFFFFFFFFFFFFFFF  18446744073709551615    UInt64

From http://docwiki.embarcadero.com/RADStudio/Rio/en/Declared_Constants (http://docwiki.embarcadero.com/RADStudio/Rio/en/Declared_Constants)

Winni
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: ASBzone on August 05, 2020, 11:28:56 pm
But if Delphi makes something correct (seldom enough) this is not adapted:
Code: Text  [Select][+][-]
  1. $FFFFFFFFFFFFFFFF  18446744073709551615    UInt64

From http://docwiki.embarcadero.com/RADStudio/Rio/en/Declared_Constants (http://docwiki.embarcadero.com/RADStudio/Rio/en/Declared_Constants)

Winni


Well, from my reading of the bug report and all its comments, I see that Delphi adopted the 64-bit unsigned integer *after* FPC already had it.

It is one thing to make things compatible with Delphi which are not yet existent in FPC.   It's a whole other thing to do so when something already exists in FPC, and will have an impact on backwards compatibility of existing code.

Some consideration should probably be given (as also noted in the bug tracker thread) to making it compatible in Delphi mode, but I also think that there could probably be a bit more clarity about exactly what version of Delphi compatibility is the target for all compatibility, as that has a bearing as well.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: jamie on August 05, 2020, 11:50:34 pm
If using Delphi Mode would actually make Fpc compile like Delphi I would have it on by default with anything I do..
 
 I checked 3.2.0 and I see the bugs are still there with type resolving changing, calling the wrong overload when doing math within a Prototype header of a function/procedure

 on top of that I still get the "Can't determine which procedure/function to call "

 Today I used Delphi at work for at least 3 hours and I must say I hate the IDE environment of it but the code I wrote compiled and actually worked without casting the hell out of the code all over the place to ensure it calls the correct overload and does not complain about it not knowing which overload to call.

 You see with Delphi it knows enough to make the final result type to same as it was when entering the equation, executing it and finalizing it on exit thus keeping the type the same as it was when started.

 This of course aids the compiler in searching for the correct overload to execute if there is more then one copy of that signature .

 As a Recap on this..
Code: Pascal  [Select][+][-]
  1. Var
  2.  W:WORD;
  3. begin
  4.  W:= $FF;
  5.   Result := HI(W+1);
  6. End;
  7.  
The results should be 1 but its 0;


Now I shouldn't have to be forced to WORD(W+1); the compiler should know it was a WORD when it started and it should be a WORD type when it's finished. It does not have to make changes in the background as to how to it does the math because it makes no difference if the level shift it all to a Integer before doing the math, but the results must still be a WORD type because otherwise this causes the compiler to generate bad code in cases where coders are using types left than integer and not aware of this issue..



 
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 06, 2020, 09:50:50 am
Every bloody mistake and error of Delphi or Turbo Pascal must be copied.
To make it "compatible".

Not quite. With so many versions started from Turbo Pascal and numerous of bugs, that would be not possible.
What would be? Declaration for each version? I do not think so.

But with basic compiler behavior FPC involved his own bugs based on some questionable decisions, at least in this case.
Again, what is the basic rule about type promotions? To upgrade or downgrade it? That is what is questionable here.

Again: Pascal has a preference for signed types, thus it's irrelevant that an unsigned type exists that has the same width.

Actually, where that is wrote? Can you point relevant document?
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: PascalDragon on August 06, 2020, 10:32:44 am
Again: Pascal has a preference for signed types, thus it's irrelevant that an unsigned type exists that has the same width.

Actually, where that is wrote? Can you point relevant document?

For Free Pascal it's documented here (https://freepascal.org/docs-html/current/ref/refsu4.html#x26-260003.1.1) at the end in front of Boolean types (https://freepascal.org/docs-html/current/ref/refsu4.html#x26-270003.1.1) (also the remark in front of table 3.3). It also stems from points such as ISO Pascal only defining a signed integer type as required type.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: winni on August 06, 2020, 11:16:01 am
It also stems from points such as ISO Pascal only defining a signed integer type as required type.


ISO-Pascal is from 1990. The extended version (nobody uses hat) is from 1991.
It has no objects. It is just unusable.
You better take Turbo Pascal.

Another useless ISO standard.

Winni
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: marcov on August 06, 2020, 11:49:43 am
You better take Turbo Pascal.

Another useless ISO standard.

All PASCAL's have this, J&W too.  But there are good reasons for it (see e.g. https://stackoverflow.com/questions/12781434/why-do-delphi-and-free-pascal-usually-prefer-a-signed-integer-data-type-to-unsig/12782028#12782028 ), most notably in determining range and associated checking.  Lesser languages don't have those features (and note, in C too the base type is int and not unsigned int)

The simple solution is to never use arithmetic with the largest unsigned integer and only use it for bitfields etc.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: Thaddy on August 06, 2020, 11:50:22 am
ISO-Pascal is from 1990. The extended version (nobody uses hat) is from 1991.
It has no objects. It is just unusable.
The goal of the ISO standards was - and still is - to be able to write compilers on very different hardware and still maintain sourcecode compatibility.
Actually it is the same with C and for the same reasons.
BTW having no objects is really meaningless in that light. C has no objects.... Both ISO Pascal / C have proven to be VERY usable.
The only thing about the ISO modes is that they never got updated, but that does not mean much. E.g. for C the only relevant update was C99.

On subject: How would one express a signed or unsigned on an untyped const with the same bitpattern? A contradiction in terms.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: marcov on August 06, 2020, 11:54:48 am

But if Delphi makes something correct (seldom enough) this is not adapted:
Code: Text  [Select][+][-]
  1. $FFFFFFFFFFFFFFFF  18446744073709551615    UInt64

From http://docwiki.embarcadero.com/RADStudio/Rio/en/Declared_Constants (http://docwiki.embarcadero.com/RADStudio/Rio/en/Declared_Constants)

This is incorrect. That value is large than the signed integer type. IOW it is a hack, and range check for the type is probably broken.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 06, 2020, 12:21:24 pm

All PASCAL's have this, J&W too.  But there are good reasons for it (see e.g. https://stackoverflow.com/questions/12781434/why-do-delphi-and-free-pascal-usually-prefer-a-signed-integer-data-type-to-unsig/12782028#12782028 ), most notably in determining range and associated checking.  Lesser languages don't have those features (and note, in C too the base type is int and not unsigned int)

The simple solution is to never use arithmetic with the largest unsigned integer and only use it for bitfields etc.

Let assume FPC documentation and decision where wrong, and let focus on Delphi and Embarcadero decisions and documentations. Or better to focus on C/C++ standards as these standards are way mature. Currently there is no larger signed type than int64, but there is unsigned int64 (unt64) which is on the top on hiearchy, thus it is clear that uint64 literals should not be implicitly converted to int64, nor int32.

On C++:

Code: [Select]
#include <iostream>

using namespace std;

int main()
{
    if (0xFFFFFFFFFFFFFFFF < 0)
      cout<<"0xFFFFFFFFFFFFFFFF is less than 0!" ; 
    else
      cout<<"0xFFFFFFFFFFFFFFFF is unsigned!" ; // Always executable

    return 0;
}


In C is the same:

Code: [Select]
#include <stdio.h>

int main()
{
   
     if (0xFFFFFFFFFFFFFFFF < 0)
      printf("0xFFFFFFFFFFFFFFFF is less than 0!") ;
    else
      printf("0xFFFFFFFFFFFFFFFF is unsigned!") ; // Always executable

    return 0;
}

Anyone here read C/C++ standards? They can be considered as "Holy Grail" and the  best reference to others as they are the most mature.

For instance, in JAVA, there is no unsigned types by design, which is basic and an arbitrary decision of the Gosling,  Sheridan, and Naughton. Among missing pointers and endless revisions, that is its main flaw in design.

But here, in Pascal (as in C/C++), we have signed and unsigned integers and we know what is hierachy order for integer types and what to do with. Or we don't? I personaly believe FPC made a mess here with this arbitrary decision.

Once again, if someone have Rio, I would be interesting to see the result of upper code with literals.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: winni on August 06, 2020, 12:41:46 pm

This is incorrect. That value is large than the signed integer type. IOW it is a hack, and range check for the type is probably broken.

Hi!

I don't get you!?

The allways used logic is

MaxUnsingnedXX = 2 *  MaxIntXX +1;

Example: 16 Bit : MaxWord = 2 * MaxSmallInt +1 : 65535 = 2 * 32767 +1

The same for 64 bit :

MaxUnit64 = 2 * MaxInt64 + 1 : 18446744073709551615  = 2 * 9223372036854775807 + 1

Where please is the error????

Winni
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: Tomas Hajny on August 06, 2020, 12:42:37 pm
If the compiler support Uint64 natively, then that is the highest integer type, not int64. Decision that that is int64 have no sense at all.

Some questions:


With regard to number 2 above, the simplest case is as follows:
Code: [Select]
const
 HexConst = $FFFFFFFFFFFFFFFE;
begin
 WriteLn (HexConst);
end.

With regard to number 3 above, let's take the following example:
Code: [Select]
const
 HexConst = $FFFFFFFFFFFFFFFE;
var
 I: int64;
 Q: qword;
begin
 I := HexConst;
 Q := HexConst;
 I := I div 2;
 Q := Q div 2;
 WriteLn (I);
 WriteLn (Q);
end.

Obviously, the real case would be much more complicated and the programmer might not necessarily see the types involved during the calculation. The warning is there to point out that although the calculation sequence is exactly the same, there is the same constant on the input (having the same value everywhere) and the final result perfectly fits both the types involved, the final value is different (which may not be expected otherwise).
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: marcov on August 06, 2020, 12:59:04 pm
Let assume FPC documentation and decision where wrong,

Circular reasoning. It is wrong because we assume it is wrong?

and let focus on Delphi and Embarcadero decisions and documentations.

Good, let's.  So let's revisit the history, and what happened with the 32-bit equivalents. It is a mess that only got fixed in D4 when Delphi introduced int64.

So the question then is if Delphi now has a native int128 type that can push the boundery oncemore. Also, are you testing in 32-bit or 64-bit ? Are you testing with native Delphi (win32/win64?)  or LLVM.  64-bit might have an int128 (twice the wordsize), while 32-bit might not (that would be a harder to emulate twice the wordsize).

It might be that the LLVM mobile compilers have support for higher integer emulation, and native Delphi not.

Or better to focus on C/C++ standards as these standards are way mature. Currently there is no larger signed type than int64, but there is unsigned int64 (unt64) which is on the top on hiearchy, thus it is clear that uint64 literals should not be implicitly converted to int64, nor int32.

Bingo, success!   C has postfix designations for literals to assign them the right type. Pascal does the same by a typecast that is also allowed in e.g. constant declarations.

So basically then it is equivalent.

Anything else would be non orthogonal.

Quote
Anyone here read C/C++ standards? They can be considered as "Holy Grail" and the  best reference to others as they are the most mature.

Please make your case then with citations from standards rather than examples that might be implementation defined.


Quote
For instance, in JAVA, there is no signed types by design, which is basic and an arbitrary decision of the Gosling,  Sheridan, and Naughton. Among missing pointers and endless revisions, that is its main flaw in design.

So there is no way to have a negative number? How odd.


Quote
But here, in Pascal (as in C/C++), we have signed and unsigned integers and we know what is hierachy order for integer types and what to do with. Or we don't? I personaly believe FPC made a mess here with this arbitrary decision.

And the hierarchy is clear in Pascal. The largest signed type is the top of the of all, and literals have the type, the corresponding unsigned type doesn't fit in its range so is orphaned.

Solution: have emulation for a even larger signed type. Patches welcome.

Title: Re: Implicit converison, literals and QWORD variable warning
Post by: marcov on August 06, 2020, 01:04:53 pm
The allways used logic is

wrong.

Quote
MaxUnsingnedXX = 2 *  MaxIntXX +1;

Expressions of literals and integers have integers as range. The highest integer type is INTXX, the operation happens in INTXX (even emulated int64 on e.g. 32-bit x86) or register size, whatever is larger.  Hence the result is already fubared when assigning.

You are conflating abstract integer math like Windows "Calculator" with typed programming languages.   

Quote
Example: 16 Bit : MaxWord = 2 * MaxSmallInt +1 : 65535 = 2 * 32767 +1

Evaluated in the 32-bit base type (register size): works. This will probably go wrong on Turbo Pascal, but can be worked around by typecast one of the components to word (doesn't fit in 16-bit integer) , causing everything to be evaluated using emulated longint


These issues are not new, they are known from the 16-bit->32-bit transition too with all sizes divided by 2, which is why we have all the answers more or less prepared. Delphi has had 2 32-bit unsigned over the years (D4+32-bit dword and D2-D3 31-bit cardinal)

 
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: winni on August 06, 2020, 01:09:11 pm
Hi!

I did not talk about typecasting nd what fits in registers,
I did talk about simple multiplying with my brain! Gosh!

And do you know this:

MaxCardinal := 2 * MaxInt32 + 1  // brain, not registers
This is  from N. Wirth  himself. In Modula II.

Winni
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: marcov on August 06, 2020, 01:14:50 pm
I did not talk about typecasting nd what fits in registers,
I did talk about simple multiplying with my brain! Gosh!

Pascal is very fine, but not a mind reader that can guess intent. That's why there is a type system to guide such choices.

Quote
And do you know this:

MaxCardinal := 2 * MaxInt32 + 1  // brain, not registers
This is  from N. Wirth  himself. In Modula II.

But Modula-2 has CARDINAL (unsigned) as basetype, so is different. Moreover the fragment is a bit weird since in Modula2 you must always typecast with mixed types. Did this really compile, or have somebody written down something abstract (from the brain as you say) in superficial M2 notation?

 
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: winni on August 06, 2020, 01:28:01 pm
Hi!

Yes - in Modula II you always had to typecast Cardinal <--> Integer.

I only wanted to point out that N. Wirth in Modula II had corrected his error.

Winni
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 06, 2020, 01:54:14 pm
Circular reasoning. It is wrong because we assume it is wrong?

You are aware of technique called "Proof by contradiction" ?

Quote
Bingo, success!   C has postfix designations for literals to assign them the right type. Pascal does the same by a typecast that is also allowed in e.g. constant declarations.

Look these examples I posted in C and C++ and tell me what is the difference comparing to the same code with FPC.

Quote
So there is no way to have a negative number? How odd.

They have only signed numbers. My bad English (fixed). Anyone worked or studied JAVA know that weirdness.

Quote
And the hierarchy is clear in Pascal. The largest signed type is the top of the of all, and literals have the type, the corresponding unsigned type doesn't fit in its range so is orphaned.

How that can be? Signed type have 2nd complement N-1 bits mantissa (N bit, i.e. MBS is just a sign), while unsigned have N bits of mantissa? Let take a signed and unsigned 8-bit integer. What is on top 7 or 8 bit of mantissa? Or simply, what is higher 127 or 255?

If we stick on current logic for 64-bits with 64 bit CPUs, then this should be the case in FPC for 8-bit CPUs:

Code: Pascal  [Select][+][-]
  1.    if ($FF<0) then
  2.        writeln ("$FF literal is a negative number by FPC decision!")
  3.  

And it is not. Why again? Becuse it is wrong! Similar to 16, 32 and 64-bits CPUs and 128 in the future. Except now for current 64-bits CPUs is not the case. If you follow the logic, follow it consistently, always!

Warnings, as pointed in the firts post and Reply #5, are absolutely useless and the mess is to downgrade uint64 constant literals to int64. That is absolutely wrong.

Again, what is the case in Rio for the same 64-bit example code?
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: FPK on August 06, 2020, 02:28:26 pm

And it is not. Why again? Becuse it is wrong! Similar to 16, 32 and 64-bits CPUs and 128 in the future. Except now for current 64-bits CPUs is not the case. If you follow the logic, follow it consistently, always!

FPC is consistent, however consistency is not as simple as expected. Why? Because reality (hardware) is limited and CPUs cannot work with infinite large numbers. So constants out of the range of the largest signed type simple follow different rules.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: marcov on August 06, 2020, 02:39:34 pm
Circular reasoning. It is wrong because we assume it is wrong?

You are aware of technique called "Proof by contradiction" ?

You are familiar with the term "gaslighting" ?

Quote
Quote
Bingo, success!   C has postfix designations for literals to assign them the right type. Pascal does the same by a typecast that is also allowed in e.g. constant declarations.

Look these examples I posted in C and C++ and tell me what is the difference comparing to the same code with FPC.

You post runtime results. These maybe implementation defined results, and thus not wholly standard C (and I know the standard of C++ even less)

Quote
Quote
So there is no way to have a negative number? How odd.

They have only signed numbers. My bad English (fixed). Anyone worked or studied JAVA know that weirdness.

Well basically that's what Pascal does too, but the unsigned types are basically predefined subranges + some compiler work to make it optimal. Or at least it was originally, things have gotten more complicated over the years.

Code: Pascal  [Select][+][-]
  1. type
  2.    WORD = 0..65535;

and all that. But the unsigned type corresponding to the highest type can't be defined in the straightforward way. That we still define it is a hack with pro and cons.

Quote
Quote
And the hierarchy is clear in Pascal. The largest signed type is the top of the of all, and literals have the type, the corresponding unsigned type doesn't fit in its range so is orphaned.

How that can be? Signed type have 2nd complement N-1 bits mantissa (N bit, i.e. MBS is just a sign), while unsigned have N bits of mantissa?

2-complement is not a hard requirement for Pascal. It is an implementation detail. And both types have the same range of values. Some are just 2^(n-1) lower than the other.

But the crucial principle is that an unsigned number can never be a superset of signed+unsigned numbers (since it can't contain the negative numbers), while a higher order signed number *can* be a superset of signed and unsigned values.


The typesystem equalizes both sides of the equation to the same type using type promotion rules to find the common subtype.
Quote
Let take a signed and unsigned 8-bit integer. What is on top 7 or 8 bit of mantissa? Or simply, what is higher 127 or 255?

A signed is always the highest value, so the signed type is on top. It has exactly the same range just shifted down. If you want full arithmetic for the unsigned 8-bit you need at least a 9-bit integer (which in practice becomes 16-bit).

Delphi 4 and higher provide a unsigned version of the highest integer type to be used as placeholder in headers. Its arithmetic is known flawed.

Quote
If we stick on current logic for 64-bits with 64 bit CPUs, then this should be the case in FPC for 8-bit CPUs:

Code: Pascal  [Select][+][-]
  1.    if ($FF<0) then
  2.        writeln ("$FF literal is a negative number by FPC decision!")
  3.  

It doesn't make sense, since you are comparing two types int8(0) and uint8(255) that don't have a common type, since that requires a 9-bit type for usual arithmetic due to type promotion rules.

Quote
Again, what is the case in Rio for the same 64-bit example code?

I don't know. Embarcadero is notoriously bad in publishing language rationales.  But maybe they implemented a native int128 type for 64-bit only. Does it compile in 32-bit ?
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 06, 2020, 03:05:15 pm
You are familiar with the term "gaslighting" ?

The main problem is that you are so touchy that on anything wrote here you react as a childs. Gosh!

I point here on logical bug and probably Delphi incompatibility, suggesting to test and correct.

Quote
I don't know. Embarcadero is notoriously bad in publishing language rationales.  But maybe they implemented a native int128 type for 64-bit only. Does it compile in 32-bit ?

Exactly, you do not know, but stii claim that decision regarding this you have made is correct.

http://docwiki.embarcadero.com/RADStudio/Rio/en/Declared_Constants

According to this, they didn't developed int128 type, but then again, I see logic why constant literals are probably not automatically converted to int64, as in FPC. I just asking confirmation from someone have Rio - I do not have it.


Title: Re: Implicit converison, literals and QWORD variable warning
Post by: ASBzone on August 06, 2020, 03:25:36 pm
I don't know. Embarcadero is notoriously bad in publishing language rationales.  But maybe they implemented a native int128 type for 64-bit only. Does it compile in 32-bit ?

Exactly, you do not know, but stii claim that decision regarding this you have made is correct.


Ironically, you don't know either (since you are seeking confirmation), but you are still certain that the decision is wrong.

One possible "solution" to this issue is to always ensure that there is some signed type that is the largest integer type available.   But it is not likely to be easy to implement this on all platforms.  (In fact, it may be non-trivial to create/implement this on *most* platforms)

In the meantime, the explicit casting does work, notwithstanding any aesthetic concerns.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: marcov on August 06, 2020, 03:41:13 pm
The main problem is that you are so touchy that on anything wrote here you react as a childs. Gosh!

Try to discuss more rational and to the point, and you get less sentiment back.

Quote
I point here on logical bug and probably Delphi incompatibility, suggesting to test and correct.

I have now explained the design 5 times in various messages. If Delphi does it otherwise, it is either a new int128 type, or some hack.

In both cases to decide what is best, one would need very detailed analysis, since as said, these would be exceptions to the core type promotion rules. It doesn't matter that a few quick tests deliver results that you like at first glance, the core problem is divining the rules from the results.

If results are appealing for a few simple cases, but the rules suck, then this is not the way to go.

Things to try is testing on 32-bit windows ( I assume you mean 64-bit windows with your "64-bit", this might also differ), and testing with some qword and/or int64 typed variables instead of literals, testing if using hex notation matters.

It might be just a int128 emulation inside the compiler for literals only.

Quote
I don't know. Embarcadero is notoriously bad in publishing language rationales.  But maybe they implemented a native int128 type for 64-bit only. Does it compile in 32-bit ?

Exactly, you do not know, but stii claim that decision regarding this you have made is correct.

Correct would be having no uint64. But instead we have a dummy uint64. That is fine as long as you avoid mixing types (and better: limit the usage to a few cases like e.g. where filesystem API's specify qwords.)

Quote
http://docwiki.embarcadero.com/RADStudio/Rio/en/Declared_Constants

According to this, they didn't developed int128 type, but then again, I see no logic why constant literals are probably not automatically converted to int64, as in FPC. I just asking confirmation from someone have Rio - I do not have it.

They don't say 64-bit only, so one must assume that they haven't. Or only emulate it inside the compiler's literal evaluation.

The only general fix is going to a full int128, the rest is just more hacks. That won't solve much, it just changes the windowdressing from one useless case to the next, and just adds more complex exceptions that can have strange combined effects (e.g. wrt overload selection).

But if you implement int128, a week later a patch for uint128 is implemented using the old hack, and the cycle starts up again.

Title: Re: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 06, 2020, 03:47:44 pm
Ironically, you don't know either (since you are seeking confirmation), but you are still certain that the decision is wrong.

If FPC warning me that I need explicity to set uint64/QWord for constant literal to already variable declared as it, this cecrtainly have no sense.
And if uint64 literals are native types now, but automaticaly converted to signed value, that also have no sense.

The C/C++ code works correctly here, I have no doubt Rio works the same, as here is basic decision is quite clear. I will probably send tha quiterion to Embarcadero as I still have account from the last bought Delphi some 15 years ago. But, I doubt any answer will be taken here as vaild, as handy, FPC team deny any flaw in design here.
Title: Re: Implicit converison, literals and QWORD variable warning
Post by: process_1 on August 06, 2020, 03:57:31 pm
Try to discuss more rational and to the point, and you get less sentiment back.

Sentiment back? That is how you call your infant reactions? I do not really know what else you expect.

All I wrote was with background and I find it was more than suitable and rational.

Though, I have nothing else to add. That is all.
TinyPortal © 2005-2018