activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r564579 - in /activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util: BitOps.h FloatingPointParser.cpp FloatingPointParser.h
Date Fri, 10 Aug 2007 13:14:07 GMT
Author: tabish
Date: Fri Aug 10 06:14:06 2007
New Revision: 564579

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

Implementing the Primitive Wrappers fully

Modified:
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/BitOps.h
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/FloatingPointParser.cpp
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/FloatingPointParser.h

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/BitOps.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/BitOps.h?view=diff&rev=564579&r1=564578&r2=564579
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/BitOps.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/BitOps.h Fri Aug 10
06:14:06 2007
@@ -66,6 +66,15 @@
         static const int INFINITE_INTBITS = 0x7F800000;
         static const int MINIMUM_INTBITS = 1;
 
+        static const unsigned int DOUBLE_SIGN_MASK_HI = 0x80000000;
+        static const unsigned int DOUBLE_EXPONENT_MASK_HI = 0x7FF00000;
+        static const unsigned int DOUBLE_MANTISSA_MASK_LO = 0xFFFFFFFF;
+        static const unsigned int DOUBLE_MANTISSA_MASK_HI = 0x000FFFFF;
+        static const unsigned int SINGLE_SIGN_MASK = 0x80000000;
+        static const unsigned int SINGLE_EXPONENT_MASK	= 0x7F800000;
+        static const unsigned int SINGLE_MANTISSA_MASK	= 0x007FFFFF;
+        static const unsigned int SINGLE_NAN_BITS = (SINGLE_EXPONENT_MASK | 0x00400000);
+
         static const unsigned long long MANTISSA_MASK = 0x000FFFFFFFFFFFFFULL;
         static const unsigned long long EXPONENT_MASK = 0x7FF0000000000000ULL;
         static const unsigned long long NORMAL_MASK = 0x0010000000000000ULL;
@@ -143,6 +152,48 @@
 
         static int& HIGH_I32_FROM_LONG64_PTR( unsigned long long* long64ptr ) {
             return ( (LONG_UNION*)long64ptr )->sintValue[HiWord];
+        }
+
+        static bool IS_DENORMAL_DBL( double dbl ) {
+            return IS_DENORMAL_DBL_PTR( &dbl );
+        }
+
+        static bool IS_DENORMAL_DBL_PTR(double* dblptr ) {
+            return (((HIGH_U32_FROM_DBL_PTR(dblptr) & DOUBLE_EXPONENT_MASK_HI) == 0)
&&
+                    ((HIGH_U32_FROM_DBL_PTR(dblptr) & DOUBLE_MANTISSA_MASK_HI) != 0 ||
+                    (LOW_U32_FROM_DBL_PTR(dblptr) != 0)));
+        }
+
+        static unsigned int& LOW_U32_FROM_DBL_PTR( double* dblptr ) {
+            return ( ( (LONG_UNION*)dblptr )->intValue[LoWord] );
+        }
+
+        static unsigned int& HIGH_U32_FROM_DBL_PTR( double* dblptr ) {
+            return ( ( (LONG_UNION*)dblptr )->intValue[HiWord] );
+        }
+
+        static int& LOW_I32_FROM_DBL_PTR( double* dblptr ) {
+            return ( ( (LONG_UNION*)dblptr )->sintValue[LoWord] );
+        }
+
+        static int& HIGH_I32_FROM_DBL_PTR( double* dblptr ) {
+            return ( ( (LONG_UNION*)dblptr )->sintValue[HiWord] );
+        }
+
+        static unsigned int& LOW_U32_FROM_DBL( double dbl ) {
+            return LOW_U32_FROM_DBL_PTR( &dbl );
+        }
+
+        static unsigned int& HIGH_U32_FROM_DBL( double dbl ) {
+            return HIGH_U32_FROM_DBL_PTR( &dbl );
+        }
+
+        static int& LOW_I32_FROM_DBL( double dbl ) {
+            return LOW_I32_FROM_DBL_PTR( &dbl );
+        }
+
+        static int& HIGH_I32_FROM_DBL( double dbl ) {
+            return HIGH_I32_FROM_DBL_PTR( &dbl );
         }
 
         static unsigned int at( unsigned int i ) {

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/FloatingPointParser.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/FloatingPointParser.cpp?view=diff&rev=564579&r1=564578&r2=564579
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/FloatingPointParser.cpp
(original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/FloatingPointParser.cpp
Fri Aug 10 06:14:06 2007
@@ -35,6 +35,21 @@
 using namespace decaf::internal::util;
 
 ////////////////////////////////////////////////////////////////////////////////
+const int FloatingPointParser::tens[11] = {
+  0x3f800000,
+  0x41200000,
+  0x42c80000,
+  0x447a0000,
+  0x461c4000,
+  0x47c35000,
+  0x49742400,
+  0x4b189680,
+  0x4cbebc20,
+  0x4e6e6b28,
+  0x501502f9                    /* 10 ^ 10 in float */
+};
+
+////////////////////////////////////////////////////////////////////////////////
 double FloatingPointParser::parseDblImpl( const std::string& value, int exp )
     throw ( exceptions::NumberFormatException ) {
 
@@ -137,7 +152,7 @@
 float FloatingPointParser::parseFltImpl( const std::string& value, int exp )
     throw ( exceptions::NumberFormatException ) {
 
-    float flt = (float)exp; // TODO - FloatOps::createFloat( str, exp );
+    float flt = FloatingPointParser::createFloat( value, exp );
 
     if( Float::floatToIntBits( flt ) >= 0 ) {
         return flt;
@@ -537,22 +552,25 @@
 ////////////////////////////////////////////////////////////////////////////////
 float FloatingPointParser::createFloat1( unsigned long long* f, int length, int exp ) {
 
-    int numBits = BigInt::highestSetBitHighPrecision (f, length) + 1;
-    double dresult;
-    int result;
+    int numBits = BigInt::highestSetBitHighPrecision( f, length ) + 1;
+    double dresult = 0.0;
+    int result = 0;
 
     numBits -= BigInt::lowestSetBitHighPrecision( f, length );
 
     if( numBits < 25 && exp >= 0 && exp < LOG5_OF_TWO_TO_THE_N )
{
-        return ((float) LOW_I32_FROM_PTR (f)) * tenToTheE (exp);
+        return Float::intBitsToFloat( BitOps::LOW_I32_FROM_LONG64_PTR( f ) ) *
+               Float::intBitsToFloat( tenToTheE( exp ) );
     } else if( numBits < 25 && exp < 0 && (-exp) < LOG5_OF_TWO_TO_THE_N
) {
-        return ((float) LOW_I32_FROM_PTR (f)) / tenToTheE (-exp);
+        return Float::intBitsToFloat( BitOps::LOW_I32_FROM_LONG64_PTR( f ) ) *
+               Float::intBitsToFloat( tenToTheE( -exp ) );
     } else if (exp >= 0 && exp < 39) {
-        result = (float) (BigInt::toDoubleHighPrecision (f, length) * pow (10.0, exp));
+        result = Float::floatToIntBits(
+            (float)( BigInt::toDoubleHighPrecision( f, length ) * Math::pow( 10.0, exp )
) );
     } else if( exp >= 39 ) {
         // Convert the partial result to make sure that the
         // non-exponential part is not zero. This check fixes the case
-        // where the user enters 0.0e309! */
+        // where the user enters 0.0e309!
         dresult = BigInt::toDoubleHighPrecision( f, length );
 
         if( dresult == 0.0 ) {
@@ -567,7 +585,8 @@
         unsigned int fmant, fovfl;
         unsigned long long dmant;
         dresult = BigInt::toDoubleHighPrecision( f, length ) / Math::pow( 10.0, -exp );
-        if( IS_DENORMAL_DBL( dresult ) ) {
+
+        if( BitOps::IS_DENORMAL_DBL( dresult ) ) {
             return 0.0f;
         }
 
@@ -610,10 +629,8 @@
             }
 
             result = fmant;
-        }
-        else
-        {
-            result = dresult;
+        } else {
+            result = Float::floatToIntBits( (float)dresult );
         }
     }
 
@@ -627,28 +644,11 @@
     return floatAlgorithm( f, length, exp, Float::intBitsToFloat( result ) );
 }
 
-/* The algorithm for the function floatAlgorithm() below can be found
- * in:
- *
- *      "How to Read Floating-Point Numbers Accurately", William D.
- *      Clinger, Proceedings of the ACM SIGPLAN '90 Conference on
- *      Programming Language Design and Implementation, June 20-22,
- *      1990, pp. 92-101.
- *
- * There is a possibility that the function will end up in an endless
- * loop if the given approximating floating-point number (a very small
- * floating-point whose value is very close to zero) straddles between
- * two approximating integer values. We modified the algorithm slightly
- * to detect the case where it oscillates back and forth between
- * incrementing and decrementing the floating-point approximation. It
- * is currently set such that if the oscillation occurs more than twice
- * then return the original approximation.
- */
 ////////////////////////////////////////////////////////////////////////////////
 float FloatingPointParser::floatAlgorithm(
     unsigned long long* f, int length, int exp, float z ) {
 
-    unsigned long long m;
+    unsigned long long m = 0;
     int k = 0;
     int comparison = 0;
     int comparison2 = 0;
@@ -662,38 +662,39 @@
     int D2Length = 0;
     int decApproxCount = 0;
     int incApproxCount = 0;
+    int result = Float::floatToIntBits( z );
 
-      do {
+    do {
 
-          m = BigInt::floatMantissa (z);
-          k = BigInt::floatExponent (z);
+        m = BigInt::floatMantissa( z );
+        k = BigInt::floatExponent( z );
 
-          if( exp >= 0 && k >= 0 ) {
+        if( exp >= 0 && k >= 0 ) {
 
-              xLength = sizeOfTenToTheE( exp ) + length;
-              x = new unsigned long long[xLength];
-              memset( x + length, 0, sizeof(unsigned long long) * (xLength - length) );
-              memcpy( x, f, sizeof(unsigned long long) * length );
-              BigInt::timesTenToTheEHighPrecision( x, xLength, exp );
-
-              yLength = (k >> 6) + 2;
-              y = new unsigned long long[yLength];
-              memset( y + 1, 0, sizeof(unsigned long long) * (yLength - 1) );
-              *y = m;
-              BigInt::simpleShiftLeftHighPrecision (y, yLength, k);
+            xLength = sizeOfTenToTheE( exp ) + length;
+            x = new unsigned long long[xLength];
+            memset( x + length, 0, sizeof(unsigned long long) * (xLength - length) );
+            memcpy( x, f, sizeof(unsigned long long) * length );
+            BigInt::timesTenToTheEHighPrecision( x, xLength, exp );
+
+            yLength = (k >> 6) + 2;
+            y = new unsigned long long[yLength];
+            memset( y + 1, 0, sizeof(unsigned long long) * (yLength - 1) );
+            *y = m;
+            BigInt::simpleShiftLeftHighPrecision (y, yLength, k);
 
         } else if( exp >= 0 ) {
 
             xLength = sizeOfTenToTheE (exp) + length + ((-k) >> 6) + 1;
-              x = new unsigned long long[xLength];
-              memset( x + length, 0, sizeof(unsigned long long) * (xLength - length) );
-              memcpy( x, f, sizeof(unsigned long long) * length );
-              BigInt::timesTenToTheEHighPrecision( x, xLength, exp );
-              BigInt::simpleShiftLeftHighPrecision( x, xLength, -k );
-
-              yLength = 1;
-              y = new unsigned long long[yLength];
-              *y = m;
+            x = new unsigned long long[xLength];
+            memset( x + length, 0, sizeof(unsigned long long) * (xLength - length) );
+            memcpy( x, f, sizeof(unsigned long long) * length );
+            BigInt::timesTenToTheEHighPrecision( x, xLength, exp );
+            BigInt::simpleShiftLeftHighPrecision( x, xLength, -k );
+
+            yLength = 1;
+            y = new unsigned long long[yLength];
+            *y = m;
 
         } else if( k >= 0 ) {
 
@@ -712,7 +713,7 @@
             xLength = length + ((-k) >> 6) + 1;
             x = new unsigned long long[xLength];
             memset( x + length, 0, sizeof(unsigned long long) * (xLength - length) );
-            memcpy( x, f, sizeof (unsigned long long) * length );
+            memcpy( x, f, sizeof(unsigned long long) * length );
             BigInt::simpleShiftLeftHighPrecision( x, xLength, -k );
 
             yLength = sizeOfTenToTheE( -exp ) + 1;
@@ -734,7 +735,7 @@
             DLength = yLength;
             D = new unsigned long long[DLength];
             memcpy (D, y, DLength * sizeof (unsigned long long));
-            BigInt::subtractHighPrecision (D, DLength, x, xLength);
+            BigInt::subtractHighPrecision( D, DLength, x, xLength );
         } else {
             /* y == x */
             DLength = 1;
@@ -743,83 +744,106 @@
         }
 
         D2Length = DLength + 1;
-        allocateU64 (D2, D2Length);
+        D = new unsigned long long[DLength];
         m <<= 1;
-        BigInt::multiplyHighPrecision (D, DLength, &m, 1, D2, D2Length);
+        BigInt::multiplyHighPrecision( D, DLength, &m, 1, D2, D2Length );
         m >>= 1;
 
-        comparison2 = BigInt::compareHighPrecision (D2, D2Length, y, yLength);
+        comparison2 = BigInt::compareHighPrecision( D2, D2Length, y, yLength );
         if( comparison2 < 0 ) {
-            if( comparison < 0 && m == NORMAL_MASK ) {
+            if( comparison < 0 && m == BitOps::NORMAL_MASK ) {
 
-                BigInt::simpleShiftLeftHighPrecision (D2, D2Length, 1);
-                if( BigInt::compareHighPrecision (D2, D2Length, y, yLength) > 0 ) {
-                    DECREMENT_FLOAT (z, decApproxCount, incApproxCount);
+                BigInt::simpleShiftLeftHighPrecision( D2, D2Length, 1 );
+                if( BigInt::compareHighPrecision( D2, D2Length, y, yLength) > 0 ) {
+                    --result;
+                    decApproxCount++;
+                    if( ( incApproxCount > 2 ) && ( decApproxCount > 2 ) )
{
+                        if( decApproxCount > incApproxCount ) {
+                            result += decApproxCount - incApproxCount;
+                        } else if( incApproxCount > decApproxCount ) {
+                            result -= incApproxCount - decApproxCount;
+                        }
+                        break;
+                    }
                 } else {
                     break;
                 }
             } else {
                 break;
             }
-        } else if (comparison2 == 0) {
+        } else if( comparison2 == 0 ) {
 
-            if ((m & 1) == 0) {
-                if (comparison < 0 && m == NORMAL_MASK) {
-                    DECREMENT_FLOAT (z, decApproxCount, incApproxCount);
+            if( (m & 1) == 0 ) {
+                if( comparison < 0 && m == BitOps::NORMAL_MASK ) {
+                    --result;
+                    decApproxCount++;
+                    if( ( incApproxCount > 2 ) && ( decApproxCount > 2 ) )
{
+                        if( decApproxCount > incApproxCount ) {
+                            result += decApproxCount - incApproxCount;
+                        } else if( incApproxCount > decApproxCount ) {
+                            result -= incApproxCount - decApproxCount;
+                        }
+                        break;
+                    }
                 } else {
                     break;
                 }
-            } else if (comparison < 0) {
-                DECREMENT_FLOAT (z, decApproxCount, incApproxCount);
+            } else if( comparison < 0 ) {
+                --result;
+                decApproxCount++;
+                if( ( incApproxCount > 2 ) && ( decApproxCount > 2 ) ) {
+                    if( decApproxCount > incApproxCount ) {
+                        result += decApproxCount - incApproxCount;
+                    } else if( incApproxCount > decApproxCount ) {
+                        result -= incApproxCount - decApproxCount;
+                    }
+                    break;
+                }
                 break;
             } else {
-                INCREMENT_FLOAT (z, decApproxCount, incApproxCount);
+                ++result;
+                incApproxCount++;
+                if( ( incApproxCount > 2 ) && ( decApproxCount > 2 ) ) {
+                    if( decApproxCount > incApproxCount ) {
+                        result += decApproxCount - incApproxCount;
+                    } else if( incApproxCount > decApproxCount ) {
+                        result -= incApproxCount - decApproxCount;
+                    }
+                }
+                break;
+            }
+        } else if( comparison < 0 ) {
+            --result;
+            decApproxCount++;
+            if( ( incApproxCount > 2 ) && ( decApproxCount > 2 ) ) {
+                if( decApproxCount > incApproxCount ) {
+                    result += decApproxCount - incApproxCount;
+                } else if( incApproxCount > decApproxCount ) {
+                    result -= incApproxCount - decApproxCount;
+                }
                 break;
             }
-        } else if (comparison < 0) {
-          DECREMENT_FLOAT (z, decApproxCount, incApproxCount);
         } else {
-              if (FLOAT_TO_INTBITS (z) == EXPONENT_MASK)
+              if( (unsigned int)result == BitOps::FLOAT_EXPONENT_MASK ) {
+                  break;
+              }
+
+              if( ( incApproxCount > 2 ) && ( decApproxCount > 2 ) ) {
+                  if( decApproxCount > incApproxCount ) {
+                      result += decApproxCount - incApproxCount;
+                  } else if( incApproxCount > decApproxCount ) {
+                      result -= incApproxCount - decApproxCount;
+                  }
                   break;
-              INCREMENT_FLOAT (z, decApproxCount, incApproxCount);
+              }
         }
     }
-    while (1);
+    while( true );
 
     delete x;
     delete y;
     delete D;
     delete D2;
 
-    return z;
+    return Float::intBitsToFloat( result );
 }
-
-
-/**
-#define INCREMENT_FLOAT(_x, _decCount, _incCount) \
-    { \
-        ++FLOAT_TO_INTBITS(_x); \
-        _incCount++; \
-        if( (_incCount > 2) && (_decCount > 2) ) { \
-            if( _decCount > _incCount ) { \
-                FLOAT_TO_INTBITS(_x) += _decCount - _incCount; \
-            } else if( _incCount > _decCount ) { \
-                FLOAT_TO_INTBITS(_x) -= _incCount - _decCount; \
-            } \
-            break; \
-        } \
-    }
-#define DECREMENT_FLOAT(_x, _decCount, _incCount) \
-    { \
-        --FLOAT_TO_INTBITS(_x); \
-        _decCount++; \
-        if( (_incCount > 2) && (_decCount > 2) ) { \
-            if( _decCount > _incCount ) { \
-                FLOAT_TO_INTBITS(_x) += _decCount - _incCount; \
-            } else if( _incCount > _decCount ) { \
-                FLOAT_TO_INTBITS(_x) -= _incCount - _decCount; \
-            } \
-            break; \
-        } \
-    }
-*/

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/FloatingPointParser.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/FloatingPointParser.h?view=diff&rev=564579&r1=564578&r2=564579
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/FloatingPointParser.h
(original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/util/FloatingPointParser.h
Fri Aug 10 06:14:06 2007
@@ -51,6 +51,8 @@
         static const int DEFAULT_WIDTH = MAX_ACCURACY_WIDTH;
         static const int LOG5_OF_TWO_TO_THE_N = 11;
 
+        static const int tens[11];
+
     public:
 
         virtual ~FloatingPointParser() {}
@@ -193,6 +195,10 @@
 //		static IS_DENORMAL_DBL_PTR(dblptr) {
 //			(((HIGH_U32_FROM_DBL_PTR(dblptr) & DOUBLE_EXPONENT_MASK_HI) == 0) && ((HIGH_U32_FROM_DBL_PTR(dblptr)
& DOUBLE_MANTISSA_MASK_HI) != 0 || (LOW_U32_FROM_DBL_PTR(dblptr) != 0)))
 //		}
+
+        static int tenToTheE( int exp ) {
+            return *(tens + exp);
+        }
 
     };
 



Mime
View raw message