Shifting question

John F. Haugh II jfh at rpp386.UUCP
Fri Jul 22 10:49:49 AEST 1988


In article <60670 at sun.uucp> alanf%smile at Sun.COM (Alan Fargusson) writes:
>In article <1818 at spar.SPAR.SLB.COM>, hunt at spar.SPAR.SLB.COM (Neil Hunt) writes:
>> We are talking factors of two in execution time for these functions.
>> What a pain !!
>
>What is your solution?

the solution which neil missed was to sacrifice code size.  he wrote

>> 	rshift = count > 0 ? count : 0;
>> 	lshift = count < 0 ? -count : 0;
>> 
>> 	for(j = 0; j < image->rows; j++)
>> 		for(i = 0; i < image->cols; i++)
>> 			image->pixels[i][j] =

where the test could have been

	if (count < 0) {
		count = - count;
		for ... 
			image->pixels[i][j] = (image->pixels[i][j] >> count);
	} else {
		for ...
			image->pixels[i][j] = (image->pixels[i][j] << count);
	}

this does not suffer a significant decrease in performance, the only added
overhead being the initial test and the possible negation.

- john.
-- 
John F. Haugh II                 +--------- Cute Chocolate Quote ---------
HASA, "S" Division               | "USENET should not be confused with
UUCP:   killer!rpp386!jfh        |  something that matters, like CHOCOLATE"
DOMAIN: jfh at rpp386.uucp          |             -- with my apologizes



More information about the Comp.lang.c mailing list