glm/test/gtc/gtc_integer.cpp

284 lines
6.0 KiB
C++
Raw Normal View History

2010-12-17 01:33:17 +00:00
///////////////////////////////////////////////////////////////////////////////////////////////////
2014-01-04 21:32:28 +00:00
// OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
2010-12-17 01:33:17 +00:00
///////////////////////////////////////////////////////////////////////////////////////////////////
2014-10-26 18:22:19 +00:00
// Created : 2014-10-25
2014-10-26 16:43:54 +00:00
// Updated : 2014-10-25
2010-12-17 01:33:17 +00:00
// Licence : This source is under MIT licence
2014-10-26 18:22:19 +00:00
// File : test/gtc/integer.cpp
2010-12-17 01:33:17 +00:00
///////////////////////////////////////////////////////////////////////////////////////////////////
2014-10-26 18:22:19 +00:00
#include <glm/gtc/integer.hpp>
#include <glm/gtc/type_precision.hpp>
2014-10-26 19:34:10 +00:00
#include <glm/gtc/vec1.hpp>
#include <ctime>
#include <vector>
2014-10-26 16:43:54 +00:00
namespace isPowerOfTwo
{
template <typename genType>
struct type
{
genType Value;
bool Return;
};
2014-10-26 19:34:10 +00:00
int test_int16()
2014-10-26 16:43:54 +00:00
{
2014-10-26 19:34:10 +00:00
type<glm::int16> const Data[] =
{
{0x0001, true},
{0x0002, true},
{0x0004, true},
{0x0080, true},
{0x0000, true},
{0x0003, false}
};
int Error(0);
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::int16>); i < n; ++i)
{
bool Result = glm::isPowerOfTwo(Data[i].Value);
Error += Data[i].Return == Result ? 0 : 1;
}
return Error;
}
int test_uint16()
{
type<glm::uint16> const Data[] =
{
{0x0001, true},
{0x0002, true},
{0x0004, true},
{0x0000, true},
{0x0000, true},
{0x0003, false}
};
int Error(0);
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint16>); i < n; ++i)
{
bool Result = glm::isPowerOfTwo(Data[i].Value);
Error += Data[i].Return == Result ? 0 : 1;
}
return Error;
}
int test_int32()
{
type<int> const Data[] =
2014-10-26 18:22:19 +00:00
{
{0x00000001, true},
{0x00000002, true},
{0x00000004, true},
2014-10-27 12:44:30 +00:00
{0x0000000f, false},
2014-10-26 18:22:19 +00:00
{0x00000000, true},
{0x00000003, false}
};
2014-10-26 16:43:54 +00:00
int Error(0);
2014-10-26 19:34:10 +00:00
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
{
bool Result = glm::isPowerOfTwo(Data[i].Value);
Error += Data[i].Return == Result ? 0 : 1;
}
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
{
glm::bvec1 Result = glm::isPowerOfTwo(glm::ivec1(Data[i].Value));
Error += glm::all(glm::equal(glm::bvec1(Data[i].Return), Result)) ? 0 : 1;
}
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
{
glm::bvec2 Result = glm::isPowerOfTwo(glm::ivec2(Data[i].Value));
Error += glm::all(glm::equal(glm::bvec2(Data[i].Return), Result)) ? 0 : 1;
}
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
{
glm::bvec3 Result = glm::isPowerOfTwo(glm::ivec3(Data[i].Value));
Error += glm::all(glm::equal(glm::bvec3(Data[i].Return), Result)) ? 0 : 1;
}
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
2014-10-26 16:43:54 +00:00
{
2014-10-26 19:34:10 +00:00
glm::bvec4 Result = glm::isPowerOfTwo(glm::ivec4(Data[i].Value));
Error += glm::all(glm::equal(glm::bvec4(Data[i].Return), Result)) ? 0 : 1;
2014-10-26 16:43:54 +00:00
}
return Error;
}
2014-10-26 18:22:19 +00:00
2014-10-26 19:34:10 +00:00
int test_uint32()
2014-10-26 18:22:19 +00:00
{
2014-10-26 19:34:10 +00:00
type<glm::uint> const Data[] =
2014-10-26 18:22:19 +00:00
{
{0x00000001, true},
{0x00000002, true},
{0x00000004, true},
{0x80000000, true},
{0x00000000, true},
{0x00000003, false}
};
int Error(0);
2014-10-26 19:34:10 +00:00
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint>); i < n; ++i)
2014-10-26 18:22:19 +00:00
{
2014-10-26 19:34:10 +00:00
bool Result = glm::isPowerOfTwo(Data[i].Value);
Error += Data[i].Return == Result ? 0 : 1;
2014-10-26 18:22:19 +00:00
}
return Error;
}
int test()
{
int Error(0);
2014-10-26 19:34:10 +00:00
Error += test_int16();
Error += test_uint16();
Error += test_int32();
Error += test_uint32();
2014-10-26 18:22:19 +00:00
return Error;
}
2014-10-26 16:43:54 +00:00
}//isPowerOfTwo
2010-12-17 01:33:17 +00:00
namespace ceilPowerOfTwo
{
template <typename genIUType>
GLM_FUNC_QUALIFIER genIUType highestBitValue(genIUType Value)
{
genIUType tmp = Value;
genIUType result = genIUType(0);
while(tmp)
{
result = (tmp & (~tmp + 1)); // grab lowest bit
tmp &= ~result; // clear lowest bit
}
return result;
}
template <typename genType>
GLM_FUNC_QUALIFIER genType ceilPowerOfTwo_loop(genType value)
{
return glm::isPowerOfTwo(value) ? value : highestBitValue(value) << 1;
}
template <typename genType>
struct type
{
genType Value;
genType Return;
};
int test_int32()
{
type<glm::int32> const Data[] =
{
{0x0000ffff, 0x00010000},
{-3, -4},
{-8, -8},
{0x00000001, 0x00000001},
{0x00000002, 0x00000002},
{0x00000004, 0x00000004},
{0x00000007, 0x00000008},
{0x0000fff0, 0x00010000},
{0x0000f000, 0x00010000},
{0x08000000, 0x08000000},
{0x00000000, 0x00000000},
{0x00000003, 0x00000004}
};
int Error(0);
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::int32>); i < n; ++i)
{
glm::int32 Result = glm::ceilPowerOfTwo(Data[i].Value);
Error += Data[i].Return == Result ? 0 : 1;
}
return Error;
}
int test_uint32()
{
type<glm::uint32> const Data[] =
{
{0x00000001, 0x00000001},
{0x00000002, 0x00000002},
{0x00000004, 0x00000004},
{0x00000007, 0x00000008},
{0x0000ffff, 0x00010000},
{0x0000fff0, 0x00010000},
{0x0000f000, 0x00010000},
{0x80000000, 0x80000000},
{0x00000000, 0x00000000},
{0x00000003, 0x00000004}
};
int Error(0);
for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint32>); i < n; ++i)
{
glm::uint32 Result = glm::ceilPowerOfTwo(Data[i].Value);
Error += Data[i].Return == Result ? 0 : 1;
}
return Error;
}
int perf()
{
int Error(0);
std::vector<glm::uint> v;
v.resize(100000000);
std::clock_t Timestramp0 = std::clock();
for(glm::uint32 i = 0, n = static_cast<glm::uint>(v.size()); i < n; ++i)
v[i] = ceilPowerOfTwo_loop(i);
std::clock_t Timestramp1 = std::clock();
for(glm::uint32 i = 0, n = static_cast<glm::uint>(v.size()); i < n; ++i)
v[i] = glm::ceilPowerOfTwo(i);
std::clock_t Timestramp2 = std::clock();
printf("ceilPowerOfTwo_loop: %d clocks\n", Timestramp1 - Timestramp0);
printf("glm::ceilPowerOfTwo: %d clocks\n", Timestramp2 - Timestramp1);
return Error;
}
int test()
{
int Error(0);
Error += test_int32();
Error += test_uint32();
return Error;
}
}//namespace ceilPowerOfTwo
2011-01-24 15:44:14 +00:00
int main()
2010-12-17 01:33:17 +00:00
{
int Error(0);
2014-10-26 16:43:54 +00:00
Error += isPowerOfTwo::test();
Error += ceilPowerOfTwo::test();
Error += ceilPowerOfTwo::perf();
2014-10-26 16:43:54 +00:00
return Error;
2010-12-17 01:33:17 +00:00
}