Split off SkConsistentChecksum from SkChecksum
authorepoger@google.com <epoger@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 2 Nov 2012 18:35:04 +0000 (18:35 +0000)
committerepoger@google.com <epoger@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 2 Nov 2012 18:35:04 +0000 (18:35 +0000)
as part of https://goto.google.com/ImprovingTheSkiaRebaseliningProcess
Review URL: https://codereview.appspot.com/6820074

git-svn-id: http://skia.googlecode.com/svn/trunk@6270 2bbb7eff-a529-9590-31e7-b0007b416f81

gyp/tests.gyp
gyp/utils.gyp
src/utils/SkConsistentChecksum.h [new file with mode: 0644]
tests/ChecksumTest.cpp [new file with mode: 0644]
tests/SortTest.cpp

index c91388c..66888dd 100644 (file)
@@ -25,6 +25,7 @@
         '../tests/BlitRowTest.cpp',
         '../tests/BlurTest.cpp',
         '../tests/CanvasTest.cpp',
+        '../tests/ChecksumTest.cpp',
         '../tests/ClampRangeTest.cpp',
         '../tests/ClipCacheTest.cpp',
         '../tests/ClipCubicTest.cpp',
         'images.gyp:images',
         'pdf.gyp:pdf',
         'tools.gyp:picture_utils',
