Recent

Author Topic: [SOLVED] Constant definition out of range problem  (Read 1468 times)

440bx

  • Hero Member
  • *****
  • Posts: 5814
Re: Constant definition out of range problem
« Reply #15 on: October 04, 2025, 04:49:41 pm »
I want the code to compile in both 32 and 64 bit, ideally without any warnings.

the xor is perfect because it does not cause arithmetic promotion.  Good thinking and thank you again Nitorami.   I'm going to be using that xor operation instead of a not.

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

creaothceann

  • Full Member
  • ***
  • Posts: 203
Re: Constant definition out of range problem
« Reply #16 on: October 04, 2025, 05:16:58 pm »
Why 32-bit?
Quote from: Thaddy
And don't start an argument, I am right.
Quote from: Thaddy
You have a thorough misunderstanding of what I wrote. Can you provide an example this time? I doubt it. (because you never do out of incompentence)

jamie

  • Hero Member
  • *****
  • Posts: 7308
Re: Constant definition out of range problem
« Reply #17 on: October 04, 2025, 07:24:01 pm »
Code: Pascal  [Select][+][-]
  1. Const
  2.   DIAGNOSTIC_REASON_INVALID_FLAGS    = DWord(not $80000007 and $ffffffff);      
  3.  

How about that?

Jamie
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 5814
Re: Constant definition out of range problem
« Reply #18 on: October 04, 2025, 10:41:34 pm »
Code: Pascal  [Select][+][-]
  1. Const
  2.   DIAGNOSTIC_REASON_INVALID_FLAGS    = DWord(not $80000007 and $ffffffff);      
  3.  

How about that?

Jamie
Try it in both 32 and 64 bits and you'll answer your own question.




ETA:

Why 32-bit?
because IMO simple code like a constant declaration should "perform" equally well regardless of bitness. 

Whenever possible I compile for both bitnesses, occasionally this reveals subtle potential problems.

« Last Edit: October 04, 2025, 10:49:14 pm by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

MathMan

  • Sr. Member
  • ****
  • Posts: 434
Re: Constant definition out of range problem
« Reply #19 on: October 04, 2025, 11:31:57 pm »
I propose

Code: Pascal  [Select][+][-]
  1. const
  2.   DIAGNOSTIC_REASON_INVALID_FLAGS = DWord( not DWord( $80000007 ) );
  3.  

Rational:
- the high bit is set in $80000007 <= requires a cast to avoid the warning in 32 bit environment
- not will again promote to 64 bit in 64 bit environment <= needs another cast to bring down to 32 bit

Caveats:
- it is still not a real 32 bit constant <= can only be achieved by typing the constant itself
- at the position in source it is used the compiler may choose to again promote to larger ordinal type

Cheers,
MathMan

440bx

  • Hero Member
  • *****
  • Posts: 5814
Re: Constant definition out of range problem
« Reply #20 on: October 05, 2025, 12:31:00 am »
@MathMan,

That still causes the compiler to emit a warning when compiling for 32 bit. 

The only thing that works so far is Nitorami's xor method.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

nanobit

  • Full Member
  • ***
  • Posts: 181
Re: [SOLVED] Constant definition out of range problem
« Reply #21 on: October 05, 2025, 10:22:17 am »
Code: Pascal  [Select][+][-]
  1. c = (not uint64($80000007)) and $FFFFFFFF;

440bx

  • Hero Member
  • *****
  • Posts: 5814
Re: [SOLVED] Constant definition out of range problem
« Reply #22 on: October 05, 2025, 10:30:52 am »
Code: Pascal  [Select][+][-]
  1. c = (not uint64($80000007)) and $FFFFFFFF;
That works too.  Nice... good example of "if you can't beat them, join them" by explicitly typecasting to int64

I still prefer Nitorami's solution, it's more compact.


FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

MathMan

  • Sr. Member
  • ****
  • Posts: 434
Re: [SOLVED] Constant definition out of range problem
« Reply #23 on: October 05, 2025, 11:19:09 am »
Code: Pascal  [Select][+][-]
  1. c = (not uint64($80000007)) and $FFFFFFFF;
That works too.  Nice... good example of "if you can't beat them, join them" by explicitly typecasting to int64

