This is the mail archive of the frysk-cvs@sources.redhat.com mailing list for the frysk project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[SCM] master: Updates for migration to BigFloatingPoint.


The branch, master has been updated
       via  33a27f5819580a5e4d175f908fc9ed9a18b5ec3e (commit)
       via  10a04274e7e6d5b785459fece631e571e2eda71a (commit)
      from  0ca8a85014ea151d6fcb01676cacd86841da3af4 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email.

- Log -----------------------------------------------------------------
commit 33a27f5819580a5e4d175f908fc9ed9a18b5ec3e
Author: Teresa <tthomas@redhat.com>
Date:   Thu Jan 17 14:32:22 2008 -0500

    Updates for migration to BigFloatingPoint.
    
    frysk-core/frysk/value/ChangeLog
    2008-01-17  Teresa Thomas  <tthomas@redhat.com>
    
    	* Value.java (asBigFloatingPoint): New.
    	(doubleValue): Use BigFloatingPoint.
    	* IntegerType.java (getBigFloatingPoint): New.
    	(bigFloatingPointValue): New.
    	* FloatingPointType.java (getBigFloatingPoint): New.
    	(bigFloatingPointValue): New.
    	(assign): Use BigFloatingPoint.
    	* ArithmeticType.java (getBigFloatingPoint): New
    	(bigFloatingPointValue): New.
    	* Format.java (printFloatingPoint): Use BigFloatingPoint.

commit 10a04274e7e6d5b785459fece631e571e2eda71a
Author: Teresa <tthomas@redhat.com>
Date:   Thu Jan 17 13:57:39 2008 -0500

    Files to define and support IEEE 754/854 floating point.
    
    frysk-core/frysk/value/ChangeLog
    2008-01-17  Teresa Thomas  <tthomas@redhat.com>
    
    	* FloatingPointFormat.java: New.
    	* TestFloatingPoint854Format.java: New.
    	* FloatingPoint854Format.java: New.
    	* BigFloatingPoint.java: New.

-----------------------------------------------------------------------

Summary of changes:
 frysk-core/frysk/value/ArithmeticType.java         |   11 +-
 frysk-core/frysk/value/BigFloatingPoint.java       |  144 ++++++++++++++
 frysk-core/frysk/value/ChangeLog                   |   18 ++
 frysk-core/frysk/value/FloatingPoint854Format.java |  197 ++++++++++++++++++++
 .../{ClassType.java => FloatingPointFormat.java}   |   18 +-
 frysk-core/frysk/value/FloatingPointType.java      |   36 ++++-
 frysk-core/frysk/value/Format.java                 |   11 +-
 frysk-core/frysk/value/IntegerType.java            |    9 +-
 .../frysk/value/TestFloatingPoint854Format.java    |  189 +++++++++++++++++++
 frysk-core/frysk/value/Value.java                  |    6 +-
 10 files changed, 614 insertions(+), 25 deletions(-)
 create mode 100644 frysk-core/frysk/value/BigFloatingPoint.java
 create mode 100644 frysk-core/frysk/value/FloatingPoint854Format.java
 copy frysk-core/frysk/value/{ClassType.java => FloatingPointFormat.java} (90%)
 create mode 100644 frysk-core/frysk/value/TestFloatingPoint854Format.java

First 500 lines of diff:
diff --git a/frysk-core/frysk/value/ArithmeticType.java b/frysk-core/frysk/value/ArithmeticType.java
index 449e97d..0dadf8a 100644
--- a/frysk-core/frysk/value/ArithmeticType.java
+++ b/frysk-core/frysk/value/ArithmeticType.java
@@ -124,7 +124,16 @@ public abstract class ArithmeticType
      * floating-point value.  This does not do type-conversion.
      */
     abstract BigFloat getBigFloat(Location location);
