Improved half implementation

This commit is contained in:
Christophe Riccio 2011-09-19 19:48:26 +01:00
parent 3ca45b7f0e
commit 16bcc4444f
11 changed files with 181 additions and 86 deletions

View file

@ -158,7 +158,7 @@ namespace detail
template <>
GLM_FUNC_QUALIFIER detail::thalf floor<detail::thalf>(detail::thalf const& x)
{
return detail::thalf(::std::floor(float(x)));
return detail::thalf(::std::floor(x.toFloat()));
}
template <typename genType>

View file

@ -53,7 +53,7 @@ namespace detail
// Cast
//operator float();
GLM_FUNC_DECL operator float() const;
//GLM_FUNC_DECL operator float() const;
//operator double();
//operator double() const;
@ -89,6 +89,30 @@ namespace detail
thalf operator++ (thalf const & s, int);
bool operator==(
detail::thalf const & x,
detail::thalf const & y);
bool operator!=(
detail::thalf const & x,
detail::thalf const & y);
bool operator<(
detail::thalf const & x,
detail::thalf const & y);
bool operator<=(
detail::thalf const & x,
detail::thalf const & y);
bool operator>(
detail::thalf const & x,
detail::thalf const & y);
bool operator>=(
detail::thalf const & x,
detail::thalf const & y);
}//namespace detail
}//namespace glm

View file

@ -250,7 +250,9 @@ namespace detail
// Assemble the half from s, e and m.
//
return hdata(s | (e << 10) | (m >> 13));
hdata Hdata(s | (e << 10) | (m >> 13));
return Hdata;
}
}
@ -273,10 +275,10 @@ namespace detail
// return toFloat();
//}
GLM_FUNC_QUALIFIER thalf::operator float() const
{
return toFloat32(this->data);
}
//GLM_FUNC_QUALIFIER thalf::operator float() const
//{
// return toFloat32(this->data);
//}
//GLM_FUNC_QUALIFIER half::operator double()
//{
@ -338,38 +340,92 @@ namespace detail
GLM_FUNC_QUALIFIER detail::thalf operator+ (detail::thalf const & s1, detail::thalf const & s2)
{
return detail::thalf(float(s1) + float(s2));
return detail::thalf(s1.toFloat() + s2.toFloat());
}
GLM_FUNC_QUALIFIER detail::thalf operator- (detail::thalf const & s1, detail::thalf const & s2)
{
return detail::thalf(float(s1) - float(s2));
return detail::thalf(s1.toFloat() - s2.toFloat());
}
GLM_FUNC_QUALIFIER detail::thalf operator* (detail::thalf const & s1, detail::thalf const & s2)
{
return detail::thalf(float(s1) * float(s2));
return detail::thalf(s1.toFloat() * s2.toFloat());
}
GLM_FUNC_QUALIFIER detail::thalf operator/ (detail::thalf const & s1, detail::thalf const & s2)
{
return detail::thalf(float(s1) / float(s2));
return detail::thalf(s1.toFloat() / s2.toFloat());
}
// Unary constant operators
GLM_FUNC_QUALIFIER detail::thalf operator- (detail::thalf const & s)
{
return detail::thalf(-float(s));
return detail::thalf(-s.toFloat());
}
GLM_FUNC_QUALIFIER detail::thalf operator-- (detail::thalf const & s, int)
{
return detail::thalf(float(s) - 1.0f);
return detail::thalf(s.toFloat() - 1.0f);
}
GLM_FUNC_QUALIFIER detail::thalf operator++ (detail::thalf const & s, int)
{
return detail::thalf(float(s) + 1.0f);
return detail::thalf(s.toFloat() + 1.0f);
}
GLM_FUNC_QUALIFIER bool operator==
(
detail::thalf const & x,
detail::thalf const & y
)
{
return x._data() == y._data();
}
GLM_FUNC_QUALIFIER bool operator!=
(
detail::thalf const & x,
detail::thalf const & y
)
{
return x._data() != y._data();
}
GLM_FUNC_QUALIFIER bool operator<
(
detail::thalf const & x,
detail::thalf const & y
)
{
return x.toFloat() < y.toFloat();
}
GLM_FUNC_QUALIFIER bool operator<=
(
detail::thalf const & x,
detail::thalf const & y
)
{
return x.toFloat() <= y.toFloat();
}
GLM_FUNC_QUALIFIER bool operator>
(
detail::thalf const & x,
detail::thalf const & y
)
{
return x.toFloat() > y.toFloat();
}
GLM_FUNC_QUALIFIER bool operator>=
(
detail::thalf const & x,
detail::thalf const & y
)
{
return x.toFloat() >= y.toFloat();
}
}//namespace detail

