KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > util > SmallFloat


1 package org.apache.lucene.util;
2 /**
3  * Copyright 2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 /** Floating point numbers smaller than 32 bits.
20  *
21  * @author yonik
22  * @version $Id$
23  */

24 public class SmallFloat {
25
26   /** Converts a 32 bit float to an 8 bit float.
27    * <br>Values less than zero are all mapped to zero.
28    * <br>Values are truncated (rounded down) to the nearest 8 bit value.
29    * <br>Values between zero and the smallest representable value
30    * are rounded up.
31    *
32    * @param f the 32 bit float to be converted to an 8 bit float (byte)
33    * @param numMantissaBits the number of mantissa bits to use in the byte, with the remainder to be used in the exponent
34    * @param zeroExp the zero-point in the range of exponent values
35    * @return the 8 bit float representation
36    */

37   public static byte floatToByte(float f, int numMantissaBits, int zeroExp) {
38     // Adjustment from a float zero exponent to our zero exponent,
39
// shifted over to our exponent position.
40
int fzero = (63-zeroExp)<<numMantissaBits;
41     int bits = Float.floatToRawIntBits(f);
42     int smallfloat = bits >> (24-numMantissaBits);
43     if (smallfloat < fzero) {
44       return (bits<=0) ?
45         (byte)0 // negative numbers and zero both map to 0 byte
46
:(byte)1; // underflow is mapped to smallest non-zero number.
47
} else if (smallfloat >= fzero + 0x100) {
48       return -1; // overflow maps to largest number
49
} else {
50       return (byte)(smallfloat - fzero);
51     }
52   }
53
54   /** Converts an 8 bit float to a 32 bit float. */
55   public static float byteToFloat(byte b, int numMantissaBits, int zeroExp) {
56     // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
57
// is only a little bit faster (anywhere from 0% to 7%)
58
if (b == 0) return 0.0f;
59     int bits = (b&0xff) << (24-numMantissaBits);
60     bits += (63-zeroExp) << 24;
61     return Float.intBitsToFloat(bits);
62   }
63
64
65   //
66
// Some specializations of the generic functions follow.
67
// The generic functions are just as fast with current (1.5)
68
// -server JVMs, but still slower with client JVMs.
69
//
70

71   /** floatToByte(b, mantissaBits=3, zeroExponent=15)
72    * <br>smallest non-zero value = 5.820766E-10
73    * <br>largest value = 7.5161928E9
74    * <br>epsilon = 0.125
75    */

76   public static byte floatToByte315(float f) {
77     int bits = Float.floatToRawIntBits(f);
78     int smallfloat = bits >> (24-3);
79     if (smallfloat < (63-15)<<3) {
80       return (bits<=0) ? (byte)0 : (byte)1;
81     }
82     if (smallfloat >= ((63-15)<<3) + 0x100) {
83       return -1;
84     }
85     return (byte)(smallfloat - ((63-15)<<3));
86  }
87
88   /** byteToFloat(b, mantissaBits=3, zeroExponent=15) */
89   public static float byte315ToFloat(byte b) {
90     // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
91
// is only a little bit faster (anywhere from 0% to 7%)
92
if (b == 0) return 0.0f;
93     int bits = (b&0xff) << (24-3);
94     bits += (63-15) << 24;
95     return Float.intBitsToFloat(bits);
96   }
97
98
99   /** floatToByte(b, mantissaBits=5, zeroExponent=2)
100    * <br>smallest nonzero value = 0.033203125
101    * <br>largest value = 1984.0
102    * <br>epsilon = 0.03125
103    */

104   public static byte floatToByte52(float f) {
105     int bits = Float.floatToRawIntBits(f);
106     int smallfloat = bits >> (24-5);
107     if (smallfloat < (63-2)<<5) {
108       return (bits<=0) ? (byte)0 : (byte)1;
109     }
110     if (smallfloat >= ((63-2)<<5) + 0x100) {
111       return -1;
112     }
113     return (byte)(smallfloat - ((63-2)<<5));
114   }
115
116   /** byteToFloat(b, mantissaBits=5, zeroExponent=2) */
117   public static float byte52ToFloat(byte b) {
118     // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
119
// is only a little bit faster (anywhere from 0% to 7%)
120
if (b == 0) return 0.0f;
121     int bits = (b&0xff) << (24-5);
122     bits += (63-2) << 24;
123     return Float.intBitsToFloat(bits);
124   }
125 }
126
Popular Tags