aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/hash/fnv1a
diff options
context:
space:
mode:
Diffstat (limited to 'v_windows/v/vlib/hash/fnv1a')
-rw-r--r--v_windows/v/vlib/hash/fnv1a/fnv1a.v44
-rw-r--r--v_windows/v/vlib/hash/fnv1a/fnv1a_test.v12
2 files changed, 56 insertions, 0 deletions
diff --git a/v_windows/v/vlib/hash/fnv1a/fnv1a.v b/v_windows/v/vlib/hash/fnv1a/fnv1a.v
new file mode 100644
index 0000000..275c8a2
--- /dev/null
+++ b/v_windows/v/vlib/hash/fnv1a/fnv1a.v
@@ -0,0 +1,44 @@
+module fnv1a
+
+const (
+ fnv64_prime = u64(1099511628211)
+ fnv64_offset_basis = u64(14695981039346656037)
+ fnv32_offset_basis = u32(2166136261)
+ fnv32_prime = u32(16777619)
+)
+
+[inline]
+pub fn sum32_string(data string) u32 {
+ mut hash := fnv1a.fnv32_offset_basis
+ for i in 0 .. data.len {
+ hash = (hash ^ u32(data[i])) * fnv1a.fnv32_prime
+ }
+ return hash
+}
+
+[inline]
+pub fn sum32(data []byte) u32 {
+ mut hash := fnv1a.fnv32_offset_basis
+ for i in 0 .. data.len {
+ hash = (hash ^ u32(data[i])) * fnv1a.fnv32_prime
+ }
+ return hash
+}
+
+[inline]
+pub fn sum64_string(data string) u64 {
+ mut hash := fnv1a.fnv64_offset_basis
+ for i in 0 .. data.len {
+ hash = (hash ^ u64(data[i])) * fnv1a.fnv64_prime
+ }
+ return hash
+}
+
+[inline]
+pub fn sum64(data []byte) u64 {
+ mut hash := fnv1a.fnv64_offset_basis
+ for i in 0 .. data.len {
+ hash = (hash ^ u64(data[i])) * fnv1a.fnv64_prime
+ }
+ return hash
+}
diff --git a/v_windows/v/vlib/hash/fnv1a/fnv1a_test.v b/v_windows/v/vlib/hash/fnv1a/fnv1a_test.v
new file mode 100644
index 0000000..f775ab1
--- /dev/null
+++ b/v_windows/v/vlib/hash/fnv1a/fnv1a_test.v
@@ -0,0 +1,12 @@
+import hash.fnv1a
+
+fn test_fnv1a() {
+ $if windows {
+ return
+ }
+ a := 'apple'
+ b := fnv1a.sum64_string(a)
+ c := fnv1a.sum64(a.bytes())
+ assert b.hex() == 'f74a62a458befdbf'
+ assert c.hex() == 'f74a62a458befdbf'
+}