| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286 |
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // Created : 2011-05-03
- // Updated : 2011-05-03
- // Licence : This source is under MIT licence
- // File : test/core/func_integer.cpp
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- #include <glm/integer.hpp>
- #include <glm/gtc/vec1.hpp>
- #include <vector>
- #include <ctime>
- #include <cstdio>
- enum result
- {
- SUCCESS,
- FAIL,
- ASSERT,
- STATIC_ASSERT
- };
- namespace bitfieldInsert
- {
- template <typename genType, typename sizeType>
- struct type
- {
- genType Base;
- genType Insert;
- sizeType Offset;
- sizeType Bits;
- genType Return;
- };
- typedef type<glm::uint, glm::uint> typeU32;
- typeU32 const Data32[] =
- {
- {0xff000000, 0x0000ff00, 8, 8, 0xff00ff00},
- {0xffff0000, 0x0000ffff, 16, 16, 0x00000000},
- {0x0000ffff, 0xffff0000, 16, 16, 0xffffffff},
- {0x00000000, 0xffffffff, 0, 32, 0xffffffff},
- {0x00000000, 0xffffffff, 0, 0, 0x00000000}
- };
- int test()
- {
- int Error = 0;
- glm::uint count = sizeof(Data32) / sizeof(typeU32);
-
- for(glm::uint i = 0; i < count; ++i)
- {
- glm::uint Return = glm::bitfieldInsert(
- Data32[i].Base,
- Data32[i].Insert,
- Data32[i].Offset,
- Data32[i].Bits);
- Error += Data32[i].Return == Return ? 0 : 1;
- }
-
- return Error;
- }
- }//bitfieldInsert
- namespace bitfieldExtract
- {
- template <typename genType, typename sizeType>
- struct type
- {
- genType Value;
- sizeType Offset;
- sizeType Bits;
- genType Return;
- result Result;
- };
- typedef type<glm::uint, glm::uint> typeU32;
- typeU32 const Data32[] =
- {
- {0xffffffff, 0,32, 0xffffffff, SUCCESS},
- {0xffffffff, 8, 0, 0x00000000, SUCCESS},
- {0x00000000, 0,32, 0x00000000, SUCCESS},
- {0x0f0f0f0f, 0,32, 0x0f0f0f0f, SUCCESS},
- {0x00000000, 8, 0, 0x00000000, SUCCESS},
- {0x80000000,31, 1, 0x00000001, SUCCESS},
- {0x7fffffff,31, 1, 0x00000000, SUCCESS},
- {0x00000300, 8, 8, 0x00000003, SUCCESS},
- {0x0000ff00, 8, 8, 0x000000ff, SUCCESS},
- {0xfffffff0, 0, 5, 0x00000010, SUCCESS},
- {0x000000ff, 1, 3, 0x00000007, SUCCESS},
- {0x000000ff, 0, 3, 0x00000007, SUCCESS},
- {0x00000000, 0, 2, 0x00000000, SUCCESS},
- {0xffffffff, 0, 8, 0x000000ff, SUCCESS},
- {0xffff0000,16,16, 0x0000ffff, SUCCESS},
- {0xfffffff0, 0, 8, 0x00000000, FAIL},
- {0xffffffff,16,16, 0x00000000, FAIL},
- //{0xffffffff,32, 1, 0x00000000, ASSERT}, // Throw an assert
- //{0xffffffff, 0,33, 0x00000000, ASSERT}, // Throw an assert
- //{0xffffffff,16,16, 0x00000000, ASSERT}, // Throw an assert
- };
- int test()
- {
- int Error = 0;
- glm::uint count = sizeof(Data32) / sizeof(typeU32);
- for(glm::uint i = 0; i < count; ++i)
- {
- glm::uint Return = glm::bitfieldExtract(
- Data32[i].Value,
- Data32[i].Offset,
- Data32[i].Bits);
-
- bool Compare = Data32[i].Return == Return;
- if(Data32[i].Result == SUCCESS && Compare)
- continue;
- else if(Data32[i].Result == FAIL && !Compare)
- continue;
- Error += 1;
- }
- return Error;
- }
- }//extractField
- namespace bitfieldReverse
- {
- /*
- GLM_FUNC_QUALIFIER unsigned int bitfieldReverseLoop(unsigned int v)
- {
- unsigned int Result(0);
- unsigned int const BitSize = static_cast<unsigned int>(sizeof(unsigned int) * 8);
- for(unsigned int i = 0; i < BitSize; ++i)
- {
- unsigned int const BitSet(v & (static_cast<unsigned int>(1) << i));
- unsigned int const BitFirst(BitSet >> i);
- Result |= BitFirst << (BitSize - 1 - i);
- }
- return Result;
- }
- GLM_FUNC_QUALIFIER glm::uint64_t bitfieldReverseLoop(glm::uint64_t v)
- {
- glm::uint64_t Result(0);
- glm::uint64_t const BitSize = static_cast<glm::uint64_t>(sizeof(unsigned int) * 8);
- for(glm::uint64_t i = 0; i < BitSize; ++i)
- {
- glm::uint64_t const BitSet(v & (static_cast<glm::uint64_t>(1) << i));
- glm::uint64_t const BitFirst(BitSet >> i);
- Result |= BitFirst << (BitSize - 1 - i);
- }
- return Result;
- }
- */
- template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
- GLM_FUNC_QUALIFIER vecType<T, P> bitfieldReverseLoop(vecType<T, P> const & v)
- {
- GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldReverse' only accept integer values");
- vecType<T, P> Result(0);
- T const BitSize = static_cast<T>(sizeof(T) * 8);
- for(T i = 0; i < BitSize; ++i)
- {
- vecType<T, P> const BitSet(v & (static_cast<T>(1) << i));
- vecType<T, P> const BitFirst(BitSet >> i);
- Result |= BitFirst << (BitSize - 1 - i);
- }
- return Result;
- }
- template <typename T>
- GLM_FUNC_QUALIFIER T bitfieldReverseLoop(T v)
- {
- return bitfieldReverseLoop(glm::tvec1<T>(v)).x;
- }
- GLM_FUNC_QUALIFIER uint32_t bitfieldReverseUint32(uint32_t x)
- {
- x = (x & 0x55555555) << 1 | (x & 0xAAAAAAAA) >> 1;
- x = (x & 0x33333333) << 2 | (x & 0xCCCCCCCC) >> 2;
- x = (x & 0x0F0F0F0F) << 4 | (x & 0xF0F0F0F0) >> 4;
- x = (x & 0x00FF00FF) << 8 | (x & 0xFF00FF00) >> 8;
- x = (x & 0x0000FFFF) << 16 | (x & 0xFFFF0000) >> 16;
- return x;
- }
- GLM_FUNC_QUALIFIER uint64_t bitfieldReverseUint64(uint64_t x)
- {
- x = (x & 0x5555555555555555) << 1 | (x & 0xAAAAAAAAAAAAAAAA) >> 1;
- x = (x & 0x3333333333333333) << 2 | (x & 0xCCCCCCCCCCCCCCCC) >> 2;
- x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x & 0xF0F0F0F0F0F0F0F0) >> 4;
- x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
- x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
- x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
- return x;
- }
- template <bool EXEC = false>
- struct compute_bitfieldReverseStep
- {
- template <typename T, glm::precision P, template <class, glm::precision> class vecType>
- GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & v, T, T)
- {
- return v;
- }
- };
- template <>
- struct compute_bitfieldReverseStep<true>
- {
- template <typename T, glm::precision P, template <class, glm::precision> class vecType>
- GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & v, T Mask, T Shift)
- {
- return (v & Mask) << Shift | (v & (~Mask)) >> Shift;
- }
- };
- template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
- GLM_FUNC_QUALIFIER vecType<T, P> bitfieldReverseOps(vecType<T, P> const & v)
- {
- vecType<T, P> x(v);
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 2>::call<T, P, vecType>(x, T(0x5555555555555555ull), static_cast<T>( 1));
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 4>::call<T, P, vecType>(x, T(0x3333333333333333ull), static_cast<T>( 2));
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 8>::call<T, P, vecType>(x, T(0x0F0F0F0F0F0F0F0Full), static_cast<T>( 4));
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 16>::call<T, P, vecType>(x, T(0x00FF00FF00FF00FFull), static_cast<T>( 8));
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 32>::call<T, P, vecType>(x, T(0x0000FFFF0000FFFFull), static_cast<T>(16));
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 64>::call<T, P, vecType>(x, T(0x00000000FFFFFFFFull), static_cast<T>(32));
- return x;
- }
- template <typename genType>
- GLM_FUNC_QUALIFIER genType bitfieldReverseOps(genType x)
- {
- return bitfieldReverseOps(glm::tvec1<genType, glm::defaultp>(x)).x;
- }
- template <typename genType>
- struct type
- {
- genType Value;
- genType Return;
- result Result;
- };
- typedef type<glm::uint> typeU32;
- typeU32 const Data32[] =
- {
- {0x00000001, 0x80000000, SUCCESS},
- {0x0000000f, 0xf0000000, SUCCESS},
- {0x000000ff, 0xff000000, SUCCESS},
- {0xf0000000, 0x0000000f, SUCCESS},
- {0xff000000, 0x000000ff, SUCCESS},
- {0xffffffff, 0xffffffff, SUCCESS},
- {0x00000000, 0x00000000, SUCCESS}
- };
- typedef type<glm::uint64> typeU64;
- #if(((GLM_COMPILER & GLM_COMPILER_GCC) == GLM_COMPILER_GCC) && (GLM_COMPILER < GLM_COMPILER_GCC44))
- typeU64 const Data64[] =
- {
- {0xf000000000000000LLU, 0x000000000000000fLLU, SUCCESS},
- {0xffffffffffffffffLLU, 0xffffffffffffffffLLU, SUCCESS},
- {0x0000000000000000LLU, 0x0000000000000000LLU, SUCCESS}
- };
- #else
- typeU64 const Data64[] =
- {
- {0x00000000000000ff, 0xff00000000000000, SUCCESS},
- {0x000000000000000f, 0xf000000000000000, SUCCESS},
- {0xf000000000000000, 0x000000000000000f, SUCCESS},
- {0xffffffffffffffff, 0xffffffffffffffff, SUCCESS},
- {0x0000000000000000, 0x0000000000000000, SUCCESS}
- };
- #endif
- int test32_bitfieldReverse()
- {
- int Error = 0;
- std::size_t const Count = sizeof(Data32) / sizeof(typeU32);
-
- for(std::size_t i = 0; i < Count; ++i)
- {
- glm::uint Return = glm::bitfieldReverse(Data32[i].Value);
-
- bool Compare = Data32[i].Return == Return;
-
- if(Data32[i].Result == SUCCESS)
- Error += Compare ? 0 : 1;
- else
- Error += Compare ? 1 : 0;
- }
-
- return Error;
- }
- int test32_bitfieldReverseLoop()
- {
- int Error = 0;
- std::size_t const Count = sizeof(Data32) / sizeof(typeU32);
-
- for(std::size_t i = 0; i < Count; ++i)
- {
- glm::uint Return = bitfieldReverseLoop(Data32[i].Value);
-
- bool Compare = Data32[i].Return == Return;
-
- if(Data32[i].Result == SUCCESS)
- Error += Compare ? 0 : 1;
- else
- Error += Compare ? 1 : 0;
- }
-
- return Error;
- }
- int test32_bitfieldReverseUint32()
- {
- int Error = 0;
- std::size_t const Count = sizeof(Data32) / sizeof(typeU32);
-
- for(std::size_t i = 0; i < Count; ++i)
- {
- glm::uint Return = bitfieldReverseUint32(Data32[i].Value);
-
- bool Compare = Data32[i].Return == Return;
-
- if(Data32[i].Result == SUCCESS)
- Error += Compare ? 0 : 1;
- else
- Error += Compare ? 1 : 0;
- }
-
- return Error;
- }
- int test32_bitfieldReverseOps()
- {
- int Error = 0;
- std::size_t const Count = sizeof(Data32) / sizeof(typeU32);
-
- for(std::size_t i = 0; i < Count; ++i)
- {
- glm::uint Return = bitfieldReverseOps(Data32[i].Value);
-
- bool Compare = Data32[i].Return == Return;
-
- if(Data32[i].Result == SUCCESS)
- Error += Compare ? 0 : 1;
- else
- Error += Compare ? 1 : 0;
- }
-
- return Error;
- }
- int test64_bitfieldReverse()
- {
- int Error = 0;
- std::size_t const Count = sizeof(Data64) / sizeof(typeU64);
-
- for(std::size_t i = 0; i < Count; ++i)
- {
- glm::uint64 Return = glm::bitfieldReverse(Data64[i].Value);
-
- bool Compare = Data64[i].Return == Return;
-
- if(Data64[i].Result == SUCCESS)
- Error += Compare ? 0 : 1;
- else
- Error += Compare ? 1 : 0;
- }
-
- return Error;
- }
- int test64_bitfieldReverseLoop()
- {
- int Error = 0;
- std::size_t const Count = sizeof(Data64) / sizeof(typeU64);
-
- for(std::size_t i = 0; i < Count; ++i)
- {
- glm::uint64 Return = bitfieldReverseLoop(Data64[i].Value);
-
- bool Compare = Data64[i].Return == Return;
-
- if(Data32[i].Result == SUCCESS)
- Error += Compare ? 0 : 1;
- else
- Error += Compare ? 1 : 0;
- }
-
- return Error;
- }
- int test64_bitfieldReverseUint64()
- {
- int Error = 0;
- std::size_t const Count = sizeof(Data64) / sizeof(typeU64);
-
- for(std::size_t i = 0; i < Count; ++i)
- {
- glm::uint64 Return = bitfieldReverseUint64(Data64[i].Value);
-
- bool Compare = Data64[i].Return == Return;
-
- if(Data64[i].Result == SUCCESS)
- Error += Compare ? 0 : 1;
- else
- Error += Compare ? 1 : 0;
- }
-
- return Error;
- }
- int test64_bitfieldReverseOps()
- {
- int Error = 0;
- std::size_t const Count = sizeof(Data64) / sizeof(typeU64);
-
- for(std::size_t i = 0; i < Count; ++i)
- {
- glm::uint64 Return = bitfieldReverseOps(Data64[i].Value);
-
- bool Compare = Data64[i].Return == Return;
-
- if(Data64[i].Result == SUCCESS)
- Error += Compare ? 0 : 1;
- else
- Error += Compare ? 1 : 0;
- }
-
- return Error;
- }
- int test()
- {
- int Error = 0;
- Error += test32_bitfieldReverse();
- Error += test32_bitfieldReverseLoop();
- Error += test32_bitfieldReverseUint32();
- Error += test32_bitfieldReverseOps();
- Error += test64_bitfieldReverse();
- Error += test64_bitfieldReverseLoop();
- Error += test64_bitfieldReverseUint64();
- Error += test64_bitfieldReverseOps();
- return Error;
- }
- int perf32()
- {
- int Error = 0;
- glm::uint32 Count = 10000000;
- std::vector<glm::uint32> Data;
- Data.resize(static_cast<std::size_t>(Count));
- std::clock_t Timestamps0 = std::clock();
- for(glm::uint32 k = 0; k < Count; ++k)
- Data[k] = glm::bitfieldReverse(k);
- std::clock_t Timestamps1 = std::clock();
- for(glm::uint32 k = 0; k < Count; ++k)
- Data[k] = bitfieldReverseLoop(k);
- std::clock_t Timestamps2 = std::clock();
- for(glm::uint32 k = 0; k < Count; ++k)
- Data[k] = bitfieldReverseUint32(k);
- std::clock_t Timestamps3 = std::clock();
- for(glm::uint32 k = 0; k < Count; ++k)
- Data[k] = bitfieldReverseOps(k);
- std::clock_t Timestamps4 = std::clock();
- std::printf("glm::bitfieldReverse: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
- std::printf("bitfieldReverseLoop: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
- std::printf("bitfieldReverseUint32: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
- std::printf("bitfieldReverseOps: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
- return Error;
- }
- int perf64()
- {
- int Error = 0;
- glm::uint64 Count = 10000000;
- std::vector<glm::uint64> Data;
- Data.resize(static_cast<std::size_t>(Count));
- std::clock_t Timestamps0 = std::clock();
- for(glm::uint32 k = 0; k < Count; ++k)
- Data[k] = glm::bitfieldReverse(k);
- std::clock_t Timestamps1 = std::clock();
- for(glm::uint64 k = 0; k < Count; ++k)
- Data[k] = bitfieldReverseLoop(k);
- std::clock_t Timestamps2 = std::clock();
- for(glm::uint64 k = 0; k < Count; ++k)
- Data[k] = bitfieldReverseUint64(k);
- std::clock_t Timestamps3 = std::clock();
- for(glm::uint64 k = 0; k < Count; ++k)
- Data[k] = bitfieldReverseOps(k);
- std::clock_t Timestamps4 = std::clock();
- std::printf("glm::bitfieldReverse - 64: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
- std::printf("bitfieldReverseLoop - 64: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
- std::printf("bitfieldReverseUint - 64: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
- std::printf("bitfieldReverseOps - 64: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
- return Error;
- }
- int perf()
- {
- int Error = 0;
- Error += perf32();
- Error += perf64();
- return Error;
- }
- }//bitfieldReverse
- namespace findMSB
- {
- template <typename genType>
- struct type
- {
- genType Value;
- genType Return;
- };
- template <typename genIUType>
- GLM_FUNC_QUALIFIER int findMSB_095(genIUType Value)
- {
- GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
-
- if(Value == genIUType(0) || Value == genIUType(-1))
- return -1;
- else if(Value > 0)
- {
- genIUType Bit = genIUType(-1);
- for(genIUType tmp = Value; tmp > 0; tmp >>= 1, ++Bit){}
- return Bit;
- }
- else //if(Value < 0)
- {
- int const BitCount(sizeof(genIUType) * 8);
- int MostSignificantBit(-1);
- for(int BitIndex(0); BitIndex < BitCount; ++BitIndex)
- MostSignificantBit = (Value & (1 << BitIndex)) ? MostSignificantBit : BitIndex;
- assert(MostSignificantBit >= 0);
- return MostSignificantBit;
- }
- }
- template <typename genIUType>
- GLM_FUNC_QUALIFIER int findMSB_nlz1(genIUType x)
- {
- GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
- /*
- int Result = 0;
- for(std::size_t i = 0, n = sizeof(genIUType) * 8; i < n; ++i)
- Result = Value & static_cast<genIUType>(1 << i) ? static_cast<int>(i) : Result;
- return Result;
- */
- /*
- genIUType Bit = genIUType(-1);
- for(genIUType tmp = Value; tmp > 0; tmp >>= 1, ++Bit){}
- return Bit;
- */
- int n;
- if (x == 0) return(32);
- n = 0;
- if (x <= 0x0000FFFF) {n = n +16; x = x <<16;}
- if (x <= 0x00FFFFFF) {n = n + 8; x = x << 8;}
- if (x <= 0x0FFFFFFF) {n = n + 4; x = x << 4;}
- if (x <= 0x3FFFFFFF) {n = n + 2; x = x << 2;}
- if (x <= 0x7FFFFFFF) {n = n + 1;}
- return n;
- }
- int findMSB_nlz2(unsigned int x)
- {
- unsigned y;
- int n;
- n = 32;
- y = x >>16; if (y != 0) {n = n -16; x = y;}
- y = x >> 8; if (y != 0) {n = n - 8; x = y;}
- y = x >> 4; if (y != 0) {n = n - 4; x = y;}
- y = x >> 2; if (y != 0) {n = n - 2; x = y;}
- y = x >> 1; if (y != 0) return n - 2;
- return n - x;
- }
- int perf_950()
- {
- type<glm::uint> const Data[] =
- {
- //{0x00000000, -1},
- {0x00000001, 0},
- {0x00000002, 1},
- {0x00000003, 1},
- {0x00000004, 2},
- {0x00000005, 2},
- {0x00000007, 2},
- {0x00000008, 3},
- {0x00000010, 4},
- {0x00000020, 5},
- {0x00000040, 6},
- {0x00000080, 7},
- {0x00000100, 8},
- {0x00000200, 9},
- {0x00000400, 10},
- {0x00000800, 11},
- {0x00001000, 12},
- {0x00002000, 13},
- {0x00004000, 14},
- {0x00008000, 15},
- {0x00010000, 16},
- {0x00020000, 17},
- {0x00040000, 18},
- {0x00080000, 19},
- {0x00100000, 20},
- {0x00200000, 21},
- {0x00400000, 22},
- {0x00800000, 23},
- {0x01000000, 24},
- {0x02000000, 25},
- {0x04000000, 26},
- {0x08000000, 27},
- {0x10000000, 28},
- {0x20000000, 29},
- {0x40000000, 30}
- };
- int Error(0);
- std::clock_t Timestamps1 = std::clock();
- for(std::size_t k = 0; k < 10000000; ++k)
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
- {
- int Result = findMSB_095(Data[i].Value);
- Error += Data[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps2 = std::clock();
- std::printf("findMSB - 0.9.5: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
- return Error;
- }
- int perf_ops()
- {
- type<int> const Data[] =
- {
- {0x00000000, -1},
- {0x00000001, 0},
- {0x00000002, 1},
- {0x00000003, 1},
- {0x00000004, 2},
- {0x00000005, 2},
- {0x00000007, 2},
- {0x00000008, 3},
- {0x00000010, 4},
- {0x00000020, 5},
- {0x00000040, 6},
- {0x00000080, 7},
- {0x00000100, 8},
- {0x00000200, 9},
- {0x00000400, 10},
- {0x00000800, 11},
- {0x00001000, 12},
- {0x00002000, 13},
- {0x00004000, 14},
- {0x00008000, 15},
- {0x00010000, 16},
- {0x00020000, 17},
- {0x00040000, 18},
- {0x00080000, 19},
- {0x00100000, 20},
- {0x00200000, 21},
- {0x00400000, 22},
- {0x00800000, 23},
- {0x01000000, 24},
- {0x02000000, 25},
- {0x04000000, 26},
- {0x08000000, 27},
- {0x10000000, 28},
- {0x20000000, 29},
- {0x40000000, 30}
- };
- int Error(0);
- std::clock_t Timestamps1 = std::clock();
- for(std::size_t k = 0; k < 10000000; ++k)
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
- {
- int Result = findMSB_nlz1(Data[i].Value);
- Error += Data[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps2 = std::clock();
- std::printf("findMSB - nlz1: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
- return Error;
- }
- int test_findMSB()
- {
- type<glm::uint> const Data[] =
- {
- //{0x00000000, -1},
- {0x00000001, 0},
- {0x00000002, 1},
- {0x00000003, 1},
- {0x00000004, 2},
- {0x00000005, 2},
- {0x00000007, 2},
- {0x00000008, 3},
- {0x00000010, 4},
- {0x00000020, 5},
- {0x00000040, 6},
- {0x00000080, 7},
- {0x00000100, 8},
- {0x00000200, 9},
- {0x00000400, 10},
- {0x00000800, 11},
- {0x00001000, 12},
- {0x00002000, 13},
- {0x00004000, 14},
- {0x00008000, 15},
- {0x00010000, 16},
- {0x00020000, 17},
- {0x00040000, 18},
- {0x00080000, 19},
- {0x00100000, 20},
- {0x00200000, 21},
- {0x00400000, 22},
- {0x00800000, 23},
- {0x01000000, 24},
- {0x02000000, 25},
- {0x04000000, 26},
- {0x08000000, 27},
- {0x10000000, 28},
- {0x20000000, 29},
- {0x40000000, 30}
- };
- int Error(0);
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
- {
- int Result = glm::findMSB(Data[i].Value);
- Error += Data[i].Return == Result ? 0 : 1;
- assert(!Error);
- }
- return Error;
- }
- int test_nlz1()
- {
- type<glm::uint> const Data[] =
- {
- //{0x00000000, -1},
- {0x00000001, 0},
- {0x00000002, 1},
- {0x00000003, 1},
- {0x00000004, 2},
- {0x00000005, 2},
- {0x00000007, 2},
- {0x00000008, 3},
- {0x00000010, 4},
- {0x00000020, 5},
- {0x00000040, 6},
- {0x00000080, 7},
- {0x00000100, 8},
- {0x00000200, 9},
- {0x00000400, 10},
- {0x00000800, 11},
- {0x00001000, 12},
- {0x00002000, 13},
- {0x00004000, 14},
- {0x00008000, 15},
- {0x00010000, 16},
- {0x00020000, 17},
- {0x00040000, 18},
- {0x00080000, 19},
- {0x00100000, 20},
- {0x00200000, 21},
- {0x00400000, 22},
- {0x00800000, 23},
- {0x01000000, 24},
- {0x02000000, 25},
- {0x04000000, 26},
- {0x08000000, 27},
- {0x10000000, 28},
- {0x20000000, 29},
- {0x40000000, 30}
- };
- int Error(0);
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
- {
- int Result = findMSB_nlz2(Data[i].Value);
- Error += Data[i].Return == Result ? 0 : 1;
- }
- return Error;
- }
- int test()
- {
- int Error(0);
- Error += test_findMSB();
- //Error += test_nlz1();
- return Error;
- }
- int perf()
- {
- int Error(0);
- Error += perf_950();
- Error += perf_ops();
- return Error;
- }
- }//findMSB
- namespace findLSB
- {
- template <typename genType>
- struct type
- {
- genType Value;
- genType Return;
- };
- type<int> const DataI32[] =
- {
- {0x00000001, 0},
- {0x00000003, 0},
- {0x00000002, 1}
- };
- int test()
- {
- int Error(0);
- for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
- {
- int Result = glm::findLSB(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- assert(!Error);
- }
- return Error;
- }
- }//findLSB
- namespace uaddCarry
- {
- int test()
- {
- int Error(0);
-
- {
- glm::uint x = 16;
- glm::uint y = 17;
- glm::uint Carry = 0;
- glm::uint Result = glm::uaddCarry(x, y, Carry);
- Error += Carry == 1 ? 0 : 1;
- Error += Result == 33 ? 0 : 1;
- }
- {
- glm::uvec1 x(16);
- glm::uvec1 y(17);
- glm::uvec1 Carry(0);
- glm::uvec1 Result(glm::uaddCarry(x, y, Carry));
- Error += glm::all(glm::equal(Carry, glm::uvec1(1))) ? 0 : 1;
- Error += glm::all(glm::equal(Result, glm::uvec1(33))) ? 0 : 1;
- }
- {
- glm::uvec2 x(16);
- glm::uvec2 y(17);
- glm::uvec2 Carry(0);
- glm::uvec2 Result(glm::uaddCarry(x, y, Carry));
- Error += glm::all(glm::equal(Carry, glm::uvec2(1))) ? 0 : 1;
- Error += glm::all(glm::equal(Result, glm::uvec2(33))) ? 0 : 1;
- }
- {
- glm::uvec3 x(16);
- glm::uvec3 y(17);
- glm::uvec3 Carry(0);
- glm::uvec3 Result(glm::uaddCarry(x, y, Carry));
- Error += glm::all(glm::equal(Carry, glm::uvec3(1))) ? 0 : 1;
- Error += glm::all(glm::equal(Result, glm::uvec3(33))) ? 0 : 1;
- }
- {
- glm::uvec4 x(16);
- glm::uvec4 y(17);
- glm::uvec4 Carry(0);
- glm::uvec4 Result(glm::uaddCarry(x, y, Carry));
- Error += glm::all(glm::equal(Carry, glm::uvec4(1))) ? 0 : 1;
- Error += glm::all(glm::equal(Result, glm::uvec4(33))) ? 0 : 1;
- }
- return Error;
- }
- }//namespace uaddCarry
- namespace usubBorrow
- {
- int test()
- {
- int Error(0);
-
- {
- glm::uint x = 16;
- glm::uint y = 17;
- glm::uint Borrow = 0;
- glm::uint Result = glm::usubBorrow(x, y, Borrow);
- Error += Borrow == 1 ? 0 : 1;
- Error += Result == 1 ? 0 : 1;
- }
- {
- glm::uvec1 x(16);
- glm::uvec1 y(17);
- glm::uvec1 Borrow(0);
- glm::uvec1 Result(glm::usubBorrow(x, y, Borrow));
- Error += glm::all(glm::equal(Borrow, glm::uvec1(1))) ? 0 : 1;
- Error += glm::all(glm::equal(Result, glm::uvec1(1))) ? 0 : 1;
- }
- {
- glm::uvec2 x(16);
- glm::uvec2 y(17);
- glm::uvec2 Borrow(0);
- glm::uvec2 Result(glm::usubBorrow(x, y, Borrow));
- Error += glm::all(glm::equal(Borrow, glm::uvec2(1))) ? 0 : 1;
- Error += glm::all(glm::equal(Result, glm::uvec2(1))) ? 0 : 1;
- }
- {
- glm::uvec3 x(16);
- glm::uvec3 y(17);
- glm::uvec3 Borrow(0);
- glm::uvec3 Result(glm::usubBorrow(x, y, Borrow));
- Error += glm::all(glm::equal(Borrow, glm::uvec3(1))) ? 0 : 1;
- Error += glm::all(glm::equal(Result, glm::uvec3(1))) ? 0 : 1;
- }
- {
- glm::uvec4 x(16);
- glm::uvec4 y(17);
- glm::uvec4 Borrow(0);
- glm::uvec4 Result(glm::usubBorrow(x, y, Borrow));
- Error += glm::all(glm::equal(Borrow, glm::uvec4(1))) ? 0 : 1;
- Error += glm::all(glm::equal(Result, glm::uvec4(1))) ? 0 : 1;
- }
- return Error;
- }
- }//namespace usubBorrow
- namespace umulExtended
- {
- int test()
- {
- int Error(0);
-
- {
- glm::uint x = 2;
- glm::uint y = 3;
- glm::uint msb = 0;
- glm::uint lsb = 0;
- glm::umulExtended(x, y, msb, lsb);
- Error += msb == 0 ? 0 : 1;
- Error += lsb == 6 ? 0 : 1;
- }
- {
- glm::uvec1 x(2);
- glm::uvec1 y(3);
- glm::uvec1 msb(0);
- glm::uvec1 lsb(0);
- glm::umulExtended(x, y, msb, lsb);
- Error += glm::all(glm::equal(msb, glm::uvec1(0))) ? 0 : 1;
- Error += glm::all(glm::equal(lsb, glm::uvec1(6))) ? 0 : 1;
- }
- {
- glm::uvec2 x(2);
- glm::uvec2 y(3);
- glm::uvec2 msb(0);
- glm::uvec2 lsb(0);
- glm::umulExtended(x, y, msb, lsb);
- Error += glm::all(glm::equal(msb, glm::uvec2(0))) ? 0 : 1;
- Error += glm::all(glm::equal(lsb, glm::uvec2(6))) ? 0 : 1;
- }
- {
- glm::uvec3 x(2);
- glm::uvec3 y(3);
- glm::uvec3 msb(0);
- glm::uvec3 lsb(0);
- glm::umulExtended(x, y, msb, lsb);
- Error += glm::all(glm::equal(msb, glm::uvec3(0))) ? 0 : 1;
- Error += glm::all(glm::equal(lsb, glm::uvec3(6))) ? 0 : 1;
- }
- {
- glm::uvec4 x(2);
- glm::uvec4 y(3);
- glm::uvec4 msb(0);
- glm::uvec4 lsb(0);
- glm::umulExtended(x, y, msb, lsb);
- Error += glm::all(glm::equal(msb, glm::uvec4(0))) ? 0 : 1;
- Error += glm::all(glm::equal(lsb, glm::uvec4(6))) ? 0 : 1;
- }
- return Error;
- }
- }//namespace umulExtended
- namespace imulExtended
- {
- int test()
- {
- int Error(0);
-
- {
- int x = 2;
- int y = 3;
- int msb = 0;
- int lsb = 0;
- glm::imulExtended(x, y, msb, lsb);
- Error += msb == 0 ? 0 : 1;
- Error += lsb == 6 ? 0 : 1;
- }
- {
- glm::ivec1 x(2);
- glm::ivec1 y(3);
- glm::ivec1 msb(0);
- glm::ivec1 lsb(0);
- glm::imulExtended(x, y, msb, lsb);
- Error += glm::all(glm::equal(msb, glm::ivec1(0))) ? 0 : 1;
- Error += glm::all(glm::equal(lsb, glm::ivec1(6))) ? 0 : 1;
- }
- {
- glm::ivec2 x(2);
- glm::ivec2 y(3);
- glm::ivec2 msb(0);
- glm::ivec2 lsb(0);
- glm::imulExtended(x, y, msb, lsb);
- Error += glm::all(glm::equal(msb, glm::ivec2(0))) ? 0 : 1;
- Error += glm::all(glm::equal(lsb, glm::ivec2(6))) ? 0 : 1;
- }
- {
- glm::ivec3 x(2);
- glm::ivec3 y(3);
- glm::ivec3 msb(0);
- glm::ivec3 lsb(0);
- glm::imulExtended(x, y, msb, lsb);
- Error += glm::all(glm::equal(msb, glm::ivec3(0))) ? 0 : 1;
- Error += glm::all(glm::equal(lsb, glm::ivec3(6))) ? 0 : 1;
- }
- {
- glm::ivec4 x(2);
- glm::ivec4 y(3);
- glm::ivec4 msb(0);
- glm::ivec4 lsb(0);
- glm::imulExtended(x, y, msb, lsb);
- Error += glm::all(glm::equal(msb, glm::ivec4(0))) ? 0 : 1;
- Error += glm::all(glm::equal(lsb, glm::ivec4(6))) ? 0 : 1;
- }
- return Error;
- }
- }//namespace imulExtended
- namespace bitCount
- {
- template <typename genType>
- struct type
- {
- genType Value;
- genType Return;
- };
- type<int> const DataI32[] =
- {
- {0x00000001, 1},
- {0x00000003, 2},
- {0x00000002, 1},
- {0x7fffffff, 31},
- {0x00000000, 0}
- };
- template <typename T>
- inline int bitCount_if(T v)
- {
- GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitCount' only accept integer values");
- int Count(0);
- for(T i = 0, n = static_cast<T>(sizeof(T) * 8); i < n; ++i)
- {
- if(v & static_cast<T>(1 << i))
- ++Count;
- }
- return Count;
- }
- template <typename T>
- inline int bitCount_vec(T v)
- {
- GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitCount' only accept integer values");
- int Count(0);
- for(T i = 0, n = static_cast<T>(sizeof(T) * 8); i < n; ++i)
- {
- Count += static_cast<int>((v >> i) & static_cast<T>(1));
- }
- return Count;
- }
- int perf()
- {
- int Error(0);
- std::size_t Size = 10000000;
- std::vector<int> v;
- v.resize(Size);
- std::vector<glm::ivec4> w;
- w.resize(Size);
- std::clock_t TimestampsA = std::clock();
- // bitCount - TimeIf
- {
- for(std::size_t i = 0, n = v.size(); i < n; ++i)
- v[i] = bitCount_if(i);
- }
- std::clock_t TimestampsB = std::clock();
- // bitCount - TimeVec
- {
- for(std::size_t i = 0, n = v.size(); i < n; ++i)
- v[i] = bitCount_vec(i);
- }
- std::clock_t TimestampsC = std::clock();
- // bitCount - TimeDefault
- {
- for(std::size_t i = 0, n = v.size(); i < n; ++i)
- v[i] = glm::bitCount(i);
- }
- std::clock_t TimestampsD = std::clock();
- // bitCount - TimeVec4
- {
- for(std::size_t i = 0, n = v.size(); i < n; ++i)
- w[i] = glm::bitCount(glm::ivec4(static_cast<int>(i)));
- }
- std::clock_t TimestampsE = std::clock();
- std::clock_t TimeIf = TimestampsB - TimestampsA;
- std::clock_t TimeVec = TimestampsC - TimestampsB;
- std::clock_t TimeDefault = TimestampsD - TimestampsC;
- std::clock_t TimeVec4 = TimestampsE - TimestampsD;
- std::printf("bitCount - TimeIf %d\n", static_cast<unsigned int>(TimeIf));
- std::printf("bitCount - TimeVec %d\n", static_cast<unsigned int>(TimeVec));
- std::printf("bitCount - TimeDefault %d\n", static_cast<unsigned int>(TimeDefault));
- std::printf("bitCount - TimeVec4 %d\n", static_cast<unsigned int>(TimeVec4));
- return Error;
- }
- int test()
- {
- int Error(0);
- for(std::size_t i = 0, n = sizeof(DataI32) / sizeof(type<int>); i < n; ++i)
- {
- int Result = glm::bitCount(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- assert(!Error);
- }
- return Error;
- }
- }//bitCount
- int main()
- {
- int Error = 0;
- Error += ::bitfieldReverse::test();
- Error += ::bitfieldReverse::perf();
- Error += ::findMSB::test();
- Error += ::findMSB::perf();
- Error += ::findLSB::test();
- Error += ::umulExtended::test();
- Error += ::imulExtended::test();
- Error += ::uaddCarry::test();
- Error += ::usubBorrow::test();
- Error += ::bitfieldInsert::test();
- Error += ::bitfieldExtract::test();
- Error += ::bitCount::test();
- Error += ::bitCount::perf();
- return Error;
- }
|