[15243] in Athena Bugs
SGI compiler bug
daemon@ATHENA.MIT.EDU (Greg Hudson)
Thu Jul 10 04:50:48 1997
Date: Thu, 10 Jul 1997 04:50:41 -0400
From: Greg Hudson <ghudson@MIT.EDU>
To: bugs@MIT.EDU
With optimization turned on, the IRIX 5.3 and 6.2 native compilers do
not properly convert a signed character contained within a promoted
argument variable to an unsigned character. The following test
program illustrates the bug:
#include <stdio.h>
void foo(int);
void foo(value)
char value;
{
if ((unsigned char) value == 255)
printf("Passed.\n");
else
printf("Failed.\n");
}
int main()
{
foo(255);
return 0;
}
With optimization turned off, the compiler does things the slow,
simple way, storing the argument ($4) into its slot on the stack (at
offset 32 from the stack pointer) and then loads the low-order byte
into $14:
sw $4, 32($sp)
lbu $14, 35($sp)
bne $14, 255, $32
With optimization turned on, the compiler decides to save a memory
access by doing the conversion inline, shifting the $4 value left 24
bits and then right again to get the low-order byte. Unfortunately,
it does an arithmetic right shift, extending the sign bit of the
low-order byte across the word so that you get -1 instead of 255:
sw $4, 32($sp)
sll $14, $4, 24
move $4, $14
sra $15, $4, 24
move $4, $15
bne $4, 255, $32