Path: utzoo!utgpu!water!watmath!clyde!att!pacbell!ames!ucsd!ucsdhub!esosun!seismo!uunet!steinmetz!davidsen From: davidsen@steinmetz.ge.com (William E. Davidsen Jr) Newsgroups: comp.arch Subject: Re: Shifting question Message-ID: <11606@steinmetz.ge.com> Date: 21 Jul 88 16:55:58 GMT References: <705@bnr-rsc.UUCP> <11556@steinmetz.ge.com> <1988Jul19.173807.6604@utzoo.uucp> Reply-To: davidsen@crdos1.UUCP (bill davidsen) Organization: General Electric CRD, Schenectady, NY Lines: 64 In article <1988Jul19.173807.6604@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes: | 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. The rules may say anything is undefined, but that doesn't make it a good idea. At one time early in standards process, the question of having sizes known to the preprocessor was discussed. At that time my recollection is that sizeof was not to be allowed in a preprocessor constant expression, although reading the standard certainly seems to say it is at this time. Then the user could use conditional code as: #include #define UNSIGN_BIT (CHAR_BIT)*sizeof(unsigned)) #if UNSIGN_BIT < MAX_SHIFT /* brain damaged stuff, user runtime checks */ if (shift >= UNSIGN_BIT) valu = 0; else valu >>= shift; #else /* reasonable machine */ valu >>= shift; #endif My reason for saying the sizeof now seems legal is: section 3.8 p83 l15: #if takes a constant expression section 3.8.1 p84 l19-27: definition of const expr section 3.3.3.4 p43 l22: action of sizeof operator section 2.2.4.2 p13 l30-34: CHAR_BIT in limits.h The only thing disallowed in 3.8.1 is casts, and footnote 74 disallows keywords. However sizeof is an operator as defined in 3.3.3.4. If sizeof is NOT legal in preprocessor directives, then the standard should be clarified, and there is simply no way (known to me) to determine the size of int in bits at preprocessor time. It can be found, of course, at runtime, but that precludes use of conditional code to avoid checks where they are not needed. Yes, I've been programming C for awhile (since it was spelled B and ran on the GE600) and I can program around stuff like this. I'm sure Henry can, too. If C is ever going to be a portable general purpose language rather than a toy for hackers, programmers should not have to guess that two shifts of 10 don't give the same result as one shift of 20 (or whatever the sizes may be), or any of the other non-intuitive things which happen in the name of speed. Getting speed should be an "unsafe optimization," like dropping stack checking, ignoring alias problems, etc. It has a definite place and I don't deny that I would use it. The default should be to have the source code work as written. -- bill davidsen (wedu@ge-crd.arpa) {uunet | philabs | seismo}!steinmetz!crdos1!davidsen "Stupidity, like virtue, is its own reward" -me