Message-Id: Comments: Authenticated sender is From: "Salvador Eduardo Tropea (SET)" Organization: INTI To: horst DOT kraemer AT snafu DOT de (Horst Kraemer), djgpp AT delorie DOT com Date: Tue, 14 Jul 1998 11:03:06 +0000 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: Re: DJGPP division optimisations In-reply-to: <35ab1f95.79373671@news.snafu.de> Precedence: bulk horst DOT kraemer AT snafu DOT de (Horst Kraemer) wrote: > On Mon, 13 Jul 1998 20:32:05 -0400, Vic wrote: > > > >it's true, I checked this. I put "unsigned" in front of the variables > >and the 2 loops performed the same (2.74 seconds) > > And the reason is clear. The operations x/2 and x>>1 are different if > x is a negative number. As you declared x to be a signed type the > compiler may not "pessimize" /2 to >>1. It doesn't know when compiling > that x will never be negative. > > int i = -1; > printf("%d %d\n", i/2 , i>>1 ); More or less, but both are shifts, the point is that integer=integer/2 is a little bit tricky: #include //#define TYPE unsigned #define TYPE int int main(int argc, char *argv[]) { TYPE x=argc/2; TYPE y=argc>>1; TYPE w=((unsigned)(argc))/2; TYPE z=((unsigned)(argc))>>1; printf("%d %d %d %d\n",x,y,w,z); return 0; } And it generates the following code: .file "p.c" gcc2_compiled.: ___gnu_compiled_c: .text LC0: .ascii "%d %d %d %d\12\0" .p2align 2 .def _main; .val _main; .scl 2; .type 044; .endef .globl _main _main: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax <<<<<<<<<<<<<<< Line 2 here comes the /2 .ln 2 movl %eax,%edx shrl $31,%edx addl %eax,%edx sarl $1,%edx <<<<<<<<<<<<<<< Line 3 is the >>1 .ln 3 movl %eax,%ecx sarl $1,%ecx <<<<<<<<<<<<<<< Using integers that's the same, so GCC calculates 1 <<<<<<<<<<<<<<< and ... .ln 4 shrl $1,%eax .ln 6 <<<<<<<<<<<<<<< ... puts it twice in the stack pushl %eax pushl %eax pushl %ecx pushl %edx pushl $LC0 call _printf .ln 7 xorl %eax,%eax .ln 8 leave ret Greetings, SET ------------------------------------ 0 -------------------------------- Visit my home page: http://set-soft.home.ml.org/ or http://www.geocities.com/SiliconValley/Vista/6552/ Salvador Eduardo Tropea (SET). (Electronics Engineer) Alternative e-mail: set-soft AT usa DOT net set AT computer DOT org ICQ: 2951574 Address: Curapaligue 2124, Caseros, 3 de Febrero Buenos Aires, (1678), ARGENTINA TE: +(541) 759 0013