|
|
@@ -3,72 +3,79 @@
|
|
|
#include <cstdlib> //To define "exit", req'd by XLC.
|
|
|
#include <ctime>
|
|
|
|
|
|
-int nlz(unsigned x) {
|
|
|
- int pop(unsigned x);
|
|
|
-
|
|
|
- x = x | (x >> 1);
|
|
|
- x = x | (x >> 2);
|
|
|
- x = x | (x >> 4);
|
|
|
- x = x | (x >> 8);
|
|
|
- x = x | (x >>16);
|
|
|
- return pop(~x);
|
|
|
+int nlz(unsigned x)
|
|
|
+{
|
|
|
+ int pop(unsigned x);
|
|
|
+
|
|
|
+ x = x | (x >> 1);
|
|
|
+ x = x | (x >> 2);
|
|
|
+ x = x | (x >> 4);
|
|
|
+ x = x | (x >> 8);
|
|
|
+ x = x | (x >>16);
|
|
|
+ return pop(~x);
|
|
|
}
|
|
|
|
|
|
-int pop(unsigned x) {
|
|
|
- x = x - ((x >> 1) & 0x55555555);
|
|
|
- x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
|
|
|
- x = (x + (x >> 4)) & 0x0F0F0F0F;
|
|
|
- x = x + (x << 8);
|
|
|
- x = x + (x << 16);
|
|
|
- return x >> 24;
|
|
|
+int pop(unsigned x)
|
|
|
+{
|
|
|
+ x = x - ((x >> 1) & 0x55555555);
|
|
|
+ x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
|
|
|
+ x = (x + (x >> 4)) & 0x0F0F0F0F;
|
|
|
+ x = x + (x << 8);
|
|
|
+ x = x + (x << 16);
|
|
|
+ return x >> 24;
|
|
|
}
|
|
|
|
|
|
-int ntz1(unsigned x) {
|
|
|
- return 32 - nlz(~x & (x-1));
|
|
|
+int ntz1(unsigned x)
|
|
|
+{
|
|
|
+ return 32 - nlz(~x & (x-1));
|
|
|
}
|
|
|
|
|
|
-int ntz2(unsigned x) {
|
|
|
- return pop(~x & (x - 1));
|
|
|
+int ntz2(unsigned x)
|
|
|
+{
|
|
|
+ return pop(~x & (x - 1));
|
|
|
}
|
|
|
|
|
|
-int ntz3(unsigned x) {
|
|
|
- int n;
|
|
|
-
|
|
|
- if (x == 0) return(32);
|
|
|
- n = 1;
|
|
|
- if ((x & 0x0000FFFF) == 0) {n = n +16; x = x >>16;}
|
|
|
- if ((x & 0x000000FF) == 0) {n = n + 8; x = x >> 8;}
|
|
|
- if ((x & 0x0000000F) == 0) {n = n + 4; x = x >> 4;}
|
|
|
- if ((x & 0x00000003) == 0) {n = n + 2; x = x >> 2;}
|
|
|
- return n - (x & 1);
|
|
|
+int ntz3(unsigned x)
|
|
|
+{
|
|
|
+ int n;
|
|
|
+
|
|
|
+ if (x == 0) return(32);
|
|
|
+ n = 1;
|
|
|
+ if ((x & 0x0000FFFF) == 0) {n = n +16; x = x >>16;}
|
|
|
+ if ((x & 0x000000FF) == 0) {n = n + 8; x = x >> 8;}
|
|
|
+ if ((x & 0x0000000F) == 0) {n = n + 4; x = x >> 4;}
|
|
|
+ if ((x & 0x00000003) == 0) {n = n + 2; x = x >> 2;}
|
|
|
+ return n - (x & 1);
|
|
|
}
|
|
|
|
|
|
-int ntz4(unsigned x) {
|
|
|
- unsigned y;
|
|
|
- int n;
|
|
|
-
|
|
|
- if (x == 0) return 32;
|
|
|
- n = 31;
|
|
|
- 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) {n = n - 1;}
|
|
|
- return n;
|
|
|
+int ntz4(unsigned x)
|
|
|
+{
|
|
|
+ unsigned y;
|
|
|
+ int n;
|
|
|
+
|
|
|
+ if (x == 0) return 32;
|
|
|
+ n = 31;
|
|
|
+ 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) {n = n - 1;}
|
|
|
+ return n;
|
|
|
}
|
|
|
|
|
|
-int ntz4a(unsigned x) {
|
|
|
- unsigned y;
|
|
|
- int n;
|
|
|
-
|
|
|
- if (x == 0) return 32;
|
|
|
- n = 31;
|
|
|
- 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;}
|
|
|
- n = n - ((x << 1) >> 31);
|
|
|
- return n;
|
|
|
+int ntz4a(unsigned x)
|
|
|
+{
|
|
|
+ unsigned y;
|
|
|
+ int n;
|
|
|
+
|
|
|
+ if (x == 0) return 32;
|
|
|
+ n = 31;
|
|
|
+ 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;}
|
|
|
+ n = n - ((x << 1) >> 31);
|
|
|
+ return n;
|
|
|
}
|
|
|
|
|
|
int ntz5(char x)
|
|
|
@@ -90,16 +97,18 @@ int ntz5(char x)
|
|
|
else return 8;
|
|
|
}
|
|
|
|
|
|
-int ntz6(unsigned x) {
|
|
|
- int n;
|
|
|
-
|
|
|
- x = ~x & (x - 1);
|
|
|
- n = 0; // n = 32;
|
|
|
- while(x != 0) { // while (x != 0) {
|
|
|
- n = n + 1; // n = n - 1;
|
|
|
- x = x >> 1; // x = x + x;
|
|
|
- } // }
|
|
|
- return n; // return n;
|
|
|
+int ntz6(unsigned x)
|
|
|
+{
|
|
|
+ int n;
|
|
|
+
|
|
|
+ x = ~x & (x - 1);
|
|
|
+ n = 0; // n = 32;
|
|
|
+ while(x != 0)
|
|
|
+ { // while (x != 0) {
|
|
|
+ n = n + 1; // n = n - 1;
|
|
|
+ x = x >> 1; // x = x + x;
|
|
|
+ } // }
|
|
|
+ return n; // return n;
|
|
|
}
|
|
|
|
|
|
int ntz6a(unsigned x)
|
|
|
@@ -201,15 +210,16 @@ int ntz8a(unsigned x)
|
|
|
/* Reiser's algorithm. Three ops including a "remainder,"
|
|
|
plus an indexed load. */
|
|
|
|
|
|
-int ntz9(unsigned x) {
|
|
|
-
|
|
|
- static char table[37] = {32, 0, 1, 26, 2, 23, 27,
|
|
|
- u, 3, 16, 24, 30, 28, 11, u, 13, 4,
|
|
|
- 7, 17, u, 25, 22, 31, 15, 29, 10, 12,
|
|
|
- 6, u, 21, 14, 9, 5, 20, 8, 19, 18};
|
|
|
-
|
|
|
- x = (x & -x)%37;
|
|
|
- return table[x];
|
|
|
+int ntz9(unsigned x)
|
|
|
+{
|
|
|
+ static char table[37] = {
|
|
|
+ 32, 0, 1, 26, 2, 23, 27,
|
|
|
+ u, 3, 16, 24, 30, 28, 11, u, 13, 4,
|
|
|
+ 7, 17, u, 25, 22, 31, 15, 29, 10, 12,
|
|
|
+ 6, u, 21, 14, 9, 5, 20, 8, 19, 18};
|
|
|
+
|
|
|
+ x = (x & -x)%37;
|
|
|
+ return table[x];
|
|
|
}
|
|
|
|
|
|
/* Using a de Bruijn sequence. This is a table lookup with a 32-entry
|
|
|
@@ -257,7 +267,7 @@ int ntz11 (unsigned int n) {
|
|
|
int errors;
|
|
|
void error(int x, int y) {
|
|
|
errors = errors + 1;
|
|
|
- printf("Error for x = %08x, got %d\n", x, y);
|
|
|
+ std::printf("Error for x = %08x, got %d\n", x, y);
|
|
|
}
|
|
|
|
|
|
int main()
|
|
|
@@ -287,7 +297,7 @@ int main()
|
|
|
if (ntz1(test[i]) != test[i+1]) error(test[i], ntz1(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz1: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz1: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -295,7 +305,7 @@ int main()
|
|
|
if (ntz2(test[i]) != test[i+1]) error(test[i], ntz2(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz2: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz2: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -303,7 +313,7 @@ int main()
|
|
|
if (ntz3(test[i]) != test[i+1]) error(test[i], ntz3(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz3: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz3: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -311,7 +321,7 @@ int main()
|
|
|
if (ntz4(test[i]) != test[i+1]) error(test[i], ntz4(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz4: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz4: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -319,7 +329,7 @@ int main()
|
|
|
if (ntz4a(test[i]) != test[i+1]) error(test[i], ntz4a(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz4a: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz4a: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for(std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -333,7 +343,7 @@ int main()
|
|
|
}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz5: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz5: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -341,7 +351,7 @@ int main()
|
|
|
if (ntz6(test[i]) != test[i+1]) error(test[i], ntz6(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz6: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz6: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -349,7 +359,7 @@ int main()
|
|
|
if (ntz6a(test[i]) != test[i+1]) error(test[i], ntz6a(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz6a: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz6a: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -357,7 +367,7 @@ int main()
|
|
|
if (ntz7(test[i]) != test[i+1]) error(test[i], ntz7(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz7: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz7: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -365,7 +375,7 @@ int main()
|
|
|
if (ntz7_christophe(test[i]) != test[i+1]) error(test[i], ntz7(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz7_christophe: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz7_christophe: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -373,7 +383,7 @@ int main()
|
|
|
if (ntz8(test[i]) != test[i+1]) error(test[i], ntz8(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz8: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz8: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -381,7 +391,7 @@ int main()
|
|
|
if (ntz8a(test[i]) != test[i+1]) error(test[i], ntz8a(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz8a: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz8a: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -389,7 +399,7 @@ int main()
|
|
|
if (ntz9(test[i]) != test[i+1]) error(test[i], ntz9(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz9: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz9: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
TimestampBeg = std::clock();
|
|
|
for (std::size_t k = 0; k < Count; ++k)
|
|
|
@@ -397,10 +407,10 @@ int main()
|
|
|
if (ntz10(test[i]) != test[i+1]) error(test[i], ntz10(test[i]));}
|
|
|
TimestampEnd = std::clock();
|
|
|
|
|
|
- printf("ntz10: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
+ std::printf("ntz10: %d clocks\n", static_cast<int>(TimestampEnd - TimestampBeg));
|
|
|
|
|
|
if (errors == 0)
|
|
|
- printf("Passed all %d cases.\n", static_cast<int>(sizeof(test)/8));
|
|
|
+ std::printf("Passed all %d cases.\n", static_cast<int>(sizeof(test)/8));
|
|
|
|
|
|
# endif//NDEBUG
|
|
|
}
|