Skip to content

Read behaviour for signed bitfields is inconsistent with Clang, MSVC and GCC #415

@DaZombieKiller

Description

@DaZombieKiller

When generating an interop structure for the following type:

struct PackedVertex
{
    union
    {
        int i;
        struct
        {
            int x : 11;
            int y : 11;
            int z : 10;
        };
    };
};

ClangSharpPInvokeGenerator produces accessors like the following:

[NativeTypeName("int : 11")]
public int x
{
    get
    {
        return _bitfield & 0x7FF;
    }

    set
    {
        _bitfield = (_bitfield & ~0x7FF) | (value & 0x7FF);
    }
}

Which does not appear to handle the sign of each component properly.

Setting i to 0xFB3E7064 should produce 100, -50, -20 for x, y, z, but with the currently generated code, it produces 100, 1998, 1004. If the code for the getters are tweaked to match the assembly generated by Clang and MSVC, then the results are correct.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions