Added cast from Half types to any scalar types

This commit is contained in:
Christophe Riccio 2011-09-20 17:44:58 +01:00
parent edff9f7bf2
commit fc30641140
3 changed files with 108 additions and 21 deletions

View File

@ -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 <typename U>
GLM_FUNC_DECL operator U() const;
// Unary updatable operators
GLM_FUNC_DECL thalf& operator= (thalf const & s);

View File

@ -269,6 +269,12 @@ namespace detail
data(toFloat16(float(s)))
{}
template <typename U>
GLM_FUNC_QUALIFIER thalf::operator U() const
{
return static_cast<U>(this->toFloat());
}
// Cast
//GLM_FUNC_QUALIFIER half::operator float()
//{

View File

@ -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 <glm/glm.hpp>
#include <glm/gtc/half_float.hpp>
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;
}