| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547 |
- #include <glm/integer.hpp>
- #include <glm/vector_relational.hpp>
- #include <glm/gtc/vec1.hpp>
- #include <vector>
- #include <ctime>
- #include <cstdio>
- enum result
- {
- SUCCESS,
- FAIL,
- ASSERT,
- STATIC_ASSERT
- };
- namespace bitfieldInsert
- {
- template<typename genType>
- struct type
- {
- genType Base;
- genType Insert;
- int Offset;
- int Bits;
- genType Return;
- };
- typedef type<glm::uint> typeU32;
- typeU32 const Data32[] =
- {
- {0x00000000, 0xffffffff, 0, 32, 0xffffffff},
- {0x00000000, 0xffffffff, 0, 31, 0x7fffffff},
- {0x00000000, 0xffffffff, 0, 0, 0x00000000},
- {0xff000000, 0x0000ff00, 8, 8, 0xff00ff00},
- {0xffff0000, 0x0000ffff, 16, 16, 0x00000000},
- {0x0000ffff, 0xffff0000, 16, 16, 0xffffffff}
- };
- static 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>
- struct type
- {
- genType Value;
- int Offset;
- int Bits;
- genType Return;
- result Result;
- };
- typedef type<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
- };
- static 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<glm::length_t L, typename T, glm::qualifier Q>
- GLM_FUNC_QUALIFIER glm::vec<L, T, Q> bitfieldReverseLoop(glm::vec<L, T, Q> const& v)
- {
- GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldReverse' only accept integer values");
- glm::vec<L, T, Q> Result(0);
- T const BitSize = static_cast<T>(sizeof(T) * 8);
- for(T i = 0; i < BitSize; ++i)
- {
- glm::vec<L, T, Q> const BitSet(v & (static_cast<T>(1) << i));
- glm::vec<L, T, Q> const BitFirst(BitSet >> i);
- Result |= BitFirst << (BitSize - 1 - i);
- }
- return Result;
- }
- template<typename T>
- GLM_FUNC_QUALIFIER T bitfieldReverseLoop(T v)
- {
- return bitfieldReverseLoop(glm::vec<1, T>(v)).x;
- }
- GLM_FUNC_QUALIFIER glm::uint32_t bitfieldReverseUint32(glm::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 glm::uint64_t bitfieldReverseUint64(glm::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<glm::length_t L, typename T, glm::qualifier Q>
- GLM_FUNC_QUALIFIER static glm::vec<L, T, Q> call(glm::vec<L, T, Q> const& v, T, T)
- {
- return v;
- }
- };
- template<>
- struct compute_bitfieldReverseStep<true>
- {
- template<glm::length_t L, typename T, glm::qualifier Q>
- GLM_FUNC_QUALIFIER static glm::vec<L, T, Q> call(glm::vec<L, T, Q> const& v, T Mask, T Shift)
- {
- return (v & Mask) << Shift | (v & (~Mask)) >> Shift;
- }
- };
- template<glm::length_t L, typename T, glm::qualifier Q>
- GLM_FUNC_QUALIFIER glm::vec<L, T, Q> bitfieldReverseOps(glm::vec<L, T, Q> const& v)
- {
- glm::vec<L, T, Q> x(v);
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 2>::call(x, static_cast<T>(0x5555555555555555ull), static_cast<T>( 1));
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 4>::call(x, static_cast<T>(0x3333333333333333ull), static_cast<T>( 2));
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 8>::call(x, static_cast<T>(0x0F0F0F0F0F0F0F0Full), static_cast<T>( 4));
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 16>::call(x, static_cast<T>(0x00FF00FF00FF00FFull), static_cast<T>( 8));
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 32>::call(x, static_cast<T>(0x0000FFFF0000FFFFull), static_cast<T>(16));
- x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 64>::call(x, static_cast<T>(0x00000000FFFFFFFFull), static_cast<T>(32));
- return x;
- }
- template<typename genType>
- GLM_FUNC_QUALIFIER genType bitfieldReverseOps(genType x)
- {
- return bitfieldReverseOps(glm::vec<1, 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;
- typeU64 const Data64[] =
- {
- {0x00000000000000ff, 0xff00000000000000, SUCCESS},
- {0x000000000000000f, 0xf000000000000000, SUCCESS},
- {0xf000000000000000, 0x000000000000000f, SUCCESS},
- {0xffffffffffffffff, 0xffffffffffffffff, SUCCESS},
- {0x0000000000000000, 0x0000000000000000, SUCCESS}
- };
- static 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;
- }
- static 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;
- }
- static 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;
- }
- static 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;
- }
- static 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;
- }
- static 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;
- }
- static 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;
- }
- static 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;
- }
- static 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;
- }
- static int perf32(glm::uint32 Count)
- {
- int Error = 0;
- 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;
- }
- static int perf64(glm::uint64 Count)
- {
- int Error = 0;
- 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;
- }
- static int perf(std::size_t Samples)
- {
- int Error = 0;
- Error += perf32(static_cast<glm::uint32>(Samples));
- Error += perf64(static_cast<glm::uint64>(Samples));
- return Error;
- }
- }//bitfieldReverse
- namespace findMSB
- {
- template<typename genType, typename retType>
- struct type
- {
- genType Value;
- retType Return;
- };
- # if GLM_HAS_BITSCAN_WINDOWS
- template<typename genIUType>
- static int findMSB_intrinsic(genIUType Value)
- {
- GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
- if(Value == 0)
- return -1;
- unsigned long Result(0);
- _BitScanReverse(&Result, Value);
- return int(Result);
- }
- # endif//GLM_HAS_BITSCAN_WINDOWS
- # if GLM_ARCH & GLM_ARCH_AVX && GLM_COMPILER & GLM_COMPILER_VC
- template<typename genIUType>
- static int findMSB_avx(genIUType Value)
- {
- GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
- if(Value == 0)
- return -1;
- return int(_tzcnt_u32(Value));
- }
- # endif//GLM_ARCH & GLM_ARCH_AVX && GLM_PLATFORM & GLM_PLATFORM_WINDOWS
- template<typename genIUType>
- static 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 static_cast<int>(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>
- static int findMSB_nlz1(genIUType x)
- {
- GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
- if (x == 0)
- return -1;
- int 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 31 - n;
- }
- static int findMSB_nlz2(unsigned int x)
- {
- unsigned int y;
- int 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 32 - (n - static_cast<int>(x));
- }
- static int findMSB_pop(unsigned int x)
- {
- x = x | (x >> 1);
- x = x | (x >> 2);
- x = x | (x >> 4);
- x = x | (x >> 8);
- x = x | (x >>16);
- return 31 - glm::bitCount(~x);
- }
- static int perf_int(std::size_t Count)
- {
- type<int, 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 Timestamps0 = std::clock();
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
- {
- int Result = glm::findMSB(Data[i].Value);
- Error += Data[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps1 = std::clock();
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
- {
- int Result = findMSB_nlz1(Data[i].Value);
- Error += Data[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps2 = std::clock();
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
- {
- int Result = findMSB_nlz2(static_cast<unsigned int>(Data[i].Value));
- Error += Data[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps3 = std::clock();
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
- {
- int Result = findMSB_095(static_cast<unsigned int>(Data[i].Value));
- Error += Data[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps4 = std::clock();
- # if GLM_HAS_BITSCAN_WINDOWS
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
- {
- int Result = findMSB_intrinsic(Data[i].Value);
- Error += Data[i].Return == Result ? 0 : 1;
- }
- # endif//GLM_HAS_BITSCAN_WINDOWS
- std::clock_t Timestamps5 = std::clock();
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
- {
- int Result = findMSB_pop(static_cast<unsigned int>(Data[i].Value));
- Error += Data[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps6 = std::clock();
- # if GLM_ARCH & GLM_ARCH_AVX && GLM_COMPILER & GLM_COMPILER_VC
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
- {
- int Result = findMSB_avx(Data[i].Value);
- Error += Data[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps7 = std::clock();
- # endif
- std::printf("glm::findMSB: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
- std::printf("findMSB - nlz1: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
- std::printf("findMSB - nlz2: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
- std::printf("findMSB - 0.9.5: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
- # if GLM_HAS_BITSCAN_WINDOWS
- std::printf("findMSB - intrinsics: %d clocks\n", static_cast<unsigned int>(Timestamps5 - Timestamps4));
- # endif//GLM_HAS_BITSCAN_WINDOWS
- std::printf("findMSB - pop: %d clocks\n", static_cast<unsigned int>(Timestamps6 - Timestamps5));
- # if GLM_ARCH & GLM_ARCH_AVX && GLM_COMPILER & GLM_COMPILER_VC
- std::printf("findMSB - avx tzcnt: %d clocks\n", static_cast<unsigned int>(Timestamps7 - Timestamps6));
- # endif//GLM_ARCH & GLM_ARCH_AVX && GLM_PLATFORM & GLM_PLATFORM_WINDOWS
- return Error;
- }
- static int test_ivec4()
- {
- type<glm::ivec4, glm::ivec4> const Data[] =
- {
- {glm::ivec4(0x00000000), glm::ivec4(-1)},
- {glm::ivec4(0x00000001), glm::ivec4( 0)},
- {glm::ivec4(0x00000002), glm::ivec4( 1)},
- {glm::ivec4(0x00000003), glm::ivec4( 1)},
- {glm::ivec4(0x00000004), glm::ivec4( 2)},
- {glm::ivec4(0x00000005), glm::ivec4( 2)},
- {glm::ivec4(0x00000007), glm::ivec4( 2)},
- {glm::ivec4(0x00000008), glm::ivec4( 3)},
- {glm::ivec4(0x00000010), glm::ivec4( 4)},
- {glm::ivec4(0x00000020), glm::ivec4( 5)},
- {glm::ivec4(0x00000040), glm::ivec4( 6)},
- {glm::ivec4(0x00000080), glm::ivec4( 7)},
- {glm::ivec4(0x00000100), glm::ivec4( 8)},
- {glm::ivec4(0x00000200), glm::ivec4( 9)},
- {glm::ivec4(0x00000400), glm::ivec4(10)},
- {glm::ivec4(0x00000800), glm::ivec4(11)},
- {glm::ivec4(0x00001000), glm::ivec4(12)},
- {glm::ivec4(0x00002000), glm::ivec4(13)},
- {glm::ivec4(0x00004000), glm::ivec4(14)},
- {glm::ivec4(0x00008000), glm::ivec4(15)},
- {glm::ivec4(0x00010000), glm::ivec4(16)},
- {glm::ivec4(0x00020000), glm::ivec4(17)},
- {glm::ivec4(0x00040000), glm::ivec4(18)},
- {glm::ivec4(0x00080000), glm::ivec4(19)},
- {glm::ivec4(0x00100000), glm::ivec4(20)},
- {glm::ivec4(0x00200000), glm::ivec4(21)},
- {glm::ivec4(0x00400000), glm::ivec4(22)},
- {glm::ivec4(0x00800000), glm::ivec4(23)},
- {glm::ivec4(0x01000000), glm::ivec4(24)},
- {glm::ivec4(0x02000000), glm::ivec4(25)},
- {glm::ivec4(0x04000000), glm::ivec4(26)},
- {glm::ivec4(0x08000000), glm::ivec4(27)},
- {glm::ivec4(0x10000000), glm::ivec4(28)},
- {glm::ivec4(0x20000000), glm::ivec4(29)},
- {glm::ivec4(0x40000000), glm::ivec4(30)}
- };
- int Error(0);
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::ivec4, glm::ivec4>); ++i)
- {
- glm::ivec4 Result0 = glm::findMSB(Data[i].Value);
- Error += glm::all(glm::equal(Data[i].Return, Result0)) ? 0 : 1;
- }
- return Error;
- }
- static int test_int()
- {
- typedef type<glm::uint, int> entry;
- entry 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(entry); ++i)
- {
- int Result0 = glm::findMSB(Data[i].Value);
- Error += Data[i].Return == Result0 ? 0 : 1;
- }
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(entry); ++i)
- {
- int Result0 = findMSB_nlz1(Data[i].Value);
- Error += Data[i].Return == Result0 ? 0 : 1;
- }
- /*
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(entry); ++i)
- {
- int Result0 = findMSB_nlz2(Data[i].Value);
- Error += Data[i].Return == Result0 ? 0 : 1;
- }
- */
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(entry); ++i)
- {
- int Result0 = findMSB_095(Data[i].Value);
- Error += Data[i].Return == Result0 ? 0 : 1;
- }
- # if GLM_HAS_BITSCAN_WINDOWS
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(entry); ++i)
- {
- int Result0 = findMSB_intrinsic(Data[i].Value);
- Error += Data[i].Return == Result0 ? 0 : 1;
- }
- # endif//GLM_HAS_BITSCAN_WINDOWS
- for(std::size_t i = 0; i < sizeof(Data) / sizeof(entry); ++i)
- {
- int Result0 = findMSB_pop(Data[i].Value);
- Error += Data[i].Return == Result0 ? 0 : 1;
- }
- return Error;
- }
- static int test()
- {
- int Error(0);
- Error += test_ivec4();
- Error += test_int();
- return Error;
- }
- static int perf(std::size_t Samples)
- {
- int Error(0);
- Error += perf_int(Samples);
- return Error;
- }
- }//findMSB
- namespace findLSB
- {
- template<typename genType, typename retType>
- struct type
- {
- genType Value;
- retType Return;
- };
- typedef type<int, int> entry;
- entry const DataI32[] =
- {
- {0x00000001, 0},
- {0x00000003, 0},
- {0x00000002, 1},
- // {0x80000000, 31}, // Clang generates an error with this
- {0x00010000, 16},
- {0x7FFF0000, 16},
- {0x7F000000, 24},
- {0x7F00FF00, 8},
- {0x00000000, -1}
- };
- # if GLM_HAS_BITSCAN_WINDOWS
- template<typename genIUType>
- static int findLSB_intrinsic(genIUType Value)
- {
- GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findLSB' only accept integer values");
- if(Value == 0)
- return -1;
- unsigned long Result(0);
- _BitScanForward(&Result, Value);
- return int(Result);
- }
- # endif
- template<typename genIUType>
- static int findLSB_095(genIUType Value)
- {
- GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findLSB' only accept integer values");
- if(Value == 0)
- return -1;
- genIUType Bit;
- for(Bit = genIUType(0); !(Value & (1 << Bit)); ++Bit){}
- return Bit;
- }
- template<typename genIUType>
- static int findLSB_ntz2(genIUType x)
- {
- if(x == 0)
- return -1;
- return glm::bitCount(~x & (x - static_cast<genIUType>(1)));
- }
- template<typename genIUType>
- static int findLSB_branchfree(genIUType x)
- {
- bool IsNull(x == 0);
- int const Keep(!IsNull);
- int const Discard(IsNull);
- return static_cast<int>(glm::bitCount(~x & (x - static_cast<genIUType>(1)))) * Keep + Discard * -1;
- }
- static int test_int()
- {
- int Error(0);
- for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
- {
- int Result = glm::findLSB(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- }
- for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
- {
- int Result = findLSB_095(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- }
- # if GLM_HAS_BITSCAN_WINDOWS
- for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
- {
- int Result = findLSB_intrinsic(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- }
- # endif
- for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
- {
- int Result = findLSB_ntz2(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- }
- for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
- {
- int Result = findLSB_branchfree(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- }
- return Error;
- }
- static int test()
- {
- int Error(0);
- Error += test_int();
- return Error;
- }
- static int perf_int(std::size_t Count)
- {
- int Error(0);
- std::clock_t Timestamps0 = std::clock();
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
- {
- int Result = glm::findLSB(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps1 = std::clock();
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
- {
- int Result = findLSB_095(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps2 = std::clock();
- # if GLM_HAS_BITSCAN_WINDOWS
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
- {
- int Result = findLSB_intrinsic(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- }
- # endif
- std::clock_t Timestamps3 = std::clock();
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
- {
- int Result = findLSB_ntz2(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps4 = std::clock();
- for(std::size_t k = 0; k < Count; ++k)
- for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
- {
- int Result = findLSB_branchfree(DataI32[i].Value);
- Error += DataI32[i].Return == Result ? 0 : 1;
- }
- std::clock_t Timestamps5 = std::clock();
- std::printf("glm::findLSB: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
- std::printf("findLSB - 0.9.5: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
- # if GLM_HAS_BITSCAN_WINDOWS
- std::printf("findLSB - intrinsics: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
- # endif
- std::printf("findLSB - ntz2: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
- std::printf("findLSB - branchfree: %d clocks\n", static_cast<unsigned int>(Timestamps5 - Timestamps4));
- return Error;
- }
- static int perf(std::size_t Samples)
- {
- int Error(0);
- Error += perf_int(Samples);
- return Error;
- }
- }//findLSB
- namespace uaddCarry
- {
- static int test()
- {
- int Error(0);
-
- {
- glm::uint x = std::numeric_limits<glm::uint>::max();
- glm::uint y = 0;
- glm::uint Carry = 0;
- glm::uint Result = glm::uaddCarry(x, y, Carry);
- Error += Carry == 0 ? 0 : 1;
- Error += Result == std::numeric_limits<glm::uint>::max() ? 0 : 1;
- }
- {
- glm::uint x = std::numeric_limits<glm::uint>::max();
- glm::uint y = 1;
- glm::uint Carry = 0;
- glm::uint Result = glm::uaddCarry(x, y, Carry);
- Error += Carry == 1 ? 0 : 1;
- Error += Result == 0 ? 0 : 1;
- }
- {
- glm::uvec1 x(std::numeric_limits<glm::uint>::max());
- glm::uvec1 y(0);
- glm::uvec1 Carry(0);
- glm::uvec1 Result(glm::uaddCarry(x, y, Carry));
- Error += glm::all(glm::equal(Carry, glm::uvec1(0))) ? 0 : 1;
- Error += glm::all(glm::equal(Result, glm::uvec1(std::numeric_limits<glm::uint>::max()))) ? 0 : 1;
- }
- {
- glm::uvec1 x(std::numeric_limits<glm::uint>::max());
- glm::uvec1 y(1);
- 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(0))) ? 0 : 1;
- }
- return Error;
- }
- }//namespace uaddCarry
- namespace usubBorrow
- {
- static 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
- {
- static 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
- {
- static 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;
- }
- template<bool EXEC = false>
- struct compute_bitfieldBitCountStep
- {
- template<glm::length_t L, typename T, glm::qualifier Q>
- GLM_FUNC_QUALIFIER static glm::vec<L, T, Q> call(glm::vec<L, T, Q> const& v, T, T)
- {
- return v;
- }
- };
- template<>
- struct compute_bitfieldBitCountStep<true>
- {
- template<glm::length_t L, typename T, glm::qualifier Q>
- GLM_FUNC_QUALIFIER static glm::vec<L, T, Q> call(glm::vec<L, T, Q> const& v, T Mask, T Shift)
- {
- return (v & Mask) + ((v >> Shift) & Mask);
- }
- };
- template<glm::length_t L, typename T, glm::qualifier Q>
- static glm::vec<L, int, Q> bitCount_bitfield(glm::vec<L, T, Q> const& v)
- {
- glm::vec<L, typename glm::detail::make_unsigned<T>::type, Q> x(*reinterpret_cast<glm::vec<L, typename glm::detail::make_unsigned<T>::type, Q> const *>(&v));
- x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 2>::call(x, static_cast<typename glm::detail::make_unsigned<T>::type>(0x5555555555555555ull), static_cast<typename glm::detail::make_unsigned<T>::type>( 1));
- x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 4>::call(x, static_cast<typename glm::detail::make_unsigned<T>::type>(0x3333333333333333ull), static_cast<typename glm::detail::make_unsigned<T>::type>( 2));
- x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 8>::call(x, static_cast<typename glm::detail::make_unsigned<T>::type>(0x0F0F0F0F0F0F0F0Full), static_cast<typename glm::detail::make_unsigned<T>::type>( 4));
- x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 16>::call(x, static_cast<typename glm::detail::make_unsigned<T>::type>(0x00FF00FF00FF00FFull), static_cast<typename glm::detail::make_unsigned<T>::type>( 8));
- x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 32>::call(x, static_cast<typename glm::detail::make_unsigned<T>::type>(0x0000FFFF0000FFFFull), static_cast<typename glm::detail::make_unsigned<T>::type>(16));
- x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 64>::call(x, static_cast<typename glm::detail::make_unsigned<T>::type>(0x00000000FFFFFFFFull), static_cast<typename glm::detail::make_unsigned<T>::type>(32));
- return glm::vec<L, int, Q>(x);
- }
- template<typename genType>
- static int bitCount_bitfield(genType x)
- {
- return bitCount_bitfield(glm::vec<1, genType, glm::defaultp>(x)).x;
- }
- static int perf(std::size_t Size)
- {
- int Error(0);
- 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(static_cast<int>(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();
- {
- for(std::size_t i = 0, n = v.size(); i < n; ++i)
- v[i] = bitCount_bitfield(static_cast<int>(i));
- }
- std::clock_t TimestampsF = std::clock();
- std::printf("bitCount - TimeIf %d\n", static_cast<unsigned int>(TimestampsB - TimestampsA));
- std::printf("bitCount - TimeVec %d\n", static_cast<unsigned int>(TimestampsC - TimestampsB));
- std::printf("bitCount - TimeDefault %d\n", static_cast<unsigned int>(TimestampsD - TimestampsC));
- std::printf("bitCount - TimeVec4 %d\n", static_cast<unsigned int>(TimestampsE - TimestampsD));
- std::printf("bitCount - bitfield %d\n", static_cast<unsigned int>(TimestampsF - TimestampsE));
- return Error;
- }
- static int test()
- {
- int Error(0);
- for(std::size_t i = 0, n = sizeof(DataI32) / sizeof(type<int>); i < n; ++i)
- {
- int ResultA = glm::bitCount(DataI32[i].Value);
- int ResultB = bitCount_if(DataI32[i].Value);
- int ResultC = bitCount_vec(DataI32[i].Value);
- int ResultE = bitCount_bitfield(DataI32[i].Value);
- Error += DataI32[i].Return == ResultA ? 0 : 1;
- Error += DataI32[i].Return == ResultB ? 0 : 1;
- Error += DataI32[i].Return == ResultC ? 0 : 1;
- Error += DataI32[i].Return == ResultE ? 0 : 1;
- assert(!Error);
- }
- return Error;
- }
- }//bitCount
- int main()
- {
- int Error = 0;
- Error += ::bitCount::test();
- Error += ::bitfieldReverse::test();
- Error += ::findMSB::test();
- Error += ::findLSB::test();
- Error += ::umulExtended::test();
- Error += ::imulExtended::test();
- Error += ::uaddCarry::test();
- Error += ::usubBorrow::test();
- Error += ::bitfieldInsert::test();
- Error += ::bitfieldExtract::test();
- # ifdef NDEBUG
- std::size_t const Samples = 1000;
- # else
- std::size_t const Samples = 1;
- # endif
- ::bitCount::perf(Samples);
- ::bitfieldReverse::perf(Samples);
- ::findMSB::perf(Samples);
- ::findLSB::perf(Samples);
- return Error;
- }
|