+        'utils.gyp:utils',
       ],
       'conditions': [
         [ 'skia_gpu == 1', {
index aa3f4a3..6fef668 100644 (file)
@@ -53,6 +53,7 @@
         '../src/utils/SkBitSet.h',
         '../src/utils/SkBoundaryPatch.cpp',
         '../src/utils/SkCamera.cpp',
+        '../src/utils/SkConsistentChecksum.h',
         '../src/utils/SkCubicInterval.cpp',
         '../src/utils/SkCullPoints.cpp',
         '../src/utils/SkDeferredCanvas.cpp',
diff --git a/src/utils/SkConsistentChecksum.h b/src/utils/SkConsistentChecksum.h
new file mode 100644 (file)
index 0000000..8b7c53d
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkConsistentChecksum_DEFINED
+#define SkConsistentChecksum_DEFINED
+
+#include "SkTypes.h"
+
+class SkConsistentChecksum : SkNoncopyable {
+private:
+    /*
+     *  Our Rotate and Mash helpers are meant to automatically do the right
+     *  thing depending if sizeof(uintptr_t) is 4 or 8.
+     */
+    enum {
+        ROTR = 17,
+        ROTL = sizeof(uintptr_t) * 8 - ROTR,
+        HALFBITS = sizeof(uintptr_t) * 4
+    };
+
+    static inline uintptr_t Mash(uintptr_t total, uintptr_t value) {
+        return ((total >> ROTR) | (total << ROTL)) ^ value;
+    }
+
+public:
+    /**
+     *  Compute a 32-bit checksum for a given data block
+     *
+     *  WARNING: As of 1 Nov 2012, this algorithm is still in
+     *  flux... but once we get it doing what we want, it will be:
+     *  1. consistent across revisions of the library (for a given set
+     *     of bytes, the checksum generated at one revision of the Skia
+     *     library will match the one generated on any other revision of
+     *     the Skia library)
+     *  2. consistent across platforms (for a given
+     *     set of bytes, the checksum generated on one platform will
+     *     match the one generated on any other platform)
+     *
+     *  @param data Memory address of the data block to be processed. Must be
+     *              32-bit aligned.
+     *  @param size Size of the data block in bytes. Must be a multiple of 4.
+     *  @return checksum result
+     */
+    static uint32_t Compute(const uint32_t* data, size_t size) {
+        SkASSERT(SkIsAlign4(size));
+
+        /*
+         *  We want to let the compiler use 32bit or 64bit addressing and math
+         *  so we use uintptr_t as our magic type. This makes the code a little
+         *  more obscure (we can't hard-code 32 or 64 anywhere, but have to use
+         *  sizeof()).
+         */
+        uintptr_t result = 0;
+        const uintptr_t* ptr = reinterpret_cast<const uintptr_t*>(data);
+
+        /*
+         *  count the number of quad element chunks. This takes into account
+         *  if we're on a 32bit or 64bit arch, since we use sizeof(uintptr_t)
+         *  to compute how much to shift-down the size.
+         */
+        size_t n4 = size / (sizeof(uintptr_t) << 2);
+        for (size_t i = 0; i < n4; ++i) {
+            result = Mash(result, *ptr++);
+            result = Mash(result, *ptr++);
+            result = Mash(result, *ptr++);
+            result = Mash(result, *ptr++);
+        }
+        size &= ((sizeof(uintptr_t) << 2) - 1);
+
+        data = reinterpret_cast<const uint32_t*>(ptr);
+        const uint32_t* stop = data + (size >> 2);
+        while (data < stop) {
+            result = Mash(result, *data++);
+        }
+
+        /*
+         *  smash us down to 32bits if we were 64. Note that when uintptr_t is
+         *  32bits, this code-path should go away, but I still got a warning
+         *  when I wrote
+         *      result ^= result >> 32;
+         *  since >>32 is undefined for 32bit ints, hence the wacky HALFBITS
+         *  define.
+         */
+        if (8 == sizeof(result)) {
+            result ^= result >> HALFBITS;
+        }
+        return static_cast<uint32_t>(result);
+    }
+};
+
+#endif
diff --git a/tests/ChecksumTest.cpp b/tests/ChecksumTest.cpp
new file mode 100644 (file)
index 0000000..3425bcd
--- /dev/null
@@ -0,0 +1,120 @@
+
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include "Test.h"
+#include "SkChecksum.h"
+#include "SkConsistentChecksum.h"
+
+namespace skiatest {
+    class ChecksumTestClass : public Test {
+    public:
+        static Test* Factory(void*) {return SkNEW(ChecksumTestClass); }
+    protected:
+        virtual void onGetName(SkString* name) { name->set("Checksum"); }
+        virtual void onRun(Reporter* reporter) {
+            this->fReporter = reporter;
+            RunTest();
+        }
+    private:
+        enum Algorithm {
+            kSkChecksum,
+            kSkConsistentChecksum
+        };
+
+        // Call Compute(data, size) on the appropriate checksum algorithm,
+        // depending on this->fWhichAlgorithm.
+        uint32_t ComputeChecksum(uint32_t* data, size_t size) {
+            // Our checksum algorithms require 32-bit aligned data.
+            // If either of these tests fail, then the algorithm
+            // doesn't have a chance.
+            REPORTER_ASSERT_MESSAGE(fReporter,
+                                    reinterpret_cast<uintptr_t>(data) % 4 == 0,
+                                    "test data pointer is not 32-bit aligned");
+            REPORTER_ASSERT_MESSAGE(fReporter, SkIsAlign4(size),
+                                    "test data size is not 32-bit aligned");
+
+            switch(fWhichAlgorithm) {
+            case kSkChecksum:
+                return SkChecksum::Compute(data, size);
+            case kSkConsistentChecksum:
+                return SkConsistentChecksum::Compute(data, size);
+            default:
+                SkString message("fWhichAlgorithm has unknown value ");
+                message.appendf("%d", fWhichAlgorithm);
+                fReporter->reportFailed(message);
+            }
+            // we never get here
+            return 0;
+        }
+
+        // Confirm that the checksum algorithm (specified by fWhichAlgorithm)
+        // generates the same results if called twice over the same data.
+        void TestChecksumSelfConsistency(size_t buf_size) {
+            SkAutoMalloc storage(buf_size);
+            uint32_t*    ptr = (uint32_t*)storage.get();
+            char*        cptr = (char*)ptr;
+
+            sk_bzero(ptr, buf_size);
+            uint32_t prev = 0;
+
+            // assert that as we change values (from 0 to non-zero) in
+            // our buffer, we get a different value
+            for (size_t i = 0; i < buf_size; ++i) {
+                cptr[i] = (i & 0x7f) + 1; // need some non-zero value here
+
+                // Try checksums of different-sized chunks, but always
+                // 32-bit aligned and big enough to contain all the
+                // nonzero bytes.
+                size_t checksum_size = (((i/4)+1)*4);
+                REPORTER_ASSERT(fReporter, checksum_size <= buf_size);
+
+                uint32_t curr = ComputeChecksum(ptr, checksum_size);
+                REPORTER_ASSERT(fReporter, prev != curr);
+                uint32_t again = ComputeChecksum(ptr, checksum_size);
+                REPORTER_ASSERT(fReporter, again == curr);
+                prev = curr;
+            }
+        }
+
+        // Return the checksum of a portion of this static test data
+        // (8 bytes repeated twice): "1234567812345678"
+        uint32_t GetTestDataChecksum(size_t offset, size_t len) {
+            static char testbytes[] = "1234567812345678";
+            uint32_t* ptr = reinterpret_cast<uint32_t*>(testbytes);
+            return ComputeChecksum(ptr, len);
+        }
+
+        void RunTest() {
+            // Test self-consistency of checksum algorithms.
+            fWhichAlgorithm = kSkChecksum;
+            REPORTER_ASSERT(fReporter,
+                            GetTestDataChecksum(0, 8) ==
+                            GetTestDataChecksum(8, 8));
+            TestChecksumSelfConsistency(128);
+            fWhichAlgorithm = kSkConsistentChecksum;
+            REPORTER_ASSERT(fReporter,
+                            GetTestDataChecksum(0, 8) ==
+                            GetTestDataChecksum(8, 8));
+            TestChecksumSelfConsistency(128);
+
+            // Test checksum results that should be consistent across
+            // versions and platforms.
+            fWhichAlgorithm = kSkChecksum;
+            REPORTER_ASSERT(fReporter, ComputeChecksum(NULL, 0) == 0);
+            fWhichAlgorithm = kSkConsistentChecksum;
+            REPORTER_ASSERT(fReporter, ComputeChecksum(NULL, 0) == 0);
+            REPORTER_ASSERT(fReporter, GetTestDataChecksum(0, 8) == 0xa12fac2c);
+            REPORTER_ASSERT(fReporter, GetTestDataChecksum(8, 8) == 0xa12fac2c);
+            REPORTER_ASSERT(fReporter, GetTestDataChecksum(8, 4) == 0x34333231);
+        }
+
+        Reporter* fReporter;
+        Algorithm fWhichAlgorithm;
+    };
+
+    static TestRegistry gReg(ChecksumTestClass::Factory);
+}
index 65c4863..4783ba9 100644 (file)
@@ -7,34 +7,8 @@
  */
 #include "Test.h"
 #include "SkRandom.h"
-#include "SkChecksum.h"
 #include "SkTSort.h"
 
-// assert that as we change values (from 0 to non-zero) in our buffer, we
-// get a different value
-static void test_checksum(skiatest::Reporter* reporter, size_t size) {
-    SkAutoMalloc storage(size);
-    uint32_t*    ptr = (uint32_t*)storage.get();
-    char*        cptr = (char*)ptr;
-
-    sk_bzero(ptr, size);
-    uint32_t prev = 0;
-    for (size_t i = 0; i < size; ++i) {
-        cptr[i] = 0x5B; // just need some non-zero value here
-        uint32_t curr = SkChecksum::Compute(ptr, size);
-        REPORTER_ASSERT(reporter, prev != curr);
-        prev = curr;
-    }
-}
-
-static void test_checksum(skiatest::Reporter* reporter) {
-    REPORTER_ASSERT(reporter, SkChecksum::Compute(NULL, 0) == 0);
-
-    for (size_t size = 4; size <= 1000; size += 4) {
-        test_checksum(reporter, size);
-    }
-}
-
 extern "C" {
     static int compare_int(const void* a, const void* b) {
         return *(const int*)a - *(const int*)b;
@@ -77,8 +51,6 @@ static void TestSort(skiatest::Reporter* reporter) {
     if (false) { // avoid bit rot, suppress warning
         compare_int(array, array);
     }
-
-    test_checksum(reporter);
 }
 
 // need tests for SkStrSearch