cvsimport
[beagle.git] / beagled / Lucene.Net / Util / SmallFloat.cs
blob9c0a6efbed19264b0a0db8a2501aa6479e5ea618
1 /*
2 * Copyright 2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
19 namespace Lucene.Net.Util
23 /// <summary>Floating point numbers smaller than 32 bits.
24 ///
25 /// </summary>
26 /// <author> yonik
27 /// </author>
28 /// <version> $Id: SmallFloat.cs,v 1.2 2006/10/02 17:09:10 joeshaw Exp $
29 /// </version>
30 public class SmallFloat
33 /// <summary>Converts a 32 bit float to an 8 bit float.
34 /// <br>Values less than zero are all mapped to zero.
35 /// <br>Values are truncated (rounded down) to the nearest 8 bit value.
36 /// <br>Values between zero and the smallest representable value
37 /// are rounded up.
38 ///
39 /// </summary>
40 /// <param name="f">the 32 bit float to be converted to an 8 bit float (byte)
41 /// </param>
42 /// <param name="numMantissaBits">the number of mantissa bits to use in the byte, with the remainder to be used in the exponent
43 /// </param>
44 /// <param name="zeroExp">the zero-point in the range of exponent values
45 /// </param>
46 /// <returns> the 8 bit float representation
47 /// </returns>
48 public static sbyte FloatToByte(float f, int numMantissaBits, int zeroExp)
50 // Adjustment from a float zero exponent to our zero exponent,
51 // shifted over to our exponent position.
52 int fzero = (63 - zeroExp) << numMantissaBits;
53 int bits = System.BitConverter.ToInt32(System.BitConverter.GetBytes(f), 0);
54 int smallfloat = bits >> (24 - numMantissaBits);
55 if (smallfloat < fzero)
57 return (bits <= 0) ? (sbyte) 0 : (sbyte) 1; // underflow is mapped to smallest non-zero number.
59 else if (smallfloat >= fzero + 0x100)
61 return - 1; // overflow maps to largest number
63 else
65 return (sbyte) (smallfloat - fzero);
69 /// <summary>Converts an 8 bit float to a 32 bit float. </summary>
70 public static float ByteToFloat(byte b, int numMantissaBits, int zeroExp)
72 // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
73 // is only a little bit faster (anywhere from 0% to 7%)
74 if (b == 0)
75 return 0.0f;
76 int bits = (b & 0xff) << (24 - numMantissaBits);
77 bits += ((63 - zeroExp) << 24);
78 return BitConverter.ToSingle(BitConverter.GetBytes(bits), 0);
83 // Some specializations of the generic functions follow.
84 // The generic functions are just as fast with current (1.5)
85 // -server JVMs, but still slower with client JVMs.
88 /// <summary>floatToByte(b, mantissaBits=3, zeroExponent=15)
89 /// <br>smallest non-zero value = 5.820766E-10
90 /// <br>largest value = 7.5161928E9
91 /// <br>epsilon = 0.125
92 /// </summary>
93 public static sbyte FloatToByte315(float f)
95 int bits = System.BitConverter.ToInt32(System.BitConverter.GetBytes(f), 0);
96 int smallfloat = bits >> (24 - 3);
97 if (smallfloat < (63 - 15) << 3)
99 return (bits <= 0) ? (sbyte) 0 : (sbyte) 1;
101 if (smallfloat >= ((63 - 15) << 3) + 0x100)
103 return - 1;
105 return (sbyte) (smallfloat - ((63 - 15) << 3));
108 /// <summary>byteToFloat(b, mantissaBits=3, zeroExponent=15) </summary>
109 public static float Byte315ToFloat(byte b)
111 // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
112 // is only a little bit faster (anywhere from 0% to 7%)
113 if (b == 0)
114 return 0.0f;
115 int bits = (b & 0xff) << (24 - 3);
116 bits += ((63 - 15) << 24);
117 return BitConverter.ToSingle(BitConverter.GetBytes(bits), 0);
121 /// <summary>floatToByte(b, mantissaBits=5, zeroExponent=2)
122 /// <br>smallest nonzero value = 0.033203125
123 /// <br>largest value = 1984.0
124 /// <br>epsilon = 0.03125
125 /// </summary>
126 public static sbyte FloatToByte52(float f)
128 int bits = System.BitConverter.ToInt32(System.BitConverter.GetBytes(f), 0);
129 int smallfloat = bits >> (24 - 5);
130 if (smallfloat < (63 - 2) << 5)
132 return (bits <= 0) ? (sbyte) 0 : (sbyte) 1;
134 if (smallfloat >= ((63 - 2) << 5) + 0x100)
136 return - 1;
138 return (sbyte) (smallfloat - ((63 - 2) << 5));
141 /// <summary>byteToFloat(b, mantissaBits=5, zeroExponent=2) </summary>
142 public static float Byte52ToFloat(byte b)
144 // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup
145 // is only a little bit faster (anywhere from 0% to 7%)
146 if (b == 0)
147 return 0.0f;
148 int bits = (b & 0xff) << (24 - 5);
149 bits += ((63 - 2) << 24);
150 return BitConverter.ToSingle(BitConverter.GetBytes(bits), 0);