Skip to content

[MIPS] __builtin_bswap16 miscompilation #103035

Closed
@markus-oberhumer

Description

@markus-oberhumer

(EDIT: I've added a new Godbolt link and updated the example to make it clear that the bug needs -march=mips32 to trigger)

Godbolt link: https://gcc.godbolt.org/z/vMGc86deh

clang 18.1.8 MIPS miscompilation in Debug mode

// clang -target mips-linux-musl -march=mips32 bug.c -o bug.out
// qemu-mips ./bug.out

unsigned bswap16_fails(unsigned v) {
    // FAILS - cast seems ignored ??
    return __builtin_bswap16((unsigned short) v);
}

unsigned bswap16_ok(unsigned v) {
    return __builtin_bswap16((unsigned short) (v & 0xffff)); // OK
}

int main(void) {
    if (bswap16_ok(0x04030201) != 0x0102)
        return 1;
    if (bswap16_fails(0x04030201) != 0x0102)
        return 2; // ERROR HERE
    return 0;
}

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions