Article View: comp.lang.c.moderated
Article #2352Re: Pointer Arithmetic
From: Chris Torek
Date: Sat, 23 Mar 1996 06:42
Date: Sat, 23 Mar 1996 06:42
55 lines
2609 bytes
2609 bytes
In article <4ip9k0$sqs@solutions.solon.com> Ari Lukumies <aril@cmt.lpr.mail.carel.fi> writes: [given m,n of type short] > m = n << 8; >... when compiled using optimization, the compiler (MSC 7 for NT) >produced incorrect code: > > MOV AX,[n] > MOV AH,AL >... it left out XOR AL,AL ... (Actually, I would expect to see something along the lines of `xor al,al; mov ah,[n]' for a case like this -- note that my optimization depends on byte order.) I would find a bug this serious quite troubling, and perhaps look for another compiler. In article <4imabr$duc@solutions.solon.com> Stan Ryckman wrote: >>It's not that much harder to implement [multiply by shifting] in a >>compiler. This is true; also, if it is done correctly, index-multiplication by shifting falls naturally out of multiplication by shifting (simply turn the indexing into multiplication before doing the multiplication optimizations). >That's a clever compiler. I haven't come across one yet in the Intel world, Gcc 2.7.2 generates this (perhaps surprising) code for `i*15': movl 8(%ebp),%eax # i [supplied as a parameter, hence 8(frame)] leal (%eax,%eax,2),%eax # i <- i + i*2 = i*3 leal (%eax,%eax,4),%eax # i <- i + i*4 = i*5 # since 3*5=15, final i <- original i * 15 For i*31 we get: movl 8(%ebp),%eax sall $5,%eax subl 8(%ebp),%eax which is just `(i << 5) - i'. GCC also optimizes division by constants, particularly in the case when the division is known to have no remainder (e.g., when producing an index from pointer subtraction). To see how it works, pick up the gcc source and look at `synth_mult' in expmed.c (touching on another thread, I note that this makes gratuitous use of `alloca'; the two `struct algorithm' objects could simply be local variables inside synth_mult). Note also that there are three variants using synth_mult (`basic', `negate', and `add'; see expand_mult for details). -- In-Real-Life: Chris Torek, Berkeley Software Design Inc El Cerrito, CA Domain: torek@bsdi.com +1 510 234 3167
Technical Details
Message-ID:
<4j06ga$7np@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4i805f$47d@solutions.solon.com> <4ijp8i$1cf@solutions.solon.com> <4imabr$duc@solutions.solon.com> <4ip9k0$sqs@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: Chris Torek <torek@elf.bsdi.com> Newsgroups: comp.lang.c.moderated Subject: Re: Pointer Arithmetic Date: 23 Mar 1996 00:42:18 -0600 Organization: Berkeley Software Design, Inc. Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4j06ga$7np@solutions.solon.com> References: <4i805f$47d@solutions.solon.com> <4ijp8i$1cf@solutions.solon.com> <4imabr$duc@solutions.solon.com> <4ip9k0$sqs@solutions.solon.com> Reply-To: torek@bsdi.com NNTP-Posting-Host: solutions.solon.com