I still prefer Nitorami's solution, it's more compact.

Hm, I obviously was wrong the first time. But now, as you bring compactness into play, why not

Code: Pascal  [Select][+][-]
  1. const
  2.   DIAGNOSTIC_REASON_INVALID_FLAGS = $7ffffff8; // <=> not $80000007
  3.  

I mean, if you accept Nitorami's solution you can as well directly resolve as above ...

440bx

  • Hero Member
  • *****
  • Posts: 5814
Re: [SOLVED] Constant definition out of range problem
« Reply #24 on: October 05, 2025, 12:40:45 pm »
Code: Pascal  [Select][+][-]
  1. const
  2.   DIAGNOSTIC_REASON_INVALID_FLAGS = $7ffffff8; // <=> not $80000007
  3.  

I mean, if you accept Nitorami's solution you can as well directly resolve as above ...
Just as general information...

That definition comes from one of the .c header files (winnt.h) and after unsuccessfully trying a number of ways using typecasts to get rid of the compiler warning, I figured I'd use the result of "not" just as you suggested above.

The problem I have with that one is that it is not "parallel" to the original definition and it is not visually obvious that not $80000007 is $7ffffff8 and a visual comparison of the original has 0x80000007 while the ported definition has a number that is visually totally different ($7ffffff8).  That bothered me.

Nitorami's solution is my preferred one because when visually comparing (~0x80000007) with $80000007 xor $FFFFFFFF  there are two ops, a "not" in one and an "xor" in the other, that makes the operator count "parallel" (the same) and, it should be reasonably easy for a programmer to quickly figure out that the "xor $FFFFFFFF" is equivalent to the "not" in this case and the original value is there to be visually matched.

For that reason, I prefer Nitorami's solution over simply using the result of the "not" operation.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

creaothceann

  • Full Member
  • ***
  • Posts: 203
Re: [SOLVED] Constant definition out of range problem
« Reply #25 on: October 05, 2025, 02:27:51 pm »
If you want it to be "visually obvious" you shouldn't use hexadecimal to begin with. Flags are usually binary:

Code: Pascal  [Select][+][-]
  1. const
  2.         DIAGNOSTIC_REASON_INVALID_FLAGS = NOT (Bit31 + 7);        // 7FFF FFF8
  3.         DIAGNOSTIC_REASON_INVALID_FLAGS = NOT (Bit31 + Bits3);    // alternative
  4.  

(bit contants & types defined here)
Quote from: Thaddy
And don't start an argument, I am right.
Quote from: Thaddy
You have a thorough misunderstanding of what I wrote. Can you provide an example this time? I doubt it. (because you never do out of incompentence)

440bx

  • Hero Member
  • *****
  • Posts: 5814
Re: [SOLVED] Constant definition out of range problem
« Reply #26 on: October 05, 2025, 03:30:05 pm »
If you want it to be "visually obvious" you shouldn't use hexadecimal to begin with.
That's fine with me.   Tell that to the MS programmers. 

When I port definitions from the C headers, I try to make the Pascal definition as "parallel" as reasonably possible to the C definition.

The original C definition is:
Code: C  [Select][+][-]
  1. #define DIAGNOSTIC_REASON_INVALID_FLAGS  (~0x80000007)
  2.  

My Pascal definition (using Nitorami's xor):
Code: Pascal  [Select][+][-]
  1. const
  2.   DIAGNOSTIC_REASON_INVALID_FLAGS        = $80000007 xor $FFFFFFFF;
  3.  

It's fairly parallel.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

nanobit

  • Full Member
  • ***
  • Posts: 181
Re: [SOLVED] Constant definition out of range problem
« Reply #27 on: October 05, 2025, 04:01:16 pm »
Another alternative (if literal has negative value) is:
Code: Pascal  [Select][+][-]
  1. c = not int32($80000007);

440bx

  • Hero Member
  • *****
  • Posts: 5814
Re: [SOLVED] Constant definition out of range problem
« Reply #28 on: October 05, 2025, 05:02:20 pm »
Another alternative (if literal has negative value) is:
Code: Pascal  [Select][+][-]
  1. c = not int32($80000007);
Yes, a very good alternative indeed.  It's very parallel to the original C declaration. 

Definitely worth considering.  Thank you!.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018