]> Shamusworld >> Repos - rmac/blobdiff - fltpoint.c
Fix a small buglet in the last patch. :-)
[rmac] / fltpoint.c
index 911e0085ae2729b6c513cf16f975061a86287adb..7e516fbd360686985aa160bb45f7f38658c6a67a 100644 (file)
@@ -2,7 +2,7 @@
 // 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
@@ -15,6 +15,7 @@
 #include <float.h>
 #include <math.h>
 #include <stdio.h>
+#include "error.h"
 
 //
 // Check for IEEE-754 conformance (C99 compilers should be OK here)
@@ -171,6 +172,28 @@ void DoubleToExtended(double d, uint8_t out[])
 }
 
 
+//
+// Convert a double to a DSP56001 style fixed point float.
+// Seems to be 23 bits of float value with 1 bit (MSB) for the sign.
+//
+uint32_t DoubleToDSPFloat(double d)
+{
+       if (d >= 1)
+       {
+               warn("DSP value clamped to +1.");
+               return 0x7FFFFF;
+       }
+       else if (d <= -1)
+       {
+               warn("DSP value clamped to -1.");
+               return 0x800000;
+       }
+
+       // The casts are here because some compilers do weird shit.  See bug #149.
+       return (uint32_t)((int32_t)trunc(round(ldexp(d, 23))));
+}
+
+
 //
 // Convert a host native floating point number to a fixed point number.
 //