|
|
|
|
@@ -45,20 +45,21 @@ namespace Nz
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
// Les parenthèses autour de la condition sont nécesaires pour que GCC compile ça
|
|
|
|
|
// The parentheses are needed for GCC
|
|
|
|
|
typename std::enable_if<(sizeof(T) > sizeof(UInt32)), unsigned int>::type IntegralLog2(T number)
|
|
|
|
|
{
|
|
|
|
|
static_assert(sizeof(T) % sizeof(UInt32) == 0, "Assertion failed");
|
|
|
|
|
|
|
|
|
|
// L'algorithme pour le logarithme base 2 (au dessus) ne fonctionne qu'avec des nombres au plus 32bits
|
|
|
|
|
// ce code décompose les nombres plus grands en nombres 32 bits par masquage et bit shifting
|
|
|
|
|
// Masking and shifting bits to the right (to bring it back to 32 bits)
|
|
|
|
|
|
|
|
|
|
// Call of the function with 32 bits number, if the result is non-null we have our answer
|
|
|
|
|
for (int i = sizeof(T)-sizeof(UInt32); i >= 0; i -= sizeof(UInt32))
|
|
|
|
|
{
|
|
|
|
|
// Le masque 32 bits sur la partie du nombre qu'on traite actuellement
|
|
|
|
|
// The 32 bits mask on the part we are treating
|
|
|
|
|
T mask = T(std::numeric_limits<UInt32>::max()) << i*8;
|
|
|
|
|
T val = (number & mask) >> i*8; // Masquage et shifting des bits vers la droite (pour le ramener sur 32bits)
|
|
|
|
|
T val = (number & mask) >> i*8; // Masking and shifting bits to the right (to bring it back to 32 bits)
|
|
|
|
|
|
|
|
|
|
// Appel de la fonction avec le nombre 32bits, si le résultat est non-nul nous avons la réponse
|
|
|
|
|
// Call of the function with 32 bits number, if the result is non-null we have our answer
|
|
|
|
|
unsigned int log2 = IntegralLog2<UInt32>(val);
|
|
|
|
|
if (log2)
|
|
|
|
|
return log2 + i*8;
|
|
|
|
|
@@ -75,20 +76,20 @@ namespace Nz
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
// Les parenthèses autour de la condition sont nécesaires pour que GCC compile ça
|
|
|
|
|
// The parentheses are needed for GCC
|
|
|
|
|
typename std::enable_if<(sizeof(T) > sizeof(UInt32)), unsigned int>::type IntegralLog2Pot(T number)
|
|
|
|
|
{
|
|
|
|
|
static_assert(sizeof(T) % sizeof(UInt32) == 0, "Assertion failed");
|
|
|
|
|
|
|
|
|
|
// L'algorithme pour le logarithme base 2 (au dessus) ne fonctionne qu'avec des nombres au plus 32bits
|
|
|
|
|
// ce code décompose les nombres plus grands en nombres 32 bits par masquage et bit shifting
|
|
|
|
|
// The algorithm for logarithm in base 2 only works with numbers greather than 32 bits
|
|
|
|
|
// This code subdivides the biggest number into 32 bits ones
|
|
|
|
|
for (int i = sizeof(T)-sizeof(UInt32); i >= 0; i -= sizeof(UInt32))
|
|
|
|
|
{
|
|
|
|
|
// Le masque 32 bits sur la partie du nombre qu'on traite actuellement
|
|
|
|
|
// The 32 bits mask on the part we are treating
|
|
|
|
|
T mask = T(std::numeric_limits<UInt32>::max()) << i*8;
|
|
|
|
|
UInt32 val = UInt32((number & mask) >> i*8); // Masquage et shifting des bits vers la droite (pour le ramener sur 32bits)
|
|
|
|
|
UInt32 val = UInt32((number & mask) >> i*8); // Masking and shifting bits to the right (to bring it back to 32 bits)
|
|
|
|
|
|
|
|
|
|
// Appel de la fonction avec le nombre 32bits, si le résultat est non-nul nous avons la réponse
|
|
|
|
|
// Call of the function with 32 bits number, if the result is non-null we have our answer
|
|
|
|
|
unsigned int log2 = IntegralLog2Pot<UInt32>(val);
|
|
|
|
|
if (log2 || val == 1)
|
|
|
|
|
return log2 + i*8;
|
|
|
|
|
|