Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uflorida!haven!adm!xadmx!WILCOX@nosc-tecr.arpa From: WILCOX@nosc-tecr.arpa Newsgroups: comp.unix.wizards Subject: C code optimization for a = (b < c); Message-ID: <19787@adm.BRL.MIL> Date: 27 May 89 21:12:37 GMT Sender: news@adm.BRL.MIL Lines: 29 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