GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-73468: Add math.fma() function by vstinner · Pull Request #116667 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Mar 12, 2024 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Added new math.fma() function, wrapping C99's fma() operation: fused multiply-add function.

  • Issue: Fused multiply-add: proposal to add math.fma() #73468

📚 Documentation preview 📚: https://cpython-previews--116667.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot mentioned this pull request Mar 12, 2024
Fused multiply-add: proposal to add math.fma() #73468
Closed
@vstinner

vstinner commented Mar 12, 2024

Copy link
Copy Markdown
Member Author

Previous attempt in 2020: #17987

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner commented Mar 13, 2024
View reviewed changes
Comment thread Modules/mathmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner force-pushed the fma branch 2 times, most recently from e988215 to 3198308 Compare March 13, 2024 16:24
@vstinner vstinner added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Mar 13, 2024
@bedevere-bot

bedevere-bot commented Mar 13, 2024

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit 3198308 🤖

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Mar 13, 2024
@vstinner

vstinner commented Mar 13, 2024

Copy link
Copy Markdown
Member Author

For WASI, I skipped tests on the zero sign. It sounds too complicated to me to work around an implementation which doesn't implement IEEE 754-2008 correctly. Maybe we should even remove the function on this platform?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Mar 13, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

test_math failed on FreeBSD and FreeBSD 14.

test_importlib failed on RHEL7 Refleaks, Ubuntu NoGIL Refleaks, and MacOS M1 Refleaks NoGIL. But it's a known and unrelated leak: #116731

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner marked this pull request as ready for review March 13, 2024 20:54
@bedevere-app bedevere-app Bot added the awaiting core review label Mar 13, 2024
@vstinner

vstinner commented Mar 13, 2024

Copy link
Copy Markdown
Member Author

@mdickinson: Would you mind to review this PR?

I had to skip test_fma_zero_result() on FreeBSD and WASI platforms, because their libc fma() doesn't compute the zero sign properly: it doesn't respect IEEE 754-2008. I prefer to skip the test rather than removing the function on these platforms. What do you think?

