Skip to content

Commit 32009f2

Browse files
committed
[PATCH] Fix float16 pack/unpack on big-endian systems
PyFloat_Pack2() and PyFloat_Unpack2() (and their underscore-prefixed predecessors) take an 'le' parameter where 0 means big-endian and 1 means little-endian storage. The code was passing PY_BIG_ENDIAN which evaluates to 0 on little-endian and 1 on big-endian - exactly the opposite of what is needed, since the bitstream always operates in big-endian (MSB-first) byte order. On little-endian systems PY_BIG_ENDIAN=0 happened to produce the correct result (big-endian storage), masking the bug. On big-endian systems PY_BIG_ENDIAN=1 caused little-endian storage, resulting in byte-swapped float16 values. Replace PY_BIG_ENDIAN with the literal 0 (= big-endian) to get consistent behavior across all architectures. Signed-off-by: Peter Lemenkov <lemenkov@gmail.com> Assisted-by: Claude (Anthropic) <https://claude.ai>
1 parent 99f8088 commit 32009f2

File tree

1 file changed

+4
-4
lines changed
  • src/bitstruct

1 file changed

+4
-4
lines changed

src/bitstruct/c.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,11 @@ static void pack_float_16(struct bitstream_writer_t *self_p,
430430
#if PY_VERSION_HEX >= 0x030B00A7
431431
PyFloat_Pack2(PyFloat_AsDouble(value_p),
432432
(char*)&buf[0],
433-
PY_BIG_ENDIAN);
433+
0);
434434
#else
435435
_PyFloat_Pack2(PyFloat_AsDouble(value_p),
436436
&buf[0],
437-
PY_BIG_ENDIAN);
437+
0);
438438
#endif
439439
bitstream_writer_write_bytes(self_p, &buf[0], sizeof(buf));
440440
}
@@ -447,9 +447,9 @@ static PyObject *unpack_float_16(struct bitstream_reader_t *self_p,
447447

448448
bitstream_reader_read_bytes(self_p, &buf[0], sizeof(buf));
449449
#if PY_VERSION_HEX >= 0x030B00A7
450-
value = PyFloat_Unpack2((const char*)&buf[0], PY_BIG_ENDIAN);
450+
value = PyFloat_Unpack2((const char*)&buf[0], 0);
451451
#else
452-
value = _PyFloat_Unpack2(&buf[0], PY_BIG_ENDIAN);
452+
value = _PyFloat_Unpack2(&buf[0], 0);
453453
#endif
454454

455455
return (PyFloat_FromDouble(value));

0 commit comments

Comments
 (0)