lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Yonik Seeley (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LUCENE-467) Use Float.floatToRawIntBits over Float.floatToIntBits
Date Thu, 17 Nov 2005 06:13:41 GMT
    [ http://issues.apache.org/jira/browse/LUCENE-467?page=comments#action_12357851 ] 

Yonik Seeley commented on LUCENE-467:
-------------------------------------

Fun with premature optimization!
I know this isn't a bottleneck, but here is the fastest floatToByte() that I could come up
with:

  public static byte floatToByte(float f) {
    int bits = Float.floatToRawIntBits(f);
    if (bits<=0) return 0;
    int mantissa = (bits & 0xffffff) >> 21;
    int exponent = (bits >>> 24) - 63 + 15;
    if ((exponent & ~0x1f)==0) return (byte)((exponent << 3) | mantissa);
    else if (exponent<0) return 1;
    return -1;
  }

Here is the original from Lucene for reference:

  public static byte floatToByte(float f) {
    if (f < 0.0f)                                 // round negatives up to zero
      f = 0.0f;

    if (f == 0.0f)                                // zero is a special case
      return 0;

    int bits = Float.floatToIntBits(f);           // parse float into parts
    int mantissa = (bits & 0xffffff) >> 21;
    int exponent = (((bits >> 24) & 0x7f) - 63) + 15;
    if (exponent > 31) {                          // overflow: use max value
      exponent = 31;
      mantissa = 7;
    }

    if (exponent < 0) {                           // underflow: use min value
      exponent = 0;
      mantissa = 1;
    }

    return (byte)((exponent << 3) | mantissa);    // pack into a byte
   }


Here is the performance (in seconds) on my P4 to do 640M conversions:

              JDK14-server  JDK14-client  JDK15-server  JDK15-client  JDK16-server  JDK16-client
orig       75.422               89.451            8.344                 57.631           
7.656                 57.984
new      67.265               78.891            5.906                 22.172            5.172
                18.750
diff        12%                    13%                41%                   160%         
    48%                   209%

Some decent gains... but the biggest moral of the story is: use Java>=1.5 and -server if
you can!



> Use Float.floatToRawIntBits over Float.floatToIntBits
> -----------------------------------------------------
>
>          Key: LUCENE-467
>          URL: http://issues.apache.org/jira/browse/LUCENE-467
>      Project: Lucene - Java
>         Type: Improvement
>   Components: Other
>     Versions: 1.9
>     Reporter: Yonik Seeley
>     Priority: Minor

>
> Copied From my Email:
>   Float.floatToRawIntBits (in Java1.4) gives the raw float bits without
> normalization (like *(int*)&floatvar would in C).  Since it doesn't do
> normalization of NaN values, it's faster (and hopefully optimized to a
> simple inline machine instruction by the JVM).
> On my Pentium4, using floatToRawIntBits is over 5 times as fast as
> floatToIntBits.
> That can really add up in something like Similarity.floatToByte() for
> encoding norms, especially if used as a way to compress an array of
> float during query time as suggested by Doug.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


Mime
View raw message