From a3e3e8b4ccd96e98da73acf1c584bbfa5a8b2b56 Mon Sep 17 00:00:00 2001 From: Richard Linden Date: Mon, 12 Nov 2012 19:08:14 -0800 Subject: SH-3406 WIP convert fast timers to lltrace system simplified llfasttimer code down to 2 classes llunit unit conversion now done in floating point or 64 bit integer precision, depending on source type --- indra/llcommon/llunit.h | 127 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 95 insertions(+), 32 deletions(-) (limited to 'indra/llcommon/llunit.h') diff --git a/indra/llcommon/llunit.h b/indra/llcommon/llunit.h index 0dcafbe26e..54ba1d67db 100644 --- a/indra/llcommon/llunit.h +++ b/indra/llcommon/llunit.h @@ -32,19 +32,44 @@ namespace LLUnits { -template + +template +struct HighestPrecisionType +{ + typedef T type_t; +}; + +template +struct HighestPrecisionType +{ + typedef typename HighestPrecisionType::type_t type_t; +}; + +template<> struct HighestPrecisionType { typedef F64 type_t; }; +template<> struct HighestPrecisionType { typedef S64 type_t; }; +template<> struct HighestPrecisionType { typedef S64 type_t; }; +template<> struct HighestPrecisionType { typedef S64 type_t; }; +template<> struct HighestPrecisionType { typedef S64 type_t; }; +template<> struct HighestPrecisionType { typedef S64 type_t; }; +template<> struct HighestPrecisionType { typedef S64 type_t; }; + +template struct ConversionFactor { - static F64 get() + static typename HighestPrecisionType::type_t get() { + // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template llstatic_assert(sizeof(DERIVED_UNITS_TAG) == 0, "Cannot convert between types."); } }; -template -struct ConversionFactor +template +struct ConversionFactor { - static F64 get() { return 1.0; } + static typename HighestPrecisionType::type_t get() + { + return 1; + } }; } @@ -91,6 +116,11 @@ struct LLUnit return mValue; } + template LLUnit as() + { + return LLUnit(*this); + } + void operator += (storage_t value) { mValue += value; @@ -121,7 +151,8 @@ struct LLUnit template void operator *= (LLUnit multiplicand) { - llstatic_assert(sizeof(OTHER_UNIT) == false, "Multiplication of unit types not supported."); + // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template + llstatic_assert(sizeof(OTHER_UNIT) == 0, "Multiplication of unit types not supported."); } void operator /= (storage_t divisor) @@ -132,15 +163,16 @@ struct LLUnit template void operator /= (LLUnit divisor) { - llstatic_assert(sizeof(OTHER_UNIT) == false, "Division of unit types not supported."); + // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template + llstatic_assert(sizeof(OTHER_UNIT) == 0, "Division of unit types not supported."); } - template - static storage_t convert(LLUnit v) + template + static storage_t convert(LLUnit v) { return (storage_t)(v.value() - * LLUnits::ConversionFactor::get() - * LLUnits::ConversionFactor::get()); + * LLUnits::ConversionFactor::get() + * LLUnits::ConversionFactor::get()); } protected: @@ -148,6 +180,32 @@ protected: storage_t mValue; }; +template +struct LLUnitStrict : public LLUnit +{ + typedef LLUnitStrict self_t; + + explicit LLUnitStrict(storage_t value = storage_t()) + : LLUnit(value) + {} + + template + LLUnitStrict(LLUnit other) + : LLUnit(convert(other)) + {} + + LLUnitStrict(self_t& other) + : LLUnit(other) + {} + + +private: + operator storage_t() const + { + return value(); + } +}; + // // operator + // @@ -221,7 +279,8 @@ LLUnit operator * (LLUnit firs template void operator * (LLUnit, LLUnit) { - llstatic_assert(sizeof(STORAGE_TYPE1) == false, "Multiplication of unit types results in new unit type - not supported."); + // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template + llstatic_assert(sizeof(STORAGE_TYPE1) == 0, "Multiplication of unit types results in new unit type - not supported."); } // @@ -242,7 +301,8 @@ LLUnit operator / (LLUnit firs template void operator / (LLUnit, LLUnit) { - llstatic_assert(sizeof(STORAGE_TYPE1) == false, "Multiplication of unit types results in new unit type - not supported."); + // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template + llstatic_assert(sizeof(STORAGE_TYPE1) == 0, "Multiplication of unit types results in new unit type - not supported."); } #define COMPARISON_OPERATORS(op) \ @@ -273,21 +333,21 @@ COMPARISON_OPERATORS(!=) namespace LLUnits { -#define LL_DECLARE_DERIVED_UNIT(base_unit_name, unit_name, conversion_factor)\ -struct unit_name \ -{ \ - typedef base_unit_name base_unit_t; \ -}; \ -template<> \ -struct ConversionFactor \ -{ \ - static F64 get() { return (conversion_factor); } \ -}; \ - \ -template<> \ -struct ConversionFactor \ -{ \ - static F64 get() { return 1.0 / (conversion_factor); } \ +#define LL_DECLARE_DERIVED_UNIT(base_unit_name, unit_name, conversion_factor) \ +struct unit_name \ +{ \ + typedef base_unit_name base_unit_t; \ +}; \ +template \ +struct ConversionFactor \ +{ \ + static typename HighestPrecisionType::type_t get() { return typename HighestPrecisionType::type_t(conversion_factor); } \ +}; \ + \ +template \ +struct ConversionFactor \ +{ \ + static typename HighestPrecisionType::type_t get() { return typename HighestPrecisionType::type_t(1.0 / (conversion_factor)); } \ } struct Bytes { typedef Bytes base_unit_t; }; @@ -302,16 +362,19 @@ LL_DECLARE_DERIVED_UNIT(Bytes, Gigabits, (1024 * 1024 * 1024 / 8)); struct Seconds { typedef Seconds base_unit_t; }; LL_DECLARE_DERIVED_UNIT(Seconds, Minutes, 60); LL_DECLARE_DERIVED_UNIT(Seconds, Hours, 60 * 60); -LL_DECLARE_DERIVED_UNIT(Seconds, Days, 60 * 60 * 24); -LL_DECLARE_DERIVED_UNIT(Seconds, Weeks, 60 * 60 * 24 * 7); LL_DECLARE_DERIVED_UNIT(Seconds, Milliseconds, (1.0 / 1000.0)); LL_DECLARE_DERIVED_UNIT(Seconds, Microseconds, (1.0 / (1000000.0))); LL_DECLARE_DERIVED_UNIT(Seconds, Nanoseconds, (1.0 / (1000000000.0))); struct Meters { typedef Meters base_unit_t; }; LL_DECLARE_DERIVED_UNIT(Meters, Kilometers, 1000); -LL_DECLARE_DERIVED_UNIT(Meters, Centimeters, (1.0 / 100)); -LL_DECLARE_DERIVED_UNIT(Meters, Millimeters, (1.0 / 1000)); +LL_DECLARE_DERIVED_UNIT(Meters, Centimeters, (1.0 / 100.0)); +LL_DECLARE_DERIVED_UNIT(Meters, Millimeters, (1.0 / 1000.0)); + +struct Hertz { typedef Hertz base_unit_t; }; +LL_DECLARE_DERIVED_UNIT(Hertz, Kilohertz, 1000); +LL_DECLARE_DERIVED_UNIT(Hertz, Megahertz, 1000 * 1000); +LL_DECLARE_DERIVED_UNIT(Hertz, Gigahertz, 1000 * 1000 * 1000); } #endif // LL_LLUNIT_H -- cgit v1.2.3