16 Apr
2014
16 Apr
'14
2:45 p.m.
If x and y both are unsigned integers, and we want to tell whether (x>0 and y>0), then how can we do it? 1. naive method: test x>0 and (if not) then also y>0. 2. x*y>0, but can fail if multiply overflows. 3. The gnu superoptimizer found 44 nonobvious methods, each involving 4 instructions, at least one of which always is subtract-with-borrow and/or add-with-carry. I think the basic idea is: if you regard (x,y) as a 2-digit number, and subtract (1,1), then you will generate at least one borrow if and only if (x=0 or y=0). 4. ((-x)&(-y))>>31 should be nonzero if x>0 and y>0, provided neither x nor y was greater than 2^31 - 1. On a 32-bit machine. -- Warren D. Smith