View file

@ -142,11 +142,11 @@ namespace detail
template <typename U, typename V>
GLM_FUNC_QUALIFIER tvec2<T>::tvec2
(
U const & x,
V const & y
U const & a,
V const & b
) :
x(value_type(x)),
y(value_type(y))
x(value_type(a)),
y(value_type(b))
{}
//////////////////////////////////////

View file

@ -47,7 +47,7 @@
namespace glm{
namespace detail
{
#ifndef _MSC_EXTENSIONS
#if 1 //ndef _MSC_EXTENSIONS
template <>
struct tvec2<thalf>
{

View file

@ -29,7 +29,7 @@
namespace glm{
namespace detail{
#ifndef _MSC_EXTENSIONS
#if 1 //ndef _MSC_EXTENSIONS
//////////////////////////////////////
// hvec2

View file

@ -29,12 +29,15 @@ GLM_FUNC_QUALIFIER genType higherMultiple
template <>
GLM_FUNC_QUALIFIER detail::thalf higherMultiple
(
detail::thalf const & Source,
detail::thalf const & Multiple
detail::thalf const & SourceH,
detail::thalf const & MultipleH
)
{
int Tmp = int(float(Source)) % int(float(Multiple));
return Tmp ? Source + Multiple - detail::thalf(float(Tmp)) : Source;
float Source = SourceH.toFloat();
float Multiple = MultipleH.toFloat();
int Tmp = int(float(Source)) % int();
return detail::thalf(Tmp ? Source + Multiple - float(Tmp) : Source);
}
template <>
@ -115,12 +118,15 @@ GLM_FUNC_QUALIFIER genType lowerMultiple
template <>
GLM_FUNC_QUALIFIER detail::thalf lowerMultiple
(
detail::thalf const & Source,
detail::thalf const & Multiple
detail::thalf const & SourceH,
detail::thalf const & MultipleH
)
{
float Source = SourceH.toFloat();
float Multiple = MultipleH.toFloat();
int Tmp = int(float(Source)) % int(float(Multiple));
return Tmp ? Source - detail::thalf(float(Tmp)) : Source;
return detail::thalf(Tmp ? Source - float(Tmp) : Source);
}
template <>

View file

@ -43,7 +43,7 @@ namespace detail
GLM_FUNC_QUALIFIER std::string to_string(detail::thalf const & x)
{
return detail::format("half(%2.4f)", float(x));
return detail::format("half(%2.4f)", x.toFloat());
}
GLM_FUNC_QUALIFIER std::string to_string(float x)
@ -111,7 +111,7 @@ namespace detail
detail::tvec2<detail::thalf> const & v
)
{
return detail::format("hvec2(%2.4f, %2.4f)", float(v.x), float(v.y));
return detail::format("hvec2(%2.4f, %2.4f)", v.x.toFloat(), v.y.toFloat());
}
template <>
@ -120,7 +120,7 @@ namespace detail
detail::tvec3<detail::thalf> const & v
)
{
return detail::format("hvec3(%2.4f, %2.4f, %2.4f)", float(v.x), float(v.y), float(v.z));
return detail::format("hvec3(%2.4f, %2.4f, %2.4f)", v.x.toFloat(), v.y.toFloat(), v.z.toFloat());
}
template <>
@ -129,7 +129,7 @@ namespace detail
detail::tvec4<detail::thalf> const & v
)
{
return detail::format("hvec4(%2.4f, %2.4f, %2.4f, %2.4f)", float(v.x), float(v.y), float(v.z), float(v.w));
return detail::format("hvec4(%2.4f, %2.4f, %2.4f, %2.4f)", v.x.toFloat(), v.y.toFloat(), v.z.toFloat(), v.w.toFloat());
}
////////////////////////////////
@ -261,115 +261,106 @@ namespace detail
detail::tmat2x2<detail::thalf> const & m
)
{
detail::tmat2x2<float> x(m);
return detail::format("hmat2x2((%f, %f), (%f, %f))",
x[0][0], x[0][1],
x[1][0], x[1][1]);
m[0][0].toFloat(), m[0][1].toFloat(),
m[1][0].toFloat(), m[1][1].toFloat());
}
template <>
GLM_FUNC_QUALIFIER std::string to_string
(
detail::tmat2x3<detail::thalf> const & m
detail::tmat2x3<detail::thalf> const & x
)
{
detail::tmat2x3<float> x(m);
return detail::format("hmat2x3((%f, %f, %f), (%f, %f, %f))",
x[0][0], x[0][1], x[0][2],
x[1][0], x[1][1], x[1][2]);
x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(),
x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat());
}
template <>
GLM_FUNC_QUALIFIER std::string to_string
(
detail::tmat2x4<detail::thalf> const & m
detail::tmat2x4<detail::thalf> const & x
)
{
detail::tmat2x4<float> x(m);
return detail::format("hmat2x4((%f, %f, %f, %f), (%f, %f, %f, %f))",
x[0][0], x[0][1], x[0][2], x[0][3],
x[1][0], x[1][1], x[1][2], x[1][3]);
x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(), x[0][3].toFloat(),
x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat(), x[1][3].toFloat());
}
template <>
GLM_FUNC_QUALIFIER std::string to_string
(
detail::tmat3x2<detail::thalf> const & m
detail::tmat3x2<detail::thalf> const & x
)
{
detail::tmat3x2<float> x(m);
return detail::format("hmat3x2((%f, %f), (%f, %f), (%f, %f))",
x[0][0], x[0][1],
x[1][0], x[1][1],
x[2][0], x[2][1]);
x[0][0].toFloat(), x[0][1].toFloat(),
x[1][0].toFloat(), x[1][1].toFloat(),
x[2][0].toFloat(), x[2][1].toFloat());
}
template <>
GLM_FUNC_QUALIFIER std::string to_string
(
detail::tmat3x3<detail::thalf> const & m
detail::tmat3x3<detail::thalf> const & x
)
{
detail::tmat3x3<float> x(m);
return detail::format("hmat3x3((%f, %f, %f), (%f, %f, %f), (%f, %f, %f))",
x[0][0], x[0][1], x[0][2],
x[1][0], x[1][1], x[1][2],
x[2][0], x[2][1], x[2][2]);
x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(),
x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat(),
x[2][0].toFloat(), x[2][1].toFloat(), x[2][2].toFloat());
}
template <>
GLM_FUNC_QUALIFIER std::string to_string
(
detail::tmat3x4<detail::thalf> const & m
detail::tmat3x4<detail::thalf> const & x
)
{
detail::tmat3x4<float> x(m);
return detail::format("hmat3x4((%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f))",
x[0][0], x[0][1], x[0][2], x[0][3],
x[1][0], x[1][1], x[1][2], x[1][3],
x[2][0], x[2][1], x[2][2], x[2][3]);
x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(), x[0][3].toFloat(),
x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat(), x[1][3].toFloat(),
x[2][0].toFloat(), x[2][1].toFloat(), x[2][2].toFloat(), x[2][3].toFloat());
}
template <>
GLM_FUNC_QUALIFIER std::string to_string
(
detail::tmat4x2<detail::thalf> const & m
detail::tmat4x2<detail::thalf> const & x
)
{
detail::tmat4x2<float> x(m);
return detail::format("hmat4x2((%f, %f), (%f, %f), (%f, %f), (%f, %f))",
x[0][0], x[0][1],
x[1][0], x[1][1],
x[2][0], x[2][1],
x[3][0], x[3][1]);
x[0][0].toFloat(), x[0][1].toFloat(),
x[1][0].toFloat(), x[1][1].toFloat(),
x[2][0].toFloat(), x[2][1].toFloat(),
x[3][0].toFloat(), x[3][1].toFloat());
}
template <>
GLM_FUNC_QUALIFIER std::string to_string
(
detail::tmat4x3<detail::thalf> const & m
detail::tmat4x3<detail::thalf> const & x
)
{
detail::tmat4x3<float> x(m);
return detail::format("hmat4x3((%f, %f, %f), (%f, %f, %f), (%f, %f, %f), (%f, %f, %f))",
x[0][0], x[0][1], x[0][2],
x[1][0], x[1][1], x[1][2],
x[2][0], x[2][1], x[2][2],
x[3][0], x[3][1], x[3][2]);
x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(),
x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat(),
x[2][0].toFloat(), x[2][1].toFloat(), x[2][2].toFloat(),
x[3][0].toFloat(), x[3][1].toFloat(), x[3][2].toFloat());
}
template <>
GLM_FUNC_QUALIFIER std::string to_string
(
detail::tmat4x4<detail::thalf> const & m
detail::tmat4x4<detail::thalf> const & x
)
{
detail::tmat4x4<float> x(m);
return detail::format("hmat4x4((%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f))",
x[0][0], x[0][1], x[0][2], x[0][3],
x[1][0], x[1][1], x[1][2], x[1][3],
x[2][0], x[2][1], x[2][2], x[2][3],
x[3][0], x[3][1], x[3][2], x[3][3]);
x[0][0].toFloat(), x[0][1].toFloat(), x[0][2].toFloat(), x[0][3].toFloat(),
x[1][0].toFloat(), x[1][1].toFloat(), x[1][2].toFloat(), x[1][3].toFloat(),
x[2][0].toFloat(), x[2][1].toFloat(), x[2][2].toFloat(), x[2][3].toFloat(),
x[3][0].toFloat(), x[3][1].toFloat(), x[3][2].toFloat(), x[3][3].toFloat());
}
////////////////////////////////

