Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!ginosko!uunet!auspex!guy From: guy@auspex.auspex.com (Guy Harris) Newsgroups: comp.sys.mips Subject: Re: Redefinition of left shift operator Message-ID: <2379@auspex.auspex.com> Date: 23 Aug 89 17:48:12 GMT References: <4724@portia.Stanford.EDU> Reply-To: guy@auspex.auspex.com (Guy Harris) Distribution: usa Organization: Auspex Systems, Santa Clara Lines: 44 >Has the left shift operator been redefined in the MIPS systems from the typical >shift to a modulo type shift? By this I mean: > i << j >redefined to > i << (j % 32) The C language specs I've seen - both K&R I and ANSI C - indicate that the behavior of shift operators is undefined if the shift count is larger than the size of the object being shifted, in bits. Therefore, there is no redefinition involved here; the definition of the left shift operator on the MIPS systems is exactly the same as it is on other systems. Its behavior in a case where said behavior is left undefined by the standard is different from its behavior in *some* systems, but the definition allows that. Other systems behave the way the MIPS systems do. The reason for this is that the specifiers wanted to allow shift operators with a non-constant right-hand operand to be compiled into the machine's shift instructions, and different machines do different things with that count. Some take it modulo the word size, some don't. >Thus, a simple statement of the form > (1L << j) - 1 >or > ~(~0 << j) >now has to have a conditional to handle the case when j is 32. Said statement would, on systems using R[23]000 (and SPARC and 80386 and some other) chips, have to have the conditional inserted into the compiled code anyway. The question is which end users are better off having the language specify the behavior in the "shift count larger than the size of the shifted object" case, and having the compiler insert the conditional in the compiled code, or having the language not specify the behavior and obliging the user to insert the condition *in those cases where it matters* - which are not all cases. (Consider, for example, code used to maintain a "bit array"; the shift count is already going to be less than the size of the object, in bits.) The ANSI C standard isn't going to change at this late date, so if you don't like the fact that it permits the type of behavior you see on the MIPS, you'll have to lobby the vendors of all systems to which you might want to port your code to have their compilers ensure the behavior you want.