Skip to content

Don't use PyLong_SHIFT with _PyLong_AsScaledDouble() #49826

Closed

Description

@vstinner
BPO 5576
Nosy @mdickinson, @vstinner
Files
  • pylong_asscaleddouble-2.patch
  • issue5576.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 = 'https://github.com/mdickinson'
    closed_at = <Date 2010-01-02.15:35:55.428>
    created_at = <Date 2009-03-27.00:23:21.811>
    labels = ['interpreter-core', 'type-feature']
    title = "Don't use PyLong_SHIFT with _PyLong_AsScaledDouble()"
    updated_at = <Date 2010-01-02.15:35:55.404>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2010-01-02.15:35:55.404>
    actor = 'mark.dickinson'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2010-01-02.15:35:55.428>
    closer = 'mark.dickinson'
    components = ['Interpreter Core']
    creation = <Date 2009-03-27.00:23:21.811>
    creator = 'vstinner'
    dependencies = []
    files = ['13428', '15694']
    hgrepos = []
    issue_num = 5576
    keywords = ['patch']
    message_count = 6.0
    messages = ['84236', '84314', '84372', '96998', '96999', '97136']
    nosy_count = 2.0
    nosy_names = ['mark.dickinson', 'vstinner']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue5576'
    versions = ['Python 2.7', 'Python 3.2']

    Activity

    1. vstinner commented on Mar 27, 2009

      @vstinner
      MemberAuthor

      _PyLong_AsScaledDouble() writes the exponent in a int which have to be
      multiplied by PyLong_SHIFT to give the power of 2. I proposed to
      multiply the exponent by PyLong_SHIFT in _PyLong_AsScaledDouble() to
      directly get the power of 2. It avoids complex code to test integer
      overflow in code using _PyLong_AsScaledDouble() (the test is only done
      once, in _PyLong_AsScaledDouble).

      I also propose to change exponent type from "int*" to "unsigned int*".
      Previous maximum exponent was INT_MAX//PyLong_SHIFT (eg. 143165576 for
      PyLong using base 2^15). The new maximum is now
      UINT_MAX//PyLong_SHIFT, the double ;-)

      And since issue bpo-4258 is commited (Use 30-bit digits instead of 15-bit
      digits for Python integers), PyLong_SHIFT value may be different
      depending on the compiler option. Using my patch, the long implement
      will be a little bit more hidden.

      The function _PyLong_AsScaledDouble() should be private because the
      name starts with "_". But I see it in Include/longobject.h. In Python,
      it's used in longobject.c and mathmodule.c.

    2. mdickinson commented on Mar 28, 2009

      @mdickinson
      Member

      Thanks for this. I'll take a look.

      How about making the exponent type size_t or Py_ssize_t, rather than
      [unsigned] int? It seems to me that that fits better with the usual size
      in digits.

    3. self-assigned this
      on Mar 28, 2009
    4. vstinner commented on Mar 29, 2009

      @vstinner
      MemberAuthor

      Long double (80 bits) exponent is in range [-16382; 16383] and so would
      fits in an int, unsigned int, size_t or Py_ssize_t. I don't know if a
      signed or unsigned number is better. I know only one operation on
      exponents: a-b in PyLong_AsDouble.

    5. mdickinson commented on Dec 29, 2009

      @mdickinson
      Member

      Long double (80 bits) exponent is in range [-16382; 16383] and so
      would
      fits in an int, unsigned int, size_t or Py_ssize_t.

      Sure, but I don't think that's relevant to the point I was attempting to
      make: PyLong_AsScaledDouble returns the number of bits in the given
      PyLong, and that number can be (theoretically) as large as
      PY_SSIZE_T_MAX * PyLong_SHIFT. So Py_ssize_t is a better fit than int
      to hold this number: otherwise you'll get unnecessary overflows on a
      64-bit system.

      I'm working on refactoring PyLong_AsScaledDouble and PyLong_AsDouble to
      have them both use the same core code. This would make
      PyLong_AsScaledDouble correctly rounded, and hence independent of the
      implementation of a PyLong (in particular, independent of the decision
      to use 15-bit or 30-bit digits for PyLongs: this decision really
      shouldn't affect the result of a log function or any other functions
      using _PyLong_AsScaledDouble).

    6. mdickinson commented on Dec 29, 2009

      @mdickinson
      Member

      Here's a patch:

      • rename _PyLong_AsScaledDouble to _PyLong_Frexp (for the benefit of
        anyone foolish enough to be using an undocumented private API
        function)
      • make the exponent type Py_ssize_t instead of int
      • do the scaling by PyLong_SHIFT in _PyLong_Frexp instead of in the
        functions that call it (e.g., loghelper in the math module)
      • remove longintrepr.h include from math module, since it's no longer
        needed
      • change _PyLong_Frexp algorithm to do correct rounding
      • refactor PyLong_Double to use _PyLong_Frexp.
    7. mdickinson commented on Jan 2, 2010

      @mdickinson
      Member

      Applied in r77234 (trunk), r77237 (py3k).

    8. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions