X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=rmac;a=blobdiff_plain;f=fltpoint.c;h=7e516fbd360686985aa160bb45f7f38658c6a67a;hp=8739e88c1500d73397e6974df8c506377ee2e91a;hb=HEAD;hpb=bdbf34766f4d074a5933eb1326fe4ce03d249e10 diff --git a/fltpoint.c b/fltpoint.c index 8739e88..ffde55e 100644 --- a/fltpoint.c +++ b/fltpoint.c @@ -15,6 +15,7 @@ #include #include #include +#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. // @@ -187,7 +210,7 @@ uint64_t DoubleToFixedPoint(double d, int intBits, int fracBits) // Invert the result, if necessary if (signBit == 1) - result = (result = 0xFFFFFFFFFFFFFFFFLL) + 1; + result = (result ^ 0xFFFFFFFFFFFFFFFFLL) + 1; return result; }