Re: When is it legal to compare any pair of pointers?

2005-09-15 Thread Alexandre Oliva
On Sep 14, 2005, Joe Buck [EMAIL PROTECTED] wrote:

 On Wed, Sep 14, 2005 at 02:15:43PM -0300, Alexandre Oliva wrote:

 Yep, it was pointer subtraction, and GCC actually optimized the
 division, that could in theory be assumed to be exact, into a
 multiplication by a large constant (aah, the wonders of modulo
 arithmetics :-)

 People that don't like the GCC optimization

It's not entirely clear that you got the impression I didn't like it.
I have no objection at all, I was just providing the additional
details as to the bug we'd run into because of unspecified uses of
pointer subtraction, as requested by DanJ.

-- 
Alexandre Oliva http://www.lsd.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer   [EMAIL PROTECTED], gcc.gnu.org}
Free Software Evangelist  [EMAIL PROTECTED], gnu.org}


Re: When is it legal to compare any pair of pointers?

2005-09-14 Thread Alexandre Oliva
On Sep 13, 2005, Daniel Jacobowitz [EMAIL PROTECTED] wrote:

 This bit binutils, in the form of a crash in a hash function on
 Solaris.  I think that was pointer subtraction, rather than comparison,
 however.

 Perhaps someone who remembers this problem more clearly than
 I do can chip in if I've gotten it totally wrong.

Yep, it was pointer subtraction, and GCC actually optimized the
division, that could in theory be assumed to be exact, into a
multiplication by a large constant (aah, the wonders of modulo
arithmetics :-), and that's what broke some sorting function on
Solaris.  And I was the lucky guy who got to debug that :-)

-- 
Alexandre Oliva http://www.lsd.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer   [EMAIL PROTECTED], gcc.gnu.org}
Free Software Evangelist  [EMAIL PROTECTED], gnu.org}


Re: When is it legal to compare any pair of pointers?

2005-09-14 Thread Joe Buck
On Wed, Sep 14, 2005 at 02:15:43PM -0300, Alexandre Oliva wrote:
 On Sep 13, 2005, Daniel Jacobowitz [EMAIL PROTECTED] wrote:
 
  This bit binutils, in the form of a crash in a hash function on
  Solaris.  I think that was pointer subtraction, rather than comparison,
  however.
 
  Perhaps someone who remembers this problem more clearly than
  I do can chip in if I've gotten it totally wrong.
 
 Yep, it was pointer subtraction, and GCC actually optimized the
 division, that could in theory be assumed to be exact, into a
 multiplication by a large constant (aah, the wonders of modulo
 arithmetics :-), and that's what broke some sorting function on
 Solaris.  And I was the lucky guy who got to debug that :-)

People that don't like the GCC optimization should be prepared to take
a very large speed penalty on inner loops that have a pointer subtraction
for an array or std::vector of objects whose size is not a power of two.
There are processors where integer division is 20x slower than
integer multiplication, or even more.



Re: When is it legal to compare any pair of pointers?

2005-09-13 Thread Paolo Carlini
chris jefferson wrote:

I realise that according to the C++ standard it isn't legal to compare
two pointers which are not from the same array. Is anyone aware of
anything in g++ which would actually forbid this, and if there is any
way of checking if will be valid?

In my opinion we should first close read ยง5.9 and 5.10. My reading is
that in general the result of pointer comparisons can be only
unspecified. Logically, this means that you can trust the result only
when both pointers point to elements of the same array.

Then, as far as *our* library (and compiler) are concerned, there is the
interesting example of basic_string::_M_disjunct: with Nathan's
substantive insight we came to the conclusion that such kind of
comparisons can be always meaningful to do (at the C++ library level) if
we use std::less()  co. Maybe Nathan has something to add...

Paolo.


Re: When is it legal to compare any pair of pointers?

2005-09-13 Thread Paolo Carlini
Paolo Carlini wrote:

Then, as far as *our* library (and compiler) are concerned, there is the
interesting example of basic_string::_M_disjunct: with Nathan's
substantive insight we came to the conclusion that such kind of
comparisons can be always meaningful to do (at the C++ library level) if
we use std::less()  co.

By the way, 20.3.3/8 is very precise about this.

Paolo.


Re: When is it legal to compare any pair of pointers?

2005-09-13 Thread Daniel Jacobowitz
On Tue, Sep 13, 2005 at 11:22:18AM +0100, chris jefferson wrote:
 I realise that according to the C++ standard it isn't legal to compare
 two pointers which are not from the same array. Is anyone aware of
 anything in g++ which would actually forbid this, and if there is any
 way of checking if will be valid?
 
 I want to be able to perform two main operations. Firstly to compare any
 pair of pointers with ==, and also to write code like:
 
 templatetypename T
   bool
   in_range(T* begin, T* end, T* value)
   { return (begin = value) != (end = value); }
 
 Where value may be a pointer not from the same array as begin and end.
 
 Apologises for sending this question to the main gcc list, but I want to
 submit such code to the debugging part of libstdc++-v3, and wanted to
 check if any optimisations may make use the fact comparing pointers from
 different arrays is undefined.

If two pointers to a T* are in the same array, their difference [in
bytes] must be 0 modulo sizeof(T).  If they aren't, and sizeof(T) 
alignof(T), then it doesn't have to be.  I'm pretty sure GCC can
optimize based on this information.

This bit binutils, in the form of a crash in a hash function on
Solaris.  I think that was pointer subtraction, rather than comparison,
however.

Perhaps someone who remembers this problem more clearly than
I do can chip in if I've gotten it totally wrong.

-- 
Daniel Jacobowitz
CodeSourcery, LLC