Re: Float/Double compare

2007-07-02 Thread Andrew Haley
Ian Rogers writes: > Andrew Haley wrote: > > We know that any calculation involving NaN returns false, right? So, > > simple cases can be done first without the (possibly expensive) move > > out of the FPU: > > > > if (x < y) > > return -1; > > if (x > y) > >

Re: Float/Double compare

2007-07-02 Thread Ian Rogers
Andrew Haley wrote: We know that any calculation involving NaN returns false, right? So, simple cases can be done first without the (possibly expensive) move out of the FPU: if (x < y) return -1; if (x > y) return 1; then you can do the doubleToLongBits:

Re: Float/Double compare

2007-07-02 Thread Andrew Haley
Ian Rogers writes: > Ian Rogers wrote: > > public static int compare(float x, float y) > > { > > if (isNaN(x)) > > return isNaN(y) ? 0 : 1; > >if (isNaN(y)) > > return -1; > > // recall that 0.0 == -0.0, so we convert to infinities and try again > > if (x == 0

Re: Float/Double compare

2007-07-01 Thread Christian Thalinger
On Sat, 2007-06-30 at 16:57 -0400, Ian Rogers wrote: > In terms of performance I just tried sorting an array of floats with the > new code (floatAsIntBits should have been floatToIntBits, sorry). The > test was to initialize an array of floats backwards and then sort it to > be ascending. Runnin

Re: Float/Double compare

2007-07-01 Thread Ian Rogers
Hi Mark, Mark Wielaard wrote: Hi Ian, On Sat, 2007-06-30 at 15:25 -0400, Ian Rogers wrote: public static int compare(float x, float y) { int ix = floatAsIntBits(x); int iy = floatAsIntBits(y); if (ix == iy) return 0; if (isNaN(x)) return 1; if (isNaN(y)) return -1; int ix_

Re: Float/Double compare

2007-07-01 Thread Mark Wielaard
Hi Ian, On Sat, 2007-06-30 at 15:25 -0400, Ian Rogers wrote: > public static int compare(float x, float y) > { >int ix = floatAsIntBits(x); >int iy = floatAsIntBits(y); >if (ix == iy) return 0; >if (isNaN(x)) return 1; >if (isNaN(y)) return -1; >int ix_sign = ix>>31; >i

Re: Float/Double compare

2007-06-30 Thread Ian Rogers
In terms of performance I just tried sorting an array of floats with the new code (floatAsIntBits should have been floatToIntBits, sorry). The test was to initialize an array of floats backwards and then sort it to be ascending. Running a large enough array and enough iterations to get a ~4 sec

Re: Float/Double compare

2007-06-30 Thread Ian Rogers
Ian Rogers wrote: public static int compare(float x, float y) { if (isNaN(x)) return isNaN(y) ? 0 : 1; if (isNaN(y)) return -1; // recall that 0.0 == -0.0, so we convert to infinities and try again if (x == 0 && y == 0) return (int) (1 / x - 1 / y); if (x ==

Float/Double compare

2007-06-30 Thread Ian Rogers
Hi everyone, I was tracking a bug and came across the code in Float.compare which is very similar to Double.compare. It just so happened that the bug was being caused by sorting arrays containing lots of floating point 0s. This occurs quite frequently in the Jikes RVM when we have an array of