Clean up platform-dependant format strings.
authorPeter Griess <pgriess@fb.com>
Mon, 23 Sep 2013 21:38:05 +0000 (16:38 -0500)
committerPeter Griess <pgriess@fb.com>
Tue, 15 Oct 2013 01:46:18 +0000 (18:46 -0700)
Summary:
- Use PRIu64 and friends from <cinttypes> to handle uint64_t requiring
different format strings on different platforms.
- Fix lint errors.

Test Plan:
- fbconfig -r folly && fbmake runtests
- ./configure && make check on Ubuntu/FC/Mac

Reviewed By: simpkins@fb.com

FB internal diff: D998512

folly/build/GenerateFingerprintTables.cpp
folly/test/SpookyHashV1Test.cpp
folly/test/SpookyHashV2Test.cpp

index 3b330f2a4a3c0e93c448238e7f7fa3b26039d004..b39c168baf9847b9cf2447845cb4efcbd8425639 100644 (file)
  * limitations under the License.
  */
 
+#ifndef __STDC_FORMAT_MACROS
+#define __STDC_FORMAT_MACROS 1
+#endif
+
 #include <cstdio>
+#include <cinttypes>
 
 #include <string>
 
@@ -84,7 +89,7 @@ void computeTables(FILE* file, const FingerprintPolynomial<DEG>& poly) {
       "const uint64_t FingerprintTable<%d>::poly[%d] = {",
       DEG+1, FingerprintPolynomial<DEG>::size()));
   for (int j = 0; j < FingerprintPolynomial<DEG>::size(); j++) {
-    CHECK_ERR(fprintf(file, "%s%luLU", j ? ", " : "", poly_val[j]));
+    CHECK_ERR(fprintf(file, "%s%" PRIu64 "LU", j ? ", " : "", poly_val[j]));
   }
   CHECK_ERR(fprintf(file, "};\n\n"));
 
@@ -101,7 +106,8 @@ void computeTables(FILE* file, const FingerprintPolynomial<DEG>& poly) {
     for (int x = 0; x < 256; x++) {
       CHECK_ERR(fprintf(file, "    {"));
       for (int j = 0; j < FingerprintPolynomial<DEG>::size(); j++) {
-        CHECK_ERR(fprintf(file, "%s%luLU", (j ? ", " : ""), table[i][x][j]));
+        CHECK_ERR(fprintf(
+          file, "%s%" PRIu64 "LU", (j ? ", " : ""), table[i][x][j]));
       }
       CHECK_ERR(fprintf(file, "},\n"));
     }
index cdaae438999e08aa56bbf99ad867daa7814b3303..2896c46135e4bf22bdddf0c741b81ef0a7c2b760 100644 (file)
  */
 // SpookyHash: a 128-bit noncryptographic hash function
 // By Bob Jenkins, public domain
+
+#ifndef __STDC_FORMAT_MACROS
+#define __STDC_FORMAT_MACROS 1
+#endif
+
 #include "folly/SpookyHashV1.h"
+#include "folly/Benchmark.h"
 
+#include <cinttypes>
 #include <cstdio>
 #include <cstddef>
 #include <cstring>
@@ -271,7 +278,8 @@ void DoTimingBig(int seed)
         SpookyHashV1::Hash128(buf[i], BUFSIZE, &hash1, &hash2);
     }
     uint64_t z = GetTickCount();
-    printf("SpookyHashV1::Hash128, uncached: time is %4lu milliseconds\n", z-a);
+    printf("SpookyHashV1::Hash128, uncached: time is "
+           "%4" PRIu64 " milliseconds\n", z-a);
 
     a = GetTickCount();
     for (uint64_t i=0; i<NUMBUF; ++i)
@@ -279,7 +287,8 @@ void DoTimingBig(int seed)
         Add(buf[i], BUFSIZE, &hash1, &hash2);
     }
     z = GetTickCount();
-    printf("Addition           , uncached: time is %4lu milliseconds\n", z-a);
+    printf("Addition           , uncached: time is "
+           "%4" PRIu64 " milliseconds\n", z-a);
 
     a = GetTickCount();
     for (uint64_t i=0; i<NUMBUF*BUFSIZE/1024; ++i)
@@ -287,7 +296,8 @@ void DoTimingBig(int seed)
         SpookyHashV1::Hash128(buf[0], 1024, &hash1, &hash2);
     }
     z = GetTickCount();
-    printf("SpookyHashV1::Hash128,   cached: time is %4lu milliseconds\n", z-a);
+    printf("SpookyHashV1::Hash128,   cached: time is "
+           "%4" PRIu64 " milliseconds\n", z-a);
 
     a = GetTickCount();
     for (uint64_t i=0; i<NUMBUF*BUFSIZE/1024; ++i)
