Add 32-bit hash helper to ImmutableString
Since the length of the hash is known prior to compilation, it can be
compared with script-generated hashes.
BUG=angleproject:2267
TEST=angle_unittests
Change-Id: Ia0a78dfd450c4ea2d526da7f3495b9750dcbd1af
Reviewed-on: https://chromium-review.googlesource.com/931884
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
diff --git a/src/compiler/translator/ImmutableString.cpp b/src/compiler/translator/ImmutableString.cpp
index 6bb26af..8f26dc8 100644
--- a/src/compiler/translator/ImmutableString.cpp
+++ b/src/compiler/translator/ImmutableString.cpp
@@ -35,4 +35,17 @@
const size_t ImmutableString::FowlerNollVoHash<8>::kFnvOffsetBasis =
static_cast<size_t>(0xcbf29ce484222325ull);
+uint32_t ImmutableString::hash32() const
+{
+ const char *data_ptr = data();
+ uint32_t hash = static_cast<uint32_t>(FowlerNollVoHash<4>::kFnvOffsetBasis);
+ while ((*data_ptr) != '\0')
+ {
+ hash = hash ^ (*data_ptr);
+ hash = hash * static_cast<uint32_t>(FowlerNollVoHash<4>::kFnvPrime);
+ ++data_ptr;
+ }
+ return hash;
+}
+
} // namespace sh
diff --git a/src/compiler/translator/ImmutableString.h b/src/compiler/translator/ImmutableString.h
index e077377..301e91c 100644
--- a/src/compiler/translator/ImmutableString.h
+++ b/src/compiler/translator/ImmutableString.h
@@ -128,6 +128,8 @@
}
};
+ uint32_t hash32() const;
+
private:
const char *mData;
size_t mLength;