On May 20, 3:25 pm, Robert Green <[email protected]> wrote:
> Romain - The idea was to see which is faster to cut an int in half -
> multiplying by a float (.5) or dividing by two.  I've read that
> divides are terribly slow but apparently not as bad as floating point
> multiplications w/ a cast back to an int.

It's all done in software.  With hardware floating point support it
would likely fall the other way.

Right-shifting 1 bit will probably win, though you have to be careful
with negative numbers.  For example (in C):

int realdiv2(int x) { return x / 2; }
int fakediv2(int x) { return x >> 1; }
int betterdiv2(int x) { return (int) (x + ((unsigned int) x >> 31)) >>
1; }
void try(int val) {
    printf("%d real=%d fake=%d better=%d\n",
        val, realdiv2(val), fakediv2(val), betterdiv2(val));
}
int main() {
    try(5);
    try(-5);
    return 0;
}

Output:
5 real=2 fake=2 better=2
-5 real=-2 fake=-3 better=-2

(The code generated by gcc -O for realdiv2 and betterdiv2 are
identical on x86 and ARM.)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to