@@ -295,7 +305,8 @@ void DoTimingBig(int seed)
         Add(buf[0], 1024, &hash1, &hash2);
     }
     z = GetTickCount();
-    printf("Addition           ,   cached: time is %4lu milliseconds\n", z-a);
+    printf("Addition           ,   cached: time is "
+           "%4" PRIu64 " milliseconds\n", z-a);
 
     for (int i=0; i<NUMBUF; ++i)
     {
@@ -311,8 +322,8 @@ void DoTimingBig(int seed)
 #define NUMITER 10000000
 void DoTimingSmall(int seed)
 {
-    printf("\ntesting timing of hashing up to %d cached aligned bytes %d times ...\n",
-           BUFSIZE, NUMITER);
+    printf("\ntesting timing of hashing up to %d cached aligned bytes %d "
+           "times ...\n", BUFSIZE, NUMITER);
 
     uint64_t buf[BUFSIZE/8];
     for (int i=0; i<BUFSIZE/8; ++i)
@@ -330,8 +341,8 @@ void DoTimingSmall(int seed)
             SpookyHashV1::Hash128((char *)buf, i, &hash1, &hash2);
         }
         uint64_t z = GetTickCount();
-        printf("%d bytes: hash is %.16lx %.16lx, time is %lu\n",
-               i, hash1, hash2, z-a);
+        printf("%d bytes: hash is %.16" PRIx64 " %.16" PRIx64 ", "
+               "time is %" PRIu64 "\n", i, hash1, hash2, z-a);
     }
 }
 #undef BUFSIZE
@@ -373,7 +384,8 @@ void TestAlignment()
 #define MEASURES 6
 void TestDeltas(int seed)
 {
-    printf("\nall 1 or 2 bit input deltas get %d tries to flip every output bit ...\n", TRIES);
+    printf("\nall 1 or 2 bit input deltas get %d tries to flip every output "
+           "bit ...\n", TRIES);
 
     Random random;
     random.Init((uint64_t)seed);
@@ -482,12 +494,12 @@ void TestPieces()
 
         if (a != c)
         {
-            printf("wrong a %d: %.16lx %.16lx\n", i, a,c);
+            printf("wrong a %d: %.16" PRIx64 " %.16" PRIx64 "\n", i, a,c);
             failed = true;
         }
         if (b != d)
         {
-            printf("wrong b %d: %.16lx %.16lx\n", i, b,d);
+            printf("wrong b %d: %.16" PRIx64 " %.16" PRIx64 "\n", i, b,d);
             failed = true;
         }
 
@@ -502,12 +514,14 @@ void TestPieces()
             state.Final(&c, &d);
             if (a != c)
             {
-                printf("wrong a %d %d: %.16lx %.16lx\n", j, i, a,c);
+                printf("wrong a %d %d: %.16" PRIx64 " %.16" PRIx64 "\n",
+                       j, i, a,c);
                 failed = true;
             }
             if (b != d)
             {
-                printf("wrong b %d %d: %.16lx %.16lx\n", j, i, b,d);
+                printf("wrong b %d %d: %.16" PRIx64 " %.16" PRIx64 "\n",
+                       j, i, b,d);
                 failed = true;
             }
         }
index cf381101f83840e71cb48d15719a4987a86ed4f3..bf7c2bb86e34708fc2a7fef58862d10b124534ae 100644 (file)
  */
 // SpookyHash: a 128-bit noncryptographic hash function
 // By Bob Jenkins, public domain
+
+#ifndef __STDC_FORMAT_MACROS
+#define __STDC_FORMAT_MACROS 1
+#endif
+
 #include "folly/SpookyHashV2.h"
+#include "folly/Benchmark.h"
 
+#include <cinttypes>
 #include <cstdio>
 #include <cstddef>
 #include <cstring>
@@ -167,7 +174,8 @@ void TestResults()
         saw[i] = SpookyHashV2::Hash32(buf, i, 0);
         if (saw[i] != expected[i])
         {
-           printf("%3d: saw 0x%.8x, expected 0x%.8lx\n", i, saw[i], expected[i]);
+            printf("%3d: saw 0x%.8x, expected 0x%.8" PRIx64 "\n", i, saw[i],
+                   expected[i]);
             failed = true;
         }
     }
@@ -193,34 +201,38 @@ void DoTimingBig(int seed)
     uint64_t hash2 = seed;
     for (uint64_t i=0; i<NUMBUF; ++i)
     {
-       SpookyHashV2::Hash128(buf[i], BUFSIZE, &hash1, &hash2);
+        SpookyHashV2::Hash128(buf[i], BUFSIZE, &hash1, &hash2);
     }
     uint64_t z = GetTickCount();
