C code optimization for a = (b < c);
WILCOX at nosc-tecr.arpa
WILCOX at nosc-tecr.arpa
Sun May 28 07:12:37 AEST 1989
Assuming that the target machine implements two's compliment arithmetic
and doesn't have store condition code instructions, there is a simple
optimization that few C compilers seem to use. Given:
int a;
int b, c;
a = (b < c);
the usual approach is something like:
a = 0;
if( ! (b < c)) goto label;
a = 1;
label:
A better approach would seem to be:
a = (unsigned)((c - b) >> (WIDTH_OF_INT_IN_BITS - 1));
In other words, subtract rather than compare, and shift the resulting sign
bit into the least-significant bit position with zero extend. There is
also a variation using signed rather than unsigned shift, which generates
either -1 or 0, followed by an increment to produce either 0 or 1.
--Dwight Wilcox
Code 412
Naval Ocean Systems Center
San Diego, CA 92152-5000
More information about the Comp.unix.wizards
mailing list