https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63303

--- Comment #11 from mikulas at artax dot karlin.mff.cuni.cz ---
Richard Biener: if the middle end tells us that one pointer is greater or equal
than the other pointer, we could do unsigned subtraction and shift.

But if we don't know which pointer is greater, it gets more complicated: To do
correct short* pointer subtraction, we need to subtract pointers using
sub %edx, %eax; rcr $1, %eax --- i.e. shift the carry bit back to the topmost
bit of the result. According to Agner's tables, rcr with 1-bit count takes 1
tick on AMD and 2 ticks on Intel, so the performance penalty isn't that big. On
other architectures that lack rcr, it would be more complicated.

Another possibility is to file a defect report on the C standard and request
that program in comment 4 be considered invalid. - for example, change the
wording to this: "If the result multiplied by the size of the array element is
not representable in an object of that type, the behavior is undefined." - that
would specify that that subtraction is invalid.

Reply via email to