-    printf("SpookyHashV2::Hash128, uncached: time is %4ld milliseconds\n", z-a);
+    printf("SpookyHashV2::Hash128, uncached: time is "
+           "%4" PRId64 " milliseconds\n", z-a);
 
     a = GetTickCount();
     for (uint64_t i=0; i<NUMBUF; ++i)
     {
-       Add(buf[i], BUFSIZE, &hash1, &hash2);
+        Add(buf[i], BUFSIZE, &hash1, &hash2);
     }
     z = GetTickCount();
-    printf("Addition           , uncached: time is %4ld milliseconds\n", z-a);
+    printf("Addition           , uncached: time is %4" PRId64 " milliseconds\n",
+           z-a);
 
     a = GetTickCount();
     for (uint64_t i=0; i<NUMBUF*BUFSIZE/1024; ++i)
     {
-       SpookyHashV2::Hash128(buf[0], 1024, &hash1, &hash2);
+        SpookyHashV2::Hash128(buf[0], 1024, &hash1, &hash2);
     }
     z = GetTickCount();
-    printf("SpookyHashV2::Hash128,   cached: time is %4ld milliseconds\n", z-a);
+    printf("SpookyHashV2::Hash128,   cached: time is "
+           "%4" PRId64 " milliseconds\n", z-a);
 
     a = GetTickCount();
     for (uint64_t i=0; i<NUMBUF*BUFSIZE/1024; ++i)
     {
-       Add(buf[0], 1024, &hash1, &hash2);
+        Add(buf[0], 1024, &hash1, &hash2);
     }
     z = GetTickCount();
-    printf("Addition           ,   cached: time is %4ld milliseconds\n", z-a);
+    printf("Addition           ,   cached: time is %4" PRId64 " milliseconds\n",
+           z-a);
 
     for (int i=0; i<NUMBUF; ++i)
     {
@@ -236,8 +248,8 @@ void DoTimingBig(int seed)
 #define NUMITER 10000000
 void DoTimingSmall(int seed)
 {
-    printf("\ntesting timing of hashing up to %d cached aligned bytes %d times ...\n",
-           BUFSIZE, NUMITER);
+    printf("\ntesting timing of hashing up to %d cached aligned bytes %d "
+           "times ...\n", BUFSIZE, NUMITER);
 
     uint64_t buf[BUFSIZE/8];
     for (int i=0; i<BUFSIZE/8; ++i)
@@ -255,8 +267,8 @@ void DoTimingSmall(int seed)
             SpookyHashV2::Hash128((char *)buf, i, &hash1, &hash2);
         }
         uint64_t z = GetTickCount();
-        printf("%d bytes: hash is %.16lx %.16lx, time is %ld\n",
-               i, hash1, hash2, z-a);
+        printf("%d bytes: hash is %.16" PRIx64 " %.16" PRIx64 ", "
+               "time is %" PRId64 "\n", i, hash1, hash2, z-a);
     }
 }
 #undef BUFSIZE
@@ -298,7 +310,8 @@ void TestAlignment()
 #define MEASURES 6
 void TestDeltas(int seed)
 {
-    printf("\nall 1 or 2 bit input deltas get %d tries to flip every output bit ...\n", TRIES);
+    printf("\nall 1 or 2 bit input deltas get %d tries to flip every output "
+           "bit ...\n", TRIES);
 
     Random random;
     random.Init((uint64_t)seed);
@@ -399,20 +412,20 @@ void TestPieces()
         SpookyHashV2::Hash128(buf, i, &a, &b);
 
         // all as one piece
-       c = 0xdeadbeefdeadbeef;
-       d = 0xbaceba11baceba11;
+        c = 0xdeadbeefdeadbeef;
+        d = 0xbaceba11baceba11;
         state.Init(seed1, seed2);
         state.Update(buf, i);
         state.Final(&c, &d);
 
         if (a != c)
         {
-            printf("wrong a %d: %.16lx %.16lx\n", i, a,c);
+            printf("wrong a %d: %.16" PRIx64 " %.16" PRIx64 "\n", i, a,c);
             failed = true;
         }
         if (b != d)
         {
-            printf("wrong b %d: %.16lx %.16lx\n", i, b,d);
+            printf("wrong b %d: %.16" PRIx64 " %.16" PRIx64 "\n", i, b,d);
             failed = true;
         }
 
@@ -427,12 +440,14 @@ void TestPieces()
             state.Final(&c, &d);
             if (a != c)
             {
-                printf("wrong a %d %d: %.16lx %.16lx\n", j, i, a,c);
+                printf("wrong a %d %d: %.16" PRIx64 " %.16" PRIx64 "\n",
+                       j, i, a,c);
                 failed = true;
             }
             if (b != d)
             {
-                printf("wrong b %d %d: %.16lx %.16lx\n", j, i, b,d);
+                printf("wrong b %d %d: %.16" PRIx64 " %.16" PRIx64 "\n",
+                       j, i, b,d);
                 failed = true;
             }
         }