1 package org.apache.lucene.util;
3 * Copyright 2005 The Apache Software Foundation
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 /** Floating point numbers smaller than 32 bits.
23 public class SmallFloat {
25 /** Converts a 32 bit float to an 8 bit float.
26 * <br>Values less than zero are all mapped to zero.
27 * <br>Values are truncated (rounded down) to the nearest 8 bit value.
28 * <br>Values between zero and the smallest representable value
31 * @param f the 32 bit float to be converted to an 8 bit float (byte)
32 * @param numMantissaBits the number of mantissa bits to use in the byte, with the remainder to be used in the exponent
33 * @param zeroExp the zero-point in the range of exponent values
34 * @return the 8 bit float representation
36 public static byte floatToByte(float f, int numMantissaBits, int zeroExp) {
37 // Adjustment from a float zero exponent to our zero exponent,
38 // shifted over to our exponent position.
39 int fzero = (63-zeroExp)<<numMantissaBits;
40 int bits = Float.floatToRawIntBits(f);
41 int smallfloat = bits >> (24-numMantissaBits);
42 if (smallfloat <= fzero) {
44 (byte)0 // negative numbers and zero both map to 0 byte
45 :(byte)1; // underflow is mapped to smallest non-zero number.
46 } else if (smallfloat >= fzero + 0x100) {
47 return -1; // overflow maps to largest number
49 return (byte)(smallfloat - fzero);
53 /** Converts an 8 bit float to a 32 bit float. */
54 public static float byteToFloat(byte b, int numMantissaBits, int zeroExp) {
55 // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
56 // is only a little bit faster (anywhere from 0% to 7%)
57 if (b == 0) return 0.0f;
58 int bits = (b&0xff) << (24-numMantissaBits);
59 bits += (63-zeroExp) << 24;
60 return Float.intBitsToFloat(bits);
65 // Some specializations of the generic functions follow.
66 // The generic functions are just as fast with current (1.5)
67 // -server JVMs, but still slower with client JVMs.
70 /** floatToByte(b, mantissaBits=3, zeroExponent=15)
71 * <br>smallest non-zero value = 5.820766E-10
72 * <br>largest value = 7.5161928E9
75 public static byte floatToByte315(float f) {
76 int bits = Float.floatToRawIntBits(f);
77 int smallfloat = bits >> (24-3);
78 if (smallfloat <= ((63-15)<<3)) {
79 return (bits<=0) ? (byte)0 : (byte)1;
81 if (smallfloat >= ((63-15)<<3) + 0x100) {
84 return (byte)(smallfloat - ((63-15)<<3));
87 /** byteToFloat(b, mantissaBits=3, zeroExponent=15) */
88 public static float byte315ToFloat(byte b) {
89 // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
90 // is only a little bit faster (anywhere from 0% to 7%)
91 if (b == 0) return 0.0f;
92 int bits = (b&0xff) << (24-3);
93 bits += (63-15) << 24;
94 return Float.intBitsToFloat(bits);
98 /** floatToByte(b, mantissaBits=5, zeroExponent=2)
99 * <br>smallest nonzero value = 0.033203125
100 * <br>largest value = 1984.0
101 * <br>epsilon = 0.03125
103 public static byte floatToByte52(float f) {
104 int bits = Float.floatToRawIntBits(f);
105 int smallfloat = bits >> (24-5);
106 if (smallfloat <= (63-2)<<5) {
107 return (bits<=0) ? (byte)0 : (byte)1;
109 if (smallfloat >= ((63-2)<<5) + 0x100) {
112 return (byte)(smallfloat - ((63-2)<<5));
115 /** byteToFloat(b, mantissaBits=5, zeroExponent=2) */
116 public static float byte52ToFloat(byte b) {
117 // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
118 // is only a little bit faster (anywhere from 0% to 7%)
119 if (b == 0) return 0.0f;
120 int bits = (b&0xff) << (24-5);
121 bits += (63-2) << 24;
122 return Float.intBitsToFloat(bits);