Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: float to int (kinda OT)



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

References: 
 >Re: float to int (kinda OT) (From: Sanjay Patel <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.