Inconsistent @bitCast
between packed struct and integer on big endian
#9914
Labels
bug
Observed behavior contradicts documented or intended behavior
miscompilation
The compiler reports success but produces semantically incorrect code.
stage1
The process of building from source via WebAssembly and the C backend.
Milestone
Zig Version
0.9.0-dev.1324+598db831f
Steps to Reproduce
On big endian hardware:
Expected Behavior
I believe the correct behavior for big endian is:
Given a packed struct with one field as defined below:
its representation in memory should have all 7 bits at the high end of the containing byte.
Packed structs containing more than one field require no changes.
@bitCast
from a packed struct to an int requires no changes.@bitCast
from an int to a packed struct should place the bits at the high end. Thus:Actual Behavior
On big endian hardware, given a packed struct as defined below:
its representation in memory will be exactly identical that of a u7 integer of the same value. That is to say, the low 7 bits of the byte occupied by our
S
instance contain the value. However, when the struct has more than one field:its representation in memory now occupies the high 7 bits of the byte. When
@bitCast
ing this struct to au7
, the value is converted such that the high 7 bits of the struct byte now occupy the low 7 bits of the integer byte. However, when@bitCast
ing theu7
back toS
the high 7 bits are kept and the low 1 is discarded.examples with binary:
The text was updated successfully, but these errors were encountered: