Newsgroups: comp.arch
Path: utzoo!henry
From: henry@utzoo.uucp (Henry Spencer)
Subject: Re: Shifting question
Message-ID: <1988Jul19.173807.6604@utzoo.uucp>
Organization: U of Toronto Zoology
References: <705@bnr-rsc.UUCP> <11556@steinmetz.ge.com>
Date: Tue, 19 Jul 88 17:38:07 GMT

In article <11556@steinmetz.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes:
> ... The action of a shift on an unsigned is pretty well defined...

Not if it's shifting the full number of bits, it's not.  Different machines
handle this case differently.

>  If your compiler isn't correcting for hardware characteristics I say
>it's broken. The idea of a C standard is that you don't have to build in
>a bunch of hardware checks at the source level.

Not quite.  The idea of a C standard is to be a treaty between the user
and the implementor.  It tells the user what he can expect from the
compiler, and -- just as important -- what he CAN'T expect from the
compiler.  One thing he cannot expect, in C, is for the compiler to try
to cover up the underlying hardware.  That is not the way C works.

>  What is needed is compiler code to check for a constant shift of 31+
>and load zero (no runtime penalty)...

What if the shift count is a runtime quantity?  Any such shift has to
pay the penalty of bounds checking, even if the programmer knows full
well that it's never more than (say) 3 bits.  Please don't tell me that
the penalty is only a few instructions; those few instructions can add
up (especially if they include branches, which pipelined machines really
hate).

X3J11 has tried to avoid, on principle, any features that would require
run-time checks on orthodox machines.  Assigning specific semantics to
overflow behavior -- which is what shifting all the bits out amounts
to -- is such a feature in most cases.

>...in my opinion reliable operation is more important than speed...

Reliable operation means operation which obeys the rules of the language.
If the language rules say -- as X3J11 does -- that the effect of such a
shift is implementation-defined, then there is no reliability issue
involved, except insofar as the programmer has to *understand* the language
he is using in order to produce working programs.  This is not Fortran or
Pascal; C has always given people more than enough rope to hang themselves,
for the sake of fast execution of programs written by those who *do* know
what they're doing.  Nobody ever said it was suitable for beginners.
-- 
Anyone who buys Wisconsin cheese is|  Henry Spencer at U of Toronto Zoology
a traitor to mankind.  --Pournelle |uunet!mnetor!utzoo! henry @zoo.toronto.edu
