问题
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?!
回答1:
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.
来源:https://stackoverflow.com/questions/4369265/is-this-a-bug-in-gcc-or-is-my-code-wrong