I tried to compute the sign manually, but oh wow, rules to compute the sign a really complicated :-(

By the way, mathmodule.c contains a custom fma() implementation using "TripleLength":

/*
   The default implementation of dl_mul() depends on the C math library
   having an accurate fma() function as required by § 7.12.13.1 of the
   C99 standard. 

   The UNRELIABLE_FMA option is provided as a slower but accurate
   alternative for builds where the fma() function is found wanting.
   The speed penalty may be modest (17% slower on an Apple M1 Max),
   so don't hesitate to enable this build option.
    
   The algorithms are from the T. J. Dekker paper:
   A Floating-Point Technique for Extending the Available Precision
   https://csclub.uwaterloo.ca/~pbarfuss/dekker1971.pdf
*/
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

erlend-aasland
erlend-aasland reviewed Mar 13, 2024
View reviewed changes
Comment thread Modules/mathmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Mar 15, 2024
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was not it blocked by the fact that the standard fma() implementation on Windows did have unsatisfying quality, so we need to provide our own implementation?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
serhiy-storchaka requested review from mdickinson and tim-one March 15, 2024 15:51
@vstinner

vstinner commented Mar 15, 2024

Copy link
Copy Markdown
Member Author

Was not it blocked by the fact that the standard fma() implementation on Windows did have unsatisfying quality, so we need to provide our own implementation?

In 2020, test_math failed on Windows 32-bit. It's no longer the case.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mdickinson
mdickinson approved these changes Mar 17, 2024
View reviewed changes

@mdickinson mdickinson left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay to me.

I prefer to skip the test rather than removing the function on these platforms. What do you think?

Yes, that's a tough one. It makes me really uncomfortable to be offering an fma implementation that we know isn't correct, even on a non-mainstream platform.

Background: math.fma isn't like math.cos or math.exp - the correctness down to the last bit really matters, since that's most of the point of its existence (well, there's the speed aspect too, but we're not going to see the benefits of that at Python level). There are delicate algorithms based on math.fma whose correctness depends on fma giving correctly-rounded results - if it fails to do so, those algorithms can deliver completely wrong results (rather than results that are just off by a few ulps here or there).

So yes, delivering the wrong value on 32-bit Windows was a major blocker.

Delivering the wrong sign of zero on an (at this point) somewhat obscure platform seems like a less heinous crime. In particular, we're at least still getting the right value (as a real number), and I don't know of algorithms that would be thrown by the wrong zero sign.

Can we open upstream issues, so that there's at least a chance that things will eventually be fixed on the underlying platforms? I'm less worried about FreeBSD, and much more interested in WASI, which could yet become mainstream.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/math.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Mar 17, 2024
mdickinson
mdickinson reviewed Mar 17, 2024
View reviewed changes
Comment thread Doc/library/math.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner @mdickinson
pythongh-73468: Add math.fma() function
be7310e
Added new math.fma() function, wrapping C99's ``fma()`` operation:
fused multiply-add function.

Co-Authored-By: Mark Dickinson <[email protected]>
@vstinner
vstinner force-pushed the fma branch from b1f8b3f to be7310e Compare March 17, 2024 13:30
@vstinner
vstinner enabled auto-merge (squash) March 17, 2024 13:31
@vstinner

vstinner commented Mar 17, 2024

Copy link
Copy Markdown
Member Author

Thanks for the review @mdickinson and @erlend-aasland. I replaced IEEE 754-2008 with IEEE 754.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner merged commit 8e3c953 into python:main Mar 17, 2024
@vstinner
vstinner deleted the fma branch March 17, 2024 13:58
@bedevere-app bedevere-app Bot removed the awaiting merge label Mar 17, 2024
@vstinner

vstinner commented Mar 18, 2024

Copy link
Copy Markdown
Member Author

Can we open upstream issues, so that there's at least a chance that things will eventually be fixed on the underlying platforms? I'm less worried about FreeBSD, and much more interested in WASI, which could yet become mainstream.

I reported the issue to FreeBSD: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=277783

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner added a commit to vstinner/cpython that referenced this pull request Mar 20, 2024
@vstinner @mdickinson
pythongh-73468: Add math.fma() function (python#116667)
335dcc1
Added new math.fma() function, wrapping C99's ``fma()`` operation:
fused multiply-add function.

Co-authored-by: Mark Dickinson <[email protected]>
@mhsmith

mhsmith commented Mar 25, 2024

Copy link
Copy Markdown
Member

This issue also appears on Android x86_64, though ARM64 is OK. And that's not surprising, because Android's libc was partly based on FreeBSD's.

I'll add a skip of this test in my next Android PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Mar 25, 2024

Copy link
Copy Markdown
Member Author

This issue also appears on Android x86_64, though ARM64 is OK. And that's not surprising, because Android's libc was partly based on FreeBSD's.

A fix was proposed for FreeBSD: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=277783

If it's merged, maybe propose the same fix to Android libc? Is it bionic?

I'll add a skip of this test in my next Android PR.

The best would be to report the issue to Android if you skip the test ;-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhsmith

mhsmith commented Mar 25, 2024

Copy link
Copy Markdown
Member

Yes, all Android devices use the Bionic libc. I'll watch the FreeBSD issue and maybe report it to Android once it's resolved.

vstinner reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
@vstinner @mdickinson @adorilson
pythongh-73468: Add math.fma() function (python#116667)
5e8f2e6
Added new math.fma() function, wrapping C99's ``fma()`` operation:
fused multiply-add function.

Co-authored-by: Mark Dickinson <[email protected]>
@mhsmith mhsmith mentioned this pull request Mar 27, 2024
gh-116622: Test updates for Android #117299
Merged
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
@vstinner @mdickinson @diegorusso
pythongh-73468: Add math.fma() function (python#116667)
1d2d0ca
Added new math.fma() function, wrapping C99's ``fma()`` operation:
fused multiply-add function.

Co-authored-by: Mark Dickinson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments
@erlend-aasland erlend-aasland erlend-aasland left review comments
@tim-one tim-one Awaiting requested review from tim-one
+1 more reviewer
@mdickinson mdickinson mdickinson approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@vstinner @bedevere-bot @mhsmith @mdickinson @serhiy-storchaka @erlend-aasland
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.