aboutsummaryrefslogtreecommitdiff
path: root/lib/std/hash.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-05-10 20:43:54 -0700
committerAndrew Kelley <andrew@ziglang.org>2023-06-10 20:42:30 -0700
commit1c7095cb7dfcba3537edf3624a61046c9b772b1f (patch)
tree36cb95ee1af69f6fe7aeb218ac69ed5c3f6ebe19 /lib/std/hash.zig
parent3ba099bfba9d3c38fe188010aa82fc589b1cabf6 (diff)
downloadzig-1c7095cb7dfcba3537edf3624a61046c9b772b1f.tar.gz
zig-1c7095cb7dfcba3537edf3624a61046c9b772b1f.zip
add std.hash.uint32
This is handy if you have a u32 and want a u32 and don't want to take a detour through many layers of abstraction elsewhere in the std.hash namespace. Copied from https://nullprogram.com/blog/2018/07/31/
Diffstat (limited to 'lib/std/hash.zig')
-rw-r--r--lib/std/hash.zig14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/std/hash.zig b/lib/std/hash.zig
index 5c85b38d55..eca7a70159 100644
--- a/lib/std/hash.zig
+++ b/lib/std/hash.zig
@@ -36,6 +36,20 @@ const xxhash = @import("hash/xxhash.zig");
pub const XxHash64 = xxhash.XxHash64;
pub const XxHash32 = xxhash.XxHash32;
+/// This is handy if you have a u32 and want a u32 and don't want to take a
+/// detour through many layers of abstraction elsewhere in the std.hash
+/// namespace.
+/// Copied from https://nullprogram.com/blog/2018/07/31/
+pub fn uint32(input: u32) u32 {
+ var x: u32 = input;
+ x ^= x >> 16;
+ x *%= 0x7feb352d;
+ x ^= x >> 15;
+ x *%= 0x846ca68b;
+ x ^= x >> 16;
+ return x;
+}
+
test {
_ = adler;
_ = auto_hash;