| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587 |
- #include <glm/integer.hpp>
- #include <glm/vector_relational.hpp>
- #include <glm/ext/vector_int1.hpp>
- #include <glm/ext/vector_int2.hpp>
- #include <glm/ext/vector_int3.hpp>
- #include <glm/ext/vector_int4.hpp>
- #include <glm/ext/vector_uint1.hpp>
- #include <glm/ext/vector_uint2.hpp>
- #include <glm/ext/vector_uint3.hpp>
- #include <glm/ext/vector_uint4.hpp>
- #include <glm/ext/scalar_int_sized.hpp>
- #include <glm/ext/scalar_uint_sized.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, 0x000000ff, 8, 8, 0xff00ff00},
- {0xffff0000, 0xffff0000, 16, 16, 0x00000000},
- {0x0000ffff, 0x0000ffff, 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)
- {
- 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 bitfieldReverseUint32(glm::uint32 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 bitfieldReverseUint64(glm::uint64 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;
- }
- };
- # if GLM_COMPILER & GLM_COMPILER_VC
- # pragma warning(push)
- # pragma warning(disable : 4309)
- # endif
- 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;
- }
- # if GLM_COMPILER & GLM_COMPILER_VC
- # pragma warning(pop)
- # endif
- template<typename genType>
- GLM_FUNC_QUALIFIER genType bitfieldReverseOps(genType x)
- {
- return bitfieldReverseOps(glm::vec<1, genType, glm::defaultp>(x)).x;
- }
- #if GLM_COMPILER & GLM_COMPILER_CLANG
- # pragma clang diagnostic push
- # pragma clang diagnostic ignored "-Wpadded"
- #endif
- template<typename genType>
- struct type
- {
- genType Value;
- genType Return;
- result Result;
- };
- #if GLM_COMPILER & GLM_COMPILER_CLANG
- # pragma clang diagnostic pop
- #endif
- 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<int>(Timestamps1 - Timestamps0));
- std::printf("bitfieldReverseLoop: %d clocks\n", static_cast<int>(Timestamps2 - Timestamps1));
- std::printf("bitfieldReverseUint32: %d clocks\n", static_cast<int>(Timestamps3 - Timestamps2));
- std::printf("bitfieldReverseOps: %d clocks\n", static_cast<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::uint64 k = 0; k < Count; ++k)
- Data[static_cast<std::size_t>(k)] = glm::bitfieldReverse(k);
- std::clock_t Timestamps1 = std::clock();
- for(glm::uint64 k = 0; k < Count; ++k)
- Data[static_cast<std::size_t>(k)] = bitfieldReverseLoop<glm::uint64>(k);
- std::clock_t Timestamps2 = std::clock();
- for(glm::uint64 k = 0; k < Count; ++k)
- Data[static_cast<std::size_t>(k)] = bitfieldReverseUint64(k);
- std::clock_t Timestamps3 = std::clock();
- for(glm::uint64 k = 0; k < Count; ++k)
- Data[static_cast<std::size_t>(k)] = bitfieldReverseOps(k);
- std::clock_t Timestamps4 = std::clock();
- std::printf("glm::bitfieldReverse - 64: %d clocks\n", static_cast<int>(Timestamps1 - Timestamps0));
- std::printf("bitfieldReverseLoop - 64: %d clocks\n", static_cast<int>(Timestamps2 - Timestamps1));
- std::printf("bitfieldReverseUint - 64: %d clocks\n", static_cast<int>(Timestamps3 - Timestamps2));
- std::printf("bitfieldReverseOps - 64: %d clocks\n", static_cast<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)
- {
- 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_BIT) && (GLM_COMPILER & GLM_COMPILER_VC))
- template<typename genIUType>
- static int findMSB_avx(genIUType Value)
- {
- 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_BIT) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS))
- template<typename genIUType>
- static int findMSB_095(genIUType Value)
- {
- 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)
- {
- 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_BIT) && (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//((GLM_ARCH & GLM_ARCH_AVX_BIT) && (GLM_COMPILER & GLM_COMPILER_VC))
- std::printf("glm::findMSB: %d clocks\n", static_cast<int>(Timestamps1 - Timestamps0));
- std::printf("findMSB - nlz1: %d clocks\n", static_cast<int>(Timestamps2 - Timestamps1));
- std::printf("findMSB - nlz2: %d clocks\n", static_cast<int>(Timestamps3 - Timestamps2));
- std::printf("findMSB - 0.9.5: %d clocks\n", static_cast<int>(Timestamps4 - Timestamps3));
- # if GLM_HAS_BITSCAN_WINDOWS
- std::printf("findMSB - intrinsics: %d clocks\n", static_cast<int>(Timestamps5 - Timestamps4));
- # endif//GLM_HAS_BITSCAN_WINDOWS
- std::printf("findMSB - pop: %d clocks\n", static_cast<int>(Timestamps6 - Timestamps5));
- # if ((GLM_ARCH & GLM_ARCH_AVX_BIT) && (GLM_COMPILER & GLM_COMPILER_VC))
- std::printf("findMSB - avx tzcnt: %d clocks\n", static_cast<int>(Timestamps7 - Timestamps6));
- # endif//((GLM_ARCH & GLM_ARCH_AVX_BIT) && (GLM_COMPILER & GLM_COMPILER_VC))
- 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)
- {
- 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)
- {
- 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<int>(Timestamps1 - Timestamps0));
- std::printf("findLSB - 0.9.5: %d clocks\n", static_cast<int>(Timestamps2 - Timestamps1));
- # if GLM_HAS_BITSCAN_WINDOWS
- std::printf("findLSB - intrinsics: %d clocks\n", static_cast<int>(Timestamps3 - Timestamps2));
- # endif
- std::printf("findLSB - ntz2: %d clocks\n", static_cast<int>(Timestamps4 - Timestamps3));
- std::printf("findLSB - branchfree: %d clocks\n", static_cast<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 == glm::uint(x-y) ? 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(x-y))) ? 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(x-y))) ? 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(x-y))) ? 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(x-y))) ? 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)
- {
- 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)
- {
- 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);
- }
- };
- # if GLM_COMPILER & GLM_COMPILER_VC
- # pragma warning(push)
- # pragma warning(disable : 4309)
- # endif
- 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(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);
- }
- # if GLM_COMPILER & GLM_COMPILER_VC
- # pragma warning(pop)
- # endif
- 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<int>(TimestampsB - TimestampsA));
- std::printf("bitCount - TimeVec %d\n", static_cast<int>(TimestampsC - TimestampsB));
- std::printf("bitCount - TimeDefault %d\n", static_cast<int>(TimestampsD - TimestampsC));
- std::printf("bitCount - TimeVec4 %d\n", static_cast<int>(TimestampsE - TimestampsD));
- std::printf("bitCount - bitfield %d\n", static_cast<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);
- Error += DataI32[i].Return == ResultA ? 0 : 1;
- assert(!Error);
- int ResultB = bitCount_if(DataI32[i].Value);
- Error += DataI32[i].Return == ResultB ? 0 : 1;
- assert(!Error);
- int ResultC = bitCount_vec(DataI32[i].Value);
- Error += DataI32[i].Return == ResultC ? 0 : 1;
- assert(!Error);
- int ResultE = bitCount_bitfield(DataI32[i].Value);
- 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;
- }
|