-
+    /**
+     * Return the entire location, interpreting the raw bytes as a
+     * floating-point value.  
+     */
+    abstract BigFloatingPoint getBigFloatingPoint(Location location);
+    /**
+     * Return the arthmetic type converted to a BigFloatingPoint, this may
+     * involve truncation and/or rounding.
+     */
+    abstract BigFloatingPoint bigFloatingPointValue(Location location);
     /**
      * Re-write the entire location with the big integer value.  This
      * does not do type conversion.  The underlying type determines if
diff --git a/frysk-core/frysk/value/BigFloatingPoint.java b/frysk-core/frysk/value/BigFloatingPoint.java
new file mode 100644
index 0000000..3c8e1d9
--- /dev/null
+++ b/frysk-core/frysk/value/BigFloatingPoint.java
@@ -0,0 +1,144 @@
+// This file is part of the program FRYSK.
+//
+// Copyright 2007, Red Hat Inc.
+//
+// FRYSK is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// FRYSK is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with FRYSK; if not, write to the Free Software Foundation,
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+// 
+// In addition, as a special exception, Red Hat, Inc. gives You the
+// additional right to link the code of FRYSK with code not covered
+// under the GNU General Public License ("Non-GPL Code") and to
+// distribute linked combinations including the two, subject to the
+// limitations in this paragraph. Non-GPL Code permitted under this
+// exception must only link to the code of FRYSK through those well
+// defined interfaces identified in the file named EXCEPTION found in
+// the source code files (the "Approved Interfaces"). The files of
+// Non-GPL Code may instantiate templates or use macros or inline
+// functions from the Approved Interfaces without causing the
+// resulting work to be covered by the GNU General Public
+// License. Only Red Hat, Inc. may make changes or additions to the
+// list of Approved Interfaces. You must obey the GNU General Public
+// License in all respects for all of the FRYSK code and other code
+// used in conjunction with FRYSK except the Non-GPL Code covered by
+// this exception. If you modify this file, you may extend this
+// exception to your version of the file, but you are not obligated to
+// do so. If you do not wish to provide this exception without
+// modification, you must delete this exception statement from your
+// version and license this file solely under the GPL without
+// exception.
+
+package frysk.value;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+/**
+ * Floating point type - uses java.math.BigDecimal.
+ */
+public class BigFloatingPoint 
+{
+    private BigDecimal value;
+    private int encoding;
+        
+    // FIXME: Does not differentiate between 
+    // signalling and non signalling NaNs.
+    public static final int proper = 0;
+    public static final int NaN = 1;
+    public static final int posInf = 2;
+    public static final int negInf = 3;
+    
+    /**
+     * Create a normal BigFloatingPoint from a BigDecimal.
+     */
+    BigFloatingPoint (BigDecimal value) {
+        this.value = value;
+        this.encoding = proper;
+    }
+    
+    /**
+     * Create a normal BigFloatingPoint from a BigInteger.
+     */
+    BigFloatingPoint (BigInteger value) {
+        this.value = new BigDecimal(value);
+        this.encoding = proper;
+    }    
+    
+    /**
+     * Create a BigFloatingPoint from a BigDecimal and encoding.
+     * Use for NaNs and infinities.
+     */
+    BigFloatingPoint (BigDecimal value, int encoding){
+    	this.value = value;
+    	this.encoding = encoding;
+    }
+
+    /**
+     * Used for testing.
+     */
+    BigFloatingPoint (String value) {
+    	this.value = new BigDecimal (value);
+    	this.encoding = proper;
+    }
+    
+    /**
+     * Converts a BigDecimal into a BigInteger. Discards 
+     * fractional part.
+     */
+    BigInteger bigIntegerValue() {
+    	return value.toBigInteger();
+    }
+    
+    BigDecimal getValue () {
+        return value;
+    }
+
+    int getEncoding () {
+        return encoding;
+    }
+
+    public String toString(int size) {
+    	String retValue;
+    	switch (encoding) {
+    	        // FIXME: Use BigDecimal's toString 
+    	case 0: 
+    		   if (size < 8)
+    		       retValue = Float.toString(floatValue());
+    	        else
+    		       retValue = Double.toString(doubleValue());
+    	       break;
+//    	       retValue = value.toString();
+//    	       break;
+    	case 1:retValue = "NaN";
+    	       break;
+    	case 2:retValue = "Positive Infinity";
+    	       break;
+    	case 3:retValue = "Negative Infinity";
+    	       break; 
+    	default: return "Unknown";
+    	}
+    	return retValue;
+    }
+
+    public double doubleValue() {
+    	   return value.doubleValue();
+    }
+    
+    public float floatValue() {
+    	return value.floatValue();
+    }
+
+    public boolean equals (BigFloatingPoint o) {
+    	return (o.getValue().compareTo(this.value) == 0
+    			&& o.getEncoding() == this.encoding);
+    }
+}
diff --git a/frysk-core/frysk/value/ChangeLog b/frysk-core/frysk/value/ChangeLog
index dce5950..143e9fe 100644
--- a/frysk-core/frysk/value/ChangeLog
+++ b/frysk-core/frysk/value/ChangeLog
@@ -1,3 +1,21 @@
+2008-01-17  Teresa Thomas  <tthomas@redhat.com>
+
+	* Value.java (asBigFloatingPoint): New.
+	(doubleValue): Use BigFloatingPoint.
+	* IntegerType.java (getBigFloatingPoint): New.
+	(bigFloatingPointValue): New.
+	* FloatingPointType.java (getBigFloatingPoint): New.
+	(bigFloatingPointValue): New.
+	(assign): Use BigFloatingPoint.
+	* ArithmeticType.java (getBigFloatingPoint): New
+	(bigFloatingPointValue): New.
+	* Format.java (printFloatingPoint): Use BigFloatingPoint.
+	
+	* FloatingPointFormat.java: New.
+	* TestFloatingPoint854Format.java: New.
+	* FloatingPoint854Format.java: New. 
+	* BigFloatingPoint.java: New.
+
 2007-01-07  Nurdin Premji  <npremji@redhat.com>
 
 	* TestClass.java (getType): Modified to do most of the work.
