bpo-41513: Improve speed and accuracy of math.hypot()#21803
Merged
rhettinger merged 10 commits intopython:masterfrom Aug 16, 2020
Merged
bpo-41513: Improve speed and accuracy of math.hypot()#21803rhettinger merged 10 commits intopython:masterfrom
rhettinger merged 10 commits intopython:masterfrom
Conversation
pganssle
reviewed
Aug 10, 2020
Member
serhiy-storchaka
left a comment
There was a problem hiding this comment.
If it requires to make tests less strict, it is potentially breaking change, and therefore it should be mentioned in What's New.
| for (i=0 ; i < n ; i++) { | ||
| x = vec[i]; | ||
| assert(Py_IS_FINITE(x) && fabs(x) <= max); | ||
| x *= scale; |
Member
There was a problem hiding this comment.
What is faster, x *= scale or x = ldexp(x, -max_e)?
Contributor
Author
There was a problem hiding this comment.
-
The test as written was over-specified. It should have been written this way from the start.
-
The
x *= scaleis faster thanx = ldexp(x, -max_e). The former is a single, fast in-line instruction and the latter is an external library call.
Here's the generated code for the loop:
L284:
movsd (%r12,%rax,8), %xmm0
addq $1, %rax
cmpq %rax, %rbp
mulsd %xmm4, %xmm0 <-- x *= scale
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1 <-- x *= x
movapd %xmm2, %xmm0
addsd %xmm1, %xmm2 <-- csum += x
subsd %xmm2, %xmm0
addsd %xmm1, %xmm0
addsd %xmm0, %xmm3
jg L284
subsd %xmm
shihai1991
pushed a commit
to shihai1991/cpython
that referenced
this pull request
Aug 20, 2020
xzy3
pushed a commit
to xzy3/cpython
that referenced
this pull request
Oct 18, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://bugs.python.org/issue41513