Path: utzoo!attcan!uunet!lll-winken!lll-lcc!ames!mailrus!purdue!i.cc.purdue.edu!k.cc.purdue.edu!l.cc.purdue.edu!cik From: cik@l.cc.purdue.edu (Herman Rubin) Newsgroups: comp.lang.c Subject: Re: Shifting question Summary: Another reason for more flexibility Message-ID: <839@l.cc.purdue.edu> Date: 20 Jul 88 13:00:42 GMT References: <705@bnr-rsc.UUCP> <11556@steinmetz.ge.com> <60290@sun.uucp> <2813@ttrdc.UUCP> Organization: Purdue University Statistics Department Lines: 57 In article <2813@ttrdc.UUCP>, levy@ttrdc.UUCP (Daniel R. Levy) writes: > In article <1818@spar.SPAR.SLB.COM>, hunt@spar.SPAR.SLB.COM (Neil Hunt) writes: > # This has been a pet peeve of mine for a long time: does the following function > # work for applying a shift (left or right) to all pixels in a large image ? > # > # shift_pixel(image, count) > # struct image *image; > # int count; > # { > # int i, j; > # > # for(j = 0; j < image->rows; j++) > # for(i = 0; i < image->cols; i++) > # image->pixels[i][j] >>= count; > # } > # > # No, it doesn't; the last line has to be: > # > # if(count > 0) > # image->pixels[i][j] >>= count; > # else > # image->pixels[i][j] <<= -count; > # > # because of the stupid undefined rule about shifts. The articles go on about alternatives. HOWEVER: This merely points out the problems. One can give a restrictive rule about shifts, which will handle some of the questions. The alternative is to allow the programmer to specify what is wanted if the shift is not of the trivial types envisaged by K&R. Do I want to allow a shift by a negative quantity to be blocked or interpreted as a shift in the opposite direction? What does the hardware do about shifts of excessive amounts? What does it do if the shift amount is negative? I have certain procedures which use variable size right shifts. Now some of the computers I use only have left shifts--right shifts are negative left shifts. On those computers, I would produce the negatives of the shift amounts _initially_. On the computers which have separate right shift instructions and do not allow negative shifts, I would naturally do the other. The implementation of procedures needing shifts being different at different times, it is clear that either the compiler must be able to receive and handle all of the questions above, and all of the questions raised by the others who have posted about shifts, or that the programmer must be able to make these decisions, including which unsafe optimizations can be used, or some combination of the above. I know of no compilers which can even receive the information. Of course, one could put enough restrictions on the implementation of shift to avoid most of the problems. If one is not careful, the execution speed of a program can be reduced by a huge factor, and an algorithm which is efficient on one machine can crawl on another. -- Herman Rubin, Dept. of Statistics, Purdue Univ., West Lafayette IN47907 Phone: (317)494-6054 hrubin@l.cc.purdue.edu (Internet, bitnet, UUCP)