diff --git a/frysk-core/frysk/value/FloatingPoint854Format.java b/frysk-core/frysk/value/FloatingPoint854Format.java
new file mode 100644
index 0000000..ed3e81e
--- /dev/null
+++ b/frysk-core/frysk/value/FloatingPoint854Format.java
@@ -0,0 +1,197 @@
+// This file is part of the program FRYSK.
+//
+// Copyright 2007, Red Hat Inc.
+//
+// FRYSK is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// FRYSK is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with FRYSK; if not, write to the Free Software Foundation,
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+// 
+// In addition, as a special exception, Red Hat, Inc. gives You the
+// additional right to link the code of FRYSK with code not covered
+// under the GNU General Public License ("Non-GPL Code") and to
+// distribute linked combinations including the two, subject to the
+// limitations in this paragraph. Non-GPL Code permitted under this
+// exception must only link to the code of FRYSK through those well
+// defined interfaces identified in the file named EXCEPTION found in
+// the source code files (the "Approved Interfaces"). The files of
+// Non-GPL Code may instantiate templates or use macros or inline
+// functions from the Approved Interfaces without causing the
+// resulting work to be covered by the GNU General Public
+// License. Only Red Hat, Inc. may make changes or additions to the
+// list of Approved Interfaces. You must obey the GNU General Public
+// License in all respects for all of the FRYSK code and other code
+// used in conjunction with FRYSK except the Non-GPL Code covered by
+// this exception. If you modify this file, you may extend this
+// exception to your version of the file, but you are not obligated to
+// do so. If you do not wish to provide this exception without
+// modification, you must delete this exception statement from your
+// version and license this file solely under the GPL without
+// exception.
+
+package frysk.value;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+public class FloatingPoint854Format
+     extends FloatingPointFormat
+{
+    public static final FloatingPointFormat IEEE32  = new FloatingPoint854Format (4, 1, 8, 9, 23);
+    public static final FloatingPointFormat IEEE64  = new FloatingPoint854Format (8, 1, 11, 12, 52);
+    public static final FloatingPointFormat IEEE128 = new FloatingPoint854Format (16, 1+48, 15, 16+48, 64);
+    public static final FloatingPointFormat IEEE80  = new FloatingPoint854Format (10, 1, 15, 16, 64);
+    public static final FloatingPointFormat IEEE96  = new FloatingPoint854Format (12, 1+16, 15, 16+16, 64);
+        
+    private Packing packExponent;
+    private Packing packFraction;
+    private int sizeF;
+    private int sizeE;
+    private int size;
+    private static final BigDecimal two = BigDecimal.ONE.add(BigDecimal.ONE);
+    
+    /**
+    * @param size - size of floating point in bytes
+    * @param idxE - begin bit index of exponent field
+    * @param sizeE - bit size of exponent field
+    * @param idxF - begin bit index of fraction field
+    * @param sizeF - size of fraction field
+    *  Note: include j bit in fraction field, where applicable.
+    */ 
+   FloatingPoint854Format(int size,
+	                  int idxE, int sizeE,
+	                  int idxF, int sizeF) {
+       packExponent = new Packing (size, idxE, sizeE);
+       packFraction = new Packing (size, idxF, sizeF);
+       this.sizeF = sizeF;
+       this.sizeE = sizeE;
+       this.size = size;
+   }
+   
+   BigFloatingPoint unpack (byte[] bytes) {
+       int sIndex = 0;
+       if (this.size == 12)
+	   sIndex = 2;
+       else if (this.size == 16)
+	   sIndex = 6;
+       int s = (((bytes[sIndex] >> 7) & 0x01) == 0) ? -1:1;
+       BigInteger e = packExponent.unpackUnsigned(bytes);
+       BigInteger f = packFraction.unpackUnsigned(bytes);
+       BigInteger maxE = (BigInteger.valueOf(2).pow(sizeE)).subtract
+                         (BigInteger.ONE);
+       return toBigFP (s, e, f, maxE);              
+   }
+
+   /**
+    * Gets mantissa value according to IEEE 754/854 floating point rules
+    * 
+    * @param f - value of fraction field (including j bit where applicable)
+    * @param e - value of exponent field
+    * @param sizeOfF - bit size of fraction field 
+    *                  (including j bit where applicable)
+    * @return mantissa
+    */
+   private BigDecimal getMantissa (BigInteger f, BigInteger e, int sizeOfF) {
+       if (sizeOfF == 64)
+	   return getMantissaExtended (f, sizeOfF);
+
+       int trailingZeroes = f.getLowestSetBit();
+       BigDecimal m = new BigDecimal (f.shiftRight(trailingZeroes));
+       m = divide (m, two.pow(sizeOfF-trailingZeroes));
+       return (e.compareTo(BigInteger.ZERO) == 0)? m : BigDecimal.ONE.add(m);
+   }
+
+   private BigDecimal getMantissaExtended (BigInteger f, int sizeOfF){
+       int trailingZeroes = f.getLowestSetBit();
+       boolean j = f.testBit(f.bitLength()-1);
+       f = f.clearBit(f.bitLength()-1);
+       BigDecimal m = new BigDecimal (f.shiftRight(trailingZeroes));
+       m = divide (m, two.pow(sizeOfF-trailingZeroes-1));
+       return (j == false)? m : BigDecimal.ONE.add(m);
+   }
+   
+   /**
+    * Calculate floating point value according to IEEE 754/854 rules.
+    * @param s - sign bit
+    * @param e - value of exponent field 
+    * @param m - mantissa
+    * @param maxE - max possible value of exponent field
+    */
+   private BigFloatingPoint toBigFP (int s, 
+	                                 BigInteger e, BigInteger f, 
+	                                 BigInteger maxE) {
+       BigDecimal m = getMantissa(f, e, sizeF);
+       BigDecimal result = BigDecimal.ZERO;
+       BigDecimal one = BigDecimal.ONE;
+       int halfMaxE = maxE.intValue()/2;
+
+       if (e.compareTo(maxE) == 0) {
+           if (f.compareTo(BigInteger.ZERO) != 0) {
+               // FIXME: Should NaNs retain value of fraction
+               // or mantissa?
+               return new BigFloatingPoint(m, BigFloatingPoint.NaN);
+           }
+           else {
+               // FIXME: 0 or m?
+               return (s == -1)? new BigFloatingPoint (m, BigFloatingPoint.posInf):
+        	                     new BigFloatingPoint (m, BigFloatingPoint.negInf);
+           }
+       }
+       else if (e.compareTo(BigInteger.ZERO) == 0) {
+           if (f.compareTo(BigInteger.ZERO) != 0) {
+               result = divide(one, two.pow(halfMaxE-1)).multiply(m);
+           }
+           else {
+               result = BigDecimal.ZERO;
+           }
+           return (s == -1)? new BigFloatingPoint(result):
+                             new BigFloatingPoint(result.negate());
+       }
+       else if (e.compareTo(BigInteger.ZERO) > 0 && e.compareTo(maxE) < 0) {
+           if (e.intValue()-halfMaxE < 0)
+               result = divide (one, two.pow(-e.intValue()+halfMaxE)).multiply(m);
+           else
+               result = two.pow(e.intValue()-halfMaxE).multiply(m);
+           return (s == -1)? new BigFloatingPoint(result):
+                             new BigFloatingPoint(result.negate());
+       }
+       else {
+           throw new RuntimeException 
+                    ("IEEE 854 Floating Point conversion error.");
+       }
+   }
+   
+   private BigDecimal divide (BigDecimal a, BigDecimal b) {
+       BigDecimal result[] = a.divideAndRemainder(b);
+       // FIXME: Use long division? Use BigDecimal's 
+       // divide(BigDecimal,MathContext) when frysk 
+       // moves to java 1.5.0.
+       double fraction = result[1].doubleValue()/b.doubleValue();
+       return result[0].add(BigDecimal.valueOf(fraction));
+   }
+      
+   /**
+    * FIXME: Convert to byte[] using Packing methods. Need to
+    * store mantissa and exponent value?
+    */
+   byte[] pack (BigFloatingPoint value, int size){
+	   switch (size) {
+	   case 4:
+		   return BigInteger.valueOf(Float.floatToRawIntBits(value.floatValue()))
+		   .toByteArray();
+	   case 8:
+		   return BigInteger.valueOf(Double.doubleToRawLongBits(value.doubleValue()))
+		   .toByteArray();
+	   default:
+		   return new byte[0];
+	   }
+   }
+}
diff --git a/frysk-core/frysk/value/ClassType.java b/frysk-core/frysk/value/FloatingPointFormat.java
similarity index 90%
copy from frysk-core/frysk/value/ClassType.java
copy to frysk-core/frysk/value/FloatingPointFormat.java
index b75f63b..28fe0b0 100644
--- a/frysk-core/frysk/value/ClassType.java
+++ b/frysk-core/frysk/value/FloatingPointFormat.java
@@ -40,15 +40,11 @@
 package frysk.value;
 
 /**
- * Type for a class
+ * All floating point standards should extend this
+ * class.
  */
-public class ClassType
-    extends CompositeType
-{
-    protected String getPrefix() {
-	return "class";
-    }
-    public ClassType(String name, int size) {
-	super(name, size);
-    }
-}
+public abstract class FloatingPointFormat 
+{    
+    abstract BigFloatingPoint unpack (byte[] bytes);    
+    abstract byte[] pack (BigFloatingPoint value, int size);
+}
\ No newline at end of file
diff --git a/frysk-core/frysk/value/FloatingPointType.java b/frysk-core/frysk/value/FloatingPointType.java
index 9baf827..55123c7 100644
--- a/frysk-core/frysk/value/FloatingPointType.java
+++ b/frysk-core/frysk/value/FloatingPointType.java
@@ -51,8 +51,26 @@ import java.math.BigInteger;
 public class FloatingPointType
     extends ArithmeticType
 {    
+	
+	private FloatingPointFormat format;
+	
     public FloatingPointType(String name, ByteOrder order, int size) {
 	super(name, order, size);
+	
+	switch (size) {
+	case 4: format = FloatingPoint854Format.IEEE32;
+	break;
+	case 8:format = FloatingPoint854Format.IEEE64;
+	break;
+	case 16:format = FloatingPoint854Format.IEEE128;
+	break;
+	case 10:format = FloatingPoint854Format.IEEE80;
+	break;
+	case 12: format = FloatingPoint854Format.IEEE96;
+	break;
+	default: format = FloatingPoint854Format.IEEE64;
+	break;
+	}
     }
 
     public void toPrint(PrintWriter writer, Location location,
@@ -82,15 +100,25 @@ public class FloatingPointType
     BigFloat bigFloatValue(Location location) {
 	return getBigFloat(location);
     }
+   
+    BigFloatingPoint getBigFloatingPoint(Location location) {
+    	return format.unpack(location.get(order()));
+    }
+
+    BigFloatingPoint bigFloatingPointValue(Location location) {
+    	return getBigFloatingPoint(location);
+    }
 
     BigInteger bigIntegerValue (Location location) {
-	return getBigFloat(location).bigIntegerValue();
+	return getBigFloatingPoint(location).bigIntegerValue();
     }
 
     void assign(Location location, Value v) {
-	BigFloat f = ((ArithmeticType)v.getType())
-	    .bigFloatValue(v.getLocation());
-	location.put(order(), f.toByteArray(getSize()), 0);
+//    	BigFloat f = ((ArithmeticType)v.getType())
+//    	.bigFloatValue(v.getLocation());
+//    	location.put(order(), f.toByteArray(getSize()), 0);
+    	BigFloatingPoint f = ((FloatingPointType)v.getType()).bigFloatingPointValue(v.getLocation());
+    	location.put(order(), format.pack(f, getSize()), 0);
     }
     
     /* getALUs are double dispatch functions to determine 
diff --git a/frysk-core/frysk/value/Format.java b/frysk-core/frysk/value/Format.java
index 7defbd0..5bec308 100644
--- a/frysk-core/frysk/value/Format.java
+++ b/frysk-core/frysk/value/Format.java
@@ -68,13 +68,10 @@ public abstract class Format
 	writer.print(new BigInteger(1, location.get(order)).toString(2));
     }


hooks/post-receive
--
frysk system monitor/debugger


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]