View file

@ -16,12 +16,17 @@
int test_packUnorm2x16()
{
int Error = 0;
/*
std::vector<glm::hvec2> A;
A.push_back(glm::hvec2(glm::half( 1.0f), glm::half( 0.0f)));
A.push_back(glm::hvec2(glm::half( 0.5f), glm::half( 0.7f)));
A.push_back(glm::hvec2(glm::half( 0.1f), glm::half( 0.2f)));
*/
std::vector<glm::vec2> A;
A.push_back(glm::vec2(1.0f, 0.0f));
A.push_back(glm::vec2(0.5f, 0.7f));
A.push_back(glm::vec2(0.1f, 0.2f));
for(std::size_t i = 0; i < A.size(); ++i)
{
glm::vec2 B(A[i]);
@ -37,12 +42,17 @@ int test_packUnorm2x16()
int test_packSnorm2x16()
{
int Error = 0;
/*
std::vector<glm::hvec2> A;
A.push_back(glm::hvec2(glm::half( 1.0f), glm::half( 0.0f)));
A.push_back(glm::hvec2(glm::half(-0.5f), glm::half(-0.7f)));
A.push_back(glm::hvec2(glm::half(-0.1f), glm::half( 0.1f)));
*/
std::vector<glm::vec2> A;
A.push_back(glm::vec2( 1.0f, 0.0f));
A.push_back(glm::vec2(-0.5f,-0.7f));
A.push_back(glm::vec2(-0.1f, 0.1f));
for(std::size_t i = 0; i < A.size(); ++i)
{
glm::vec2 B(A[i]);
@ -98,12 +108,17 @@ int test_packSnorm4x8()
int test_packHalf2x16()
{
int Error = 0;
/*
std::vector<glm::hvec2> A;
A.push_back(glm::hvec2(glm::half( 1.0f), glm::half( 2.0f)));
A.push_back(glm::hvec2(glm::half(-1.0f), glm::half(-2.0f)));
A.push_back(glm::hvec2(glm::half(-1.1f), glm::half( 1.1f)));
*/
std::vector<glm::vec2> A;
A.push_back(glm::vec2( 1.0f, 2.0f));
A.push_back(glm::vec2(-1.0f,-2.0f));
A.push_back(glm::vec2(-1.1f, 1.1f));
for(std::size_t i = 0; i < A.size(); ++i)
{
glm::vec2 B(A[i]);

View file

@ -18,14 +18,14 @@ int main()
glm::half B(2.0f);
glm::half C = A + B;
glm::half D(C);
float E = D;
int F = float(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;
int I = float(H);
double J = H.toFloat();
int I = H.toFloat();
Result += J == 3.0 ? 0 : 1;
Result += I == 3 ? 0 : 1;

View file

@ -139,7 +139,10 @@ int test_half_ctor_vec2()
int Error = 0;
{
glm::hvec2 A(1, 2);
glm::hvec2 A;
A.x = glm::half(1);
A.y = glm::half(2);
//glm::hvec2 A(1, 2);
glm::hvec2 B(A);
glm::vec2 C(1, 2);
glm::hvec2 D(C);