Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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.
122 changes: 122 additions & 0 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,69 @@ _PyFloat_DebugMallocStats(FILE *out)
int
PyFloat_Pack2(double x, char *data, int le)
{
#if HAVE_FLOAT16
unsigned char *p = (unsigned char *)data;
_Float16 y = (_Float16)x;
int i, incr = 1;

if (isinf(y) && !isinf(x)) {
PyErr_SetString(PyExc_OverflowError,
"float too large to pack with e format");
return -1;
}

/* correct y if x was a sNaN, transformed to qNaN by conversion */
if (isnan(x)) {
uint64_t v;

memcpy(&v, &x, 8);
#ifndef __riscv
if ((v & (1ULL << 51)) == 0) {
uint16_t u16;
memcpy(&u16, &y, 2);
/* if have payload, make sNaN */
if (u16 & 0x1ff) {
u16 &= ~(1 << 9);
}
memcpy(&y, &u16, 2);
}
#else
uint16_t u16;

memcpy(&u16, &y, 2);
/* Workaround RISC-V: "If a NaN value is converted to a
* different floating-point type, the result is the
* canonical NaN of the new type". The canonical NaN here
* is a positive qNaN with zero payload. */
if (v & (1ULL << 63)) {
u16 |= (uint16_t)(1U << 15); /* set sign */
}
/* add payload */
u16 -= (u16 & 0x1ff);
u16 += (uint16_t)((v & 0x7ffffffffffffULL) >> 42);
/* if have payload, make sNaN */
if ((v & (1ULL << 51)) == 0 && (u16 & 0x1ff)) {
u16 &= ~(uint16_t)(1 << 9);
}

memcpy(&y, &u16, 2);
#endif
}

unsigned char s[sizeof(_Float16)];
memcpy(s, &y, sizeof(_Float16));

if ((_PY_FLOAT_LITTLE_ENDIAN && !le) || (_PY_FLOAT_BIG_ENDIAN && le)) {
p += 1;
incr = -1;
}

for (i = 0; i < 2; i++) {
*p = s[i];
p += incr;
}
return 0;
#else
unsigned char *p = (unsigned char *)data;
unsigned char sign;
int e;
Expand Down Expand Up @@ -1997,6 +2060,7 @@ PyFloat_Pack2(double x, char *data, int le)
PyErr_SetString(PyExc_OverflowError,
"float too large to pack with e format");
return -1;
#endif /* HAVE_FLOAT16 */
}

int
Expand Down Expand Up @@ -2089,6 +2153,63 @@ PyFloat_Pack8(double x, char *data, int le)
double
PyFloat_Unpack2(const char *data, int le)
{
#if HAVE_FLOAT16
unsigned char *p = (unsigned char *)data;
_Float16 x;

if ((_PY_FLOAT_LITTLE_ENDIAN && !le) || (_PY_FLOAT_BIG_ENDIAN && le)) {
char buf[2];
char *d = &buf[1];
int i;

for (i = 0; i < 2; i++) {
*d-- = *p++;
}
memcpy(&x, buf, 2);
}
else {
memcpy(&x, p, 2);
}

/* return sNaN double if x was sNaN float */
if (isnan(x)) {
uint16_t v;
memcpy(&v, &x, 2);

#ifndef __riscv
if ((v & (1 << 9)) == 0) {
double y = x; /* will make qNaN double */
uint64_t u64;

memcpy(&u64, &y, 8);
u64 &= ~(1ULL << 51); /* make sNaN */
memcpy(&y, &u64, 8);
return y;
}

#else
double y = x;
uint64_t u64;

memcpy(&u64, &y, 8);
if ((v & (1 << 9)) == 0) {
u64 &= ~(1ULL << 51);
}
/* Workaround RISC-V, see PyFloat_Pack4() */
if (v & (1U << 15)) {
u64 |= (1ULL << 63); /* set sign */
}
/* add payload */
u64 -= (u64 & 0x7ffffffffffffULL);
u64 += ((v & 0x1ffULL) << 42);

memcpy(&y, &u64, 8);
return y;
#endif
}

return x;
#else
unsigned char *p = (unsigned char *)data;
unsigned char sign;
int e;
Expand Down Expand Up @@ -2140,6 +2261,7 @@ PyFloat_Unpack2(const char *data, int le)
x = -x;

return x;
#endif /* HAVE_FLOAT16 */
}

double
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 @@ -4462,6 +4462,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.
AC_CACHE_CHECK([for _Float16 support], [ac_cv_float16_supported],
WITH_SAVE_ENV([
CFLAGS="$CFLAGS -O0"
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