Efficient coding considered harmful?

Chris Torek chris at mimsy.UUCP
Thu Nov 10 05:15:49 AEST 1988


In article <23459 at amdcad.AMD.COM> tim at crackle.amd.com (Tim Olson) writes:
>A good compiler can also recognize a signed integer divided by a power
>of 2, and use the following trick:
[copy and add sign bit]

It is worth noting that Sun's PCC-based compiler does something similar:

	f() {
		register i, j;
		...
		i = j / 2;
		...
		i = (unsigned)j / 2;
		...
	}

compiles (using the SunOS 3.5 compiler running under SunOS 3.2) to

	movl	d6,d0
	tstl	d0
	jge	L2000000
	addql	#1,d0
L2000000:
	asrl	#1,d0
	movl	d0,d7

(clearly not the best code in the world) and

	movl	d6,d0
	lsrl	#0x1,d0
	movl	d0,d7

(why some constants in hex and others decimal we may never know :-) ).
The optimiser transforms the latter to the more appropriate

	movl	d6,d7
	lsrl	#1,d7

(yes, the 0x disappears).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list