From 014969690bed06d77cc2e08efbd2dc9b71fb0cd2 Mon Sep 17 00:00:00 2001 From: Richard Linden Date: Tue, 27 Aug 2013 16:04:15 -0700 Subject: shuffled things around to get MSVC optimizer to generate optimal code in particular, created shortcut overloads for conversions and moved comparison operators into member functions --- indra/llcommon/llunittype.h | 445 +++++++++++++++++++++++++------------------- 1 file changed, 253 insertions(+), 192 deletions(-) diff --git a/indra/llcommon/llunittype.h b/indra/llcommon/llunittype.h index 949e4492c7..a5e99070b6 100644 --- a/indra/llcommon/llunittype.h +++ b/indra/llcommon/llunittype.h @@ -75,10 +75,10 @@ struct LLResultTypePromote typedef LL_TYPEOF((true) ? S() : T()) type_t; }; -template +template struct LLUnit { - typedef LLUnit self_t; + typedef LLUnit self_t; typedef STORAGE_TYPE storage_t; // value initialization @@ -87,55 +87,55 @@ struct LLUnit {} // unit initialization and conversion - template - LL_FORCE_INLINE LLUnit(LLUnit other) + template + LL_FORCE_INLINE LLUnit(LLUnit other) : mValue(convert(other).mValue) {} - storage_t value() const + LL_FORCE_INLINE storage_t value() const { return mValue; } - void value(storage_t value) + LL_FORCE_INLINE void value(storage_t value) { mValue = value; } - template + template storage_t valueInUnits() { - return LLUnit(*this).value(); + return LLUnit(*this).value(); } - template + template void valueInUnits(storage_t value) { - *this = LLUnit(value); + *this = LLUnit(value); } - void operator += (self_t other) + LL_FORCE_INLINE void operator += (self_t other) { mValue += convert(other).mValue; } - void operator -= (self_t other) + LL_FORCE_INLINE void operator -= (self_t other) { mValue -= convert(other).mValue; } - void operator *= (storage_t multiplicand) + LL_FORCE_INLINE void operator *= (storage_t multiplicand) { mValue *= multiplicand; } - void operator *= (self_t multiplicand) + LL_FORCE_INLINE void operator *= (self_t multiplicand) { // spurious use of dependent type to stop gcc from triggering the static assertion before instantiating the template LL_BAD_TEMPLATE_INSTANTIATION(STORAGE_TYPE, "Multiplication of unit types not supported."); } - void operator /= (storage_t divisor) + LL_FORCE_INLINE void operator /= (storage_t divisor) { mValue /= divisor; } @@ -146,11 +146,58 @@ struct LLUnit LL_BAD_TEMPLATE_INSTANTIATION(STORAGE_TYPE, "Illegal in-place division of unit types."); } - template - LL_FORCE_INLINE static self_t convert(LLUnit v) + template + LL_FORCE_INLINE bool operator == (LLUnit other) + { + return mValue == convert(other).value(); + } + + template + LL_FORCE_INLINE bool operator != (LLUnit other) + { + return mValue != convert(other).value(); + } + + template + LL_FORCE_INLINE bool operator < (LLUnit other) + { + return mValue < convert(other).value(); + } + + template + LL_FORCE_INLINE bool operator <= (LLUnit other) + { + return mValue <= convert(other).value(); + } + template + LL_FORCE_INLINE bool operator > (LLUnit other) + { + return mValue > convert(other).value(); + } + template + LL_FORCE_INLINE bool operator >= (LLUnit other) + { + return mValue >= convert(other).value(); + } + + LL_FORCE_INLINE static self_t convert(self_t v) { - typedef typename LLResultTypePromote::type_t result_storage_t; - LLUnit result; + return v; + } + + template + LL_FORCE_INLINE static self_t convert(LLUnit v) + { + self_t result; + result.mValue = (STORAGE_TYPE)v.value(); + return result; + } + + template + LL_FORCE_INLINE static self_t convert(LLUnit v) + { + typedef typename LLResultTypePromote::type_t result_storage_t; + LLUnit result; result_storage_t divisor = ll_convert_units(v, result); result.value(result.value() / divisor); return self_t(result.value()); @@ -160,15 +207,15 @@ protected: storage_t mValue; }; -template -std::ostream& operator <<(std::ostream& s, const LLUnit& unit) +template +std::ostream& operator <<(std::ostream& s, const LLUnit& unit) { - s << unit.value() << UNIT_TYPE::getUnitLabel(); + s << unit.value() << UNITS::getUnitLabel(); return s; } -template -std::istream& operator >>(std::istream& s, LLUnit& unit) +template +std::istream& operator >>(std::istream& s, LLUnit& unit) { STORAGE_TYPE val; s >> val; @@ -176,68 +223,145 @@ std::istream& operator >>(std::istream& s, LLUnit& unit return s; } -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()) + LL_FORCE_INLINE LLUnitImplicit(storage_t value = storage_t()) : base_t(value) {} - template - LLUnitImplicit(LLUnit other) + template + LL_FORCE_INLINE LLUnitImplicit(LLUnit other) : base_t(other) {} // unlike LLUnit, LLUnitImplicit is *implicitly* convertable to a POD value (F32, S32, etc) // this allows for interoperability with legacy code - operator storage_t() const + LL_FORCE_INLINE operator storage_t() const { return base_t::value(); } using base_t::operator +=; - void operator += (storage_t value) + LL_FORCE_INLINE void operator += (storage_t value) { base_t::mValue += value; } // this overload exists to explicitly catch use of another implicit unit // without ambiguity between conversion to storage_t vs conversion to base_t - template - void operator += (LLUnitImplicit other) + template + LL_FORCE_INLINE void operator += (LLUnitImplicit other) { base_t::mValue += convert(other).value(); } using base_t::operator -=; - void operator -= (storage_t value) + LL_FORCE_INLINE void operator -= (storage_t value) { base_t::mValue -= value; } // this overload exists to explicitly catch use of another implicit unit // without ambiguity between conversion to storage_t vs conversion to base_t - template - void operator -= (LLUnitImplicit other) + template + LL_FORCE_INLINE void operator -= (LLUnitImplicit other) { base_t::mValue -= convert(other).value(); } + using base_t::operator ==; + template + LL_FORCE_INLINE bool operator == (LLUnitImplicit other) + { + return mValue == convert(other).value(); + } + + template + LL_FORCE_INLINE bool operator == (STORAGE_T other) + { + return mValue == other; + } + + using base_t::operator !=; + template + LL_FORCE_INLINE bool operator != (LLUnitImplicit other) + { + return mValue != convert(other).value(); + } + + template + LL_FORCE_INLINE bool operator != (STORAGE_T other) + { + return mValue != other; + } + + using base_t::operator <; + template + LL_FORCE_INLINE bool operator < (LLUnitImplicit other) + { + return mValue < convert(other).value(); + } + + template + LL_FORCE_INLINE bool operator < (STORAGE_T other) + { + return mValue < other; + } + + using base_t::operator <=; + template + LL_FORCE_INLINE bool operator <= (LLUnitImplicit other) + { + return mValue <= convert(other).value(); + } + + template + LL_FORCE_INLINE bool operator <= (STORAGE_T other) + { + return mValue <= other; + } + + using base_t::operator >; + template + LL_FORCE_INLINE bool operator > (LLUnitImplicit other) + { + return mValue > convert(other).value(); + } + + template + LL_FORCE_INLINE bool operator > (STORAGE_T other) + { + return mValue > other; + } + + using base_t::operator >=; + template + LL_FORCE_INLINE bool operator >= (LLUnitImplicit other) + { + return mValue >= convert(other).value(); + } + + template + LL_FORCE_INLINE bool operator >= (STORAGE_T other) + { + return mValue >= other; + } }; -template -std::ostream& operator <<(std::ostream& s, const LLUnitImplicit& unit) +template +std::ostream& operator <<(std::ostream& s, const LLUnitImplicit& unit) { - s << unit.value() << UNIT_TYPE::getUnitLabel(); + s << unit.value() << UNITS::getUnitLabel(); return s; } -template -std::istream& operator >>(std::istream& s, LLUnitImplicit& unit) +template +std::istream& operator >>(std::istream& s, LLUnitImplicit& unit) { STORAGE_TYPE val; s >> val; @@ -283,8 +407,8 @@ struct LLStorageType typedef T type_t; }; -template -struct LLStorageType > +template +struct LLStorageType > { typedef STORAGE_TYPE type_t; }; @@ -296,65 +420,65 @@ struct LLStorageType > // // operator + // -template -LLUnit::type_t, UNIT_TYPE1> operator + (LLUnit first, LLUnit second) +template +LL_FORCE_INLINE LLUnit::type_t, UNITS1> operator + (LLUnit first, LLUnit second) { - LLUnit::type_t, UNIT_TYPE1> result(first); + LLUnit::type_t, UNITS1> result(first); result += second; return result; } -template -LLUnit operator + (LLUnit first, UNITLESS second) +template +LLUnit operator + (LLUnit first, UNITLESS second) { LL_BAD_TEMPLATE_INSTANTIATION(STORAGE_TYPE, "operator + requires compatible unit types"); - return LLUnit(0); + return LLUnit(0); } -template -LLUnit operator + (UNITLESS first, LLUnit second) +template +LLUnit operator + (UNITLESS first, LLUnit second) { LL_BAD_TEMPLATE_INSTANTIATION(STORAGE_TYPE, "operator + requires compatible unit types"); - return LLUnit(0); + return LLUnit(0); } -template -LLUnitImplicit::type_t, UNIT_TYPE1> operator + (LLUnitImplicit first, LLUnitImplicit second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t, UNITS1> operator + (LLUnitImplicit first, LLUnitImplicit second) { - LLUnitImplicit::type_t, UNIT_TYPE1> result(first); + LLUnitImplicit::type_t, UNITS1> result(first); result += second; return result; } -template -LLUnitImplicit::type_t, UNIT_TYPE1> operator + (LLUnit first, LLUnitImplicit second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t, UNITS1> operator + (LLUnit first, LLUnitImplicit second) { - LLUnitImplicit::type_t, UNIT_TYPE1> result(first); + LLUnitImplicit::type_t, UNITS1> result(first); result += second; return result; } -template -LLUnitImplicit::type_t, UNIT_TYPE1> operator + (LLUnitImplicit first, LLUnit second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t, UNITS1> operator + (LLUnitImplicit first, LLUnit second) { - LLUnitImplicit::type_t, UNIT_TYPE1> result(first); - result += LLUnitImplicit(second); + LLUnitImplicit::type_t, UNITS1> result(first); + result += LLUnitImplicit(second); return result; } -template -LLUnitImplicit::type_t>::type_t, UNIT_TYPE> operator + (LLUnitImplicit first, UNITLESS_TYPE second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t>::type_t, UNITS> operator + (LLUnitImplicit first, UNITLESS_TYPE second) { - LLUnitImplicit::type_t>::type_t, UNIT_TYPE> result(first); + LLUnitImplicit::type_t>::type_t, UNITS> result(first); result += second; return result; } -template -LLUnitImplicit::type_t, STORAGE_TYPE>:: - type_t, UNIT_TYPE> operator + (UNITLESS_TYPE first, LLUnitImplicit second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t, STORAGE_TYPE>:: + type_t, UNITS> operator + (UNITLESS_TYPE first, LLUnitImplicit second) { - LLUnitImplicit::type_t, STORAGE_TYPE>::type_t, UNIT_TYPE> result(first); + LLUnitImplicit::type_t, STORAGE_TYPE>::type_t, UNITS> result(first); result += second; return result; } @@ -362,64 +486,64 @@ LLUnitImplicit::t // // operator - // -template -LLUnit::type_t, UNIT_TYPE1> operator - (LLUnit first, LLUnit second) +template +LL_FORCE_INLINE LLUnit::type_t, UNITS1> operator - (LLUnit first, LLUnit second) { - LLUnit::type_t, UNIT_TYPE1> result(first); + LLUnit::type_t, UNITS1> result(first); result -= second; return result; } -template -LLUnit operator - (LLUnit first, UNITLESS second) +template +LLUnit operator - (LLUnit first, UNITLESS second) { LL_BAD_TEMPLATE_INSTANTIATION(STORAGE_TYPE, "operator - requires compatible unit types"); - return LLUnit(0); + return LLUnit(0); } -template -LLUnit operator - (UNITLESS first, LLUnit second) +template +LLUnit operator - (UNITLESS first, LLUnit second) { LL_BAD_TEMPLATE_INSTANTIATION(STORAGE_TYPE, "operator - requires compatible unit types"); - return LLUnit(0); + return LLUnit(0); } -template -LLUnitImplicit::type_t, UNIT_TYPE1> operator - (LLUnitImplicit first, LLUnitImplicit second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t, UNITS1> operator - (LLUnitImplicit first, LLUnitImplicit second) { - LLUnitImplicit::type_t, UNIT_TYPE1> result(first); + LLUnitImplicit::type_t, UNITS1> result(first); result -= second; return result; } -template -LLUnitImplicit::type_t, UNIT_TYPE1> operator - (LLUnit first, LLUnitImplicit second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t, UNITS1> operator - (LLUnit first, LLUnitImplicit second) { - LLUnitImplicit::type_t, UNIT_TYPE1> result(first); + LLUnitImplicit::type_t, UNITS1> result(first); result -= second; return result; } -template -LLUnitImplicit::type_t, UNIT_TYPE1> operator - (LLUnitImplicit first, LLUnit second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t, UNITS1> operator - (LLUnitImplicit first, LLUnit second) { - LLUnitImplicit::type_t, UNIT_TYPE1> result(first); - result -= LLUnitImplicit(second); + LLUnitImplicit::type_t, UNITS1> result(first); + result -= LLUnitImplicit(second); return result; } -template -LLUnitImplicit::type_t>::type_t, UNIT_TYPE> operator - (LLUnitImplicit first, UNITLESS_TYPE second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t>::type_t, UNITS> operator - (LLUnitImplicit first, UNITLESS_TYPE second) { - LLUnitImplicit::type_t>::type_t, UNIT_TYPE> result(first); + LLUnitImplicit::type_t>::type_t, UNITS> result(first); result -= second; return result; } -template -LLUnitImplicit::type_t, STORAGE_TYPE>::type_t, UNIT_TYPE> operator - (UNITLESS_TYPE first, LLUnitImplicit second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t, STORAGE_TYPE>::type_t, UNITS> operator - (UNITLESS_TYPE first, LLUnitImplicit second) { - LLUnitImplicit::type_t, STORAGE_TYPE>::type_t, UNIT_TYPE> result(first); + LLUnitImplicit::type_t, STORAGE_TYPE>::type_t, UNITS> result(first); result -= second; return result; } @@ -427,44 +551,44 @@ LLUnitImplicit -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 LL_BAD_TEMPLATE_INSTANTIATION(STORAGE_TYPE1, "multiplication of unit types results in new unit type - not supported."); - return LLUnit(); + return LLUnit(); } -template -LLUnit::type_t>::type_t, UNIT_TYPE> operator * (LLUnit first, UNITLESS_TYPE second) +template +LL_FORCE_INLINE LLUnit::type_t>::type_t, UNITS> operator * (LLUnit first, UNITLESS_TYPE second) { - return LLUnit::type_t>::type_t, UNIT_TYPE>(first.value() * second); + return LLUnit::type_t>::type_t, UNITS>(first.value() * second); } -template -LLUnit::type_t, STORAGE_TYPE>::type_t, UNIT_TYPE> operator * (UNITLESS_TYPE first, LLUnit second) +template +LL_FORCE_INLINE LLUnit::type_t, STORAGE_TYPE>::type_t, UNITS> operator * (UNITLESS_TYPE first, LLUnit second) { - return LLUnit::type_t, STORAGE_TYPE>::type_t, UNIT_TYPE>(first * second.value()); + return LLUnit::type_t, STORAGE_TYPE>::type_t, UNITS>(first * second.value()); } -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 LL_BAD_TEMPLATE_INSTANTIATION(STORAGE_TYPE1, "multiplication of unit types results in new unit type - not supported."); - return LLUnitImplicit(); + return LLUnitImplicit(); } -template -LLUnitImplicit::type_t>::type_t, UNIT_TYPE> operator * (LLUnitImplicit first, UNITLESS_TYPE second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t>::type_t, UNITS> operator * (LLUnitImplicit first, UNITLESS_TYPE second) { - return LLUnitImplicit::type_t, UNIT_TYPE>(first.value() * second); + return LLUnitImplicit::type_t, UNITS>(first.value() * second); } -template -LLUnitImplicit::type_t, STORAGE_TYPE>::type_t, UNIT_TYPE> operator * (UNITLESS_TYPE first, LLUnitImplicit second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t, STORAGE_TYPE>::type_t, UNITS> operator * (UNITLESS_TYPE first, LLUnitImplicit second) { - return LLUnitImplicit::type_t, STORAGE_TYPE>::type_t, UNIT_TYPE>(first * second.value()); + return LLUnitImplicit::type_t, STORAGE_TYPE>::type_t, UNITS>(first * second.value()); } @@ -472,105 +596,42 @@ LLUnitImplicit -LLUnit::type_t>::type_t, UNIT_TYPE> operator / (LLUnit first, UNITLESS_TYPE second) +template +LL_FORCE_INLINE LLUnit::type_t>::type_t, UNITS> operator / (LLUnit first, UNITLESS_TYPE second) { - return LLUnit::type_t>::type_t, UNIT_TYPE>(first.value() / second); + return LLUnit::type_t>::type_t, UNITS>(first.value() / second); } -template -typename LLResultTypeDivide::type_t operator / (LLUnit first, LLUnit second) +template +LL_FORCE_INLINE typename LLResultTypeDivide::type_t operator / (LLUnit first, LLUnit second) { return first.value() / first.convert(second).value(); } -template -LLUnitImplicit::type_t>::type_t, UNIT_TYPE> operator / (LLUnitImplicit first, UNITLESS_TYPE second) +template +LL_FORCE_INLINE LLUnitImplicit::type_t>::type_t, UNITS> operator / (LLUnitImplicit first, UNITLESS_TYPE second) { - return LLUnitImplicit::type_t>::type_t, UNIT_TYPE>(first.value() / second); + return LLUnitImplicit::type_t>::type_t, UNITS>(first.value() / second); } -template -typename LLResultTypeDivide::type_t operator / (LLUnitImplicit first, LLUnitImplicit second) +template +LL_FORCE_INLINE typename LLResultTypeDivide::type_t operator / (LLUnitImplicit first, LLUnitImplicit second) { return (typename LLResultTypeDivide::type_t)(first.value() / first.convert(second).value()); } -template -typename LLResultTypeDivide::type_t operator / (LLUnit first, LLUnitImplicit second) +template +LL_FORCE_INLINE typename LLResultTypeDivide::type_t operator / (LLUnit first, LLUnitImplicit second) { return (typename LLResultTypeDivide::type_t)(first.value() / first.convert(second).value()); } -template -typename LLResultTypeDivide::type_t operator / (LLUnitImplicit first, LLUnit second) +template +LL_FORCE_INLINE typename LLResultTypeDivide::type_t operator / (LLUnitImplicit first, LLUnit second) { return (typename LLResultTypeDivide::type_t)(first.value() / first.convert(second).value()); } -// -// comparison operators -// - -#define LL_UNIT_DECLARE_COMPARISON_OPERATOR(op) \ -template \ -bool operator op (LLUnitImplicit first, LLUnitImplicit second) \ -{ \ - return first.value() op first.convert(second).value(); \ -} \ - \ -template \ -bool operator op (LLUnitImplicit first, UNITLESS_TYPE second) \ -{ \ - return first.value() op second; \ -} \ - \ -template \ -bool operator op (UNITLESS_TYPE first, LLUnitImplicit second) \ -{ \ - return first op second.value(); \ -} \ - \ -template \ -bool operator op (LLUnit first, LLUnit second) \ -{ \ - return first.value() op first.convert(second).value(); \ -} \ - \ -template \ -bool operator op (LLUnit first, UNITLESS_TYPE second) \ -{ \ - LL_BAD_TEMPLATE_INSTANTIATION(UNITLESS_TYPE, "operator " #op " requires compatible unit types"); \ - return false; \ -} \ - \ -template \ -bool operator op (UNITLESS_TYPE first, LLUnit second) \ -{ \ - LL_BAD_TEMPLATE_INSTANTIATION(UNITLESS_TYPE, "operator " #op " requires compatible unit types"); \ - return false; \ -} \ - \ -template \ -bool operator op (LLUnit first, LLUnitImplicit second) \ -{ \ - return first.value() op first.convert(second).value(); \ -} \ - \ -template \ -bool operator op (LLUnitImplicit first, LLUnit second) \ -{ \ - return first.value() op first.convert(second).value(); \ -} - -LL_UNIT_DECLARE_COMPARISON_OPERATOR(<); -LL_UNIT_DECLARE_COMPARISON_OPERATOR(<=); -LL_UNIT_DECLARE_COMPARISON_OPERATOR(>); -LL_UNIT_DECLARE_COMPARISON_OPERATOR(>=); -LL_UNIT_DECLARE_COMPARISON_OPERATOR(==); -LL_UNIT_DECLARE_COMPARISON_OPERATOR(!=); - - template struct LLGetUnitLabel { -- cgit v1.2.3