Add single <-> double conversions.

- With explicit casting now possible, converting from double to single
  precision floating point and back again needs to be possible, too.
This commit is contained in:
Randy Heit 2013-11-01 21:45:02 -05:00
parent b66de4116d
commit 3063df4f74
2 changed files with 29 additions and 2 deletions

View file

@ -160,10 +160,15 @@ public:
size_t len = mysnprintf(buf, countof(buf), "%08x", x); size_t len = mysnprintf(buf, countof(buf), "%08x", x);
Add(buf, len); Add(buf, len);
} }
void AddFloat(double f) void AddFloat(double f, bool single)
{ {
char buf[32]; char buf[32];
size_t len = mysnprintf(buf, countof(buf), "%.4f", f); size_t len = mysnprintf(buf, countof(buf), "%.4f", f);
if (single)
{
buf[len++] = 'f';
buf[len] = '\0';
}
Add(buf, len); Add(buf, len);
} }
private: private:
@ -501,7 +506,11 @@ static void PrintExprConstant(FLispString &out, ZCC_TreeNode *node)
} }
else if (enode->Type == TypeFloat64) else if (enode->Type == TypeFloat64)
{ {
out.AddFloat(enode->DoubleVal); out.AddFloat(enode->DoubleVal, false);
}
else if (enode->Type == TypeFloat32)
{
out.AddFloat(enode->DoubleVal, true);
} }
else if (enode->Type == TypeName) else if (enode->Type == TypeName)
{ {

View file

@ -613,6 +613,21 @@ static void DtoU32(ZCC_ExprConstant *expr, FSharedStringArena &str_arena)
expr->Type = TypeUInt32; expr->Type = TypeUInt32;
} }
static void FtoD(ZCC_ExprConstant *expr, FSharedStringArena &str_arena)
{
// Constant single precision numbers are stored as doubles.
assert(expr->Type == TypeFloat32);
expr->Type = TypeFloat64;
}
static void DtoF(ZCC_ExprConstant *expr, FSharedStringArena &str_arena)
{
// Truncate double precision to single precision.
float poop = (float)expr->DoubleVal;
expr->DoubleVal = poop;
expr->Type = TypeFloat32;
}
static void S32toS(ZCC_ExprConstant *expr, FSharedStringArena &str_arena) static void S32toS(ZCC_ExprConstant *expr, FSharedStringArena &str_arena)
{ {
char str[16]; char str[16];
@ -664,7 +679,10 @@ void ZCC_InitConversions()
TypeSInt32->AddConversion(TypeFloat64, S32toD); TypeSInt32->AddConversion(TypeFloat64, S32toD);
TypeSInt32->AddConversion(TypeString, S32toS); TypeSInt32->AddConversion(TypeString, S32toS);
TypeFloat32->AddConversion(TypeFloat64, FtoD);
TypeFloat64->AddConversion(TypeUInt32, DtoU32); TypeFloat64->AddConversion(TypeUInt32, DtoU32);
TypeFloat64->AddConversion(TypeSInt32, DtoS32); TypeFloat64->AddConversion(TypeSInt32, DtoS32);
TypeFloat64->AddConversion(TypeFloat32, DtoF);
TypeFloat64->AddConversion(TypeString, DtoS); TypeFloat64->AddConversion(TypeString, DtoS);
} }