On the 0x5A4 day of Apache Harmony Tim Ellison wrote: > /** > @@ -402,25 +384,31 @@ > * @since 1.4 > */ > public static int compare(float float1, float float2) { > - int f1, f2; > - int NaNbits = Float.floatToIntBits(Float.NaN); > - if ((f1 = Float.floatToIntBits(float1)) == NaNbits) { > - if (Float.floatToIntBits(float2) == NaNbits) { > - return 0; > - } > + > + if (float1 > float2) { > return 1; > } > - if ((f2 = Float.floatToIntBits(float2)) == NaNbits) { > + if (float2 > float1) { > return -1; > } > - if (float1 == float2) { > - if (f1 == f2) { > + > + int f1 = floatToRawIntBits(float1); > + int f2 = floatToRawIntBits(float2); > + if (f1 == f2) { > + return 0; > + }
I'd suggest a slightly different code path: move native calls further below. floatToRawIntBits() is *very* slow and only useful for -0f checking. Hitting two zeroes is less likely than hitting a NaN IMHO. Should look like this: // Non-zero and non-NaN equality checking. if (float1 == float2 && (0.0f != float1 || 0.0f != float2)) { return 0; } // NaNs are equal to other NaNs and larger than any other float. [...] // Sort out zeroes. int f1 = floatToRawIntBits(float1); int f2 = floatToRawIntBits(float2); [...] > + > + // NaNs are equal to other NaNs and larger than any other float > + if (isNaN(float1)) { > + if (isNaN(float2)) { > return 0; > } > - // check for -0 > - return f1 > f2 ? 1 : -1; > + return 1; > + } else if (isNaN(float2)) { > + return -1; > } > - return float1 > float2 ? 1 : -1; > + > + return (f1 < f2) ? -1 : 1; > } > > /** > -- Egor Pasko