Math2mat
|
00001 /****************************************************************************** 00002 * STDLogic 00003 ****************************************************************************** 00004 * Auteur : Trolliet Gregory 00005 * Date : 6 mai 2009 00006 * Description : 00007 ******************************************************************************/ 00008 00009 package m2m.backend.vhdl; 00010 00011 import java.util.regex.Pattern; 00012 import java.util.regex.Matcher; 00013 00022 public class BinaryFloatingPoint { 00023 00024 private static BinaryFloatingPoint instance = null; 00025 private int sizeMant; 00026 private int sizeExp; 00027 00028 protected BinaryFloatingPoint() { 00029 this.sizeMant = 23; 00030 this.sizeExp = 8; 00031 } 00032 00033 public static BinaryFloatingPoint getInstance() { 00034 if(instance == null) { 00035 instance = new BinaryFloatingPoint(); 00036 } 00037 return instance; 00038 } 00039 00040 private String toBinary(double value) { 00041 String sVal = new String(); 00042 String hex = Double.toHexString((float)value); 00043 Pattern p = Pattern.compile("(-?)0x(1|0).(.*)p(.*)"); 00044 Matcher m = p.matcher(hex); 00045 boolean b = m.matches(); 00046 if (!b) { 00047 return sVal; 00048 } 00049 Boolean sign = m.group(1).equals("-"); 00050 int mant = Integer.parseInt(m.group(3), 16); 00051 int exp = Integer.parseInt(m.group(4), 16); 00052 sVal += (sign)?"1":"0"; 00053 if (m.group(2).equals("1")) { 00054 sVal += this.createBinaryExp(exp); 00055 sVal += this.createBinaryMant(mant); 00056 } else { 00057 sVal += this.createBinaryExp(-127); 00058 sVal += this.createBinaryMant(0); 00059 } 00060 return sVal; 00061 } 00062 00063 private double toFloat(String valueBit) { 00064 double val = 0; 00065 double biais = Math.pow(2,this.sizeExp-1.0)-1.0; 00066 String sVal = new String(); 00067 String sign = new String(); 00068 String sExp = new String(); 00069 String sMant = new String(); 00070 String hexExp = new String(); 00071 String hexMant = new String(); 00072 try { 00073 sign = (valueBit.substring(0, 1).equals("1"))?"-":""; 00074 sExp = valueBit.substring(1,this.sizeExp+1); 00075 sMant = valueBit.substring(this.sizeExp+1, 00076 this.sizeExp+this.sizeMant+1); 00077 } catch (IndexOutOfBoundsException e) { 00078 System.out.println("#Not a correct format number"); 00079 e.printStackTrace(); 00080 return 0.0; 00081 } 00082 for (int i=0; i<(4-this.sizeMant%4);i++) { 00083 sMant += "0"; 00084 } 00085 int exp = Integer.parseInt(sExp,2); 00086 int mant = Integer.parseInt(sMant,2); 00087 if (exp == 0 && mant == 0) { 00088 System.out.println("#Null number"); 00089 sVal = sign+"0x0.0p0"; 00090 val = 0.0; 00091 } else if (exp == 0) { 00092 System.out.println("#Denormalized number"); 00093 hexMant = Integer.toHexString(mant); 00094 sVal = sign+"0x0."+hexMant+"p0"; 00095 val = Double.valueOf(sVal); 00096 } else { 00097 hexExp = Integer.toHexString(exp-(int)biais); 00098 hexMant = Integer.toHexString(mant); 00099 sVal = sign+"0x1."+hexMant+"p"+hexExp; 00100 val = Double.valueOf(sVal); 00101 } 00102 return val; 00103 } 00104 00105 private String createBinaryExp(int exp) { 00106 String sExp = new String(); 00107 double biais = Math.pow(2,this.sizeExp-1.0)-1.0; 00108 String convert = Integer.toBinaryString(exp+(int)biais); 00109 int length = convert.length(); 00110 if (length < this.sizeExp) { 00111 for (int i=0; i<(this.sizeExp-length); i++) { 00112 sExp += "0"; 00113 } 00114 sExp += convert; 00115 } else if (length > this.sizeExp) { 00116 convert = convert.substring(0, this.sizeExp); 00117 sExp += convert; 00118 } else { 00119 sExp = convert; 00120 } 00121 return sExp; 00122 } 00123 00124 private String createBinaryMant(int mant) { 00125 String sMant = new String(); 00126 String convertTmp = Integer.toBinaryString(mant); 00127 String sMantTmp = Integer.toHexString(mant); 00128 String convert = new String(); 00129 //complete with '0' on the left if convertTmp.length is not a multiple of 4 00130 if (convertTmp.length() < (sMantTmp.length()*4)) { 00131 while (convertTmp.length() < (sMantTmp.length()*4)) { 00132 convertTmp = "0" + convertTmp; 00133 } 00134 } 00135 convert += convertTmp; 00136 int length = convert.length(); 00137 //complete with '0' on the right if the length of convert is different from the size of the mantissa 00138 if (length < this.sizeMant) { 00139 sMant += convert; 00140 for (int i=0; i<(this.sizeMant-length); i++) { 00141 sMant += "0"; 00142 } 00143 } else if (length > this.sizeMant) { 00144 convert = convert.substring(0, this.sizeMant); 00145 sMant += convert; 00146 } else { 00147 sMant = convert; 00148 } 00149 return sMant; 00150 } 00151 00152 public String getBinary(double value) { 00153 return this.toBinary(value); 00154 } 00155 public double getValue(String valBit) { 00156 return this.toFloat(valBit); 00157 } 00158 public void setSizeMant(int size) { 00159 this.sizeMant = size; 00160 } 00161 public int getSizeMant() { 00162 return this.sizeMant; 00163 } 00164 public void setSizeExp(int size) { 00165 this.sizeExp = size; 00166 } 00167 public int getSizeExp() { 00168 return this.sizeExp; 00169 } 00170 }