Better type punning
This commit is contained in:
parent
c239ac5220
commit
c1aea15254
|
@ -72,6 +72,16 @@ double fraction(double v)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
template<typename T, typename F>
|
||||||
|
struct alias_cast_t
|
||||||
|
{
|
||||||
|
union
|
||||||
|
{
|
||||||
|
F raw;
|
||||||
|
T data;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// As AngelScript doesn't allow bitwise manipulation of float types we'll provide a couple of
|
// As AngelScript doesn't allow bitwise manipulation of float types we'll provide a couple of
|
||||||
// functions for converting float values to IEEE 754 formatted values etc. This also allow us to
|
// functions for converting float values to IEEE 754 formatted values etc. This also allow us to
|
||||||
// provide a platform agnostic representation to the script so the scripts don't have to worry
|
// provide a platform agnostic representation to the script so the scripts don't have to worry
|
||||||
|
@ -80,19 +90,27 @@ float fpFromIEEE(asUINT raw)
|
||||||
{
|
{
|
||||||
// TODO: Identify CPU family to provide proper conversion
|
// TODO: Identify CPU family to provide proper conversion
|
||||||
// if the CPU doesn't natively use IEEE style floats
|
// if the CPU doesn't natively use IEEE style floats
|
||||||
return *reinterpret_cast<float*>(&raw);
|
alias_cast_t<asUINT, float> t;
|
||||||
|
t.raw = raw;
|
||||||
|
return t.data;
|
||||||
}
|
}
|
||||||
asUINT fpToIEEE(float fp)
|
asUINT fpToIEEE(float fp)
|
||||||
{
|
{
|
||||||
return *reinterpret_cast<asUINT*>(&fp);
|
alias_cast_t<float, asUINT> t;
|
||||||
|
t.raw = fp;
|
||||||
|
return t.data;
|
||||||
}
|
}
|
||||||
double fpFromIEEE(asQWORD raw)
|
double fpFromIEEE(asQWORD raw)
|
||||||
{
|
{
|
||||||
return *reinterpret_cast<double*>(&raw);
|
alias_cast_t<asQWORD, double> t;
|
||||||
|
t.raw = raw;
|
||||||
|
return t.data;
|
||||||
}
|
}
|
||||||
asQWORD fpToIEEE(double fp)
|
asQWORD fpToIEEE(double fp)
|
||||||
{
|
{
|
||||||
return *reinterpret_cast<asQWORD*>(&fp);
|
alias_cast_t<double, asQWORD> t;
|
||||||
|
t.raw = fp;
|
||||||
|
return t.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// closeTo() is used to determine if the binary representation of two numbers are
|
// closeTo() is used to determine if the binary representation of two numbers are
|
||||||
|
|
Loading…
Reference in New Issue