Skip to content

bpo-45412: Move _Py_SET_53BIT_PRECISION_START to pycore_pymath.h #28882

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

Merged
merged 1 commit into from
Oct 11, 2021
Merged

bpo-45412: Move _Py_SET_53BIT_PRECISION_START to pycore_pymath.h #28882

merged 1 commit into from
Oct 11, 2021

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Oct 11, 2021

Move the following macros to the internal C API, to pycore_pymath.h:

  • _Py_SET_53BIT_PRECISION_HEADER
  • _Py_SET_53BIT_PRECISION_START
  • _Py_SET_53BIT_PRECISION_END

PEP 7: add braces to if in these macros.

Move also _Py_get_387controlword() and _Py_set_387controlword()
definitions to pycore_pymath.h.

pystrtod.c now includes pycore_pymath.h.

https://bugs.python.org/issue45412

@vstinner
Copy link
Member Author

Since my PR only changes private macros, I didn't document my changes (no NEWS entry).

Move the following macros to the internal C API, to pycore_pymath.h:

* _Py_SET_53BIT_PRECISION_HEADER
* _Py_SET_53BIT_PRECISION_START
* _Py_SET_53BIT_PRECISION_END

PEP 7: add braces to if in these macros.

Move also _Py_get_387controlword() and _Py_set_387controlword()
definitions to pycore_pymath.h. These functions are no longer
exported.

pystrtod.c now includes pycore_pymath.h.
@vstinner vstinner merged commit 7103356 into python:main Oct 11, 2021
@vstinner vstinner deleted the set_53bit_prec branch October 11, 2021 21:09
@@ -327,86 +327,25 @@ extern "C" {
*
* #define HAVE_PY_SET_53BIT_PRECISION 1
*
* and also give appropriate definitions for the following three macros:
Copy link
Member

Choose a reason for hiding this comment

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

@vstinner Why did you remove this portion of the comment? The comment now no longer makes any sense: defining HAVE_PY_SET_53BIT_PRECISION isn't enough to magically make dtoa.c work if you don't have 53-bit precision.

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't remove it, I moved it to the internal pycore_pymath.h header. I'm trying to not mention implementation details and private functions in the public header files.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but the remaining comment in this file is now no longer valid. It says that all you have to do is define HAVE_PY_SET_53BIT_PRECISION. That doesn't work.

Copy link
Member

Choose a reason for hiding this comment

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

I'm trying to not mention implementation details and private functions in the public header files.

If someone is trying to port CPython to a new platform - say one that still uses 80-bit extended precision, then they may need to define these macros. In that case, where should they put those definitions? Is pyport.h no longer the right place for this sort of thing?

Copy link
Member Author

Choose a reason for hiding this comment

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

Before my change, it wasn't possible to define macros like _Py_SET_53BIT_PRECISION_START before including Python.h: it would fail since Python.h always define them. It's different than the Py_NAN macro which is not redefined if it's already defined. My change doesn't change that.

If someone is trying to port CPython to a new platform, they should patch the Python header file. First maintain a patch downstream, and than propose a change upstream.

I don't understand well your idea to port Python to a new platform without modifying header files. Would it mean that each C extensions would have to define macros before including Python.h?

If someone has to modify header files, is it an issue that these macros are now in Include/internal/pycore_pymath.h rather than Include/pyport.h?

Copy link
Member

Choose a reason for hiding this comment

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

But please could you fix the comment that your change broke?

Apologies. I'm being unreasonably grumpy here. I'll aim to make a PR to fix this sometime this week.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would appreciate if you can write it since I'm not sure which changes you expect exactly :-)

Well, it would be better to move HAVE_PY_SET_53BIT_PRECISION in pycore_pymath.h. I kept it in pyport.h since it's a public macro.

Does it make sense to expose HAVE_PY_SET_53BIT_PRECISION in the public C API whereas the actual implementation is in the internal C API?

Copy link
Member

@mdickinson mdickinson Feb 6, 2022

Choose a reason for hiding this comment

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

Does it make sense to expose HAVE_PY_SET_53BIT_PRECISION in the public C API whereas the actual implementation is in the internal C API?

Not really - all four of these definitions really belong in the same place.

To try to explain: dtoa.c requires that floating-point operations are performed in 53-bit precision. If you have a machine where an x87 FPU is being used for floating-point, and where the precision is set to 64 bits rather than 53 bits, dtoa.c therefore can't be used.

If you want to fix this so that dtoa.c can be used, so that you can get the nice short floating-point representation that most people are used to instead of the "legacy" representation, you have to:

  1. Define the three _Py_SET_53BIT_* macros appropriately for your machine (which can't be done portably), so that Python knows how to temporarily set the FPU precision to 53 bits
  2. Then, once you've done step 1, you can set HAVE_PY_SET_53BIT_PRECISION to tell Python that it's safe to use dtoa.c.

The current comment in pyport.h now says that if you want to use dtoa.c on one of these problem machines, all you have to do is set HAVE_PY_SET_53BIT_PRECISION, and you're done. And that's just not true: you also have to define the three _PY_SET_53BIT_* macros properly. If you read the previous comment (i.e., the comment as it was before this PR was merged), it explains that you have to carry out both these steps, not just one. That's why I'm saying that after the changes in this PR, the comment is wrong.

The four definitions really do belong all together.

Copy link
Member Author

Choose a reason for hiding this comment

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

I created #31171 to move again all code together. I avoided that previously since the implementation is a little bit tricky (let's discuss it there ;-)).

Copy link
Member Author

Choose a reason for hiding this comment

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

I merged my PR: 9bbdde2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants