]> Shamusworld >> Repos - rmac/blobdiff - fltpoint.c
Version bump for last patch; now at v1.13.4.
[rmac] / fltpoint.c
index 1de5d0ac42a3cd5d53aae63beabf0e1eec17e9e8..911e0085ae2729b6c513cf16f975061a86287adb 100644 (file)
@@ -14,6 +14,7 @@
 #include "fltpoint.h"
 #include <float.h>
 #include <math.h>
 #include "fltpoint.h"
 #include <float.h>
 #include <math.h>
+#include <stdio.h>
 
 //
 // Check for IEEE-754 conformance (C99 compilers should be OK here)
 
 //
 // Check for IEEE-754 conformance (C99 compilers should be OK here)
@@ -72,7 +73,7 @@ uint32_t FloatToIEEE754(float f)
        // [+0.5, +1)) and base-2 exponent
        // d = mantissa * (2 ^ exponent) *exactly* for FLT_RADIX=2
        // Also, since we want the mantissa to be non-inverted (2's complemented),
        // [+0.5, +1)) and base-2 exponent
        // d = mantissa * (2 ^ exponent) *exactly* for FLT_RADIX=2
        // Also, since we want the mantissa to be non-inverted (2's complemented),
-       // we make sure to pass in a positive number (floats/doubles are not 2's
+       // we make sure to pass in a positive number (floats/doubles are *not* 2's
        // complemented) as we already captured the sign bit above.
        int32_t exponent;
        float mantissa = frexpf((f < 0 ? -f : f), &exponent);
        // complemented) as we already captured the sign bit above.
        int32_t exponent;
        float mantissa = frexpf((f < 0 ? -f : f), &exponent);
@@ -111,7 +112,7 @@ uint64_t DoubleToIEEE754(double d)
        // and base-2 exponent
        // d = mantissa * (2 ^ exponent) *exactly* for FLT_RADIX=2
        // Also, since we want the mantissa to be non-inverted (2's complemented),
        // and base-2 exponent
        // d = mantissa * (2 ^ exponent) *exactly* for FLT_RADIX=2
        // Also, since we want the mantissa to be non-inverted (2's complemented),
-       // we make sure to pass in a positive number (floats/doubles are not 2's
+       // we make sure to pass in a positive number (floats/doubles are *not* 2's
        // complemented) as we already captured the sign bit above.
        double mantissa = frexp((d < 0 ? -d : d), &exponent);
 
        // complemented) as we already captured the sign bit above.
        double mantissa = frexp((d < 0 ? -d : d), &exponent);
 
@@ -169,3 +170,25 @@ void DoubleToExtended(double d, uint8_t out[])
        out[11] = intMant & 0xFF;
 }
 
        out[11] = intMant & 0xFF;
 }
 
+
+//
+// Convert a host native floating point number to a fixed point number.
+//
+uint64_t DoubleToFixedPoint(double d, int intBits, int fracBits)
+{
+       uint8_t signBit = (signbit(d) ? 1 : 0);
+
+       // Ensure what we're working on is positive...
+       if (d < 0)
+               d *= -1;
+
+       double scaleFactor = (double)(1 << fracBits);
+       uint64_t result = (uint64_t)(d * scaleFactor);
+
+       // Invert the result, if necessary
+       if (signBit == 1)
+               result = (result = 0xFFFFFFFFFFFFFFFFLL) + 1;
+
+       return result;
+}
+