diff --git a/glm/core/type_half.hpp b/glm/core/type_half.hpp index 445487c2..ee3e606b 100644 --- a/glm/core/type_half.hpp +++ b/glm/core/type_half.hpp @@ -22,7 +22,7 @@ /// /// @ref core /// @file glm/core/type_half.hpp -/// @date 2008-08-17 / 2011-06-15 +/// @date 2008-08-17 / 2011-09-20 /// @author Christophe Riccio /////////////////////////////////////////////////////////////////////////////////// @@ -40,7 +40,7 @@ namespace detail hdata toFloat16(float const & value); /// 16-bit floating point type. - /// \ingroup gtc_half_float + /// @ingroup gtc_half_float class thalf { public: @@ -52,10 +52,8 @@ namespace detail GLM_FUNC_DECL explicit thalf(U const & s); // Cast - //operator float(); - //GLM_FUNC_DECL operator float() const; - //operator double(); - //operator double() const; + template + GLM_FUNC_DECL operator U() const; // Unary updatable operators GLM_FUNC_DECL thalf& operator= (thalf const & s); diff --git a/glm/core/type_half.inl b/glm/core/type_half.inl index cf720575..ec71fd16 100644 --- a/glm/core/type_half.inl +++ b/glm/core/type_half.inl @@ -269,6 +269,12 @@ namespace detail data(toFloat16(float(s))) {} + template + GLM_FUNC_QUALIFIER thalf::operator U() const + { + return static_cast(this->toFloat()); + } + // Cast //GLM_FUNC_QUALIFIER half::operator float() //{ diff --git a/test/core/core_type_half.cpp b/test/core/core_type_half.cpp index e432b9af..0e4d3d21 100644 --- a/test/core/core_type_half.cpp +++ b/test/core/core_type_half.cpp @@ -2,7 +2,7 @@ // OpenGL Mathematics Copyright (c) 2005 - 2011 G-Truc Creation (www.g-truc.net) /////////////////////////////////////////////////////////////////////////////////////////////////// // Created : 2008-08-31 -// Updated : 2010-08-25 +// Updated : 2011-09-20 // Licence : This source is under MIT licence // File : test/core/type_half.cpp /////////////////////////////////////////////////////////////////////////////////////////////////// @@ -10,24 +10,107 @@ #include #include +int test_half_ctor() +{ + int Error = 0; + + glm::half A(1.0f); + Error += A.toFloat() == 1.0f ? 0 : 1; + + glm::half B = glm::half(1.0f); + Error += B.toFloat() == 1.0f ? 0 : 1; + + glm::half C = B; + Error += C.toFloat() == 1.0f ? 0 : 1; + + return Error; +} + +int test_half_cast() +{ + int Error = 0; + + glm::half A(2.0f); + Error += A.toFloat() == 2.0f ? 0 : 1; + + glm::half B(2.0); + Error += B.toFloat() == 2.0f ? 0 : 1; + + glm::half C(2); + Error += C.toFloat() == 2.0f ? 0 : 1; + + float D(A); + Error += D == 2.0f ? 0 : 1; + + double E(A); + Error += E == 2.0 ? 0 : 1; + + int F(A); + Error += F == 2.0 ? 0 : 1; + + return Error; +} + +int test_half_relational() +{ + int Error = 0; + + glm::half A(1.0f); + glm::half B(2.0f); + + Error += !(A == B) ? 0 : 1; + Error += (A != B) ? 0 : 1; + Error += (A <= B) ? 0 : 1; + Error += (A < B) ? 0 : 1; + Error += !(A >= B) ? 0 : 1; + Error += !(A > B) ? 0 : 1; + + return Error; +} + +int test_half_arithmetic_unary_ops() +{ + int Error = 0; + + glm::half A(2.0f); + glm::half B(3.0f); + + Error += A + B == glm::half( 5.0f) ? 0 : 1; + Error += A - B == glm::half(-1.0f) ? 0 : 1; + Error += B - A == glm::half( 1.0f) ? 0 : 1; + Error += A * B == glm::half( 6.0f) ? 0 : 1; + Error += A / B == glm::half(2.0f / 3.0f) ? 0 : 1; + Error += B / A == glm::half(3.0f / 2.0f) ? 0 : 1; + + return Error; +} + +int test_half_arithmetic_binary_ops() +{ + int Error = 0; + + glm::half A(2.0f); + glm::half B(3.0f); + + Error += A + B == glm::half( 5.0f) ? 0 : 1; + Error += A - B == glm::half(-1.0f) ? 0 : 1; + Error += B - A == glm::half( 1.0f) ? 0 : 1; + Error += A * B == glm::half( 6.0f) ? 0 : 1; + Error += A / B == glm::half(2.0f / 3.0f) ? 0 : 1; + Error += B / A == glm::half(3.0f / 2.0f) ? 0 : 1; + + return Error; +} + int main() { int Result = 0; - glm::half A(1.0f); - glm::half B(2.0f); - glm::half C = A + B; - glm::half D(C); - float E = D.toFloat(); - int F = C.toFloat(); - Result += float(F) == E ? 0 : 1; - glm::half G = B * C; - glm::half H = G / C; - H += glm::half(1.0f); - double J = H.toFloat(); - int I = H.toFloat(); - Result += J == 3.0 ? 0 : 1; - Result += I == 3 ? 0 : 1; + Result += test_half_ctor(); + Result += test_half_cast(); + Result += test_half_relational(); + Result += test_half_arithmetic_unary_ops(); + Result += test_half_arithmetic_binary_ops(); return Result; }