// Floating point to IEEE-754 conversion routines
//
// by James Hammons
-// (C) 2018 Underground Software
+// (C) 2019 Underground Software
//
// Since there are no guarantees vis-a-vis floating point numbers in C, we have
// to utilize routines like the following in order to guarantee that the thing
#include "fltpoint.h"
#include <float.h>
#include <math.h>
+#include <stdio.h>
//
// Check for IEEE-754 conformance (C99 compilers should be OK here)
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;
+}
+