This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

bit shift bug with GCC 2.95.2 19991024 under Solaris 7


bit_shift_bug.ii.gz

Originator: Andrew Trotman
Synopsis:   bit shifting of 64 bit int outputs incorrect values
Severity:   serious
Priority:   medium
Category:   c
Class:      wrong-code
Release:    >gcc -v
  Reading specs from /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/specs
  gcc version 2.95.2 19991024 (release)
Environment: >uname -a
  SunOS tucana 5.7 Generic_106541-12 sun4u sparc SUNW,Ultra-5_10
Description: When bit shifting a 64 bit long long, gcc doesn't cast
  the constant values appropriately. When code below is run, output
  is:
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
18446744071562067968
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
18446744071562067968

(note the incorrect spike). Based on ANSI C Standard, when doing a bit
shift, both sides should be promoted to the most significant
type. Therefore, '1 << foo' (when foo is a 64 bit int) should always
raise 1 to 64 bits.

How-To-Repeat:
#include <stdio.h>
int main(void)
{
unsigned long long pos, val;
for (pos = 0; pos < 64; pos++)
	{
	val = 1 << pos;
	printf("%llu\n", val);
	}
}
Fix: Change the 'val = 1 << pos;' line to 'val = 1ULL << pos;'

GCC Command line: gcc -v -save-temps -o bit_shift_bug bit_shift_bug.c

GCC Output:
Reading specs from /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/specs
gcc version 2.95.2 19991024 (release)
 /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/cpp -lang-c++ -v -D__GNUC__=2 -D__GNUG__=2 -D__GNUC_MINOR__=95 -D__cplusplus -Dsparc -Dsun -Dunix -D__svr4__ -D__SVR4 -D__sparc__ -D__sun__ -D__unix__ -D__svr4__ -D__SVR4 -D__sparc -D__sun -D__unix -Asystem(unix) -Asystem(svr4) -D__EXCEPTIONS -D__GCC_NEW_VARARGS__ -Acpu(sparc) -Amachine(sparc) bit_shift_bug.cc bit_shift_bug.ii
GNU CPP version 2.95.2 19991024 (release) (sparc)
#include "..." search starts here:
#include <...> search starts here:
 /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/../../../../include/g++-3
 /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/../../../../sparc-sun-solaris2.7/include
 /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/include
 /usr/include
End of search list.
The following default directories have been omitted from the search path:
 /usr/local/include
End of omitted list.
 /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/cc1plus bit_shift_bug.ii -quiet -dumpbase bit_shift_bug.cc -version -o bit_shift_bug.s
GNU C++ version 2.95.2 19991024 (release) (sparc-sun-solaris2.7) compiled by GNU C version 2.95.2 19991024 (release).
 /usr/ccs/bin/as -V -Qy -s -o bit_shift_bug.o bit_shift_bug.s
/usr/ccs/bin/as: WorkShop Compilers 5.0 98/12/21
 /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/collect2 -V -Y P,/usr/ccs/lib:/usr/lib -Qy -o bit_shift_bug /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/crt1.o /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/crti.o /usr/ccs/lib/values-Xa.o /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/crtbegin.o -L/home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2 -L/usr/ccs/bin -L/usr/ccs/lib -L/home/janderso/root/lib bit_shift_bug.o -lgcc -lc -lgcc /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/crtend.o /home/janderso/root/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/crtn.o
ld: Software Generation Utilities - Solaris-ELF (4.0)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]