FirteX-高性能全文索引和检索平台API Documentation |
00001 // 00002 // Copyright(C) 2005--2006 Institute of Computing Tech, Chinese Academy of Sciences. 00003 // All rights reserved. 00004 // This file is part of FirteX (www.firtex.org) 00005 // 00006 // Use of the FirteX is subject to the terms of the software license set forth in 00007 // the LICENSE file included with this software, and also available at 00008 // http://www.firtex.org/license.html 00009 // 00010 // Author : 郭瑞杰(GuoRuijie) 00011 // Email : ruijieguo@software.ict.ac.cn,ruijieguo@gmail.com 00012 // Created : 2006/5/20 00013 // 00014 #ifndef _SMALLFLOAT_H 00015 #define _SMALLFLOAT_H 00016 00017 #include "StdHeader.h" 00018 00019 namespace firtex 00020 { 00021 namespace utility 00022 { 00023 class CSmallFloat 00024 { 00025 public: 00026 CSmallFloat(void) 00027 { 00028 } 00029 00030 ~CSmallFloat(void) 00031 { 00032 } 00033 protected: 00034 //float to bits conversion utilities... 00035 union ifvalue 00036 { 00037 int32_t i; 00038 float f; 00039 }; 00040 public: 00041 static int32_t floatToIntBits(float value) 00042 { 00043 ifvalue u; 00044 int32_t e, f; 00045 u.f = value; 00046 e = u.i & 0x7f800000; 00047 f = u.i & 0x007fffff; 00048 00049 if (e == 0x7f800000 && f != 0) 00050 u.i = 0x7fc00000; 00051 00052 return u.i; 00053 } 00054 00055 static float intBitsToFloat(int32_t bits) 00056 { 00057 ifvalue u; 00058 u.i = bits; 00059 return u.f; 00060 } 00061 00074 static byte floatToByte(float f, int numMantissaBits, int zeroExp) 00075 { 00076 // Adjustment from a float zero exponent to our zero exponent, 00077 // shifted over to our exponent position. 00078 int fzero = (63-zeroExp)<<numMantissaBits; 00079 int bits = floatToIntBits(f); 00080 int smallfloat = bits >> (24-numMantissaBits); 00081 if (smallfloat < fzero) 00082 { 00083 return (bits<=0) ? 00084 (byte)0 // negative numbers and zero both map to 0 byte 00085 :(byte)1; // underflow is mapped to smallest non-zero number. 00086 } else if (smallfloat >= fzero + 0x100) 00087 { 00088 return (byte)-1; // overflow maps to largest number 00089 } 00090 else 00091 { 00092 return (byte)(smallfloat - fzero); 00093 } 00094 } 00095 00097 static float byteToFloat(byte b, int numMantissaBits, int zeroExp) 00098 { 00099 if (b == 0) return 0.0f; 00100 int bits = (b&0xff) << (24-numMantissaBits); 00101 bits += (63-zeroExp) << 24; 00102 return intBitsToFloat(bits); 00103 } 00104 00105 00106 // 00107 // Some specializations of the generic functions follow. 00108 // The generic functions are just as fast with current (1.5) 00109 // -server JVMs, but still slower with client JVMs. 00110 // 00111 00117 static byte floatToByte315(float f) 00118 { 00119 int bits = floatToIntBits(f); 00120 int smallfloat = bits >> (24-3); 00121 if (smallfloat < (63-15)<<3) { 00122 return (bits<=0) ? (byte)0 : (byte)1; 00123 } 00124 if (smallfloat >= ((63-15)<<3) + 0x100) 00125 { 00126 return (byte)-1; 00127 } 00128 return (byte)(smallfloat - ((63-15)<<3)); 00129 } 00130 00132 static float byte315ToFloat(byte b) 00133 { 00134 // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup 00135 // is only a little bit faster (anywhere from 0% to 7%) 00136 if (b == 0) return 0.0f; 00137 int bits = (b&0xff) << (24-3); 00138 bits += (63-15) << 24; 00139 return intBitsToFloat(bits); 00140 } 00141 00142 00148 static byte floatToByte52(float f) 00149 { 00150 int bits = floatToIntBits(f); 00151 int smallfloat = bits >> (24-5); 00152 if (smallfloat < (63-2)<<5) { 00153 return (bits<=0) ? (byte)0 : (byte)1; 00154 } 00155 if (smallfloat >= ((63-2)<<5) + 0x100) { 00156 return (byte)-1; 00157 } 00158 return (byte)(smallfloat - ((63-2)<<5)); 00159 } 00160 00162 static float byte52ToFloat(byte b) 00163 { 00164 // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup 00165 // is only a little bit faster (anywhere from 0% to 7%) 00166 if (b == 0) return 0.0f; 00167 int bits = (b&0xff) << (24-5); 00168 bits += (63-2) << 24; 00169 return intBitsToFloat(bits); 00170 } 00171 }; 00172 } 00173 } 00174 00175 #endif
http://www.firtex.org http://www.sourceforge.net/projects/firtex