home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!decwrl!pa.dec.com!engage.pko.dec.com!nntpd.lkg.dec.com!ryn.mro4.dec.com!news
- From: blair@snogum.enet.dec.com (Blair Phillips - Digital)
- Newsgroups: comp.os.vms
- Subject: Re: VAXC divide problems (V3.2)
- Message-ID: <1993Jan26.012657.5983@ryn.mro4.dec.com>
- Date: 26 Jan 93 01:26:57 GMT
- References: <1993Jan22.060311.5226@cs.rit.edu> <1993Jan22.231842.4799@eco.twg.com>
- Sender: news@ryn.mro4.dec.com (USENET News System)
- Reply-To: Blair@snogum.enet.dec.com
- Organization: Digital Equipment Corporation
- Lines: 71
-
-
- In article <1993Jan22.231842.4799@eco.twg.com>, reece@eco.twg.com (Reece R. Pollack) writes:
-
- >In article <1993Jan22.060311.5226@cs.rit.edu>, whd0675@cs.rit.edu (Walter H Dick, III) writes:
- >|>A statement in VAXC such as:
- >|>
- >|> i = (x + 511) / 512;
- >|>
- >|>generates ten or so instructions. Is there anyway to get it to generate:
- >|>
- >|> ADDL3 #511,x,R0
- >|> DIVL3 R0,#512,i
- >
- >That's funny, when I compiled this function:
- >
- >func( int x )
- >{
- > int i;
- >
- > i = (x + 511) / 512;
- > return( i );
- >}
- >
- >VAX C V3.2-044 generated this code:
- >
- > .entry func,^m<>
- > subl2 #4,sp
- > addl3 #511,4(ap),r0
- > divl2 #512,r0
- > ret
-
- If x is unsigned int rather than int, you get an unholy mess of code.
-
- There are two reasons for this:
- 1. The VAX doesn't have an unsigned divide operation, so the compiler generates
- code to cater for different combinations of signs in the dividend & divisor.
- 2. The compiler does a lousy job of optimizing unsigned arithmetic. In
- particular, it treats a constant divisor exactly the same as a variable,
- generating lots of useless conditional branches and dead code.
-
- The fix is simple - use a shift op instead.
-
- func( unsigned int x )
- {
- int i;
-
- i = (x + 511) >> 9;
- return( i );
- }
-
- generates
-
- func:
- .entry func,^m<>
- subl2 #4,sp
-
- addl3 #511,4(ap),r0
- extzv #9,#23,r0,r1
- movl r1,r0
- ret
-
- >
- >Don't ask me why it allocated space on the stack without using it,
-
- I think the stack space is for the condition handler.
- --
- ----------
- Blair Phillips Blair@snogum.enet.dec.com
- Digital Equipment Corp (Aust) P/L Phone: (+61 6) 2754874
- Canberra, Australia FAX : (+61 6) 2473654
- {Any opinions expressed are my own, not those of Digital Equipment Corporation}
-