classification
Title: ABS(x) == 0 can be simplified to x == 0
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: stutzbach Nosy List: mark.dickinson, stutzbach
Priority: normal Keywords: needs review, patch

Created on 2010-05-08 06:15 by stutzbach, last changed 2010-05-08 10:07 by mark.dickinson. This issue is now closed.

Files
File name Uploaded Description Edit
abs-zero.patch stutzbach, 2010-05-08 06:15
Messages (4)
msg105265 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2010-05-08 06:15
I noticed that in longobject.c, there are a few spots that take the absolute value of an int before comparing the int to 0.  There's no -0 for C integers, so the absolute value isn't necessary.

I traced back through the commit history, and it looks like they're an artifact of the original one's-complement longobject.c (which COULD have -0).  longobject was switched to two's-complement in 1992. ;-)
msg105267 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-05-08 08:05
Thanks!  Fixed in r80961 (trunk) and r80962 (py3k).

BTW, are there any particular commit messages or news entries that indicate a switch to two's complement?  I've looked for these in the past, but not found them.
msg105268 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-05-08 08:41
I found:

- Long integer shift/mask operations now simulate 2's complement
  to give more useful results for negative operands

in Misc/HISTORY, for the Python 0.9.6 release.  That's different, though:  it's about Python's semantics, not C's.  It's orthogonal to the question of whether the underlying C implementation uses two's complement, ones' complement or sign-magnitude.

I can't find any explicit indication of a decision to assume that the hardware integers are two's complement anywhere;  as far as I know, no such decision was ever taken.

But of course your simplifications are valid regardless of the integer representation of the machine.
msg105273 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-05-08 10:07
Ah, now I understand :)

r2604 introduced a scheme where for a negative PyLongObject x with n digits, the value stored in x->ob_size was -1-n.  A little like ones' complement, but unrelated to anything to do with the platform integer representation.  So at that stage there were two possible internal representations of 0L, which is why all the ZABS() stuff was necessary to make sure that those two representations of 0L compared equal.

r2751 (in 1992, indeed!) reversed this, using -n instead of -1-n for this case, but didn't remove the extra tests for the two different representations of 0L.  r2751 also changed the semantics of long bitwise operations for negative numbers, but that's not relevant to this issue.

Sorry for being slow.

It's impressive that this piece of redundant code has survived this long.
History
Date User Action Args
2010-05-08 10:07:43mark.dickinsonsetmessages: + msg105273
2010-05-08 08:41:53mark.dickinsonsetmessages: + msg105268
2010-05-08 08:05:05mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg105267

resolution: fixed
stage: patch review -> resolved
2010-05-08 06:15:56stutzbachsetkeywords: + needs review
2010-05-08 06:15:21stutzbachcreate