Recent

Author Topic: Are pointers normalized before comparison  (Read 7333 times)

munair

  • Hero Member
  • *****
  • Posts: 697
  • keep it simple
    • SharpBASIC
Re: Are pointers normalized before comparison
« Reply #15 on: October 02, 2017, 08:50:12 am »
Code: Pascal  [Select][+][-]
  1. Writeln(@A - @B);
And where is the cast?
If address of B is greater than address of A, is the result not automatically cast to signed?
« Last Edit: October 02, 2017, 08:51:56 am by munair »

joho

  • Jr. Member
  • **
  • Posts: 69
  • Joaquim Homrighausen
    • ~/JoHo
Re: Are pointers normalized before comparison
« Reply #16 on: October 02, 2017, 08:53:42 am »
My original concern was raw untyped pointer comparison in a 32-bit code/memory model/environment.

I needed to know, for certain, that doing untyped pointer comparison/arithmetic would _always_ generate unsigned comparison code properly.

If untyped pointers are baseline Ints to the compiler, it would seem I get signed comparisons as well. If untyped pointers are unsigned Ints to the compiler, it would seem I get signed comparisons (as it should be). In a less-than-32-bit-environments, a seg:ofs construct would obviously have to be normalized before pointer comparison.

If they are treated as signed Ints, I have no opinion about it (though I think all pointers should always be "unsigned"), I just need to know, so I can cast them before doing arithmetic and/or comparisons.


marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8734
  • FPC developer.
Re: Are pointers normalized before comparison
« Reply #17 on: October 02, 2017, 10:19:37 am »
The results here are different:
Quote
-16
-16
-16
1. The first and second lines show that the arithmetic is signed.
2. In the 32 compiler, if the result does not fit in 32 bits, but is fit in Int64, then it will be Int64, for compatibility. Use the additional PtrUInt (Result).

Pointer diffs are not pointers (which scale with ptruint), but something that scales with sizeints and array index calculation. So those are flawed to prove arithmetic on pointers themselves. When originally I read the original message I thought of a test closer to what Joho wants to do AND operates on pointers directly.

Turns out not to be that hard :-)


Code: Pascal  [Select][+][-]
  1. var p1 ,p2 : pointer;
  2. begin
  3.    p1:=pointer(ptruint($FFFFFFFF));   // -1, adjust for 64-bit
  4.    p2:=pointer(ptruint(1));
  5.    if p1>p2 then
  6.      writeln('-1>1')
  7.    else
  8.      writeln('-1<1')
  9. end.
  10.  

compiles to:

Code: Pascal  [Select][+][-]
  1. movl    U_$P$PROGRAM_$$_P1,%eax
  2.         cmpl    U_$P$PROGRAM_$$_P2,%eax
  3.         ja      .Lj3                                              // ja jmp is turned into jna if you turn on optimization
  4.         jmp     .Lj4
  5. .Lj3:
  6. # [7] writeln('-1>1')
  7.      (write code skipped)
  8.         jmp     .Lj5
  9. .Lj4:
  10. # [9] writeln('-1<1')
  11.      (write code skipped)
  12. .Lj5:

and prints   "-1>1"  The last time I checked that is not signed arithmetic.
« Last Edit: October 02, 2017, 08:45:30 pm by marcov »

ASerge

  • Hero Member
  • *****
  • Posts: 1671
Re: Are pointers normalized before comparison
« Reply #18 on: October 02, 2017, 07:51:55 pm »
Code: ASM  [Select][+][-]
  1. cmpl    U_$P$PROGRAM_$$_P2,%eax
  2. ja      .Lj3                                              // ja jmp is turned into jna if you turn on optimization
  3. ...
and prints   "-1>1"  The last time I checked that is not signed arithmetic.
I give up. You convinced me.

 

TinyPortal © 2005-2018