Is this a bug in GCC or is my code wrong?
Asked Answered
B

1

7

I have this C code:

int test(signed char anim_col)
{
    if (anim_col >= 31) {
        return 1;
    } else if (anim_col <= -15) {
        return -2;
    }
    return 0;
}

That compiles to the following thumb code with -Os -mthumb using Android NDK r4b:

test:
    mov r3, #1
    cmp r0, #30
    bgt .L3
    mov r3, #0
    add r0, r0, #14
    bge .L3
    mov r3, #2
    neg r3, r3
.L3:
    mov r0, r3
    bx  lr

But with the latest Android NDK r5 it compiles to this broken code:

test:
    mov r3, #1
    cmp r0, #30
    bgt .L3
    lsl r0, r0, #24
    lsr r0, r0, #24
    mov r3, #0
    cmp r0, #127    @@ WTF?! should be <= -15 @@
    bls .L3
    mov r3, #2
    neg r3, r3
.L3:
    mov r0, r3
    bx  lr

This seems... strange. If anim_col is less than 0 it will return -2 instead of only returning -2 when less than or equal to -15. The complete command line to reproduce this is as follows:

android-ndk-r4b/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin/arm-eabi-gcc -c -o test.o -Os test.c --save-temps -mthumb

and

android-ndk-r5/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc -c -o test.o -Os test.c --save-temps -mthumb

Is this a known GCC bug? I find it hard to believe, that doesn't happen in real life! Surely my code is wrong?!

Bolshevist answered 6/12, 2010 at 17:41 Comment(3)
Does the code in fact give the wrong answer?Palladous
@Chris absolutely, if the input is < 0 but > -15 it is incorrect. This is a minimum test case of code from a real app that had a mysterious bug when I updated to r5.Bolshevist
You might want to post it to the android-ndk group. Might also be interesting to grab the corresponding upstream arm gcc version and see if it generates comparable code, and if there've been any bugs filed on that. I keep thinking that LSL/LSR is something clever in the way of masking, but it doesn't seem like that "idea" can work unless it shifts further to mask off part of the last byte.Palladous
B
3

It's a GCC bug!

As of NDK r5b, this bug has been fixed.

This release of the NDK does not include any new features compared to r5. The r5b release addresses the following problems in the r5 release:

  • Fixes a compiler bug in the arm-linux-androideabi-4.4.3 toolchain. The previous binary generated invalid thumb instruction sequences when dealing with signed chars.
Bolshevist answered 8/12, 2010 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.