[ 
http://issues.apache.org/jira/browse/LUCENE-467?page=comments#action_12357926 ] 

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

Here's a version that further generalizes the exponent zero point (below are 
negative exponents, above are positive), and includes the reverse byteToFloat 
as well.

  public static float byteToFloat(byte b, int numMantissaBits, int zeroExp) {
    if (b == 0) return 0.0f;
    int bits = (b&0xff) << (24-numMantissaBits);
    bits += (63-zeroExp) << 24;
    return Float.intBitsToFloat(bits);
  }

  public float byteToFloat(byte b) {
    return byteToFloat(b, 3, 15);
  }

  public static byte floatToByte(float f, int numMantissaBits, int zeroExp) {
    int shift = 24-numMantissaBits;   // 21 in old func
    int maxexp = 0xff >> numMantissaBits; // 31 in old func
    // int zeroExp = 0xff >> (numMantissaBits+1); // 15 in old func
    // int overflowexp = 0x100 >> numMantissaBits; // 32 in old func
    int overflowexp = maxexp+1;
    int bits = Float.floatToRawIntBits(f);
    int smallfloat = bits >> shift;
    if (smallfloat < (63-zeroExp)<<numMantissaBits) {
      return (bits<=0) ? (byte)0 : (byte)1;  // 0 or underflow
    } else if (smallfloat >= (63-zeroExp +overflowexp)<<numMantissaBits) {
      return -1;
    } else {
      return (byte)(smallfloat - ((63-zeroExp)<<numMantissaBits));
    }
  }

  public byte floatToByte(float f) {
    return floatToByte(f,3,15);
  }


> 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to