On Fri, 22 Oct 2004, Sanjay Patel wrote:
> Note that you can 'optimize' this function by converting logical ops into
> arithmetic ops. (I didn't test the speed of this code, so I'm not sure that
> this is actually faster.)
>
> int foo(int a, int b, int c) {
> unsigned int boolSum = (a<0) + (b<0) + (c<0) ;
> if (boolSum>0) return 1;
> else return 0;
> }
>
How about using the sign bits directly:
if ((a | b | c) <0) return 1; else return 0;
or totally without branch in this particular example:
return ((unsigned)(a | b | c)) >> ((sizeof(int) << 3) - 1);
The weird shift distance is just an attempt to be portable between 32 and
64 bit platforms. There is still a portability problem with the shift,
though, because AFAIK there is no guarantee that rightshifts do the
expected thing with the sign bit on every platform (i.e. some machines
might not implement both signed and unsigned shift primitives, but I
think all do nowadays).
Holger
_______________________________________________
Do not post admin requests to the list. They will be ignored.
PerfOptimization-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/perfoptimization-dev/email@hidden
This email sent to email@hidden