1 package org.apache.lucene.util; 2 17 18 19 24 public class SmallFloat { 25 26 37 public static byte floatToByte(float f, int numMantissaBits, int zeroExp) { 38 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 :(byte)1; } else if (smallfloat >= fzero + 0x100) { 48 return -1; } else { 50 return (byte)(smallfloat - fzero); 51 } 52 } 53 54 55 public static float byteToFloat(byte b, int numMantissaBits, int zeroExp) { 56 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 71 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 89 public static float byte315ToFloat(byte b) { 90 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 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 117 public static float byte52ToFloat(byte b) { 118 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 |