From 9fd3af3c389ed491b515cbb5136b344b069913e4 Mon Sep 17 00:00:00 2001 From: Richard Linden Date: Thu, 13 Jun 2013 15:29:15 -0700 Subject: SH-3931 WIP Interesting: Add graphs to visualize scene load metrics changed Units macros and argument order to make it more clear optimized units for integer types fixed merging of periodicrecordings...should eliminate duplicate entries in sceneloadmonitor history --- indra/llcommon/llunit.h | 372 +++++++++++++++++++++++++++++------------------- 1 file changed, 229 insertions(+), 143 deletions(-) (limited to 'indra/llcommon/llunit.h') diff --git a/indra/llcommon/llunit.h b/indra/llcommon/llunit.h index f48cbe0e11..5b961c81f0 100644 --- a/indra/llcommon/llunit.h +++ b/indra/llcommon/llunit.h @@ -35,31 +35,31 @@ namespace LLUnits { template -struct ConversionFactor +struct Convert { - static F64 get() + static VALUE_TYPE get(VALUE_TYPE val) { // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template llstatic_assert_template(DERIVED_UNITS_TAG, false, "Cannot convert between types."); - return 0; + return val; } }; template -struct ConversionFactor +struct Convert { - static F64 get() + static VALUE_TYPE get(VALUE_TYPE val) { - return 1; + return val; } }; } -template +template struct LLUnit { - typedef LLUnit self_t; + typedef LLUnit self_t; typedef STORAGE_TYPE storage_t; // value initialization @@ -68,11 +68,16 @@ struct LLUnit {} // unit initialization and conversion - template - LLUnit(LLUnit other) + template + LLUnit(LLUnit other) : mValue(convert(other)) {} + bool operator == (const self_t& other) + { + return mValue = other.mValue; + } + // value assignment self_t& operator = (storage_t value) { @@ -81,8 +86,8 @@ struct LLUnit } // unit assignment - template - self_t& operator = (LLUnit other) + template + self_t& operator = (LLUnit other) { mValue = convert(other); return *this; @@ -93,9 +98,9 @@ struct LLUnit return mValue; } - template LLUnit as() + template LLUnit as() { - return LLUnit(*this); + return LLUnit(*this); } @@ -104,8 +109,8 @@ struct LLUnit mValue += value; } - template - void operator += (LLUnit other) + template + void operator += (LLUnit other) { mValue += convert(other); } @@ -115,8 +120,8 @@ struct LLUnit mValue -= value; } - template - void operator -= (LLUnit other) + template + void operator -= (LLUnit other) { mValue -= convert(other); } @@ -127,7 +132,7 @@ struct LLUnit } template - void operator *= (LLUnit multiplicand) + void operator *= (LLUnit multiplicand) { // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template llstatic_assert_template(OTHER_UNIT, false, "Multiplication of unit types not supported."); @@ -139,37 +144,43 @@ struct LLUnit } template - void operator /= (LLUnit divisor) + void operator /= (LLUnit divisor) { // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template llstatic_assert_template(OTHER_UNIT, false, "Illegal in-place division of unit types."); } - 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()); + return (storage_t)LLUnits::Convert::get((STORAGE_TYPE) + LLUnits::Convert::get(v.value())); } + template + static storage_t convert(LLUnit v) + { + return (storage_t)(v.value()); + } + + protected: storage_t mValue; }; -template -struct LLUnitImplicit : public LLUnit +template +struct LLUnitImplicit : public LLUnit { - typedef LLUnitImplicit self_t; - typedef typename LLUnit::storage_t storage_t; - typedef LLUnit base_t; + typedef LLUnitImplicit self_t; + typedef typename LLUnit::storage_t storage_t; + typedef LLUnit base_t; LLUnitImplicit(storage_t value = storage_t()) : base_t(value) {} - template - LLUnitImplicit(LLUnit other) + template + LLUnitImplicit(LLUnit other) : base_t(convert(other)) {} @@ -184,50 +195,50 @@ struct LLUnitImplicit : public LLUnit // // operator + // -template -LLUnit operator + (LLUnit first, LLUnit second) +template +LLUnit operator + (LLUnit first, LLUnit second) { - LLUnit result(first); + LLUnit result(first); result += second; return result; } -template -LLUnit operator + (LLUnit first, SCALAR_TYPE second) +template +LLUnit operator + (LLUnit first, SCALAR_TYPE second) { - LLUnit result(first); + LLUnit result(first); result += second; return result; } -template -LLUnit operator + (SCALAR_TYPE first, LLUnit second) +template +LLUnit operator + (SCALAR_TYPE first, LLUnit second) { - LLUnit result(first); + LLUnit result(first); result += second; return result; } -template -LLUnitImplicit operator + (LLUnitImplicit first, LLUnit second) +template +LLUnitImplicit operator + (LLUnitImplicit first, LLUnit second) { - LLUnitImplicit result(first); + LLUnitImplicit result(first); result += second; return result; } -template -LLUnitImplicit operator + (LLUnitImplicit first, SCALAR_TYPE second) +template +LLUnitImplicit operator + (LLUnitImplicit first, SCALAR_TYPE second) { - LLUnitImplicit result(first); + LLUnitImplicit result(first); result += second; return result; } -template -LLUnitImplicit operator + (LLUnitImplicit first, LLUnitImplicit second) +template +LLUnitImplicit operator + (LLUnitImplicit first, LLUnitImplicit second) { - LLUnitImplicit result(first); + LLUnitImplicit result(first); result += second; return result; } @@ -235,50 +246,50 @@ LLUnitImplicit operator + (LLUnitImplicit -LLUnit operator - (LLUnit first, LLUnit second) +template +LLUnit operator - (LLUnit first, LLUnit second) { - LLUnit result(first); + LLUnit result(first); result -= second; return result; } -template -LLUnit operator - (LLUnit first, SCALAR_TYPE second) +template +LLUnit operator - (LLUnit first, SCALAR_TYPE second) { - LLUnit result(first); + LLUnit result(first); result -= second; return result; } -template -LLUnit operator - (SCALAR_TYPE first, LLUnit second) +template +LLUnit operator - (SCALAR_TYPE first, LLUnit second) { - LLUnit result(first); + LLUnit result(first); result -= second; return result; } -template -LLUnitImplicit operator - (LLUnitImplicit first, LLUnitImplicit second) +template +LLUnitImplicit operator - (LLUnitImplicit first, LLUnitImplicit second) { - LLUnitImplicit result(first); + LLUnitImplicit result(first); result -= second; return result; } -template -LLUnitImplicit operator - (LLUnitImplicit first, SCALAR_TYPE second) +template +LLUnitImplicit operator - (LLUnitImplicit first, SCALAR_TYPE second) { - LLUnitImplicit result(first); + LLUnitImplicit result(first); result -= second; return result; } -template -LLUnitImplicit operator - (SCALAR_TYPE first, LLUnitImplicit second) +template +LLUnitImplicit operator - (SCALAR_TYPE first, LLUnitImplicit second) { - LLUnitImplicit result(first); + LLUnitImplicit result(first); result -= second; return result; } @@ -286,102 +297,100 @@ LLUnitImplicit operator - (SCALAR_TYPE first, LLUnitImp // // operator * // -template -LLUnit operator * (SCALAR_TYPE first, LLUnit second) +template +LLUnit operator * (SCALAR_TYPE first, LLUnit second) { - return LLUnit((STORAGE_TYPE)(first * second.value())); + return LLUnit((STORAGE_TYPE)(first * second.value())); } -template -LLUnit operator * (LLUnit first, SCALAR_TYPE second) +template +LLUnit operator * (LLUnit first, SCALAR_TYPE second) { - return LLUnit((STORAGE_TYPE)(first.value() * second)); + return LLUnit((STORAGE_TYPE)(first.value() * second)); } -template -LLUnit operator * (LLUnit, LLUnit) +template +LLUnit operator * (LLUnit, LLUnit) { // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template llstatic_assert_template(STORAGE_TYPE1, false, "Multiplication of unit types results in new unit type - not supported."); - return LLUnit(); + return LLUnit(); } -template -LLUnitImplicit operator * (SCALAR_TYPE first, LLUnitImplicit second) +template +LLUnitImplicit operator * (SCALAR_TYPE first, LLUnitImplicit second) { - return LLUnitImplicit(first * second.value()); + return LLUnitImplicit(first * second.value()); } -template -LLUnitImplicit operator * (LLUnitImplicit first, SCALAR_TYPE second) +template +LLUnitImplicit operator * (LLUnitImplicit first, SCALAR_TYPE second) { - return LLUnitImplicit(first.value() * second); + return LLUnitImplicit(first.value() * second); } -template -LLUnitImplicit operator * (LLUnitImplicit, LLUnitImplicit) +template +LLUnitImplicit operator * (LLUnitImplicit, LLUnitImplicit) { // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template llstatic_assert_template(STORAGE_TYPE1, false, "Multiplication of unit types results in new unit type - not supported."); - return LLUnitImplicit(); + return LLUnitImplicit(); } // // operator / // -template -SCALAR_TYPE operator / (SCALAR_TYPE first, LLUnit second) +template +SCALAR_TYPE operator / (SCALAR_TYPE first, LLUnit second) { return SCALAR_TYPE(first / second.value()); } -template -LLUnit operator / (LLUnit first, SCALAR_TYPE second) +template +LLUnit operator / (LLUnit first, SCALAR_TYPE second) { - return LLUnit((STORAGE_TYPE)(first.value() / second)); + return LLUnit((STORAGE_TYPE)(first.value() / second)); } -template -STORAGE_TYPE1 operator / (LLUnit first, LLUnit second) +template +STORAGE_TYPE1 operator / (LLUnit first, LLUnit second) { - // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template - return STORAGE_TYPE1(first.value() / second.value()); + return STORAGE_TYPE1(first.value() / first.convert(second)); } -template -LLUnitImplicit operator / (LLUnitImplicit first, SCALAR_TYPE second) +template +LLUnitImplicit operator / (LLUnitImplicit first, SCALAR_TYPE second) { - return LLUnitImplicit((STORAGE_TYPE)(first.value() / second)); + return LLUnitImplicit((STORAGE_TYPE)(first.value() / second)); } -template -STORAGE_TYPE1 operator / (LLUnitImplicit first, LLUnitImplicit second) +template +STORAGE_TYPE1 operator / (LLUnitImplicit first, LLUnitImplicit second) { - // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template - return STORAGE_TYPE1(first.value() / second.value()); + return STORAGE_TYPE1(first.value() / first.convert(second)); } #define COMPARISON_OPERATORS(op) \ -template \ -bool operator op (SCALAR_TYPE first, LLUnit second) \ +template \ +bool operator op (SCALAR_TYPE first, LLUnit second) \ { \ return first op second.value(); \ } \ \ -template \ -bool operator op (LLUnit first, SCALAR_TYPE second) \ +template \ +bool operator op (LLUnit first, SCALAR_TYPE second) \ { \ return first.value() op second; \ } \ \ -template \ -bool operator op (LLUnitImplicit first, LLUnitImplicit second) \ +template \ +bool operator op (LLUnitImplicit first, LLUnitImplicit second) \ { \ return first.value() op first.convert(second); \ } \ \ -template \ - bool operator op (LLUnit first, LLUnit second) \ +template \ + bool operator op (LLUnit first, LLUnit second) \ { \ return first.value() op first.convert(second); \ } @@ -401,7 +410,7 @@ struct LLGetUnitLabel }; template -struct LLGetUnitLabel > +struct LLGetUnitLabel > { static const char* getUnitLabel() { return T::getUnitLabel(); } }; @@ -411,70 +420,147 @@ struct LLGetUnitLabel > // namespace LLUnits { + +template +struct LinearOps +{ + typedef LinearOps self_t; + LinearOps(VALUE_TYPE val) : mValue (val) {} + + operator VALUE_TYPE() const { return mValue; } + VALUE_TYPE mValue; + + template + self_t operator * (T other) + { + return mValue * other; + } + + template + self_t operator / (T other) + { + return mValue / other; + } + + template + self_t operator + (T other) + { + return mValue + other; + } + + template + self_t operator - (T other) + { + return mValue - other; + } +}; + +template +struct InverseLinearOps +{ + typedef InverseLinearOps self_t; + + InverseLinearOps(VALUE_TYPE val) : mValue (val) {} + operator VALUE_TYPE() const { return mValue; } + VALUE_TYPE mValue; + + template + self_t operator * (T other) + { + return mValue / other; + } + + template + self_t operator / (T other) + { + return mValue * other; + } + + template + self_t operator + (T other) + { + return mValue - other; + } + + template + self_t operator - (T other) + { + return mValue + other; + } +}; + + template -T rawValue(T val) { return val; } +T storageValue(T val) { return val; } template -STORAGE_TYPE rawValue(LLUnit val) { return val.value(); } +STORAGE_TYPE storageValue(LLUnit val) { return val.value(); } template -STORAGE_TYPE rawValue(LLUnitImplicit val) { return val.value(); } +STORAGE_TYPE storageValue(LLUnitImplicit val) { return val.value(); } -#define LL_DECLARE_DERIVED_UNIT(conversion_factor, base_unit_name, unit_name, unit_label) \ +#define LL_DECLARE_BASE_UNIT(base_unit_name, unit_label) \ +struct base_unit_name { typedef base_unit_name base_unit_t; static const char* getUnitLabel() { return unit_label; }} + +#define LL_DECLARE_DERIVED_UNIT(unit_name, unit_label, base_unit_name, conversion_operation) \ struct unit_name \ { \ typedef base_unit_name base_unit_t; \ static const char* getUnitLabel() { return unit_label; } \ }; \ template \ -struct ConversionFactor \ +struct Convert \ { \ - static F64 get() \ + static STORAGE_TYPE get(STORAGE_TYPE val) \ { \ - return (F64)conversion_factor; \ + return (LinearOps(val) conversion_operation).mValue; \ } \ }; \ \ template \ -struct ConversionFactor \ +struct Convert \ { \ - static F64 get() \ + static STORAGE_TYPE get(STORAGE_TYPE val) \ { \ - return (F64)(1.0 / (conversion_factor)); \ + return (InverseLinearOps(val) conversion_operation).mValue; \ } \ } -#define LL_DECLARE_BASE_UNIT(base_unit_name, unit_label) \ -struct base_unit_name { typedef base_unit_name base_unit_t; static const char* getUnitLabel() { return unit_label; }} - LL_DECLARE_BASE_UNIT(Bytes, "B"); -LL_DECLARE_DERIVED_UNIT(1024, Bytes, Kibibytes, "KiB"); -LL_DECLARE_DERIVED_UNIT(1024 * 1024, Bytes, Mibibytes, "MiB"); -LL_DECLARE_DERIVED_UNIT(1024 * 1024 * 1024, Bytes, Gibibytes, "GiB"); -LL_DECLARE_DERIVED_UNIT(1.0 / 8.0, Bytes, Bits, "b"); -LL_DECLARE_DERIVED_UNIT(1024 / 8, Bytes, Kibibits, "Kib"); -LL_DECLARE_DERIVED_UNIT(1024 / 8, Bytes, Mibibits, "Mib"); -LL_DECLARE_DERIVED_UNIT(1024 * 1024 * 1024 / 8, Bytes, Gibibits, "Gib"); +LL_DECLARE_DERIVED_UNIT(Kilobytes, "KB", Bytes, * 1000); +LL_DECLARE_DERIVED_UNIT(Megabytes, "MB", Bytes, * 1000 * 1000); +LL_DECLARE_DERIVED_UNIT(Gigabytes, "GB", Bytes, * 1000 * 1000 * 1000); +LL_DECLARE_DERIVED_UNIT(Kibibytes, "KiB", Bytes, * 1024); +LL_DECLARE_DERIVED_UNIT(Mibibytes, "MiB", Bytes, * 1024 * 1024); +LL_DECLARE_DERIVED_UNIT(Gibibytes, "GiB", Bytes, * 1024 * 1024 * 1024); + +LL_DECLARE_DERIVED_UNIT(Bits, "b", Bytes, / 8); +LL_DECLARE_DERIVED_UNIT(Kilobits, "Kb", Bytes, * (1000 / 8)); +LL_DECLARE_DERIVED_UNIT(Megabits, "Mb", Bytes, * (1000 / 8)); +LL_DECLARE_DERIVED_UNIT(Gigabits, "Gb", Bytes, * (1000 * 1000 * 1000 / 8)); +LL_DECLARE_DERIVED_UNIT(Kibibits, "Kib", Bytes, * (1024 / 8)); +LL_DECLARE_DERIVED_UNIT(Mibibits, "Mib", Bytes, * (1024 / 8)); +LL_DECLARE_DERIVED_UNIT(Gibibits, "Gib", Bytes, * (1024 * 1024 * 1024 / 8)); LL_DECLARE_BASE_UNIT(Seconds, "s"); -LL_DECLARE_DERIVED_UNIT(60, Seconds, Minutes, "min"); -LL_DECLARE_DERIVED_UNIT(60 * 60, Seconds, Hours, "h"); -LL_DECLARE_DERIVED_UNIT(1.0 / 1000.0, Seconds, Milliseconds, "ms"); -LL_DECLARE_DERIVED_UNIT(1.0 / 1000000.0, Seconds, Microseconds, "\x09\x3cs"); -LL_DECLARE_DERIVED_UNIT(1.0 / 1000000000.0, Seconds, Nanoseconds, "ns"); +LL_DECLARE_DERIVED_UNIT(Minutes, "min", Seconds, * 60); +LL_DECLARE_DERIVED_UNIT(Hours, "h", Seconds, * 60 * 60); +LL_DECLARE_DERIVED_UNIT(Milliseconds, "ms", Seconds, / 1000); +LL_DECLARE_DERIVED_UNIT(Microseconds, "\x09\x3cs", Seconds, / 1000000); +LL_DECLARE_DERIVED_UNIT(Nanoseconds, "ns", Seconds, / 1000000000); LL_DECLARE_BASE_UNIT(Meters, "m"); -LL_DECLARE_DERIVED_UNIT(1000, Meters, Kilometers, "km"); -LL_DECLARE_DERIVED_UNIT(1.0 / 100.0, Meters, Centimeters, "cm"); -LL_DECLARE_DERIVED_UNIT(1.0 / 1000.0, Meters, Millimeters, "mm"); +LL_DECLARE_DERIVED_UNIT(Kilometers, "km", Meters, * 1000); +LL_DECLARE_DERIVED_UNIT(Centimeters, "cm", Meters, * 100); +LL_DECLARE_DERIVED_UNIT(Millimeters, "mm", Meters, * 1000); LL_DECLARE_BASE_UNIT(Hertz, "Hz"); -LL_DECLARE_DERIVED_UNIT(1000, Hertz, Kilohertz, "KHz"); -LL_DECLARE_DERIVED_UNIT(1000 * 1000, Hertz, Megahertz, "MHz"); -LL_DECLARE_DERIVED_UNIT(1000 * 1000 * 1000, Hertz, Gigahertz, "GHz"); +LL_DECLARE_DERIVED_UNIT(Kilohertz, "KHz", Hertz, * 1000); +LL_DECLARE_DERIVED_UNIT(Megahertz, "MHz", Hertz, * 1000 * 1000); +LL_DECLARE_DERIVED_UNIT(Gigahertz, "GHz", Hertz, * 1000 * 1000 * 1000); LL_DECLARE_BASE_UNIT(Radians, "rad"); -LL_DECLARE_DERIVED_UNIT(0.01745329251994, Radians, Degrees, "deg"); +LL_DECLARE_DERIVED_UNIT(Degrees, "deg", Radians, * 0.01745329251994); } // namespace LLUnits -- cgit v1.2.3