- xs_Float.h: Convert header to constexpr.

* Allow use of this library in static initialisers.
* Required re-arranging a little bit to order everything so inlines could be used without prototypes.
This commit is contained in:
Mitch Richters 2021-11-01 23:30:17 +11:00
parent eb8b075727
commit a84aa84838

View file

@ -42,10 +42,15 @@
typedef double real64; typedef double real64;
union _xs_doubleints struct _xs_doubleints
{ {
real64 val; union
uint32_t ival[2]; {
real64 val;
uint32_t ival[2];
};
constexpr _xs_doubleints(real64 v) : val(v) {}
}; };
#if 0 #if 0
@ -56,73 +61,9 @@ union _xs_doubleints
// ==================================================================================================================== // ====================================================================================================================
// Constants // Constants
// ==================================================================================================================== // ====================================================================================================================
const real64 _xs_doublemagic = real64 (6755399441055744.0); //2^52 * 1.5, uses limited precisicion to floor constexpr real64 _xs_doublemagic = real64 (6755399441055744.0); //2^52 * 1.5, uses limited precisicion to floor
const real64 _xs_doublemagicdelta = (1.5e-8); //almost .5f = .5f + 1e^(number of exp bit) constexpr real64 _xs_doublemagicdelta = (1.5e-8); //almost .5f = .5f + 1e^(number of exp bit)
const real64 _xs_doublemagicroundeps = (.5f-_xs_doublemagicdelta); //almost .5f = .5f - 1e^(number of exp bit) constexpr real64 _xs_doublemagicroundeps = (.5f-_xs_doublemagicdelta); //almost .5f = .5f - 1e^(number of exp bit)
// ====================================================================================================================
// Prototypes
// ====================================================================================================================
static int32_t xs_CRoundToInt (real64 val, real64 dmr = _xs_doublemagic);
static int32_t xs_ToInt (real64 val, real64 dme = -_xs_doublemagicroundeps);
static int32_t xs_FloorToInt (real64 val, real64 dme = _xs_doublemagicroundeps);
static int32_t xs_CeilToInt (real64 val, real64 dme = _xs_doublemagicroundeps);
static int32_t xs_RoundToInt (real64 val);
//int32_t versions
finline static int32_t xs_CRoundToInt (int32_t val) {return val;}
finline static int32_t xs_ToInt (int32_t val) {return val;}
// ====================================================================================================================
// Fix Class
// ====================================================================================================================
template <int32_t N> class xs_Fix
{
public:
typedef int32_t Fix;
// ====================================================================================================================
// Basic Conversion from Numbers
// ====================================================================================================================
finline static Fix ToFix (int32_t val) {return val<<N;}
finline static Fix ToFix (real64 val) {return xs_ConvertToFixed(val);}
// ====================================================================================================================
// Basic Conversion to Numbers
// ====================================================================================================================
finline static real64 ToReal (Fix f) {return real64(f)/real64(1<<N);}
finline static int32_t ToInt (Fix f) {return f>>N;}
protected:
// ====================================================================================================================
// Helper function - mainly to preserve _xs_DEFAULT_CONVERSION
// ====================================================================================================================
finline static int32_t xs_ConvertToFixed (real64 val)
{
#if _xs_DEFAULT_CONVERSION==0
return xs_CRoundToInt(val, _xs_doublemagic/(1<<N));
#else
return (long)((val)*(1<<N));
#endif
}
};
finline static int32_t xs_ToFixed(int32_t n, real64 val)
{
#if _xs_DEFAULT_CONVERSION==0
return xs_CRoundToInt(val, _xs_doublemagic/(1<<n));
#else
return (long)((val)*(1<<N));
#endif
}
// ==================================================================================================================== // ====================================================================================================================
@ -130,12 +71,10 @@ finline static int32_t xs_ToFixed(int32_t n, real64 val)
// Inline implementation // Inline implementation
// ==================================================================================================================== // ====================================================================================================================
// ==================================================================================================================== // ====================================================================================================================
finline static int32_t xs_CRoundToInt(real64 val, real64 dmr) finline constexpr int32_t xs_CRoundToInt(real64 val, real64 dmr = _xs_doublemagic)
{ {
#if _xs_DEFAULT_CONVERSION==0 #if _xs_DEFAULT_CONVERSION==0
_xs_doubleints uval; return _xs_doubleints(val + dmr).ival[_xs_iman_];
uval.val = val + dmr;
return uval.ival[_xs_iman_];
#else #else
return int32_t(floor(val+.5)); return int32_t(floor(val+.5));
#endif #endif
@ -143,7 +82,7 @@ finline static int32_t xs_CRoundToInt(real64 val, real64 dmr)
// ==================================================================================================================== // ====================================================================================================================
finline static int32_t xs_ToInt(real64 val, real64 dme) finline constexpr int32_t xs_ToInt(real64 val, real64 dme = -_xs_doublemagicroundeps)
{ {
/* unused - something else I tried... /* unused - something else I tried...
_xs_doublecopysgn(dme,val); _xs_doublecopysgn(dme,val);
@ -170,7 +109,7 @@ finline static int32_t xs_ToInt(real64 val, real64 dme)
// ==================================================================================================================== // ====================================================================================================================
finline static int32_t xs_FloorToInt(real64 val, real64 dme) finline constexpr int32_t xs_FloorToInt(real64 val, real64 dme = _xs_doublemagicroundeps)
{ {
#if _xs_DEFAULT_CONVERSION==0 #if _xs_DEFAULT_CONVERSION==0
return xs_CRoundToInt (val - dme); return xs_CRoundToInt (val - dme);
@ -181,7 +120,7 @@ finline static int32_t xs_FloorToInt(real64 val, real64 dme)
// ==================================================================================================================== // ====================================================================================================================
finline static int32_t xs_CeilToInt(real64 val, real64 dme) finline constexpr int32_t xs_CeilToInt(real64 val, real64 dme = _xs_doublemagicroundeps)
{ {
#if _xs_DEFAULT_CONVERSION==0 #if _xs_DEFAULT_CONVERSION==0
return xs_CRoundToInt (val + dme); return xs_CRoundToInt (val + dme);
@ -192,7 +131,7 @@ finline static int32_t xs_CeilToInt(real64 val, real64 dme)
// ==================================================================================================================== // ====================================================================================================================
finline static int32_t xs_RoundToInt(real64 val) finline constexpr int32_t xs_RoundToInt(real64 val)
{ {
#if _xs_DEFAULT_CONVERSION==0 #if _xs_DEFAULT_CONVERSION==0
// Yes, it is important that two fadds be generated, so you cannot override the dmr // Yes, it is important that two fadds be generated, so you cannot override the dmr
@ -205,33 +144,84 @@ finline static int32_t xs_RoundToInt(real64 val)
} }
// ====================================================================================================================
finline constexpr int32_t xs_ToFixed(int32_t n, real64 val)
{
#if _xs_DEFAULT_CONVERSION==0
return xs_CRoundToInt(val, _xs_doublemagic/(1<<n));
#else
return (long)((val)*(1<<N));
#endif
}
//int32_t versions
finline constexpr int32_t xs_CRoundToInt (int32_t val) {return val;}
finline constexpr int32_t xs_ToInt (int32_t val) {return val;}
// ==================================================================================================================== // ====================================================================================================================
// ==================================================================================================================== // ====================================================================================================================
// Unsigned variants // Unsigned variants
// ==================================================================================================================== // ====================================================================================================================
// ==================================================================================================================== // ====================================================================================================================
finline static uint32_t xs_CRoundToUInt(real64 val) finline constexpr uint32_t xs_CRoundToUInt(real64 val)
{ {
return (uint32_t)xs_CRoundToInt(val); return (uint32_t)xs_CRoundToInt(val);
} }
finline static uint32_t xs_FloorToUInt(real64 val) finline constexpr uint32_t xs_FloorToUInt(real64 val)
{ {
return (uint32_t)xs_FloorToInt(val); return (uint32_t)xs_FloorToInt(val);
} }
finline static uint32_t xs_CeilToUInt(real64 val) finline constexpr uint32_t xs_CeilToUInt(real64 val)
{ {
return (uint32_t)xs_CeilToInt(val); return (uint32_t)xs_CeilToInt(val);
} }
finline static uint32_t xs_RoundToUInt(real64 val) finline constexpr uint32_t xs_RoundToUInt(real64 val)
{ {
return (uint32_t)xs_RoundToInt(val); return (uint32_t)xs_RoundToInt(val);
} }
// ====================================================================================================================
// Fix Class
// ====================================================================================================================
template <int32_t N> class xs_Fix
{
public:
typedef int32_t Fix;
// ====================================================================================================================
// Basic Conversion from Numbers
// ====================================================================================================================
finline static constexpr Fix ToFix (int32_t val) {return val<<N;}
finline static constexpr Fix ToFix (real64 val) {return xs_ConvertToFixed(val);}
// ====================================================================================================================
// Basic Conversion to Numbers
// ====================================================================================================================
finline static constexpr real64 ToReal (Fix f) {return real64(f)/real64(1<<N);}
finline static constexpr int32_t ToInt (Fix f) {return f>>N;}
protected:
// ====================================================================================================================
// Helper function - mainly to preserve _xs_DEFAULT_CONVERSION
// ====================================================================================================================
finline static constexpr int32_t xs_ConvertToFixed (real64 val)
{
#if _xs_DEFAULT_CONVERSION==0
return xs_CRoundToInt(val, _xs_doublemagic/(1<<N));
#else
return (long)((val)*(1<<N));
#endif
}
};
// ==================================================================================================================== // ====================================================================================================================
// ==================================================================================================================== // ====================================================================================================================