🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Luke Kendall Thu, 27 Jun 1991 20:22
Bug in C compiler(s)
#8728
Author: Luke Kendall
Date: Thu, 27 Jun 1991 20:22
40 lines
1613 bytes
A bug in the Sun and GNU C compilers.  The following tiny program does
not work:

main()
{
    int		i, shift;

    printf("%x\n", 0x10000 >> 1000);

    for (shift = 1, i = 0; i < 5; ++i, shift *= 10)
	printf("%x >> %d = %x\n", 0x10000, shift, 0x10000 >> shift);
}

The output of the program from the Sun C compiler (for a SPARC, SunOS 4.1):

100
10000 >> 1 = 8000
10000 >> 10 = 40
10000 >> 100 = 1000
10000 >> 1000 = 100
10000 >> 10000 = 1

The output of the program from the GNU C compiler (version 1.40):

0
10000 >> 1 = 8000
10000 >> 10 = 40
10000 >> 100 = 1000
10000 >> 1000 = 100
10000 >> 10000 = 1

Looking into the assembly code for the SPARC, it uses an `sra'
instruction (shift-right-arithmetic).  Unfortunately, this instruction
is documented as only working for numbers with the bottom 5 bits set.
In other words, both compilers are failing to mask the right operand
of the shift.
-- 
Luke Kendall, Senior Software Engineer.      | Net:   luke@research.canon.oz.au
Canon Information Systems Research Australia | Phone: +61 2 805 2914
P.O. Box 313 North Ryde, NSW, Australia 2113 | Fax:   +61 2 805 2929
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads