Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ABS(x) == 0 can be simplified to x == 0 #52905

Closed
stutzbach mannequin opened this issue May 8, 2010 · 4 comments
Closed

ABS(x) == 0 can be simplified to x == 0 #52905

stutzbach mannequin opened this issue May 8, 2010 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@stutzbach
Copy link
Mannequin

stutzbach mannequin commented May 8, 2010

BPO 8659
Nosy @mdickinson
Files
  • abs-zero.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2010-05-08.08:05:05.781>
    created_at = <Date 2010-05-08.06:15:20.845>
    labels = ['interpreter-core', 'performance']
    title = 'ABS(x) == 0 can be simplified to x == 0'
    updated_at = <Date 2010-05-08.10:07:43.271>
    user = 'https://bugs.python.org/stutzbach'

    bugs.python.org fields:

    activity = <Date 2010-05-08.10:07:43.271>
    actor = 'mark.dickinson'
    assignee = 'stutzbach'
    closed = True
    closed_date = <Date 2010-05-08.08:05:05.781>
    closer = 'mark.dickinson'
    components = ['Interpreter Core']
    creation = <Date 2010-05-08.06:15:20.845>
    creator = 'stutzbach'
    dependencies = []
    files = ['17259']
    hgrepos = []
    issue_num = 8659
    keywords = ['patch', 'needs review']
    message_count = 4.0
    messages = ['105265', '105267', '105268', '105273']
    nosy_count = 2.0
    nosy_names = ['mark.dickinson', 'stutzbach']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue8659'
    versions = ['Python 3.2']

    @stutzbach
    Copy link
    Mannequin Author

    stutzbach mannequin commented May 8, 2010

    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. ;-)

    @stutzbach stutzbach mannequin self-assigned this May 8, 2010
    @stutzbach stutzbach mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels May 8, 2010
    @mdickinson
    Copy link
    Member

    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.

    @mdickinson
    Copy link
    Member

    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.

    @mdickinson
    Copy link
    Member

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant