You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/08/03 22:04:29 UTC

svn commit: r562567 - in /activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang: Double.cpp Float.cpp

Author: tabish
Date: Fri Aug  3 13:04:28 2007
New Revision: 562567

URL: http://svn.apache.org/viewvc?view=rev&rev=562567
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103

Adding in more Types wrappers

Modified:
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.cpp
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.cpp

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.cpp?view=diff&rev=562567&r1=562566&r2=562567
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.cpp Fri Aug  3 13:04:28 2007
@@ -16,6 +16,7 @@
  */
 
 #include "Double.h"
+#include <decaf/lang/Long.h>
 
 using namespace std;
 using namespace decaf;
@@ -142,7 +143,87 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 std::string Double::toHexString( double value ) {
-    return ""; //TODO
+    /*
+     * Reference: http://en.wikipedia.org/wiki/IEEE_754
+     */
+    if( value != value ) {
+        return "NaN";
+    }
+    if( value == POSITIVE_INFINITY ) {
+        return "Infinity";
+    }
+    if( value == NEGATIVE_INFINITY ) {
+        return "-Infinity";
+    }
+
+    unsigned long long bitValue = Double::doubleToLongBits( value );
+
+    bool negative = ( bitValue & 0x8000000000000000LL ) != 0;
+    // mask exponent bits and shift down
+    unsigned long long exponent = ( bitValue & 0x7FF0000000000000LL ) >> 52;
+    // mask significand bits and shift up
+    unsigned long long significand = bitValue & 0x000FFFFFFFFFFFFFLL;
+
+    if( exponent == 0 && significand == 0 ) {
+        return ( negative ? "-0x0.0p0" : "0x0.0p0" );
+    }
+
+    // Start with sign and hex indicator
+    std::string hexString( negative ? "-0x" : "0x" );
+
+    if( exponent == 0 ) {
+        // denormal (subnormal) value
+        hexString.append("0.");
+        // significand is 52-bits, so there can be 13 hex digits
+        unsigned int fractionDigits = 13;
+        // remove trailing hex zeros, so Integer.toHexString() won't print
+        // them
+        while( ( significand != 0 ) && ( ( significand & 0xF ) == 0 ) ) {
+            significand >>= 4;
+            fractionDigits--;
+        }
+        // this assumes Integer.toHexString() returns lowercase characters
+        std::string hexSignificand = Long::toHexString( significand );
+
+        // if there are digits left, then insert some '0' chars first
+        if( significand != 0 && fractionDigits > hexSignificand.length() ) {
+            int digitDiff = fractionDigits - hexSignificand.length();
+            while( digitDiff-- != 0 ) {
+                hexString.append( "0" );
+            }
+        }
+
+        hexString.append( hexSignificand );
+        hexString.append( "p-1022" );
+    } else {
+        // normal value
+        hexString.append( "1." );
+        // significand is 52-bits, so there can be 13 hex digits
+        unsigned int fractionDigits = 13;
+        // remove trailing hex zeros, so Integer.toHexString() won't print
+        // them
+        while( (significand != 0 ) && ( ( significand & 0xF ) == 0 ) ) {
+            significand >>= 4;
+            fractionDigits--;
+        }
+        // this assumes Integer.toHexString() returns lowercase characters
+        std::string hexSignificand = Long::toHexString( significand );
+
+        // if there are digits left, then insert some '0' chars first
+        if( significand != 0 && fractionDigits > hexSignificand.length() ) {
+            int digitDiff = fractionDigits - hexSignificand.length();
+            while( digitDiff-- != 0 ) {
+                hexString.append( "0" );
+            }
+        }
+
+        hexString.append( hexSignificand );
+        hexString.append( "p" );
+        // remove exponent's 'bias' and convert to a string
+        hexString.append( Long::toString( exponent - 1023 ) );
+    }
+
+    return hexString;
 }
 
 ////////////////////////////////////////////////////////////////////////////////

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.cpp?view=diff&rev=562567&r1=562566&r2=562567
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.cpp Fri Aug  3 13:04:28 2007
@@ -16,6 +16,7 @@
  */
 
 #include "Float.h"
+#include <decaf/lang/Integer.h>
 
 using namespace std;
 using namespace decaf;
@@ -150,7 +151,86 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 std::string Float::toHexString( float value ) {
-    return ""; //TODO
+    /*
+     * Reference: http://en.wikipedia.org/wiki/IEEE_754
+     */
+    if( value != value ) {
+        return "NaN";
+    }
+    if( value == POSITIVE_INFINITY ) {
+        return "Infinity";
+    }
+    if( value == NEGATIVE_INFINITY ) {
+        return "-Infinity";
+    }
+
+    unsigned int bitValue = Float::floatToIntBits( value );
+
+    bool negative = ( bitValue & 0x80000000 ) != 0;
+    // mask exponent bits and shift down
+    unsigned int exponent = ( bitValue & 0x7f800000 ) >> 23;
+    // mask significand bits and shift up
+    // significand is 23-bits, so we shift to treat it like 24-bits
+    unsigned int significand = ( bitValue & 0x007FFFFF ) << 1;
+
+    if( exponent == 0 && significand == 0 ) {
+        return ( negative ? "-0x0.0p0" : "0x0.0p0" );
+    }
+
+    // Start with the correct sign and Hex indicator
+    std::string hexString( negative ? "-0x" : "0x" );
+
+    if( exponent == 0 ) {
+        // denormal (subnormal) value
+        hexString.append( "0." );
+        // significand is 23-bits, so there can be 6 hex digits
+        unsigned int fractionDigits = 6;
+        // remove trailing hex zeros, so Integer.toHexString() won't print
+        // them
+        while( ( significand != 0 ) && ( ( significand & 0xF ) == 0 ) ) {
+            significand >>= 4;
+            fractionDigits--;
+        }
+        // this assumes Integer.toHexString() returns lowercase characters
+        std::string hexSignificand = Integer::toHexString( significand );
+
+        // if there are digits left, then insert some '0' chars first
+        if( significand != 0 && fractionDigits > hexSignificand.length() ) {
+            unsigned int digitDiff = fractionDigits - hexSignificand.length();
+            while( digitDiff-- != 0 ) {
+                hexString.append( "0" );
+            }
+        }
+        hexString.append( hexSignificand );
+        hexString.append( "p-126" );
+    } else {
+        // normal value
+        hexString.append( "1." );
+        // significand is 23-bits, so there can be 6 hex digits
+        unsigned int fractionDigits = 6;
+        // remove trailing hex zeros, so Integer.toHexString() won't print
+        // them
+        while( (significand != 0) && ((significand & 0xF ) == 0 ) ) {
+            significand >>= 4;
+            fractionDigits--;
+        }
+        // this assumes Integer.toHexString() returns lowercase characters
+        std::string hexSignificand = Integer::toHexString( significand );
+
+        // if there are digits left, then insert some '0' chars first
+        if( significand != 0 && fractionDigits > hexSignificand.length() ) {
+            unsigned int digitDiff = fractionDigits - hexSignificand.length();
+            while( digitDiff-- != 0 ) {
+                hexString.append( "0" );
+            }
+        }
+        hexString.append( hexSignificand );
+        hexString.append( "p" );
+        // remove exponent's 'bias' and convert to a string
+        hexString.append( Integer::toString( exponent - 127 ) );
+    }
+
+    return hexString;
 }
 
 ////////////////////////////////////////////////////////////////////////////////