Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!rutgers!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: <11556@steinmetz.ge.com> Date: 18 Jul 88 15:19:38 GMT References: <705@bnr-rsc.UUCP> Reply-To: davidsen@crdos1.UUCP (bill davidsen) Organization: General Electric CRD, Schenectady, NY Lines: 46 In article <705@bnr-rsc.UUCP> jim@bnr-rsc.UUCP (Jim Somerville) writes: | What should be the value of y after the shifting is complete? | a) x | b) 0 | c) none of the above | | Do I have a valid complaint if the compiler I am using gives x? I would certainly think so. The action of a shift on an unsigned is pretty well defined, and the compiler you mention is not doing it. x>>32 should work the same on any machine, 8, 16, 32, or 64 bits. Also, the action of doing: x >>= 16; x>>= 16; better be the same as: x = x>>32; 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. People will try to tell you that's the way the hardware shift works... there is no justification for that, you are talking about the C source, and it should work correctly. What is needed is compiler code to check for a constant shift of 31+ and load zero (no runtime penalty) and generated code for variable shifts which does (pseudocode): cmp shftreg,32 jnc lbl1 mov datareg,0 jmp lbl2 lbl1: shiftr datareg,shftreg lbl2: Of course the logic could be improved in a machine dependent way... in my opinion reliable operation is more important than speed. I have no objection to a compiler switch which allows "unsafe optimization," and use that feature in some places myself. "it doesn't matter how fast you get the wrong answer" -me -- bill davidsen (wedu@ge-crd.arpa) {uunet | philabs | seismo}!steinmetz!crdos1!davidsen "Stupidity, like virtue, is its own reward" -me