Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
502589d
gh-153740: use native _Float16 in PyFloat_Pack/Unpack2
skirpichev Jul 24, 2026
d90dafb
+1
skirpichev Jul 28, 2026
8bda53a
+ test conversion from double
skirpichev Jul 29, 2026
7f7b1c4
Merge branch 'master' into use-_Float16/153740
skirpichev Jul 30, 2026
ac38cb8
+ -O0
skirpichev Jul 30, 2026
5d88691
Merge branch 'master' into use-_Float16/153740
skirpichev Jul 30, 2026
a8aa77a
drop -mf16c
skirpichev Jul 30, 2026
7267d2d
+1
skirpichev Jul 30, 2026
00d48b7
add risc-v workaround
skirpichev Jul 31, 2026
702b888
Merge branch 'master' into use-_Float16/153740
skirpichev Jul 31, 2026
d7a79fa
+1
skirpichev Jul 31, 2026
7776661
+ test sign of nan value
skirpichev Jul 31, 2026
272c538
+ cast
skirpichev Jul 31, 2026
eb06f94
Merge branch 'master' into use-_Float16/153740
skirpichev Aug 3, 2026
f2770cb
+1
skirpichev Aug 3, 2026
6bfff56
+1
skirpichev Aug 3, 2026
d5b2feb
Merge branch 'master' into use-_Float16/153740
skirpichev Aug 4, 2026
972124e
address review
skirpichev Aug 4, 2026
0358727
Merge branch 'master' into use-_Float16/153740
skirpichev Aug 11, 2026
924fd20
address review: use old code to handle NaN's
skirpichev Aug 11, 2026
e416b31
Update Objects/floatobject.c
skirpichev Aug 11, 2026
165a48f
Apply batched suggestions from code review
skirpichev Aug 31, 2026
1d7e15a
Merge branch 'master' into use-_Float16/153740
skirpichev Aug 31, 2026
b5203e1
+1
skirpichev Aug 31, 2026
44e602d
address review: unroll loops
skirpichev Aug 31, 2026
d0254b0
address review: comments on handling NaN's
skirpichev Aug 31, 2026
5202c97
Merge branch 'main' into use-_Float16/153740
skirpichev Sep 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/test/test_capi/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ def test_pack_unpack_roundtrip_for_nans(self):
value = unpack(data1, endian)
data2 = pack(size, value, endian)
self.assertTrue(math.isnan(value))
self.assertEqual(math.copysign(1.0, value),
-1.0 if sign else 1.0)
self.assertEqual(data1, data2)

@unittest.skipUnless(HAVE_IEEE_754, "requires IEEE 754")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
If available on the platform, use native :c:type:`_Float16` in
:c:func:`PyFloat_Pack2` and :c:func:`PyFloat_Unpack2` functions. Patch by
Sergey B Kirpichev.
43 changes: 43 additions & 0 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,31 @@ int
PyFloat_Pack2(double x, char *data, int le)
{
unsigned char *p = (unsigned char *)data;
#if HAVE_FLOAT16
/* Conversion can change NaNs type or alter payload. Here we
just fallback to the generic code, instead of providing
workarounds as for single/double precision. */
if (!isnan(x)) {
Comment thread
skirpichev marked this conversation as resolved.
_Float16 y = (_Float16)x;

if (isinf(y) && !isinf(x)) {
goto Overflow;
}

unsigned char s[sizeof(_Float16)];

memcpy(s, &y, sizeof(_Float16));
if ((_PY_FLOAT_LITTLE_ENDIAN && !le) || (_PY_FLOAT_BIG_ENDIAN && le)) {
p[1] = s[0];
p[0] = s[1];
}
else {
p[0] = s[0];
p[1] = s[1];
}
Comment thread
skirpichev marked this conversation as resolved.
return 0;
}
#endif
unsigned char sign;
int e;
double f;
Expand Down Expand Up @@ -2090,6 +2115,24 @@ double
PyFloat_Unpack2(const char *data, int le)
{
unsigned char *p = (unsigned char *)data;
#if HAVE_FLOAT16
_Float16 x16;

if ((_PY_FLOAT_LITTLE_ENDIAN && !le) || (_PY_FLOAT_BIG_ENDIAN && le)) {
char buf[2];

buf[1] = p[0];
buf[0] = p[1];
memcpy(&x16, buf, 2);
}
else {
memcpy(&x16, p, 2);
}
if (!isnan(x16)) {
Comment thread
skirpichev marked this conversation as resolved.
return x16;
}
/* Fallback to the generic code for NaNs, see PyFloat_Pack2(). */
#endif
unsigned char sign;
int e;
unsigned int f;
Expand Down
59 changes: 59 additions & 0 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -4469,6 +4469,25 @@ if test "$ac_cv_ffi_complex_double_supported" = "yes"; then
[Defined if _Complex C type can be used with libffi.])
fi

# Check for native half-float type (_Float16).
AC_CACHE_CHECK([for _Float16 support], [ac_cv_float16_supported],
WITH_SAVE_ENV([
CFLAGS="$CFLAGS -O0"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you change the compiler flags?

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.

To disable optimizations.

AC_RUN_IFELSE([AC_LANG_SOURCE([[
int main(void)
{
_Float16 val = 1.0f16;
double d = 3.14;
val = d;
return 0;
}
]])], [ac_cv_float16_supported=yes],
[ac_cv_float16_supported=no],
[ac_cv_float16_supported=no])]))
AS_VAR_IF([ac_cv_float16_supported], [yes],
[AC_DEFINE([HAVE_FLOAT16], [1],
[Defined if _Float16 C type is supported])])

dnl Check for libmpdec >= 2.5.0
PKG_CHECK_MODULES([LIBMPDEC], [libmpdec >= 2.5.0], [have_mpdec=yes], [
WITH_SAVE_ENV([
Expand Down
3 changes: 3 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@
/* Define if you have the 'ffi_prep_closure_loc' function. */
#undef HAVE_FFI_PREP_CLOSURE_LOC

/* Defined if _Float16 C type is supported */
#undef HAVE_FLOAT16

/* Define to 1 if you have the 'flock' function. */
#undef HAVE_FLOCK

Expand Down
Loading