Skip to content

gh-153740: use native _Float16 in PyFloat_Pack/Unpack2 - #154796

Open
skirpichev wants to merge 7 commits into
python:mainfrom
skirpichev:use-_Float16/153740
Open

gh-153740: use native _Float16 in PyFloat_Pack/Unpack2#154796
skirpichev wants to merge 7 commits into
python:mainfrom
skirpichev:use-_Float16/153740

Conversation

@skirpichev

@skirpichev skirpichev commented Jul 28, 2026

Copy link
Copy Markdown
Member

@skirpichev

Copy link
Copy Markdown
Member Author

Not sure how to fix this error:

/usr/bin/ld: /usr/bin/ld: DWARF error: invalid or unhandled FORM value: 0x25
Objects/floatobject.o: in function `PyFloat_Pack2':
floatobject.c:(.text.PyFloat_Pack2[PyFloat_Pack2]+0x32): undefined reference to `__truncdfhf2'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

CC @StanFromIreland (fuzzers)

@StanFromIreland

Copy link
Copy Markdown
Member

The configure probe only tests a compile-time _Float16 constant, so it reports "yes" in the OSS-Fuzz Docker container where clang accepts the type but the container's pre-GCC-12 libgcc lacks __truncdfhf2 which is the runtime helper the PyFloat_Pack2 double to _Float16 conversion links against. I suggest you make configure do a runtime double to _Float16 conversion.

@skirpichev
skirpichev force-pushed the use-_Float16/153740 branch from 46fe384 to 8bda53a Compare July 30, 2026 00:18
@skirpichev

Copy link
Copy Markdown
Member Author

I suggest you make configure do a runtime double to _Float16 conversion.

Good idea, that works.

@skirpichev
skirpichev marked this pull request as ready for review July 30, 2026 01:03
Comment thread configure.ac Outdated
# Check for native half-float type.
AC_CACHE_CHECK([for _Float16 support], [ac_cv_float16_supported],
WITH_SAVE_ENV([
CFLAGS="$CFLAGS -mf16c -O0"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you need/want -mf16c?
I think GCC will call __truncdfhf2 without it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You can get support for half-floats without real hardware support. This happened on my old laptop. I don't think it's useful.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The F16C extensions (VCVTPH2PS and VCVTPS2PH) convert to/from binary32, which doesn't help you if you're trying to convert directly from binary64. So, I think you might as well eliminate the -mf16c from the configuration test above. (Also, it doesn't apply to AArch64, for which I use -march=armv8.4-a+sve when using half precsion.)
I hope you do get this PR merged. If CPython is compiled with -mavx512fp16 or -mavx10.1 or -mavx10.2, then the compiler will generate vcvtsd2sh (scalar double to scalar half) without calling the library's __truncdfhf2 function.

@skirpichev skirpichev added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 30, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @skirpichev for commit ac38cb8 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F154796%2Fmerge

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

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 30, 2026
@skirpichev skirpichev removed their assignment Jul 30, 2026
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