Fix float16 pack/unpack on big-endian architectures#55
Merged
eerimoq merged 1 commit intoeerimoq:masterfrom Feb 17, 2026
Merged
Fix float16 pack/unpack on big-endian architectures#55eerimoq merged 1 commit intoeerimoq:masterfrom
eerimoq merged 1 commit intoeerimoq:masterfrom
Conversation
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>
Owner
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pack_float_16()andunpack_float_16()passPY_BIG_ENDIANas the byte-order parameter toPyFloat_Pack2/PyFloat_Unpack2(and their underscore-prefixed predecessors).That parameter is named
le(little-endian): 0 means big-endian storage, 1 means little-endian.PY_BIG_ENDIANexpands to 1 on big-endian hosts, which tells the API to use little-endian storage — the opposite of what the bitstream layer expects (MSB-first).On little-endian hosts
PY_BIG_ENDIANis 0, which accidentally matches the bitstream convention, so the bug is invisible there.Fix: always pass 0 (big-endian) so the buffer matches the bitstream byte order regardless of host architecture.
This fixes the big-endian test failures reported in #21 (second comment, by @besser82):
Note: the first comment in #21 (byte-order suffixes
</>being silently ignored by the C extension) is a separate issue and is not addressed here.